feat(indent): ecma - support common use-cases

This commit is contained in:
Munif Tanjim
2022-01-21 18:44:30 +06:00
committed by Christian Clason
parent 782e299cd6
commit 7a6d93ca5b
8 changed files with 149 additions and 20 deletions

View File

@@ -1,33 +1,49 @@
[
(object)
(array)
(arguments)
(statement_block)
(object_pattern)
(array)
(binary_expression)
(class_body)
(export_clause)
(formal_parameters)
(method_definition)
(named_imports)
(binary_expression)
(object)
(object_pattern)
(return_statement)
(template_substitution)
(expression_statement (call_expression))
(export_clause)
(switch_statement)
(statement_block)
(switch_case)
(switch_statement)
(template_substitution)
(ternary_expression)
] @indent
(arguments (call_expression) @indent)
(binary_expression (call_expression) @indent)
(expression_statement (call_expression) @indent)
(arrow_function
body: (_) @_body
(#not-has-type? @_body statement_block)
) @indent
(assignment_expression
right: (_) @_right
(#not-has-type? @_right arrow_function function)
) @indent
(variable_declarator
value: (_) @_value
(#not-has-type? @_value arrow_function call_expression function)
) @indent
(arguments ")" @indent_end)
(object "}" @indent_end)
(statement_block "}" @indent_end)
[
(arguments (object))
"("
")"
"{"
"}"
"["
"]"
] @branch
(statement_block "{" @branch)
[
(comment)

View File

@@ -0,0 +1,4 @@
if_this_is_correct &&
run_this_thing()
.filter()
.map()

View File

@@ -0,0 +1,6 @@
const itemById = Array.from(
new Set()
).reduce((byId, item) => {
byId[item.id] = item
return result;
}, {})

37
tests/indent/ecma/func.js Normal file
View File

@@ -0,0 +1,37 @@
const arrow_func = (
a,
b,
c
) => {
log(a, b, c)
}
const arrow_func_without_brace = (a, b, c) =>
log(
a,
b,
c
)
function func_def(
a,
b,
{ c = '' }
) {
log(a, b, c)
}
func_call(
a,
(b) => b
)
chained_func_call()
.map()
.filter()
func_call(
chained_func_call()
.map()
.filter()
)

View File

@@ -0,0 +1,5 @@
const obj = {
a: 1,
b: "2",
["c"]: `three`
}

View File

@@ -0,0 +1,6 @@
const value =
condition
? typeof number === 'string'
? Number(number)
: number
: null;

View File

@@ -0,0 +1,6 @@
let a =
if_this_is_right() && then_this()
a = func_call()
.map()
.filter()

View File

@@ -19,14 +19,30 @@ describe("indent JavaScript:", function()
describe("new line:", function()
for _, info in ipairs {
{ 1, 2 },
{ 2, 2 },
{ 3, 2 },
{ 4, 2 },
{ 5, 2 },
{ 6, 2 },
{ 7, 0 },
{ 2, 4 },
{ 3, 4 },
} do
run:new_line("ecma/try_catch.js", { on_line = info[1], text = "hello()", indent = info[2] })
run:new_line("ecma/binary_expression.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 4, 2 },
{ 6, 0 },
} do
run:new_line("ecma/callback.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 2, 2 },
{ 5, 2 },
{ 7, 0 },
{ 12, 4 },
{ 18, 2 },
{ 19, 2 },
{ 20, 2 },
{ 25, 2 },
} do
run:new_line("ecma/func.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
@@ -40,7 +56,40 @@ describe("indent JavaScript:", function()
{ 12, 2 },
{ 13, 0 },
} do
run:new_line("ecma/if_else.js", { on_line = info[1], text = "hello()", indent = info[2] })
run:new_line("ecma/if_else.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 2, 2 },
{ 5, 0 },
} do
run:new_line("ecma/object.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 3, 6 },
{ 4, 6 },
} do
run:new_line("ecma/ternary.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 1, 2 },
{ 2, 2 },
{ 3, 2 },
{ 4, 2 },
{ 5, 2 },
{ 6, 2 },
{ 7, 0 },
} do
run:new_line("ecma/try_catch.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4])
end
for _, info in ipairs {
{ 1, 2 },
{ 2, 0 },
} do
run:new_line("ecma/variable.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4])
end
end)
end)