@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,257 @@
|
|
|
1
|
+
# Loops, branches, reachability, returns and switch fallthrough.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn problem_update_direction(update) =
|
|
6
|
+
if update is ts_update then (if update.operator == "++" then 1 else -1)
|
|
7
|
+
else if contains(["+=", "-="], update.operator) then {
|
|
8
|
+
value number: ts_runtime_number(update.right.node)
|
|
9
|
+
value sign: if number != null && number > 0 then 1 else if number != null && number < 0 then -1 else 0
|
|
10
|
+
value if update.operator == "+=" then sign else -sign
|
|
11
|
+
} else 0
|
|
12
|
+
fn problem_counter_wrong(update, counter, wrong) = {
|
|
13
|
+
value name: unparenthesised_node(counter)
|
|
14
|
+
value changes: {
|
|
15
|
+
value walk(update, child => if child is ts_parenthesized then child.expression.nodes
|
|
16
|
+
else if child is ts_sequence then child.expression.nodes else [])
|
|
17
|
+
filter (child) child is (ts_update, ts_assignment) && {
|
|
18
|
+
value target: unparenthesised_node(if child is ts_update then child.argument.node else child.left.node)
|
|
19
|
+
value target is ts_identifier && target.identifier_name == name.identifier_name
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
value name is ts_identifier && count(changes) == 1 && problem_update_direction(first(changes)) == wrong
|
|
23
|
+
}
|
|
24
|
+
in ts_for rule es-for-direction {
|
|
25
|
+
let comparison unparenthesised_node(test.node)
|
|
26
|
+
when comparison is ts_binary && update != null
|
|
27
|
+
when contains(["<", "<=", ">", ">="], comparison.operator)
|
|
28
|
+
let wrong if contains(["<", "<="], comparison.operator) then -1 else 1
|
|
29
|
+
when problem_counter_wrong(update.node, comparison.left.node, wrong)
|
|
30
|
+
|| problem_counter_wrong(update.node, comparison.right.node, -wrong)
|
|
31
|
+
report at { start: offset, end: body.node.token_neighbours.before.end }
|
|
32
|
+
warn "The update clause in this loop moves the variable in the wrong direction."
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
# `if (true)`, `while (1)`, `do ... while (false)`, `false ? a : b`.
|
|
36
|
+
in ts_if.test, ts_conditional.test rule es-no-constant-condition {
|
|
37
|
+
when constant_truthiness
|
|
38
|
+
warn "Unexpected constant condition."
|
|
39
|
+
}
|
|
40
|
+
in ts_node let problem_declared_yields flow_declared_effects(node, "yielded")
|
|
41
|
+
fn problem_loop_yields(loop) = {
|
|
42
|
+
value events: flow_entry(loop).problem_declared_yields
|
|
43
|
+
value events == null || any(events, event => event.site.offset >= loop.offset && event.site.end_offset <= loop.end_offset
|
|
44
|
+
&& !(loop is ts_for && loop.init != null && event.site.offset >= loop.init.offset && event.site.end_offset <= loop.init.end_offset))
|
|
45
|
+
}
|
|
46
|
+
in ts_while, ts_do_while, ts_for let constant_loop_condition_checked !(is ts_while && re_test(test, /^[(\s]*true[)\s]*$/))
|
|
47
|
+
in ts_while.test, ts_do_while.test, ts_for.test rule es-no-constant-condition:loop {
|
|
48
|
+
when constant_truthiness && parent.constant_loop_condition_checked && !problem_loop_yields(parent.node)
|
|
49
|
+
warn "Unexpected constant condition."
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn problem_condition_parts(n, operator) = {
|
|
53
|
+
value walk(unparenthesised_node(n), part => if part is ts_logical && part.operator == operator
|
|
54
|
+
then [unparenthesised_node(part.left.node), unparenthesised_node(part.right.node)] else [])
|
|
55
|
+
filter (part) part not ts_logical || part.operator != operator
|
|
56
|
+
}
|
|
57
|
+
fn problem_condition_equal(a, b) = {
|
|
58
|
+
value left: unparenthesised_node(a)
|
|
59
|
+
value right: unparenthesised_node(b)
|
|
60
|
+
value kind_name(left) == kind_name(right) && (if left is ts_logical
|
|
61
|
+
&& contains(["&&", "||"], left.operator) && left.operator == right.operator then
|
|
62
|
+
problem_condition_equal(left.left.node, right.left.node) && problem_condition_equal(left.right.node, right.right.node)
|
|
63
|
+
|| problem_condition_equal(left.left.node, right.right.node) && problem_condition_equal(left.right.node, right.left.node)
|
|
64
|
+
else left.token_texts == right.token_texts)
|
|
65
|
+
}
|
|
66
|
+
fn problem_condition_covered(condition, previous) = {
|
|
67
|
+
value problem_condition_parts(condition, "||")
|
|
68
|
+
all (operand) {
|
|
69
|
+
value terms: problem_condition_parts(operand, "&&")
|
|
70
|
+
value any(previous, earlier => all(earlier, term => any(terms, current => problem_condition_equal(term, current))))
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
fn problem_duplicate_condition(branch) = {
|
|
74
|
+
value previous: {
|
|
75
|
+
value walk(branch, child => if child.parent is ts_if && child.parent.alternate.node == child then child.parent.node else null)
|
|
76
|
+
drop 1
|
|
77
|
+
flat_map (earlier) problem_condition_parts(earlier.test.node, "||")
|
|
78
|
+
map (operand) problem_condition_parts(operand, "&&")
|
|
79
|
+
}
|
|
80
|
+
value condition: unparenthesised_node(branch.test.node)
|
|
81
|
+
value problem_condition_covered(condition, previous) || condition is ts_logical && condition.operator == "&&"
|
|
82
|
+
&& any(problem_condition_parts(condition, "&&"), part => problem_condition_covered(part, previous))
|
|
83
|
+
}
|
|
84
|
+
in ts_if.test rule es-no-dupe-else-if {
|
|
85
|
+
when problem_duplicate_condition(parent.node)
|
|
86
|
+
report at unparenthesised_node(node)
|
|
87
|
+
warn "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain."
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
in ts_case let problem_case_tokens if test != null then unparenthesised_node(test.node).token_texts else null
|
|
91
|
+
in ts_case rule es-no-duplicate-case {
|
|
92
|
+
when problem_case_tokens != null
|
|
93
|
+
when { value parent.case.problem_case_tokens; slice 0 index; contains problem_case_tokens }
|
|
94
|
+
warn "Duplicate case label."
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn problem_finally_exit(jump) = {
|
|
98
|
+
value boundary: find(walk(jump.parent.node, ancestor => ancestor.parent.node), ancestor =>
|
|
99
|
+
ancestor is ts_try.finalizer
|
|
100
|
+
|| ancestor is (ts_function_like, ts_class, ts_static_block, ts_class_property)
|
|
101
|
+
|| jump is (ts_break, ts_continue) && (ancestor == jump.problem_label_target
|
|
102
|
+
|| jump.label == null && ancestor is (ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while))
|
|
103
|
+
|| jump is ts_break && jump.label == null && ancestor is ts_switch)
|
|
104
|
+
value boundary is ts_try.finalizer
|
|
105
|
+
}
|
|
106
|
+
in ts_return, ts_throw, ts_break, ts_continue rule es-no-unsafe-finally {
|
|
107
|
+
when problem_finally_exit(node)
|
|
108
|
+
warn "Unsafe usage of {if is ts_return then 'ReturnStatement' else if is ts_throw then 'ThrowStatement' else if is ts_break then 'BreakStatement' else 'ContinueStatement'}."
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn problem_label_context(n) = { value n; walk (current) if current is ts_labeled then current.parent else null; last }
|
|
112
|
+
group problem_label_jump ts_break, ts_continue
|
|
113
|
+
fn problem_jump_label(jump) = {
|
|
114
|
+
value jump.parent
|
|
115
|
+
walk (current) if current is (ts_function_like, ts_static_block) then null else current.parent
|
|
116
|
+
filter (current) current is ts_labeled && current.label.identifier_name == jump.label.identifier_name
|
|
117
|
+
first
|
|
118
|
+
}
|
|
119
|
+
in problem_label_jump let problem_label_target if label != null then problem_jump_label(node) else null
|
|
120
|
+
in ts_labeled let problem_label_used { value descendants(problem_label_jump); any (jump) jump.problem_label_target.offset == offset }
|
|
121
|
+
in ts_labeled.label rule es-no-unused-labels {
|
|
122
|
+
when !parent.problem_label_used
|
|
123
|
+
let label_body parent.body.node
|
|
124
|
+
let label_context problem_label_context(parent.parent)
|
|
125
|
+
let label_expression unparenthesised_node(label_body.expression.node)
|
|
126
|
+
let label_directive label_body is ts_expression_statement
|
|
127
|
+
&& (label_expression is ts_string || label_expression is ts_template && count(label_expression.expression) == 0)
|
|
128
|
+
&& (label_context is ts_program || label_context is ts_block && label_context.parent is ts_function_like)
|
|
129
|
+
let label_previous parent.token_neighbours.before
|
|
130
|
+
let label_asi label_previous != null && !contains([":", ";", "{{"], label_previous.text) && re_test(label_body.text, /^[\[(+\-\/`]/)
|
|
131
|
+
fix at { start: offset, end: label_body.offset } if !space_after.comments && !label_body.space_before.comments && !label_directive && !label_asi as ""
|
|
132
|
+
warn "'{text}:' is defined but never used."
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
in ts_function, ts_method rule es-require-yield {
|
|
136
|
+
let declared_yields flow_declared_effects(body.node, "yielded")
|
|
137
|
+
when contains(modifier, "*") && declared_yields != null && count(declared_yields) == 0 && !{ value body.nonempty ?? ""; re_test /^\{\s*\}$/ }
|
|
138
|
+
report at function_head_site(node)
|
|
139
|
+
warn "This generator function does not have 'yield'."
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
in ts_node let problem_constructor_call_count
|
|
143
|
+
if is ts_method && node.key == "constructor" then 0
|
|
144
|
+
else {
|
|
145
|
+
value count: { value [] children.problem_constructor_call_count; sum }
|
|
146
|
+
value count + (if is ts_call && node.callee is ts_super then 1 else 0)
|
|
147
|
+
}
|
|
148
|
+
fn problem_never_constructed(cls) = {
|
|
149
|
+
value constructors: filter(cls.member.nodes, member => member is ts_method && member.key == "constructor")
|
|
150
|
+
value constructor: first(constructors)
|
|
151
|
+
value constructor != null && constructor.body.problem_constructor_call_count == 0
|
|
152
|
+
}
|
|
153
|
+
group code_path_statement ts_block, ts_break, ts_class, ts_continue, ts_debugger, ts_do_while, ts_expression_statement, ts_for_in, ts_for_of, ts_for, ts_if, ts_labeled, ts_return, ts_switch, ts_throw, ts_try, ts_while, ts_with, ts_export, ts_variable_declaration, ts_class_property
|
|
154
|
+
fn problem_unreachable_statement(n) = n not ts_class_property
|
|
155
|
+
&& (n not ts_variable_declaration || n.kind != "var" || any(n.declaration.nodes, declaration => declaration.init != null))
|
|
156
|
+
fn problem_unreachable_step(state, statement, dead) = {
|
|
157
|
+
value range: state.range
|
|
158
|
+
value inside: range != null && statement.offset >= range.offset && statement.end_offset <= range.end_offset
|
|
159
|
+
value adjacent: range != null && statement.token_neighbours.before.start >= range.offset && statement.token_neighbours.before.start < range.end_offset
|
|
160
|
+
value if dead && inside then state
|
|
161
|
+
else if dead && adjacent then { range: { node: range.node, offset: range.offset, end_offset: max(range.end_offset, statement.end_offset) }, reports: state.reports }
|
|
162
|
+
else {
|
|
163
|
+
range: if dead then { node: statement, offset: statement.offset, end_offset: statement.end_offset } else null,
|
|
164
|
+
reports: state.reports (if range != null then [{ start: range.offset, end: range.end_offset }] else [])
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
fn problem_unreachable_ranges(statements, always_dead) = {
|
|
168
|
+
value state: {
|
|
169
|
+
value statements
|
|
170
|
+
sort_by (statement) statement.offset
|
|
171
|
+
fold ({ range: null, reports: [] }) (state, statement) {
|
|
172
|
+
value dead: always_dead || reachable(statement) == false
|
|
173
|
+
value problem_unreachable_step(state, statement, dead)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
value state.reports (if state.range != null then [{ start: state.range.offset, end: state.range.end_offset }] else [])
|
|
177
|
+
}
|
|
178
|
+
in ts_program let problem_unreachable_runs {
|
|
179
|
+
value statements: { value descendants(code_path_statement); filter (statement) problem_unreachable_statement(statement) }
|
|
180
|
+
value problem_unreachable_ranges(statements, false)
|
|
181
|
+
}
|
|
182
|
+
in ts_program rule es-no-unreachable {
|
|
183
|
+
each statement in problem_unreachable_runs {
|
|
184
|
+
report at statement
|
|
185
|
+
warn "Unreachable code."
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
in ts_class rule es-no-unreachable:derived {
|
|
189
|
+
when extends != null && problem_never_constructed(node)
|
|
190
|
+
let statements filter(member.nodes, property => {
|
|
191
|
+
value modifiers: [] property.modifier
|
|
192
|
+
value property is ts_class_property && !contains(modifiers, "static")
|
|
193
|
+
})
|
|
194
|
+
each statement in problem_unreachable_ranges(statements, true) {
|
|
195
|
+
report at statement
|
|
196
|
+
warn "Unreachable code."
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
in ts_method, ts_function, ts_arrow rule es-getter-return {
|
|
201
|
+
option allowImplicit boolean = false "a bare `return;` counts as returning"
|
|
202
|
+
when accessor == "get" && flow_normal(body.node) == true
|
|
203
|
+
report at function_head_site(node)
|
|
204
|
+
warn if count(scoping_own_returns) == 0 || !allowImplicit && all(scoping_own_returns, returned => returned.argument == null) then "Expected to return a value in {function_name}." else "Expected {function_name} to always return a value."
|
|
205
|
+
}
|
|
206
|
+
in ts_return rule es-getter-return:bare {
|
|
207
|
+
option allowImplicit boolean = false "a bare `return;` counts as returning"
|
|
208
|
+
when argument == null && !allowImplicit && enclosing_accessor == "get"
|
|
209
|
+
warn "Expected to return a value in {enclosing_function_name}."
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
# `case` statements whose end is reachable run into the next case.
|
|
213
|
+
in ts_case let case_path if reachable(node) == true then if flow_normal(node) == true then "falls_through" else if flow_normal(node) == false then "leaves" else null else null
|
|
214
|
+
in ts_case let has_body count(body) > 0
|
|
215
|
+
in ts_switch {
|
|
216
|
+
let case_paths [] case.case_path
|
|
217
|
+
let case_bodies [] case.has_body
|
|
218
|
+
}
|
|
219
|
+
# The last comment before this case, or before the closing brace of a case
|
|
220
|
+
# whose only statement is a block.
|
|
221
|
+
@macro last_comment(t) re_find(t, /(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\r\n]*)\s*$/)
|
|
222
|
+
@macro block_case /^\s*(?:case\b[\s\S]*?|default\s*):\s*\{([\s\S]*)\}\s*$/
|
|
223
|
+
in ts_case let case_comment_in_block { value previous_sibling.nonempty ?? ""; re_test @block_case }
|
|
224
|
+
in ts_case let case_comment_block_source
|
|
225
|
+
if case_comment_in_block then re_replace(previous_sibling, /\}\s*$/, "")
|
|
226
|
+
else ""
|
|
227
|
+
in ts_case let case_comment_in_block_text @last_comment(case_comment_block_source)
|
|
228
|
+
in ts_case let case_comment_source
|
|
229
|
+
if case_comment_in_block_text != null then case_comment_block_source else gap_before.nonempty ?? ""
|
|
230
|
+
in ts_case let case_comment @last_comment(case_comment_source)
|
|
231
|
+
in ts_case let case_comment_offset offset - len(gap_before.nonempty ?? "") - (if case_comment_in_block_text != null then len(previous_sibling) else 0) + len(case_comment_source) - len(case_comment.nonempty ?? "")
|
|
232
|
+
in ts_case rule es-no-fallthrough {
|
|
233
|
+
option allowEmptyCase boolean = false "allow empty cases separated by blank lines"
|
|
234
|
+
option commentPattern regex | null = null "comments that allow falling through (default: /falls?\\s?through/i)"
|
|
235
|
+
option reportUnusedFallthroughComment boolean = false "report such comments where nothing falls through"
|
|
236
|
+
when index > 0
|
|
237
|
+
let previous_path { value parent.case_paths; slice 0 index; last }
|
|
238
|
+
when previous_path == "falls_through"
|
|
239
|
+
let previous_has_body { value parent.case_bodies; slice 0 index; last }
|
|
240
|
+
when previous_has_body || !allowEmptyCase && { value gap_before.nonempty ?? ""; re_count /\n/ } >= 2
|
|
241
|
+
let comment_body { value case_comment.nonempty ?? ""; re_replace /^\/[*\/]|\*\/$/ ""; trim }
|
|
242
|
+
let allowing case_comment != null && { value default: commentPattern.nonempty ?? "(?i)falls?\\s?through"; value re_test(comment_body, default) } && !re_test(comment_body, /^(?:eslint[- ]|(?:globals?|exported) )/)
|
|
243
|
+
when !allowing
|
|
244
|
+
warn "Expected a 'break' statement before '{if test == null then 'default' else 'case'}'."
|
|
245
|
+
}
|
|
246
|
+
in ts_case rule es-no-fallthrough:unused-comment {
|
|
247
|
+
option reportUnusedFallthroughComment boolean = false "report such comments where nothing falls through"
|
|
248
|
+
option commentPattern regex | null = null "comments that allow falling through (default: /falls?\\s?through/i)"
|
|
249
|
+
let comment_body { value case_comment.nonempty ?? ""; re_replace /^\/[*\/]|\*\/$/ ""; trim }
|
|
250
|
+
when reportUnusedFallthroughComment && index > 0 && case_comment != null
|
|
251
|
+
when { value parent.case_paths; slice 0 index; last } == "leaves"
|
|
252
|
+
when { value default: commentPattern.nonempty ?? "(?i)falls?\\s?through"; value re_test(comment_body, default) } && !re_test(comment_body, /^(?:eslint[- ]|(?:globals?|exported) )/)
|
|
253
|
+
report at case_comment_offset
|
|
254
|
+
warn "Found a comment that would permit fallthrough, but case cannot fall through."
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Writes to bindings, undefined names and unusable assignments.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_assignment.name_write_identifiers, ts_update.name_write_identifiers, ts_for_in.name_write_identifiers, ts_for_of.name_write_identifiers, ts_declarator.name_write_identifiers rule es-no-ex-assign {
|
|
6
|
+
when reference_kind == "catch" && @is_write
|
|
7
|
+
warn "Do not assign to the exception parameter."
|
|
8
|
+
}
|
|
9
|
+
in ts_identifier rule es-no-const-assign {
|
|
10
|
+
when (reference_kind == "const" || reference_kind == "using") && @is_write
|
|
11
|
+
warn "'{text}' is constant."
|
|
12
|
+
}
|
|
13
|
+
in ts_identifier rule es-no-class-assign {
|
|
14
|
+
when (reference_kind == "class" || reference_kind == "class_name") && @is_write
|
|
15
|
+
warn "'{text}' is a class."
|
|
16
|
+
}
|
|
17
|
+
in ts_identifier rule es-no-func-assign {
|
|
18
|
+
when (reference_kind == "function" || reference_kind == "function_name") && @is_write
|
|
19
|
+
warn "'{text}' is a function."
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
in ts_unary rule es-no-delete-var {
|
|
23
|
+
when operator == "delete" && is_identifier(argument)
|
|
24
|
+
warn "Variables should not be deleted."
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
in ts_variable_declaration let declares_loop_variable parent is (ts_for_of, ts_for_in)
|
|
28
|
+
# `let x;` that is read but never written.
|
|
29
|
+
in ts_declarator rule es-no-unassigned-vars {
|
|
30
|
+
when init == null && is_identifier(name) && ts_variable_declaration.kind != "const"
|
|
31
|
+
when name.writes == 0 && name.uses > 0 && name.ambient == false && parent.declares_loop_variable != true
|
|
32
|
+
warn "'{name}' is always 'undefined' because it's never assigned."
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
# Writes to the built-in globals.
|
|
36
|
+
in ts_assignment.name_write_identifiers, ts_update.name_write_identifiers, ts_for_in.name_write_identifiers, ts_for_of.name_write_identifiers rule es-no-global-assign {
|
|
37
|
+
option exceptions string[] = [] "read-only globals that may be modified"
|
|
38
|
+
when scope_declaration == null && global == "readonly" && @is_write && !contains(exceptions, identifier_name)
|
|
39
|
+
warn "Read-only global '{text}' should not be modified."
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
in ts_identifier rule es-no-undef {
|
|
43
|
+
option typeof boolean = false "also report `typeof x` of an undeclared `x`"
|
|
44
|
+
when resolved == "global" && (global == null || global == "off") && access != "type"
|
|
45
|
+
when typeof || !under_typeof
|
|
46
|
+
warn "'{text}' is not defined."
|
|
47
|
+
}
|
|
48
|
+
# The operand of `typeof`, through parentheses.
|
|
49
|
+
in ts_identifier, ts_parenthesized let under_typeof parent is ts_unary && parent.operator == "typeof" || parent is ts_parenthesized && parent.under_typeof == true
|
|
50
|
+
|
|
51
|
+
in ts_node let problem_in_try
|
|
52
|
+
if is (ts_program, ts_function, ts_arrow, ts_method, ts_static_block) then false
|
|
53
|
+
else if is ts_try.block then true else parent.problem_in_try
|
|
54
|
+
in ts_node let problem_assignment_export_read
|
|
55
|
+
any(references, reference => reference is ts_export_specifier.local)
|
|
56
|
+
|| ts_usage_global(ts_usage_binding, scoping_program) && contains(ts_exported_names(scoping_program), identifier_name)
|
|
57
|
+
|
|
58
|
+
# Disallow variable assignments when the value is not used
|
|
59
|
+
in ts_identifier rule es-no-useless-assignment {
|
|
60
|
+
when declaration != null && declaration.exported != true && !problem_in_try
|
|
61
|
+
when name_write_owner != null || is ts_declarator.name && parent.init != null
|
|
62
|
+
when declaration.scoping_binding_execution_scope.id != null && declaration.scoping_binding_execution_scope.id == scoping_execution_scope.id
|
|
63
|
+
when !declaration.problem_assignment_export_read && flow_has_reads(declaration.node) == true && flow_value_used(node) == false
|
|
64
|
+
warn "The value assigned to '{text}' is not used in subsequent statements."
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Duplicate object keys, class members, parameters and enum values.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
# Getters and setters of one name pair up; anything else of the same name
|
|
6
|
+
# (in the same object, or the same static-ness of a class) is a duplicate.
|
|
7
|
+
in ts_method let accessor_kind if kind == "get" || kind == "set" then kind else "init"
|
|
8
|
+
in ts_property, ts_class_property let accessor_kind "init"
|
|
9
|
+
@macro duplicate_of(e) (e != null && e.name == static_name && (accessor_kind == "init" || e.kind == "init" || e.kind == accessor_kind))
|
|
10
|
+
# (`""` is a name: `type_of` tells it from null.)
|
|
11
|
+
@macro has_static_name (type_of(static_name) == "str")
|
|
12
|
+
# `__proto__: x` sets the prototype rather than defining a key.
|
|
13
|
+
in ts_property let proto_setter static_name == "__proto__" && value != null && !starts_with(key, "[")
|
|
14
|
+
in ts_method let proto_setter false
|
|
15
|
+
in ts_property, ts_method let key_entry if !@has_static_name || proto_setter then null else { name: static_name, kind: accessor_kind }
|
|
16
|
+
in ts_object let keyed [] property.key_entry
|
|
17
|
+
in ts_property, ts_method let duplicate_key parent is ts_object && parent.flow_destructuring_target != true && key_entry != null && { value parent.keyed; slice 0 index; any (e) @duplicate_of(e) }
|
|
18
|
+
# At the name: a computed key reports the expression inside its brackets.
|
|
19
|
+
@macro at_key_name (if node is ts_computed_key then node.expression.node else node)
|
|
20
|
+
in ts_property.key, ts_method.key rule es-no-dupe-keys {
|
|
21
|
+
when parent.duplicate_key
|
|
22
|
+
report at @at_key_name
|
|
23
|
+
warn "Duplicate key '{parent.static_name}'."
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
# Overload signatures (no body) are not members of their own. (`is_static`
|
|
27
|
+
# comes from .ts/style.)
|
|
28
|
+
in ts_method let member_entry if !@has_static_name || body == null || static_name == "constructor" && !is_static && !starts_with(key, "[") then null else { name: static_name, kind: accessor_kind, static: is_static }
|
|
29
|
+
in ts_class_property let member_entry if !@has_static_name then null else { name: static_name, kind: accessor_kind, static: is_static }
|
|
30
|
+
in ts_class let member_entries [] member.member_entry
|
|
31
|
+
in ts_method, ts_class_property let duplicate_member parent is ts_class && member_entry != null && { value parent.member_entries; slice 0 index; any (e) @duplicate_of(e) && e.static == is_static }
|
|
32
|
+
in ts_method.key, ts_class_property.key rule es-no-dupe-class-members {
|
|
33
|
+
when parent.duplicate_member
|
|
34
|
+
report at @at_key_name
|
|
35
|
+
warn "Duplicate name '{parent.static_name}'."
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn problem_duplicate_parameters(owner) = {
|
|
39
|
+
value walk(owner.parameter.name.nodes,
|
|
40
|
+
child => if child is (ts_function, ts_arrow, ts_method, ts_class) then [] else child.children.nodes)
|
|
41
|
+
filter (site) site is ts_identifier && site.ts_name_site == owner && site.ts_name_kind == "parameter"
|
|
42
|
+
map (site) name_binding(site)
|
|
43
|
+
filter (binding) binding != null
|
|
44
|
+
unique_by (binding) binding.key
|
|
45
|
+
filter (binding) count(filter(binding.declarations,
|
|
46
|
+
site => site.ts_name_site == owner && site.ts_name_kind == "parameter")) >= 2
|
|
47
|
+
map (binding) binding.key
|
|
48
|
+
}
|
|
49
|
+
in ts_function rule es-no-dupe-args {
|
|
50
|
+
when body != null
|
|
51
|
+
let duplicate_open node_at(function_head_site(node).end)
|
|
52
|
+
when duplicate_open is ts_open_paren
|
|
53
|
+
each duplicate_param in problem_duplicate_parameters(node) {
|
|
54
|
+
report at { start: duplicate_open.offset, end: return_type.end_offset ?? duplicate_open.paired_token.end }
|
|
55
|
+
warn "Duplicate param '{duplicate_param}'."
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
in ts_enum_member let enum_value init.literal_value
|
|
60
|
+
in ts_enum let enum_values [] member.enum_value
|
|
61
|
+
in ts_enum_member rule es-no-duplicate-enum-values {
|
|
62
|
+
when enum_value != null && {
|
|
63
|
+
value parent.enum_values
|
|
64
|
+
slice 0 index
|
|
65
|
+
any (v) v != null && v.type == enum_value.type && v.value == enum_value.value
|
|
66
|
+
}
|
|
67
|
+
warn "Duplicate enum member value {if enum_value.value == '-0' then '0' else enum_value.value}."
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Expression mistakes: comparisons, conditions, assignments and literals.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_member rule es-no-caller {
|
|
6
|
+
when object == "arguments" && property ~ /callee|caller/
|
|
7
|
+
warn "Avoid arguments.{property}."
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
in ts_binary rule es-no-compare-neg-zero {
|
|
11
|
+
when operator ~ /===?|!==?|<=?|>=?/ && (re_test(left, /^-\s*0$/) || re_test(right, /^-\s*0$/))
|
|
12
|
+
warn "Do not use the '{operator}' operator to compare against -0."
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
# Assignments as conditions, unless wrapped in their own parentheses (a `?:`
|
|
16
|
+
# test needs two pairs: it has none of its own). With "always", any
|
|
17
|
+
# assignment inside a condition.
|
|
18
|
+
in ts_assignment let is_assignment true
|
|
19
|
+
in ts_parenthesized let wraps_assignment expression.is_assignment == true
|
|
20
|
+
in ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test rule es-no-cond-assign {
|
|
21
|
+
option mode "except-parens" | "always" = "except-parens"
|
|
22
|
+
when mode == "except-parens"
|
|
23
|
+
when is ts_assignment || parent is ts_conditional && is ts_parenthesized && wraps_assignment == true
|
|
24
|
+
report at unparenthesised_node(node)
|
|
25
|
+
warn "Expected a conditional expression and instead saw an assignment."
|
|
26
|
+
}
|
|
27
|
+
in ts_assignment rule es-no-cond-assign:always {
|
|
28
|
+
option mode "except-parens" | "always" = "except-parens"
|
|
29
|
+
let where if conditional_ancestor is ts_if then "an 'if' statement" else if conditional_ancestor is ts_while then "a 'while' statement" else if conditional_ancestor is ts_do_while then "a 'do...while' statement" else if conditional_ancestor is ts_for then "a 'for' statement" else "ConditionalExpression"
|
|
30
|
+
when mode == "always" && conditional_ancestor != null
|
|
31
|
+
warn "Unexpected assignment within {where}."
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
# Comparisons with `NaN` (or `Number.NaN`, or a sequence ending in it).
|
|
35
|
+
in ts_identifier let is_nan text == "NaN" && is_real_global
|
|
36
|
+
in ts_member, ts_index let is_nan object == "Number" && object.is_real_global == true && static_name == "NaN"
|
|
37
|
+
in ts_parenthesized let is_nan expression.is_nan == true
|
|
38
|
+
in ts_sequence let is_nan last([] expression.is_nan) == true
|
|
39
|
+
in ts_binary rule es-use-isnan {
|
|
40
|
+
when operator ~ /(?:[<>]|[!=]=)=?/ && (left.is_nan == true || right.is_nan == true)
|
|
41
|
+
warn "Use the isNaN function to compare with NaN."
|
|
42
|
+
}
|
|
43
|
+
group use_isnan_searches
|
|
44
|
+
in ts_switch rule es-use-isnan:switch {
|
|
45
|
+
when discriminant.is_nan == true
|
|
46
|
+
warn "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch."
|
|
47
|
+
}
|
|
48
|
+
in ts_case rule es-use-isnan:case {
|
|
49
|
+
when test.is_nan == true
|
|
50
|
+
warn "'case NaN' can never match. Use Number.isNaN before the switch."
|
|
51
|
+
}
|
|
52
|
+
in use_isnan_searches rule es-use-isnan:index-of {
|
|
53
|
+
when (callee.static_name == "indexOf" || callee.static_name == "lastIndexOf") && count(argument) <= 2 && first([] argument.is_nan) == true
|
|
54
|
+
warn "Array prototype method '{callee.static_name}' cannot find NaN."
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
# `typeof x === "strnig"`, either way round.
|
|
58
|
+
in ts_unary let is_typeof operator == "typeof"
|
|
59
|
+
in ts_parenthesized let is_typeof expression.is_typeof == true
|
|
60
|
+
in ts_binary {
|
|
61
|
+
let left_is_typeof left.is_typeof == true
|
|
62
|
+
let right_is_typeof right.is_typeof == true
|
|
63
|
+
}
|
|
64
|
+
@macro valid_typeof_names ["symbol", "undefined", "object", "boolean", "number", "string", "function", "bigint"]
|
|
65
|
+
in ts_binary.left, ts_binary.right rule es-valid-typeof {
|
|
66
|
+
option requireStringLiterals boolean = false "compare only with string literals"
|
|
67
|
+
let compared_with_typeof parent.operator ~ /[=!]==?/ && (if is ts_node.left then parent.right_is_typeof else parent.left_is_typeof)
|
|
68
|
+
when compared_with_typeof
|
|
69
|
+
when literal_value != null && (literal_value.type != "string" || !contains(@valid_typeof_names, literal_value.value))
|
|
70
|
+
|| is_global_undefined
|
|
71
|
+
|| requireStringLiterals && literal_value == null && is_typeof != true
|
|
72
|
+
report at unparenthesised_node(node)
|
|
73
|
+
warn if requireStringLiterals && literal_value == null then "Typeof comparisons should be to string literals." else "Invalid typeof comparison value."
|
|
74
|
+
}
|
|
75
|
+
in ts_identifier let is_global_undefined text == "undefined" && global == "readonly"
|
|
76
|
+
in ts_parenthesized let is_global_undefined expression.is_global_undefined == true
|
|
77
|
+
|
|
78
|
+
# `a = a`, also inside destructuring and members (`a.b = a.b`).
|
|
79
|
+
fn problem_self_assignment_pairs(a, b) = {
|
|
80
|
+
value left: unparenthesised_node(a)
|
|
81
|
+
value right: unparenthesised_node(b)
|
|
82
|
+
value if left == null || right == null then []
|
|
83
|
+
else if left is (ts_array, ts_array_pattern) && right is ts_array then {
|
|
84
|
+
value items: right.element.nodes
|
|
85
|
+
value last: find(items, item => item is ts_spread).index ?? (count(items) - 1)
|
|
86
|
+
value left.element.nodes
|
|
87
|
+
zip items (target, value) { left: target, right: value }
|
|
88
|
+
take_while (pair, i) i <= last && !(pair.left is (ts_spread, ts_rest) && i < count(items) - 1)
|
|
89
|
+
}
|
|
90
|
+
else if left is (ts_spread, ts_rest) && right is ts_spread then [{left: left.argument.node, right: right.argument.node}]
|
|
91
|
+
else if left is (ts_object, ts_object_pattern) && right is ts_object then {
|
|
92
|
+
value spread: find_last(right.property.nodes, property => property is ts_spread)
|
|
93
|
+
value items: { value right.property.nodes; slice (if spread == null then 0 else spread.index + 1) }
|
|
94
|
+
value left.property.nodes
|
|
95
|
+
flat_map (target) map(items, value => {left: target, right: value})
|
|
96
|
+
}
|
|
97
|
+
else if left is (ts_property, ts_pattern_property) && right is ts_property && (right.kind == null || right.kind == "init")
|
|
98
|
+
&& left.static_name != null && left.static_name == right.static_name then
|
|
99
|
+
[{left: left.value.node ?? left.key.node, right: right.value.node ?? right.key.node}]
|
|
100
|
+
else []
|
|
101
|
+
}
|
|
102
|
+
fn problem_self_assignments(a, b) = {
|
|
103
|
+
value {left: a, right: b, path: ""}
|
|
104
|
+
walk (item) {
|
|
105
|
+
value pairs: problem_self_assignment_pairs(item.left, item.right)
|
|
106
|
+
value width: len(str(len(pairs)))
|
|
107
|
+
value map(pairs, (pair, index) => {left: pair.left, right: pair.right, path: item.path pad_start(str(index), width, "0")})
|
|
108
|
+
}
|
|
109
|
+
sort_by (item) item.path
|
|
110
|
+
filter (item) {
|
|
111
|
+
value left: unparenthesised_node(item.left)
|
|
112
|
+
value right: unparenthesised_node(item.right)
|
|
113
|
+
value left is ts_identifier && right is ts_identifier && left.identifier_name == right.identifier_name
|
|
114
|
+
|| left is (ts_member, ts_index) && right is (ts_member, ts_index)
|
|
115
|
+
&& left.reference_key != null && left.reference_key == right.reference_key
|
|
116
|
+
}
|
|
117
|
+
map (item) unparenthesised_node(item.right)
|
|
118
|
+
}
|
|
119
|
+
in ts_assignment rule es-no-self-assign {
|
|
120
|
+
option props boolean = true
|
|
121
|
+
when contains(["=", "&&=", "||=", "??="], operator)
|
|
122
|
+
each self in map(problem_self_assignments(left.node, right.node), assigned => { node: assigned, name: re_replace(assigned.text, /\s+/, "") }) {
|
|
123
|
+
when props || self.node is ts_identifier
|
|
124
|
+
report at self.node
|
|
125
|
+
warn "'{self.name}' is assigned to itself."
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
# Comparisons and short circuits whose result cannot change.
|
|
130
|
+
in ts_binary, ts_logical let constant_relation_checked false
|
|
131
|
+
in ts_binary, ts_logical rule es-no-constant-binary-expression {
|
|
132
|
+
let c constant_comparison
|
|
133
|
+
when c != null || constant_relation_checked && constant_relation
|
|
134
|
+
report at if c != null then c.node else node
|
|
135
|
+
warn if c == null then "Unexpected constant relational comparison. Both sides of the `{operator}` are literal values."
|
|
136
|
+
else if c.kind == "constantShortCircuit" then "Unexpected constant {c.detail} on the left-hand side of a `{operator}` expression."
|
|
137
|
+
else if c.kind == "constantBinaryOperand" then "Unexpected constant binary expression. Compares constantly with the {c.detail}-hand side of the `{operator}`."
|
|
138
|
+
else if c.kind == "alwaysNew" then "Unexpected comparison to newly constructed object. These two values can never be equal."
|
|
139
|
+
else "Unexpected comparison of two newly constructed objects. These two values can never be equal."
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
# `!a in b`: the `!` applies to `a` alone.
|
|
143
|
+
in ts_binary let unsafe_negation_checked contains(["in", "instanceof"], operator)
|
|
144
|
+
in ts_binary.left rule es-no-unsafe-negation {
|
|
145
|
+
when is ts_unary && re_test(text, /^!/)
|
|
146
|
+
when parent.unsafe_negation_checked
|
|
147
|
+
warn "Unexpected negating the left operand of '{parent.operator}' operator."
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
in ts_string rule es-no-template-curly-in-string warn "Unexpected template string expression." {
|
|
151
|
+
when text ~ /.*\$\{[^}\r\n]+\}.*/
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Shared helpers for the `.ts/problems` topic files.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
@macro normalize_space(x) re_replace(x, /\s+/, " ")
|
|
6
|
+
group ts_literal ts_string, ts_number, ts_boolean, ts_null, ts_bigint, ts_regex
|
|
7
|
+
|
|
8
|
+
# A global (not shadowed, not switched off) of this name.
|
|
9
|
+
@macro is_global(name) (text == name && (global == "readonly" || global == "writable"))
|
|
10
|
+
in ts_identifier let is_real_global global == "readonly" || global == "writable"
|
|
11
|
+
|
|
12
|
+
@macro is_write (access == "write" || access == "read_write")
|
|
13
|
+
|
|
14
|
+
in ts_node let problem_expression_value_used
|
|
15
|
+
if parent == null || is (ts_expression_statement.expression, ts_for.init, ts_for.update) then false
|
|
16
|
+
else if is (ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression, ts_type_assertion.expression, ts_instantiation.expression) then parent.problem_expression_value_used
|
|
17
|
+
else if is ts_sequence.expression then index == count(parent.expression) - 1 && parent.problem_expression_value_used
|
|
18
|
+
else if is (ts_logical.right, ts_conditional.consequent, ts_conditional.alternate) then parent.problem_expression_value_used
|
|
19
|
+
else if is ts_unary.argument && parent.operator == "void" then false
|
|
20
|
+
else true
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Imports that are assigned to, invisible or unused.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_identifier rule es-no-import-assign {
|
|
6
|
+
when reference_kind == "import" && @is_write
|
|
7
|
+
report at name_write_owner
|
|
8
|
+
warn "'{text}' is read-only."
|
|
9
|
+
}
|
|
10
|
+
# Members of a namespace import: `ns.x = 1`, `ns.x++`, `delete ns.x`.
|
|
11
|
+
in ts_member, ts_index rule es-no-import-assign:member {
|
|
12
|
+
when write_site != null && object.imported_name == "*"
|
|
13
|
+
report at name_write_owner
|
|
14
|
+
warn "The members of '{unparenthesised_node(object.node)}' are read-only."
|
|
15
|
+
}
|
|
16
|
+
# `Object.assign(ns, ...)`, `Reflect.set(ns, ...)` and the like.
|
|
17
|
+
in ts_member let on_real_global object.is_real_global == true || object.on_real_global == true
|
|
18
|
+
in ts_parenthesized, ts_non_null let on_real_global expression.on_real_global == true
|
|
19
|
+
in ts_call rule es-no-import-assign:call {
|
|
20
|
+
when callee.on_real_global == true
|
|
21
|
+
when {
|
|
22
|
+
value callee
|
|
23
|
+
re_replace /[\s()]/ ""
|
|
24
|
+
re_test /^(?:Object\??\.(?:assign|defineProperty|defineProperties|freeze|setPrototypeOf)|Reflect\??\.(?:defineProperty|deleteProperty|set|setPrototypeOf))$/
|
|
25
|
+
}
|
|
26
|
+
when first([] argument.imported_name) == "*"
|
|
27
|
+
warn "The members of '{unparenthesised_node(first(argument))}' are read-only."
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
in ts_import rule es-no-unassigned-import {
|
|
31
|
+
when default_import == null && namespace == null && count(specifier) == 0
|
|
32
|
+
warn "Imported module should be assigned"
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
in ts_import.default_import, ts_import.namespace, ts_import_specifier.local, ts_import_specifier.imported, ts_import_equals.name rule es-no-unused-imports {
|
|
36
|
+
when is ts_identifier
|
|
37
|
+
when declaration_kind == "import" && uses == 0
|
|
38
|
+
# TypeScript counts `{@link X}` in JSDoc as a use.
|
|
39
|
+
when !{ value result: "\\x7B@link(?:code|plain)?\\s+" re_escape(text) "\\b"; value re_test(source, result) }
|
|
40
|
+
warn "'{text}' is defined but never used."
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|