@inthedark/dxlint 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/bin/dxlint +15 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +40 -4
- package/rules/conventions.dx +34 -0
- package/rules/correctness.dx +33 -0
- package/rules/es-no-semi.dx +11 -0
- package/rules/format.dx +49 -0
- package/rules/recommended.dx +51 -0
- package/rules/suspicious.dx +31 -0
- package/rules/types.dx +21 -0
- package/std/c.dx +23 -0
- package/std/core/lines.dx +51 -0
- package/std/core/lists.dx +76 -0
- package/std/core.dx +30 -0
- package/std/css.dx +17 -0
- package/std/dx.dx +31 -0
- package/std/json.dx +104 -0
- package/std/markdown.dx +11 -0
- package/std/python.dx +21 -0
- package/std/rust/format.dx +316 -0
- package/std/rust.dx +34 -0
- package/std/toml.dx +19 -0
- package/std/ts/arithmetic.dx +87 -0
- package/std/ts/assertions.dx +477 -0
- package/std/ts/calls.dx +246 -0
- package/std/ts/comments.dx +10 -0
- package/std/ts/conditions.dx +531 -0
- package/std/ts/constants.dx +335 -0
- package/std/ts/deprecation.dx +185 -0
- package/std/ts/directives.dx +60 -0
- package/std/ts/effects.dx +219 -0
- package/std/ts/enums.dx +233 -0
- package/std/ts/expression-fixes.dx +67 -0
- package/std/ts/expressions.dx +270 -0
- package/std/ts/flow.dx +2 -0
- package/std/ts/format.dx +755 -0
- package/std/ts/globals.dx +106 -0
- package/std/ts/grammar.dx +488 -0
- package/std/ts/helpers.dx +102 -0
- package/std/ts/import-attributes.dx +88 -0
- package/std/ts/import-no-deprecated.dx +73 -0
- package/std/ts/indentation.dx +442 -0
- package/std/ts/jsx.dx +646 -0
- package/std/ts/layout/blocks.dx +281 -0
- package/std/ts/layout/calls.dx +79 -0
- package/std/ts/layout/expressions.dx +213 -0
- package/std/ts/layout/literals.dx +114 -0
- package/std/ts/layout/separators.dx +184 -0
- package/std/ts/layout/shared.dx +203 -0
- package/std/ts/layout/text.dx +142 -0
- package/std/ts/layout/types.dx +182 -0
- package/std/ts/layout.dx +9 -0
- package/std/ts/license.dx +54 -0
- package/std/ts/literal-text.dx +3 -0
- package/std/ts/literals.dx +95 -0
- package/std/ts/methods.dx +69 -0
- package/std/ts/modules.dx +268 -0
- package/std/ts/names.dx +348 -0
- package/std/ts/naming.dx +169 -0
- package/std/ts/narrowing.dx +96 -0
- package/std/ts/parentheses.dx +262 -0
- package/std/ts/precedence.dx +25 -0
- package/std/ts/problems/calls.dx +34 -0
- package/std/ts/problems/classes.dx +39 -0
- package/std/ts/problems/control-flow.dx +257 -0
- package/std/ts/problems/declarations.dx +67 -0
- package/std/ts/problems/duplicates.dx +70 -0
- package/std/ts/problems/expressions.dx +154 -0
- package/std/ts/problems/helpers.dx +22 -0
- package/std/ts/problems/imports.dx +43 -0
- package/std/ts/problems/non-null.dx +78 -0
- package/std/ts/problems/numeric-precision.dx +66 -0
- package/std/ts/problems/regexp.dx +66 -0
- package/std/ts/problems/statements.dx +36 -0
- package/std/ts/problems/typescript.dx +55 -0
- package/std/ts/problems/whitespace.dx +40 -0
- package/std/ts/problems.dx +20 -0
- package/std/ts/regex/checks.dx +249 -0
- package/std/ts/regex/findings.dx +209 -0
- package/std/ts/regex/grammar.dx +80 -0
- package/std/ts/regex/suggestions.dx +94 -0
- package/std/ts/regex/values.dx +378 -0
- package/std/ts/regex.dx +5 -0
- package/std/ts/restrictions/comments.dx +76 -0
- package/std/ts/restrictions.dx +811 -0
- package/std/ts/scopes.dx +68 -0
- package/std/ts/scoping/atomic.dx +40 -0
- package/std/ts/scoping/bindings.dx +126 -0
- package/std/ts/scoping/constness.dx +72 -0
- package/std/ts/scoping/globals.dx +52 -0
- package/std/ts/scoping/loops.dx +86 -0
- package/std/ts/scoping/member-uses.dx +111 -0
- package/std/ts/scoping/receivers.dx +107 -0
- package/std/ts/scoping/shadowing.dx +202 -0
- package/std/ts/scoping/usage.dx +393 -0
- package/std/ts/scoping.dx +842 -0
- package/std/ts/selectors.dx +10 -0
- package/std/ts/spreads.dx +97 -0
- package/std/ts/structure/declarations.dx +52 -0
- package/std/ts/structure/lines.dx +79 -0
- package/std/ts/structure/lists.dx +144 -0
- package/std/ts/structure/padding.dx +258 -0
- package/std/ts/structure.dx +963 -0
- package/std/ts/style/classes.dx +157 -0
- package/std/ts/style/comments.dx +75 -0
- package/std/ts/style/declarations.dx +82 -0
- package/std/ts/style/expressions.dx +125 -0
- package/std/ts/style/imports.dx +44 -0
- package/std/ts/style/ordering.dx +192 -0
- package/std/ts/style/returns.dx +152 -0
- package/std/ts/style/signatures.dx +308 -0
- package/std/ts/style/type-exports.dx +62 -0
- package/std/ts/style/type-imports.dx +166 -0
- package/std/ts/style/types.dx +474 -0
- package/std/ts/style.dx +29 -0
- package/std/ts/suggestions/accessors.dx +138 -0
- package/std/ts/suggestions/builtins.dx +110 -0
- package/std/ts/suggestions/comparisons.dx +41 -0
- package/std/ts/suggestions/computed-keys.dx +20 -0
- package/std/ts/suggestions/constructors.dx +54 -0
- package/std/ts/suggestions/control-flow.dx +204 -0
- package/std/ts/suggestions/curly.dx +124 -0
- package/std/ts/suggestions/directives.dx +79 -0
- package/std/ts/suggestions/errors.dx +106 -0
- package/std/ts/suggestions/expressions.dx +230 -0
- package/std/ts/suggestions/functions.dx +260 -0
- package/std/ts/suggestions/globals.dx +187 -0
- package/std/ts/suggestions/helpers.dx +17 -0
- package/std/ts/suggestions/object-shorthand.dx +148 -0
- package/std/ts/suggestions/object-spread.dx +96 -0
- package/std/ts/suggestions/regexp.dx +59 -0
- package/std/ts/suggestions/renames.dx +50 -0
- package/std/ts/suggestions/strings.dx +47 -0
- package/std/ts/suggestions/templates.dx +104 -0
- package/std/ts/suggestions.dx +24 -0
- package/std/ts/template-fixes.dx +22 -0
- package/std/ts/tokens.dx +124 -0
- package/std/ts/type-specifiers.dx +93 -0
- package/std/ts/typed/assignments.dx +154 -0
- package/std/ts/typed/awaitability.dx +157 -0
- package/std/ts/typed/boolean-comparisons.dx +82 -0
- package/std/ts/typed/defaults.dx +97 -0
- package/std/ts/typed/enum-consistency.dx +68 -0
- package/std/ts/typed/enums.dx +52 -0
- package/std/ts/typed/errors.dx +32 -0
- package/std/ts/typed/floating-promises.dx +147 -0
- package/std/ts/typed/for-in-arrays.dx +21 -0
- package/std/ts/typed/includes.dx +102 -0
- package/std/ts/typed/nullish-coalescing.dx +149 -0
- package/std/ts/typed/optional-chains.dx +317 -0
- package/std/ts/typed/promises.dx +205 -0
- package/std/ts/typed/qualifiers.dx +108 -0
- package/std/ts/typed/readonly-members.dx +159 -0
- package/std/ts/typed/readonly-parameters.dx +121 -0
- package/std/ts/typed/redundant-constituents.dx +171 -0
- package/std/ts/typed/regexp-exec.dx +79 -0
- package/std/ts/typed/rejection-handlers.dx +91 -0
- package/std/ts/typed/return-await.dx +116 -0
- package/std/ts/typed/return-this.dx +67 -0
- package/std/ts/typed/strict-void-return.dx +121 -0
- package/std/ts/typed/string-boundaries.dx +153 -0
- package/std/ts/typed/switches.dx +147 -0
- package/std/ts/typed/template-expressions.dx +127 -0
- package/std/ts/typed/template-values.dx +88 -0
- package/std/ts/typed/transfers.dx +106 -0
- package/std/ts/typed/type-arguments.dx +337 -0
- package/std/ts/typed/type-constituents.dx +143 -0
- package/std/ts/typed/type-conversions.dx +82 -0
- package/std/ts/typed/type-parameters.dx +280 -0
- package/std/ts/typed/unbound-method.dx +167 -0
- package/std/ts/typed/unsafe-calls.dx +73 -0
- package/std/ts/typed/unsafe-members.dx +36 -0
- package/std/ts/typed/void-expressions.dx +101 -0
- package/std/ts/typed.dx +585 -0
- package/std/ts/values/arguments.dx +13 -0
- package/std/ts/values/enums.dx +104 -0
- package/std/ts/values/inspection.dx +274 -0
- package/std/ts/values.dx +1217 -0
- package/std/ts.dx +77 -0
- package/std/whitespace.dx +37 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Thrown values, useless catches and preserved caught errors.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_throw let thrown_identifier if (argument.unwrapped_node.nonempty ?? argument.node) is ts_identifier then argument.unwrapped_text.nonempty ?? argument else null
|
|
6
|
+
in ts_block let first_throw first([] body.thrown_identifier)
|
|
7
|
+
in ts_try let catch_rethrows parameter is ts_identifier && handler.first_throw == parameter
|
|
8
|
+
|
|
9
|
+
in ts_try rule es-no-useless-catch {
|
|
10
|
+
when catch_rethrows && finalizer == null
|
|
11
|
+
warn "Unnecessary try/catch wrapper."
|
|
12
|
+
}
|
|
13
|
+
in ts_try.kind rule es-no-useless-catch:clause {
|
|
14
|
+
when parent.catch_rethrows && parent.finalizer != null
|
|
15
|
+
report at { start: offset, end: parent.handler.end_offset }
|
|
16
|
+
warn "Unnecessary catch clause."
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn suggestion_possible_error(n) = {
|
|
20
|
+
value n
|
|
21
|
+
walk (current) {
|
|
22
|
+
value bare: unparenthesised_node(current)
|
|
23
|
+
value when {
|
|
24
|
+
bare is ts_assignment && contains(["=", "&&="], bare.operator) then bare.right.node
|
|
25
|
+
bare is ts_assignment && contains(["||=", "??="], bare.operator) then [bare.left.node, bare.right.node]
|
|
26
|
+
bare is ts_logical then if bare.operator == "&&" then bare.right.node else [bare.right.node, bare.left.node]
|
|
27
|
+
bare is ts_conditional then [bare.consequent.node, bare.alternate.node]
|
|
28
|
+
bare is ts_sequence then last(bare.expression.nodes)
|
|
29
|
+
else then null
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
any (current) unparenthesised_node(current) is (ts_identifier, ts_call, ts_new, ts_member, ts_index, ts_tagged_template, ts_yield, ts_await)
|
|
33
|
+
}
|
|
34
|
+
in ts_node let could_be_error suggestion_possible_error(node)
|
|
35
|
+
in ts_identifier let error_undefined_identifier identifier_name == "undefined"
|
|
36
|
+
in ts_parenthesized forward error_undefined_identifier from expression
|
|
37
|
+
in ts_identifier let error_undefined_global identifier_name == "undefined" && resolved == "global"
|
|
38
|
+
in ts_parenthesized forward error_undefined_global from expression
|
|
39
|
+
in ts_throw rule es-no-throw-literal {
|
|
40
|
+
when !argument.could_be_error || argument.error_undefined_global == true
|
|
41
|
+
warn if argument.could_be_error then "Do not throw undefined." else "Expected an error object to be thrown."
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn caught_throw_property_value(property) = unparenthesised_node(property.value.node ?? property)
|
|
45
|
+
fn caught_throw_property_kind(parameter, property) = {
|
|
46
|
+
value value: caught_throw_property_value(property)
|
|
47
|
+
value when {
|
|
48
|
+
property == null then "missing"
|
|
49
|
+
value not ts_identifier || value.text != parameter.text then "incorrect"
|
|
50
|
+
value.declaration.node != parameter then "shadowed"
|
|
51
|
+
else then "preserved"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
fn caught_throw_cause_kind(parameter, argument, preceding) =
|
|
55
|
+
if any(preceding, arg => unparenthesised_node(arg) is ts_spread)
|
|
56
|
+
|| argument != null && (argument not ts_object || any([] argument.property.nodes, property => property is ts_spread)) then "unknown"
|
|
57
|
+
else {
|
|
58
|
+
value [] argument.property.nodes
|
|
59
|
+
property_2: find_last (property) property.static_name == "cause"
|
|
60
|
+
value caught_throw_property_kind(parameter, property_2)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
in ts_throw rule es-preserve-caught-error {
|
|
64
|
+
option requireCatchParameter boolean = false "require a catch parameter when throwing a symptom error"
|
|
65
|
+
option errorClassNames (string | { name: string; argumentPosition: number })[] = [] "additional error constructors and options positions"
|
|
66
|
+
let caught_info catch_throw
|
|
67
|
+
when caught_info != null
|
|
68
|
+
let caught_parameter get(caught_info, "parameter")
|
|
69
|
+
let catch_kind when {
|
|
70
|
+
caught_parameter == null then "missing"
|
|
71
|
+
caught_parameter is ts_identifier then "present"
|
|
72
|
+
else then "destructured"
|
|
73
|
+
}
|
|
74
|
+
let builtin_error caught_info.global && contains(["Error", "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "AggregateError"], caught_info.name)
|
|
75
|
+
let configured_error find_last(errorClassNames, x => caught_info.name == (if type_of(x) == "str" then x else x.name))
|
|
76
|
+
when builtin_error || configured_error != null
|
|
77
|
+
let cause_position when {
|
|
78
|
+
builtin_error then (if caught_info.name == "AggregateError" then 2 else 1)
|
|
79
|
+
type_of(configured_error) == "str" then 1
|
|
80
|
+
else then configured_error.argumentPosition - 1
|
|
81
|
+
}
|
|
82
|
+
let cause_argument {
|
|
83
|
+
value argument: { value caught_info.arguments; slice cause_position; first }
|
|
84
|
+
value unparenthesised_node(argument)
|
|
85
|
+
}
|
|
86
|
+
let cause_kind {
|
|
87
|
+
value preceding: if cause_argument == null then caught_info.arguments else slice(caught_info.arguments, 0, cause_position)
|
|
88
|
+
value caught_throw_cause_kind(caught_parameter, cause_argument, preceding)
|
|
89
|
+
}
|
|
90
|
+
let cause_property { value [] cause_argument.property.nodes; find_last (property) property.static_name == "cause" }
|
|
91
|
+
when catch_kind == "destructured" || catch_kind == "missing" && requireCatchParameter || catch_kind == "present" && contains(["missing", "incorrect", "shadowed"], cause_kind)
|
|
92
|
+
report at when {
|
|
93
|
+
catch_kind == "destructured" then caught_info.catch_offset
|
|
94
|
+
catch_kind == "present" && cause_kind == "incorrect" then caught_throw_property_value(cause_property).offset
|
|
95
|
+
else then caught_info.throw_offset
|
|
96
|
+
}
|
|
97
|
+
warn when {
|
|
98
|
+
catch_kind == "destructured" then "Re-throws cannot preserve the caught error as a part of it is being lost due to destructuring."
|
|
99
|
+
catch_kind == "missing" then "The caught error is not accessible because the catch clause lacks the error parameter. Start referencing the caught error using the catch parameter."
|
|
100
|
+
cause_kind == "missing" then "There is no `cause` attached to the symptom error being thrown."
|
|
101
|
+
cause_kind == "incorrect" then "The symptom error is being thrown with an incorrect `cause`."
|
|
102
|
+
else then "The caught error is being attached as `cause`, but is shadowed by a closer scoped redeclaration."
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# Expression preferences: sequences, comparisons, concatenation, ternaries and coercions.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_binary let arithmetic_operator if contains(["+", "-", "*", "/", "%", "**", "<<", ">>", ">>>", "&", "|", "^"], operator) then operator else null
|
|
6
|
+
in ts_binary let strict_left_reference_key left.reference_key_strict
|
|
7
|
+
in ts_binary let strict_right_reference_key right.reference_key_strict
|
|
8
|
+
in ts_parenthesized forward arithmetic_operator, strict_left_reference_key, strict_right_reference_key from expression
|
|
9
|
+
|
|
10
|
+
# Comma operators, except loop init/update and explicitly grouped expressions.
|
|
11
|
+
in ts_sequence rule es-no-sequences {
|
|
12
|
+
option allowInParentheses boolean = true "allow explicitly parenthesised sequences"
|
|
13
|
+
let enclosed walk(node, current => if current.parent is ts_parenthesized then current.parent.node else null)
|
|
14
|
+
let outside last(enclosed)
|
|
15
|
+
when outside not (ts_for.init, ts_for.update)
|
|
16
|
+
let paren_count count(enclosed) - 1
|
|
17
|
+
when !allowInParentheses || paren_count == 0 || paren_count == 1 && outside is ts_arrow.body
|
|
18
|
+
let comma_offset opener.ts_comma.offset
|
|
19
|
+
when comma_offset != null
|
|
20
|
+
report at opener.ts_comma.node
|
|
21
|
+
warn "Unexpected use of comma operator."
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
in ts_binary rule es-no-self-compare {
|
|
25
|
+
when re_test(operator, /^(?:===?|!==?|>=?|<=?)$/)
|
|
26
|
+
when left != null && right != null && left.unparenthesised_tokens == right.unparenthesised_tokens
|
|
27
|
+
warn "Comparing to itself is potentially pointless."
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
in ts_binary rule es-no-eq-null {
|
|
31
|
+
when (operator == "==" || operator == "!=") && (left == "null" || right == "null")
|
|
32
|
+
warn "Use '===' to compare with null."
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn concat_endpoint_next(n, right_side) = when {
|
|
36
|
+
n is ts_parenthesized then n.expression.node
|
|
37
|
+
n is ts_binary && n.operator == "+" then if right_side then n.right.node else n.left.node
|
|
38
|
+
else then null
|
|
39
|
+
}
|
|
40
|
+
fn concat_endpoint(n, right_side) = { value n; walk (current) concat_endpoint_next(current, right_side); last }
|
|
41
|
+
|
|
42
|
+
in ts_binary rule es-no-useless-concat {
|
|
43
|
+
when operator == "+"
|
|
44
|
+
let left_endpoint concat_endpoint(left.node, true)
|
|
45
|
+
let right_endpoint concat_endpoint(right.node, false)
|
|
46
|
+
when left_endpoint is (ts_string, ts_template) && right_endpoint is (ts_string, ts_template)
|
|
47
|
+
when !{ value source; byte_slice left_endpoint.end_offset right_endpoint.offset; re_test /[\r\n\u{2028}\u{2029}]/ }
|
|
48
|
+
report at { start: operator.offset, end: operator.end_offset }
|
|
49
|
+
warn "Unexpected string concatenation of literals."
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn ternary_fix_inverse(operand) = {
|
|
53
|
+
value bare: unparenthesised_node(operand)
|
|
54
|
+
value inverse: if bare is ts_binary then get({ "==": "!=", "!=": "==", "===": "!==", "!==": "===" }, bare.operator) else null
|
|
55
|
+
value if inverse != null then byte_slice(bare.text, 0, bare.operator.offset - bare.offset) inverse byte_slice(bare.text, bare.operator.end_offset - bare.offset)
|
|
56
|
+
else "!" expression_fix_wrap(operand.text, bare.precedence < 15)
|
|
57
|
+
}
|
|
58
|
+
fn ternary_fix_boolean(n, consequent, alternate) = {
|
|
59
|
+
value operand: n.test.node
|
|
60
|
+
value bare: unparenthesised_node(operand)
|
|
61
|
+
value when {
|
|
62
|
+
consequent == alternate then if bare is ts_identifier then consequent else null
|
|
63
|
+
alternate == "true" then ternary_fix_inverse(operand)
|
|
64
|
+
bare is ts_binary && contains(["==", "===", "!=", "!==", ">", ">=", "<", "<=", "in", "instanceof"], bare.operator)
|
|
65
|
+
|| bare is ts_unary && bare.operator == "!" then operand.text
|
|
66
|
+
else then "!" ternary_fix_inverse(operand)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
in ts_conditional rule es-no-unneeded-ternary {
|
|
71
|
+
let consequent_value consequent.literal_value
|
|
72
|
+
let alternate_value alternate.literal_value
|
|
73
|
+
when consequent_value.type == "boolean" && alternate_value.type == "boolean"
|
|
74
|
+
warn "Unnecessary use of boolean literals in conditional expression."
|
|
75
|
+
let replacement ternary_fix_boolean(node, consequent_value.value, alternate_value.value)
|
|
76
|
+
fix if replacement != null && expression_fix_retains_comments(node, replacement)
|
|
77
|
+
&& unparenthesised_node(consequent.node) is ts_boolean && unparenthesised_node(alternate.node) is ts_boolean as expression_fix_edges(node, replacement)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
in ts_conditional rule es-no-unneeded-ternary:default-assignment {
|
|
81
|
+
option defaultAssignment boolean = true "allow conditional expressions used as default assignments"
|
|
82
|
+
when !defaultAssignment && (test.unwrapped_node.nonempty ?? test.node) is ts_identifier && (consequent.unwrapped_node.nonempty ?? consequent.node) is ts_identifier && (test.unwrapped_text.nonempty ?? test) == (consequent.unwrapped_text.nonempty ?? consequent)
|
|
83
|
+
warn "Unnecessary use of conditional expression for default assignment."
|
|
84
|
+
let operand alternate.node
|
|
85
|
+
let bare unparenthesised_node(operand)
|
|
86
|
+
let grouped operand not ts_parenthesized && (bare.precedence < 4 || bare is (ts_as, ts_satisfies, ts_type_assertion) || bare is ts_logical && bare.operator == "??")
|
|
87
|
+
let replacement test " || " expression_fix_wrap(operand.text, grouped)
|
|
88
|
+
fix if expression_fix_retains_comments(node, replacement) as expression_fix_edges(node, replacement)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn suggestion_coercion_numeric(n) = {
|
|
92
|
+
value n: unparenthesised_node(n)
|
|
93
|
+
value n is ts_number || n is ts_call && n.callee is ts_identifier
|
|
94
|
+
&& contains(["Number", "parseInt", "parseFloat"], n.callee.text)
|
|
95
|
+
}
|
|
96
|
+
fn suggestion_coercion_string(n) = {
|
|
97
|
+
value n: unparenthesised_node(n)
|
|
98
|
+
value n is (ts_string, ts_template) || n is ts_call && n.callee is ts_identifier && n.callee.text == "String"
|
|
99
|
+
}
|
|
100
|
+
fn suggestion_coercion_empty(n) = {
|
|
101
|
+
value n: unparenthesised_node(n)
|
|
102
|
+
value n is (ts_string, ts_template) && n.literal_value.type == "string" && n.literal_value.value == ""
|
|
103
|
+
}
|
|
104
|
+
fn suggestion_coercion_operand(n) = {
|
|
105
|
+
value n: unparenthesised_node(n)
|
|
106
|
+
value if n is ts_sequence then "(" n.text ")" else n.text
|
|
107
|
+
}
|
|
108
|
+
fn suggestion_coercion_one(n) = n is ts_number && js_number(n.literal_value) == 1
|
|
109
|
+
fn suggestion_implicit_coercion(source, n) = {
|
|
110
|
+
value argument: unparenthesised_node(n.argument.node)
|
|
111
|
+
value left: unparenthesised_node(n.left.node)
|
|
112
|
+
value right: unparenthesised_node(n.right.node)
|
|
113
|
+
value if n is ts_unary then when {
|
|
114
|
+
contains(["!", "-"], n.operator) && argument is ts_unary && argument.operator == n.operator then {
|
|
115
|
+
value inner: argument.argument.node
|
|
116
|
+
value if n.operator == "!" then {kind: "boolean", operator: "!!", recommendation: "Boolean(" suggestion_coercion_operand(inner) ")"}
|
|
117
|
+
else if !suggestion_coercion_numeric(inner) then {kind: "number", operator: "- -", recommendation: "Number(" suggestion_coercion_operand(inner) ")"}
|
|
118
|
+
else null
|
|
119
|
+
}
|
|
120
|
+
n.operator == "+" && !suggestion_coercion_numeric(argument) then {
|
|
121
|
+
kind: "number", operator: "+", recommendation: "Number(" suggestion_coercion_operand(argument) ")"
|
|
122
|
+
}
|
|
123
|
+
n.operator == "~" && argument is ts_call then {
|
|
124
|
+
value raw_callee: argument.callee.node
|
|
125
|
+
value callee: unparenthesised_node(raw_callee)
|
|
126
|
+
value optional: argument.optional == "?." || raw_callee not ts_parenthesized && callee.optional == "?."
|
|
127
|
+
value if contains(["indexOf", "lastIndexOf"], callee.static_name) then {
|
|
128
|
+
kind: "boolean", operator: "~", recommendation: argument.text (if optional then " >= 0" else " !== -1")
|
|
129
|
+
} else null
|
|
130
|
+
}
|
|
131
|
+
else then null
|
|
132
|
+
} else if n is (ts_binary, ts_assignment) then when {
|
|
133
|
+
n.operator == "*" then {
|
|
134
|
+
value fraction: suggestion_coercion_one(right) && n is ts_binary.left && n.parent.operator == "/"
|
|
135
|
+
value operand: find([right, left], item => item not ts_binary && !suggestion_coercion_numeric(item))
|
|
136
|
+
value if !fraction && (suggestion_coercion_one(left) || suggestion_coercion_one(right)) && operand != null
|
|
137
|
+
then {kind: "number", operator: "*", recommendation: "Number(" suggestion_coercion_operand(operand) ")"}
|
|
138
|
+
else null
|
|
139
|
+
}
|
|
140
|
+
n.operator == "-" && right is ts_number && js_number(right.literal_value) == 0 && !suggestion_coercion_numeric(left) then {
|
|
141
|
+
kind: "number", operator: "-", recommendation: "Number(" suggestion_coercion_operand(left) ")"
|
|
142
|
+
}
|
|
143
|
+
n.operator == "+" && (suggestion_coercion_empty(left) && !suggestion_coercion_string(right)
|
|
144
|
+
|| suggestion_coercion_empty(right) && !suggestion_coercion_string(left)) then {
|
|
145
|
+
value operand: if suggestion_coercion_empty(left) then right else left
|
|
146
|
+
value {kind: "string", operator: "+", recommendation: "String(" suggestion_coercion_operand(operand) ")"}
|
|
147
|
+
}
|
|
148
|
+
n.operator == "+=" && suggestion_coercion_empty(right) then {
|
|
149
|
+
kind: "string", operator: "+", recommendation: left.text " = String(" left.text ")"
|
|
150
|
+
}
|
|
151
|
+
else then null
|
|
152
|
+
} else if n is ts_template && n.parent not ts_tagged_template && count(n.expression) == 1 then {
|
|
153
|
+
value expression: first(n.expression.nodes)
|
|
154
|
+
value prefix: trim_end(byte_slice(source, n.offset + 1, expression.offset))
|
|
155
|
+
value suffix: trim_start(byte_slice(source, expression.end_offset, n.end_offset - 1))
|
|
156
|
+
value head: js_string_parts("`" slice(prefix, 0, -2) "`")
|
|
157
|
+
value tail: js_string_parts("`" slice(suffix, 1) "`")
|
|
158
|
+
value if !suggestion_coercion_string(expression) && ends_with(prefix, "${{") && starts_with(suffix, "}}")
|
|
159
|
+
&& head != null && tail != null && head.text == "" && tail.text == ""
|
|
160
|
+
then {kind: "template", operator: "", recommendation: "String(" suggestion_coercion_operand(expression) ")"}
|
|
161
|
+
else null
|
|
162
|
+
} else null
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
in ts_unary, ts_binary, ts_assignment, ts_template rule es-no-implicit-coercion {
|
|
166
|
+
option boolean boolean = true "check boolean conversions"
|
|
167
|
+
option number boolean = true "check number conversions"
|
|
168
|
+
option string boolean = true "check string conversions"
|
|
169
|
+
option disallowTemplateShorthand boolean = false "check single-expression templates"
|
|
170
|
+
option allow string[] = [] "shorthand operators to allow"
|
|
171
|
+
let coercion_info suggestion_implicit_coercion(source, node)
|
|
172
|
+
when coercion_info != null
|
|
173
|
+
when coercion_info.kind == "boolean" && boolean || coercion_info.kind == "number" && number || coercion_info.kind == "string" && string || coercion_info.kind == "template" && disallowTemplateShorthand
|
|
174
|
+
when coercion_info.kind == "template" || !contains(allow, coercion_info.operator)
|
|
175
|
+
warn "Unexpected implicit coercion encountered. Use `{coercion_info.recommendation}` instead."
|
|
176
|
+
let program ts_module_program(node)
|
|
177
|
+
let coercion_boolean_fixable coercion_info.operator == "!!" && ts_global_visible(node, "Boolean") == false
|
|
178
|
+
&& contains(["readonly", "writable"], ts_global_setting(program, "Boolean"))
|
|
179
|
+
&& !contains(program.ts_global_written, "Boolean")
|
|
180
|
+
&& expression_fix_retains_comments(node, coercion_info.recommendation)
|
|
181
|
+
fix if coercion_boolean_fixable as expression_fix_edges(node, coercion_info.recommendation)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fn assignment_fix_reference(n) = {
|
|
185
|
+
value bare: unparenthesised_node(n)
|
|
186
|
+
value bare is ts_identifier
|
|
187
|
+
|| bare is (ts_member, ts_index) && bare.optional == null
|
|
188
|
+
&& bare.object is (ts_identifier, ts_this)
|
|
189
|
+
&& (bare is ts_member || unparenthesised_node(bare.index.node) is (ts_string, ts_number, ts_bigint, ts_boolean, ts_null, ts_regex))
|
|
190
|
+
}
|
|
191
|
+
fn assignment_fix_rank(operator) = get({
|
|
192
|
+
"|": 6, "^": 7, "&": 8, "<<": 11, ">>": 11, ">>>": 11,
|
|
193
|
+
"+": 12, "-": 12, "*": 13, "/": 13, "%": 13, "**": 14
|
|
194
|
+
}, operator)
|
|
195
|
+
fn assignment_fix_text(source, n, mode, shorthand) = {
|
|
196
|
+
value binary: unparenthesised_node(n.right.node)
|
|
197
|
+
value before: byte_slice(source, n.offset, n.operator.offset)
|
|
198
|
+
value when {
|
|
199
|
+
!assignment_fix_reference(n.left.node) then null
|
|
200
|
+
mode == "always" then {
|
|
201
|
+
value if binary not ts_binary || n.left.reference_key_strict != binary.left.reference_key_strict
|
|
202
|
+
|| !assignment_fix_reference(binary.left.node)
|
|
203
|
+
|| !expression_fix_clean({ offset: n.operator.end_offset, end_offset: binary.operator.offset }) then null
|
|
204
|
+
else before shorthand byte_slice(source, binary.operator.end_offset, binary.end_offset)
|
|
205
|
+
}
|
|
206
|
+
else then {
|
|
207
|
+
value operator: slice(n.operator, 0, -1)
|
|
208
|
+
value if !expression_fix_clean({ offset: n.offset, end_offset: n.operator.offset }) then null
|
|
209
|
+
else {
|
|
210
|
+
value leading: byte_slice(source, n.operator.end_offset, n.right.offset)
|
|
211
|
+
value needed: n.right not ts_parenthesized && n.right.precedence <= assignment_fix_rank(operator)
|
|
212
|
+
value following: byte_slice(source, n.operator.end_offset, n.end_offset)
|
|
213
|
+
value right: if needed then leading expression_fix_wrap(n.right.text, true)
|
|
214
|
+
else expression_fix_separator(operator, following) following
|
|
215
|
+
value before "= " before operator right
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
in ts_assignment rule es-operator-assignment {
|
|
221
|
+
option mode ("always" | "never") = "always" "compound arithmetic assignment policy"
|
|
222
|
+
let assigned_key left.reference_key_strict
|
|
223
|
+
let shorthand if operator == "=" && right.arithmetic_operator != null && assigned_key != null && (assigned_key == right.strict_left_reference_key || contains(["*", "&", "^", "|"], right.arithmetic_operator) && assigned_key == right.strict_right_reference_key) then right.arithmetic_operator "=" else null
|
|
224
|
+
when if mode == "never" then operator != "=" && !contains(["&&=", "||=", "??="], operator) else shorthand != null
|
|
225
|
+
warn if mode == "never" then "Unexpected operator assignment ({operator}) shorthand." else "Assignment (=) can be replaced with operator assignment ({shorthand})."
|
|
226
|
+
let replacement assignment_fix_text(source, node, mode, shorthand)
|
|
227
|
+
fix if replacement != null && expression_fix_retains_comments(node, replacement) as replacement
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Function forms, bindings, bodies and calls.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_return let return_value_object (argument.unwrapped_node.nonempty ?? argument.node) is ts_object
|
|
6
|
+
in ts_return let return_value_braced { value result: "{{"; value starts_with(argument, result) }
|
|
7
|
+
in ts_block let single_return count([] body) == 1 && first(body.nodes) is ts_return
|
|
8
|
+
in ts_block let return_value_object first([] body.return_value_object) == true
|
|
9
|
+
in ts_block let return_value_braced first([] body.return_value_braced) == true
|
|
10
|
+
|
|
11
|
+
in ts_function let function_this function_uses_this(node)
|
|
12
|
+
in ts_member let bind_property_site property.node
|
|
13
|
+
in ts_index let bind_property_site index.node
|
|
14
|
+
in ts_member, ts_index let bind_target when {
|
|
15
|
+
static_name != "bind" then null
|
|
16
|
+
(object.unwrapped_node.nonempty ?? object.node) is ts_arrow || (object.unwrapped_node.nonempty ?? object.node) is ts_function && object.function_this != true then bind_property_site
|
|
17
|
+
else then null
|
|
18
|
+
}
|
|
19
|
+
in ts_parenthesized forward function_this, bind_target from expression
|
|
20
|
+
|
|
21
|
+
in ts_parameter let required_parameter init == null && optional == null && name not ts_rest && !starts_with(text, "...")
|
|
22
|
+
in ts_function, ts_arrow, ts_method let parameter_required [] parameter.required_parameter
|
|
23
|
+
|
|
24
|
+
in ts_node let return_assignment_context when {
|
|
25
|
+
parent is ts_return then { arrow: false, offset: parent.offset }
|
|
26
|
+
is ts_arrow.body then { arrow: true, offset: parent.offset }
|
|
27
|
+
parent is (ts_function, ts_method, ts_arrow, ts_class, ts_block, ts_static_block, ts_expression_statement, ts_variable_declaration, ts_if, ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_switch, ts_throw, ts_with, ts_labeled, ts_try, ts_export) then null
|
|
28
|
+
else then parent.return_assignment_context
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
in ts_unary let void_operator operator == "void"
|
|
32
|
+
in ts_parenthesized let inner_null_receiver expression.null_call_receiver
|
|
33
|
+
in ts_node let null_call_receiver if is ts_parenthesized then inner_null_receiver else is ts_null || is ts_identifier && text == "undefined" || is ts_unary && void_operator == true
|
|
34
|
+
in ts_member, ts_index let member_receiver_tokens object.unparenthesised_tokens
|
|
35
|
+
in ts_member, ts_index let invoked_receiver if object == null then null else { member: (object.unwrapped_node.nonempty ?? object.node) is (ts_member, ts_index), tokens: object.member_receiver_tokens }
|
|
36
|
+
in ts_member let direct_method_name property
|
|
37
|
+
in ts_member, ts_index let called_method_name static_name
|
|
38
|
+
in ts_parenthesized forward member_receiver_tokens, invoked_receiver, direct_method_name, called_method_name from expression
|
|
39
|
+
fn unchanged_call_receiver(receiver, tokens, null_receiver) = receiver != null && (if receiver.member then receiver.tokens == tokens else null_receiver == true)
|
|
40
|
+
|
|
41
|
+
# `.bind(x)` on a function that never uses `this` (or on an arrow).
|
|
42
|
+
fn suggestion_bind_fix(n) = {
|
|
43
|
+
value wrapped_callee: n.callee.node
|
|
44
|
+
value member: unparenthesised_node(wrapped_callee)
|
|
45
|
+
value receiver: member.object.node
|
|
46
|
+
value property_start: receiver.token_neighbours.after
|
|
47
|
+
value argument_start: wrapped_callee.token_neighbours.after
|
|
48
|
+
value argument: first(n.argument.nodes)
|
|
49
|
+
value bare_argument: unparenthesised_node(argument)
|
|
50
|
+
value region: {offset: property_start.start, end_offset: n.end_offset}
|
|
51
|
+
value edits: [
|
|
52
|
+
{start: property_start.start, end: member.end_offset, text: ""},
|
|
53
|
+
{start: argument_start.start, end: n.end_offset, text: ""}
|
|
54
|
+
]
|
|
55
|
+
value suffix: byte_slice(n.text, property_start.start - n.offset)
|
|
56
|
+
value if property_start == null || argument_start == null
|
|
57
|
+
|| bare_argument not (ts_string, ts_number, ts_boolean, ts_null, ts_bigint, ts_regex, ts_identifier, ts_this, ts_function)
|
|
58
|
+
|| !expression_fix_clean(region) then null
|
|
59
|
+
else {start: property_start.start, end: n.end_offset, text: expression_fix_edits(suffix, property_start.start, edits)}
|
|
60
|
+
}
|
|
61
|
+
in ts_call rule es-no-extra-bind {
|
|
62
|
+
when count(argument) == 1 && first(argument.nodes) not ts_spread && callee.bind_target != null
|
|
63
|
+
report at callee.bind_target
|
|
64
|
+
warn "The function binding is unnecessary."
|
|
65
|
+
let replacement suggestion_bind_fix(node)
|
|
66
|
+
fix at replacement ?? node if replacement != null as replacement.text
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
in ts_identifier rule es-prefer-rest-params {
|
|
70
|
+
when text == "arguments" && resolved == "local" && declaration == null && (ts_function != null || ts_method != null)
|
|
71
|
+
when !(parent is ts_member && index == 0)
|
|
72
|
+
warn "Use the rest parameters instead of 'arguments'."
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
in ts_call rule es-prefer-spread {
|
|
76
|
+
when callee.called_method_name == "apply" && count(argument) == 2
|
|
77
|
+
let second_argument_node {
|
|
78
|
+
value [] argument.unwrapped_node
|
|
79
|
+
slice 1
|
|
80
|
+
first
|
|
81
|
+
nonempty
|
|
82
|
+
} ?? { value argument.nodes; slice 1; first }
|
|
83
|
+
when second_argument_node not (ts_array, ts_spread)
|
|
84
|
+
when {
|
|
85
|
+
value [] argument.unparenthesised_tokens
|
|
86
|
+
tokens: first
|
|
87
|
+
value [] argument.null_call_receiver
|
|
88
|
+
null_receiver: first
|
|
89
|
+
value unchanged_call_receiver(callee.invoked_receiver, tokens, null_receiver)
|
|
90
|
+
}
|
|
91
|
+
warn "Use the spread operator instead of '.apply()'."
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
in ts_parameter rule es-default-param-last {
|
|
95
|
+
when parent is (ts_function, ts_arrow, ts_method) && (init != null || optional != null)
|
|
96
|
+
when { value parent.parameter_required; slice (index + 1); any (p) p == true }
|
|
97
|
+
warn "Default parameters should be last."
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fn suggestion_function_style(n) = {
|
|
101
|
+
value declaration: n is ts_function && n is (ts_node.body, ts_node.declaration, ts_if.consequent, ts_if.alternate)
|
|
102
|
+
value outer: outer_parenthesised_node(n)
|
|
103
|
+
value holder: if declaration then n else if outer.parent is ts_declarator then outer.parent.node else null
|
|
104
|
+
value export: if declaration then n.parent.node else holder.parent.parent.node
|
|
105
|
+
value exported: export is ts_export
|
|
106
|
+
value default_export: exported && export.kind == "default"
|
|
107
|
+
value owner: if exported || n.parent is ts_case then n.parent.parent.node else n.parent.node
|
|
108
|
+
value siblings: flat_map(owner.children.nodes, sibling => if sibling is ts_case then sibling.body.nodes else [sibling])
|
|
109
|
+
value declarations: map(siblings, sibling => if sibling is ts_export then sibling.declaration.node ?? sibling else sibling)
|
|
110
|
+
value overloaded: declaration && n.name.node != null && any(declarations, sibling =>
|
|
111
|
+
sibling is ts_function && sibling.body.node == null && sibling.name.text == n.name.text)
|
|
112
|
+
value nested: walk(n.children.nodes, current =>
|
|
113
|
+
if current is (ts_function, ts_arrow, ts_method) then [] else current.children.nodes)
|
|
114
|
+
value if holder == null then null else {
|
|
115
|
+
declaration: declaration,
|
|
116
|
+
exported: exported && !default_export,
|
|
117
|
+
exempt: default_export || overloaded || n.body.node == null,
|
|
118
|
+
arrow: n is ts_arrow,
|
|
119
|
+
captures: n is ts_arrow && any(nested, current => current is (ts_this, ts_super)),
|
|
120
|
+
annotated: holder.type.node != null,
|
|
121
|
+
offset: holder.offset
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
in ts_function, ts_arrow rule es-func-style {
|
|
126
|
+
option style ("declaration" | "expression") = "expression" "function syntax policy"
|
|
127
|
+
option allowArrowFunctions boolean = false "allow arrow expressions"
|
|
128
|
+
option allowTypeAnnotation boolean = false "allow annotated function variables"
|
|
129
|
+
option overrides { namedExports: "" | "declaration" | "expression" | "ignore" } = { namedExports: "" } "named export policy"
|
|
130
|
+
let function_info suggestion_function_style(node)
|
|
131
|
+
when function_info != null && !function_info.exempt
|
|
132
|
+
let requested_style if function_info.exported && overrides.namedExports != "" then overrides.namedExports else style
|
|
133
|
+
when requested_style == "expression" && function_info.declaration || requested_style == "declaration" && !function_info.declaration && !(function_info.arrow && (allowArrowFunctions || function_info.captures)) && !(allowTypeAnnotation && function_info.annotated)
|
|
134
|
+
report at function_info.offset
|
|
135
|
+
warn "Expected a function {requested_style}."
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
in ts_assignment rule es-no-return-assign {
|
|
139
|
+
option mode ("except-parens" | "always") = "except-parens" "assignment return policy"
|
|
140
|
+
let return_info return_assignment_context
|
|
141
|
+
when return_info != null && (mode == "always" || parent not ts_parenthesized)
|
|
142
|
+
report at return_info.offset
|
|
143
|
+
warn if return_info.arrow then "Arrow function should not return assignment." else "Return statement should not contain assignment."
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
fn arrow_body_expression_fix(source, n) = if n.body.single_return != true || first(n.body.body.nodes).argument == null then null else {
|
|
147
|
+
value body: n.body.node
|
|
148
|
+
value statement: first(body.body.nodes)
|
|
149
|
+
value argument: statement.argument.node
|
|
150
|
+
value after: body.token_neighbours.after.text
|
|
151
|
+
value hazard: after != null && expression_fix_continuation(after)
|
|
152
|
+
value parens: expression_fix_parens(source, body, argument)
|
|
153
|
+
value initialiser: { value n; walk (current) current.parent.node; any (current) current is ts_for.init }
|
|
154
|
+
value binaries: overlapping(ts_binary, body.offset, body.end_offset)
|
|
155
|
+
value has_in: any(binaries, binary => binary.operator == "in")
|
|
156
|
+
value wrap: argument not ts_parenthesized && (parens > 0 || initialiser && has_in)
|
|
157
|
+
value semicolons: overlapping(ts_semicolon, argument.end_offset, body.end_offset)
|
|
158
|
+
value semicolon: first(semicolons)
|
|
159
|
+
value last_end: semicolon.end_offset ?? argument.end_offset
|
|
160
|
+
value comments: overlapping(ts_comment, body.offset, body.end_offset)
|
|
161
|
+
value outside_comments: any(comments, comment => comment.offset < argument.offset || comment.offset >= argument.end_offset)
|
|
162
|
+
value edits: if outside_comments then
|
|
163
|
+
[{ start: body.offset, end: body.offset + 1, text: "" },
|
|
164
|
+
{ start: statement.offset, end: statement.offset + 6, text: "" },
|
|
165
|
+
{ start: body.end_offset - 1, end: body.end_offset, text: "" }]
|
|
166
|
+
else [{ start: body.offset, end: argument.offset, text: "" }, { start: last_end, end: body.end_offset, text: "" }]
|
|
167
|
+
value punctuation: ((if semicolon != null then [{ start: semicolon.offset, end: semicolon.end_offset, text: "" }] else [])
|
|
168
|
+
(if wrap then [{ start: argument.offset, end: argument.offset, text: "(" }, { start: last_end, end: last_end, text: ")" }] else []))
|
|
169
|
+
value changes: punctuation edits
|
|
170
|
+
value replacement: expression_fix_edits(body.text, body.offset, changes)
|
|
171
|
+
value if argument == null || hazard || parens == null || !expression_fix_retains_comments(body, replacement) then null
|
|
172
|
+
else { start: body.offset, end: body.end_offset, text: replacement }
|
|
173
|
+
}
|
|
174
|
+
fn arrow_body_block_fix(source, n) = {
|
|
175
|
+
value body: n.body.node
|
|
176
|
+
value tokens: overlapping(ts_open_paren, body.offset, body.offset + 1)
|
|
177
|
+
value opening: first(tokens)
|
|
178
|
+
value next: opening.token_neighbours.after
|
|
179
|
+
value objects: if next.text == "{{" then overlapping(ts_object, next.start, next.end) else []
|
|
180
|
+
value object: any(objects, candidate => candidate.offset == next.start)
|
|
181
|
+
value newline: object && { value source; byte_slice opening.end_offset next.start; re_test /[\n\r\u{2028}\u{2029}]/ }
|
|
182
|
+
value edits: if object then
|
|
183
|
+
([{ start: opening.offset, end: opening.end_offset, text: if newline then "{{" else "{{return " },
|
|
184
|
+
{ start: opening.paired_token.start, end: opening.paired_token.end, text: "" },
|
|
185
|
+
{ start: body.end_offset, end: body.end_offset, text: "}}" }]
|
|
186
|
+
(if newline then [{ start: next.start, end: next.start, text: "return " }] else []))
|
|
187
|
+
else [{ start: body.offset, end: body.offset, text: "{{return " }, { start: body.end_offset, end: body.end_offset, text: "}}" }]
|
|
188
|
+
value replacement: expression_fix_edits(body.text, body.offset, edits)
|
|
189
|
+
value if !expression_fix_retains_comments(body, replacement) then null
|
|
190
|
+
else { start: body.offset, end: body.end_offset, text: replacement }
|
|
191
|
+
}
|
|
192
|
+
in ts_arrow rule es-arrow-body-style {
|
|
193
|
+
option mode ("always" | "never" | "as-needed") = "as-needed" "arrow body block policy"
|
|
194
|
+
option requireReturnForObjectLiteral boolean = false "require a block for object values"
|
|
195
|
+
when body != null
|
|
196
|
+
let body_block body is ts_block
|
|
197
|
+
let body_object if body_block then body.return_value_object == true else (body.unwrapped_node.nonempty ?? body.node) is ts_object
|
|
198
|
+
when body_block && (mode == "never" || mode == "as-needed" && body.single_return == true && !(requireReturnForObjectLiteral && body_object)) || !body_block && (mode == "always" || mode == "as-needed" && requireReturnForObjectLiteral && body_object)
|
|
199
|
+
report at unparenthesised_node(body.node)
|
|
200
|
+
warn when {
|
|
201
|
+
!body_block then "Expected block statement surrounding arrow body."
|
|
202
|
+
body.statement_count == 0 then "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`."
|
|
203
|
+
body.single_return != true then "Unexpected block statement surrounding arrow body."
|
|
204
|
+
body.return_value_braced == true then "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`."
|
|
205
|
+
else then "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`."
|
|
206
|
+
}
|
|
207
|
+
let edit if body_block then arrow_body_expression_fix(source, node) else arrow_body_block_fix(source, node)
|
|
208
|
+
fix at (edit ?? { start: body.offset, end: body.end_offset }) if edit != null as edit.text
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
fn empty_function_constructor(n) = n is ts_method && n.parent is ts_class && n.key.text == "constructor" && !contains(n.modifier, "static")
|
|
212
|
+
fn empty_function_category(n, constructor) = when {
|
|
213
|
+
n is ts_arrow then "arrowFunctions"
|
|
214
|
+
constructor then "constructors"
|
|
215
|
+
n is ts_method && n.kind == "get" then "getters"
|
|
216
|
+
n is ts_method && n.kind == "set" then "setters"
|
|
217
|
+
contains(n.modifier, "*") then (if n is ts_method then "generatorMethods" else "generatorFunctions")
|
|
218
|
+
contains(n.modifier, "async") then (if n is ts_method then "asyncMethods" else "asyncFunctions")
|
|
219
|
+
n is ts_method then "methods"
|
|
220
|
+
else then "functions"
|
|
221
|
+
}
|
|
222
|
+
fn empty_function_decorated(n, constructor) = n is ts_method && !constructor && count(n.decorator.node) > 0
|
|
223
|
+
|
|
224
|
+
in ts_function, ts_arrow, ts_method rule es-no-empty-function {
|
|
225
|
+
option allow string[] = [] "function categories that may be empty"
|
|
226
|
+
when body.uncommented_empty_block == true
|
|
227
|
+
let constructor empty_function_constructor(node)
|
|
228
|
+
when !constructor || !any(parameter.nodes, param => count(param.modifier) > 0)
|
|
229
|
+
when !{
|
|
230
|
+
value node
|
|
231
|
+
empty_function_category: empty_function_category constructor
|
|
232
|
+
value contains(allow, empty_function_category)
|
|
233
|
+
}
|
|
234
|
+
when !(constructor && contains(modifier, "private") && (contains(allow, "privateConstructors") || contains(allow, "private-constructors")))
|
|
235
|
+
when !(constructor && contains(modifier, "protected") && (contains(allow, "protectedConstructors") || contains(allow, "protected-constructors")))
|
|
236
|
+
when !(empty_function_decorated(node, constructor) && contains(allow, "decoratedFunctions"))
|
|
237
|
+
when !(is ts_method && !constructor && contains(modifier, "override") && contains(allow, "overrideMethods"))
|
|
238
|
+
report at body.node
|
|
239
|
+
warn "Unexpected empty { function_name.nonempty ?? 'function' }."
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
in ts_call rule es-no-useless-call {
|
|
243
|
+
let method_name callee.direct_method_name
|
|
244
|
+
when method_name == "call" && count(argument) > 0 || method_name == "apply" && count(argument) == 2 && ({
|
|
245
|
+
value [] argument.unwrapped_node
|
|
246
|
+
slice 1
|
|
247
|
+
first
|
|
248
|
+
nonempty
|
|
249
|
+
} ?? { value argument.nodes; slice 1; first }) is ts_array
|
|
250
|
+
when {
|
|
251
|
+
value [] argument.unparenthesised_tokens
|
|
252
|
+
tokens: first
|
|
253
|
+
value [] argument.null_call_receiver
|
|
254
|
+
null_receiver: first
|
|
255
|
+
value unchanged_call_receiver(callee.invoked_receiver, tokens, null_receiver)
|
|
256
|
+
}
|
|
257
|
+
warn "Unnecessary '.{method_name}()'."
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
}
|