Skip to content
Merged

Clean #156

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions fmt/internal/comment/mapper_visitor.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,11 @@ impl IterVisitor for Mapper with fn visit_Impl_TopFuncDef(
}
NoErrorType => (None, None)
}
match where_clause {
None => ()
Some(where_clause) => {
ignore(s.cursor_find(TK_WHERE, before_pos=where_clause.loc.end))
ignore(s.cursor_find(TK_LBRACE, before_pos=where_clause.loc.end))
s.visit_WhereClause(where_clause)
ignore(s.cursor_find(TK_RBRACE, before_pos=where_clause.loc.end))
}
if where_clause is Some(where_clause) {
ignore(s.cursor_find(TK_WHERE, before_pos=where_clause.loc.end))
ignore(s.cursor_find(TK_LBRACE, before_pos=where_clause.loc.end))
s.visit_WhereClause(where_clause)
ignore(s.cursor_find(TK_RBRACE, before_pos=where_clause.loc.end))
}
let (lbrace, rbrace) = match decl_body {
DeclBody(expr~) => {
Expand Down
23 changes: 12 additions & 11 deletions fmt/internal/comment/mapper_visitor_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ fn parse_impls(source : String) -> (@syntax.Impls, @tokens.Triples) raise {
fn find_iter_array_loc(impls : @syntax.Impls) -> Location raise {
let finder = { loc: None }
impls.map(item => finder.visit_Impl(item)) |> ignore
match finder.loc {
Some(loc) => loc
None => fail("expected fixture to contain iter array")
if finder.loc is Some(loc) {
loc
} else {
fail("expected fixture to contain iter array")
}
}

Expand All @@ -60,14 +61,14 @@ fn assert_token_kind(
expected : TokenKind,
name : String,
) -> Unit raise {
match key {
Some(key) =>
match token_kind_at(tokens, key) {
Some(kind) if kind == expected => ()
Some(kind) => fail("expected \{name} to be \{expected}, got \{kind}")
None => fail("expected \{name} to point at a source token")
}
None => fail("expected \{name} token to be recorded")
if key is Some(key) {
match token_kind_at(tokens, key) {
Some(kind) if kind == expected => ()
Some(kind) => fail("expected \{name} to be \{expected}, got \{kind}")
None => fail("expected \{name} to point at a source token")
}
} else {
fail("expected \{name} token to be recorded")
}
}

Expand Down
41 changes: 17 additions & 24 deletions fmt/internal/format/config_format.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -185,45 +185,38 @@ fn render_moon_mod_json(json : Json) -> String {
Some(value) => sections.push(render_assignment("version", value))
None => ()
}
match first_field(fields, ["deps", "import"]) {
Some(value) =>
match render_import_block(value) {
Some(imports) => sections.push(imports)
None => ()
}
None => ()
if first_field(fields, ["deps", "import"]) is Some(value) {
match render_import_block(value) {
Some(imports) => sections.push(imports)
None => ()
}
}
match first_field(fields, ["warn-list", "warnings"]) {
Some(value) => sections.push(render_assignment("warnings", value))
None => ()
if first_field(fields, ["warn-list", "warnings"]) is Some(value) {
sections.push(render_assignment("warnings", value))
}
for
key in [
"readme", "repository", "license", "keywords", "description", "source",
] {
match object_get(fields, key) {
Some(value) => sections.push(render_assignment(key, value))
None => ()
if object_get(fields, key) is Some(value) {
sections.push(render_assignment(key, value))
}
}
match first_field(fields, ["supported-targets", "supported_targets"]) {
Some(value) =>
sections.push(render_assignment("supported_targets", value))
None => ()
if first_field(fields, ["supported-targets", "supported_targets"])
is Some(value) {
sections.push(render_assignment("supported_targets", value))
}
match first_field(fields, ["preferred-target", "preferred_target"]) {
Some(value) =>
sections.push(render_assignment("preferred_target", value))
None => ()
if first_field(fields, ["preferred-target", "preferred_target"])
is Some(value) {
sections.push(render_assignment("preferred_target", value))
}
let known = [
"name", "version", "deps", "import", "warn-list", "warnings", "readme", "repository",
"license", "keywords", "description", "source", "supported-targets", "supported_targets",
"preferred-target", "preferred_target", "rule",
]
match render_options(fields, known) {
Some(options) => sections.push(options)
None => ()
if render_options(fields, known) is Some(options) {
sections.push(options)
}
sections[:].join("\n\n")
}
Expand Down
50 changes: 25 additions & 25 deletions fmt/internal/format/source_format.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,19 @@ fn insert_header_comment(formatted : String, comment : String) -> String {
///|
fn source_header_comment(source : String, line_number : Int) -> String? {
let lines = split_lines(source)
match lines.get(line_number - 1) {
None => None
Some(line) =>
match (line.find("{"), line.find("//")) {
(Some(brace), Some(comment_start)) if comment_start > brace =>
Some(
line
.unsafe_substring(start=comment_start, end=line.length())
.trim()
.to_owned(),
)
_ => None
}
if lines.get(line_number - 1) is Some(line) {
match (line.find("{"), line.find("//")) {
(Some(brace), Some(comment_start)) if comment_start > brace =>
Some(
line
.unsafe_substring(start=comment_start, end=line.length())
.trim()
.to_owned(),
)
_ => None
}
} else {
None
}
}

Expand All @@ -214,9 +214,10 @@ fn restore_source_header_comment(
source : String,
line_number : Int,
) -> String {
match source_header_comment(source, line_number) {
Some(comment) => insert_header_comment(formatted, comment)
None => formatted
if source_header_comment(source, line_number) is Some(comment) {
insert_header_comment(formatted, comment)
} else {
formatted
}
}

Expand Down Expand Up @@ -257,15 +258,14 @@ fn format_nth_plain_impl(source : String, index : Int) -> (Int, Int, String)? {
return None
}
let items = impls.to_array()
match items.get(index) {
None => None
Some(item) => {
let loc = item.loc()
let line_start = impl_start_line(item)
let raw = format_single_impl(item, impls, tokens, block_line=true)
let formatted = restore_source_header_comment(raw, source, loc.start.lnum)
Some((line_start, loc.end.lnum, formatted))
}
if items.get(index) is Some(item) {
let loc = item.loc()
let line_start = impl_start_line(item)
let raw = format_single_impl(item, impls, tokens, block_line=true)
let formatted = restore_source_header_comment(raw, source, loc.start.lnum)
Some((line_start, loc.end.lnum, formatted))
} else {
None
}
}

Expand Down
12 changes: 6 additions & 6 deletions lexer/basic/loc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ pub impl Compare for Position with fn compare(self, other) {
test "compre position" {
let a = { fname: "", lnum: 10, bol: 0, cnum: 0 }
let b = { fname: "", lnum: 10, bol: 0, cnum: 0 }
inspect(a.compare(b), content="0")
assert_eq(a.compare(b), 0)
let a = { fname: "", lnum: 10, bol: 0, cnum: 0 }
let b = { fname: "", lnum: 1, bol: 0, cnum: 0 }
inspect(a.compare(b), content="1")
inspect(b.compare(a), content="-1")
assert_eq(a.compare(b), 1)
assert_eq(b.compare(a), -1)
let a = { fname: "", lnum: 10, bol: 2, cnum: 5 }
let b = { fname: "", lnum: 10, bol: 2, cnum: 5 }
inspect(a.compare(b), content="0")
assert_eq(a.compare(b), 0)
let a = { fname: "", lnum: 10, bol: 2, cnum: 5 }
let b = { fname: "", lnum: 10, bol: 2, cnum: 10 }
inspect(a.compare(b), content="-1")
inspect(b.compare(a), content="1")
assert_eq(a.compare(b), -1)
assert_eq(b.compare(a), 1)
}

///|
Expand Down
8 changes: 4 additions & 4 deletions test/mbti_parser_test/legacy_fixture_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn expect_constructor_name(
test "parse legacy mbti simple fixture" {
let mbti = parse_fixture("simple")
inspect(mbti.package_name, content="username/hello/lib")
inspect(mbti.imports.length().to_string(), content="2")
assert_eq(mbti.imports.length(), 2)
expect_sig_count(mbti, 11)
expect_sig_name(mbti, 0, "g")
expect_sig_name(mbti, 1, "h")
Expand Down Expand Up @@ -133,7 +133,7 @@ test "parse legacy mbti simple fixture" {
fail("expected T::f to be a method")
}
inspect(type_name.name, content="T")
inspect(type_name.is_object.to_string(), content="false")
assert_eq(type_name.is_object, false)
inspect(type_params.head().unwrap().name.name, content="A")
guard sig_at(mbti, 5) is Func({ is_async: Some(_), .. }) else {
fail("expected async function")
Expand Down Expand Up @@ -187,7 +187,7 @@ test "parse legacy mbti trait fixture" {
guard methods.head().unwrap() is { name: { name: "g", .. }, params, .. } else {
fail("expected I2.g method")
}
inspect(params.length().to_string(), content="2")
assert_eq(params.length(), 2)
guard sig_at(mbti, 2) is Trait({ name: { name: "I3", .. }, vis, methods, .. }) else {
fail("expected pub(open) I3 trait")
}
Expand Down Expand Up @@ -227,7 +227,7 @@ test "parse legacy mbti attributes fixture" {
expect_one_attr(
attrs, "#callsite(migration(a, allow_positional=true), migration(d, allow_positional=true))",
)
inspect(params.length().to_string(), content="4")
assert_eq(params.length(), 4)
guard params.nth(0) is Some(Labelled(..)) else {
fail("expected labelled first parameter")
}
Expand Down
12 changes: 6 additions & 6 deletions test/mbti_parser_test/regression_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ test "mbti float constants strip F suffix" {
is Const({ value: Float(value), .. }) else {
fail("expected float const")
}
inspect(value, content="1.5")
assert_eq(value, "1.5")
let negative_float_const =
#|pub const X : Float = -2.5F
#|
guard first_sig("negative_float_suffix.mbti", negative_float_const)
is Const({ value: Float(value), .. }) else {
fail("expected negative float const")
}
inspect(value, content="-2.5")
assert_eq(value, "-2.5")
}

///|
Expand Down Expand Up @@ -76,7 +76,7 @@ test "mbti parse string records base position" {
#|
let mbti = parse_ok("base_pos.mbti", content)
inspect(mbti.base_pos.base_fname, content="base_pos.mbti")
inspect(mbti.base_pos.base_lnum, content="0")
assert_eq(mbti.base_pos.base_lnum, 0)
}

///|
Expand Down Expand Up @@ -109,13 +109,13 @@ test "trait_method_type_params" {
is { name: { name: "map", .. }, type_params, .. } else {
fail("expected generic method")
}
inspect(type_params.length().to_string(), content="1")
assert_eq(type_params.length(), 1)
let type_param = type_params.head().unwrap()
inspect(type_param.name.name, content="A")
inspect(type_param.constraints.length().to_string(), content="1")
assert_eq(type_param.constraints.length(), 1)
guard methods.nth(1)
is Some({ name: { name: "extend", .. }, type_params: extend_params, .. }) else {
fail("expected extend method")
}
inspect(extend_params.length().to_string(), content="0")
assert_eq(extend_params.length(), 0)
}
Loading