@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,205 @@
|
|
|
1
|
+
import .ts/constants
|
|
2
|
+
import .ts/values/inspection
|
|
3
|
+
import .ts/typed/awaitability
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
in ts_node let promise_boolean_test
|
|
8
|
+
is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test)
|
|
9
|
+
|| is ts_unary.argument && node.parent.operator == "!"
|
|
10
|
+
|| (parent is (ts_parenthesized, ts_logical) || parent is ts_conditional && not ts_conditional.test)
|
|
11
|
+
&& parent.promise_boolean_test == true
|
|
12
|
+
in ts_node let promise_boolean_left
|
|
13
|
+
is ts_logical.left && node.parent.operator != "??"
|
|
14
|
+
|| (parent is ts_parenthesized || is ts_logical.left && node.parent.operator != "??"
|
|
15
|
+
|| parent is ts_conditional && not ts_conditional.test)
|
|
16
|
+
&& parent.promise_boolean_left == true
|
|
17
|
+
|
|
18
|
+
in ts_parenthesized.expression, ts_logical.left, ts_logical.right, ts_conditional.test, ts_conditional.consequent, ts_conditional.alternate, ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_unary.argument rule es-no-misused-promises:conditionals {
|
|
19
|
+
option flagUnions "none" | "strict" | "all" = "none" "Which mixed promise unions to flag"
|
|
20
|
+
when not (ts_parenthesized, ts_logical, ts_conditional, ts_null, ts_boolean, ts_number, ts_bigint, ts_string)
|
|
21
|
+
when !(is ts_unary && node.operator == "!")
|
|
22
|
+
when parent is (ts_parenthesized, ts_logical, ts_conditional)
|
|
23
|
+
|| is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test)
|
|
24
|
+
|| is ts_unary.argument && node.parent.operator == "!"
|
|
25
|
+
when promise_boolean_test || promise_boolean_left
|
|
26
|
+
# Comparisons and these unary operators make primitives: never thenable.
|
|
27
|
+
when !(is ts_binary && contains(["===", "!==", "==", "!=", "<", "<=", ">", ">=", "in", "instanceof"], node.operator))
|
|
28
|
+
when !(is ts_unary && contains(["typeof", "void", "delete", "+", "~"], node.operator))
|
|
29
|
+
when node could thenable && (flagUnions == "all" || node always thenable || flagUnions == "strict" && ts_await_union_equivalent(node) == true)
|
|
30
|
+
warn "Expected non-Promise value in a boolean conditional."
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
in ts_spread rule es-no-misused-promises:spreads {
|
|
34
|
+
when parent is ts_object
|
|
35
|
+
when argument could thenable
|
|
36
|
+
report at unparenthesised_node(argument.node)
|
|
37
|
+
warn "Expected a non-Promise value to be spread in an object."
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
fn promise_return_arms(signatures) = {
|
|
41
|
+
value headers: flat_map(signatures ?? [], arm => arm)
|
|
42
|
+
value returns: map(headers, header => if header.result == null then null else value_constituent_traits(header.result, ["intersection", "any", "unknown", "error", "thenable", "void"]))
|
|
43
|
+
value if signatures == null || count(headers) == 0 || any(returns, arms => arms == null) then null
|
|
44
|
+
else flat_map(returns, arms => arms)
|
|
45
|
+
}
|
|
46
|
+
fn promise_returns_known(arms) = arms != null && all(arms, arm => all(arm, part =>
|
|
47
|
+
contains(part.traits, "intersection") || !any(["any", "unknown", "error"], trait => contains(part.traits, trait))
|
|
48
|
+
&& !contains(part.uncertain_traits, "thenable") && !contains(part.uncertain_traits, "void")))
|
|
49
|
+
fn promise_returns_thenable(arms) = promise_returns_known(arms)
|
|
50
|
+
&& any(arms, arm => any(arm, part => contains(part.traits, "thenable")))
|
|
51
|
+
fn promise_returns_void(arms) = promise_returns_known(arms) && count(arms) > 0
|
|
52
|
+
&& any(arms, arm => any(arm, part => contains(part.traits, "void")))
|
|
53
|
+
&& !any(arms, arm => any(arm, part => contains(part.traits, "thenable")))
|
|
54
|
+
fn promise_void_context(n) = {
|
|
55
|
+
value source: promise_return_arms(value_signatures(n))
|
|
56
|
+
value if !promise_returns_thenable(source) then false else {
|
|
57
|
+
value targets: expected_call_signatures(n)
|
|
58
|
+
value headers: flat_map(targets ?? [], arm => arm)
|
|
59
|
+
value target: flat_map(headers, header => header.result)
|
|
60
|
+
value targets != null && count(headers) > 0
|
|
61
|
+
&& all(headers, header => header.result_known) && promise_returns_void(target)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
# ESLint unions every call signature and drops an argument index when any
|
|
65
|
+
# signature's parameter at that index accepts a thenable-returning function.
|
|
66
|
+
fn promise_void_overload_accepts(n) = if n.parent.node not (ts_call, ts_new) then false else {
|
|
67
|
+
value call: n.parent.node
|
|
68
|
+
value signatures: if call is ts_new then value_constructors(call.callee.node) else value_signatures(call.callee.node)
|
|
69
|
+
value headers: flat_map(signatures ?? [], arm => arm)
|
|
70
|
+
value index: index_of(call.argument.nodes, n)
|
|
71
|
+
value chosen: map(headers, header => first(slice(header.parameters ?? [], index, index + 1)))
|
|
72
|
+
value found: any(chosen, item => item != null && promise_returns_thenable(promise_return_arms(value_signatures(item))))
|
|
73
|
+
}
|
|
74
|
+
fn promise_dispose_void_context(n) = {
|
|
75
|
+
value key: {global: "Symbol", class: "value", path: ["dispose"]}
|
|
76
|
+
# A disposal contract is only asked of `using` bindings and of targets whose
|
|
77
|
+
# declared type can hold one; anything else answers without a value lookup.
|
|
78
|
+
value using: n is ts_declarator.init && n.parent.parent.kind == "using"
|
|
79
|
+
value expected: if using then null else value_expected_handle(n)
|
|
80
|
+
value if !using && expected == null then false else {
|
|
81
|
+
value source: value_lookup(n, key).member
|
|
82
|
+
value source != null && promise_returns_thenable(promise_return_arms(value_signatures(source)))
|
|
83
|
+
&& (using || {
|
|
84
|
+
value target: value_lookup(expected, key).member
|
|
85
|
+
value target != null && promise_returns_void(promise_return_arms(value_signatures(target)))
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
fn promise_property_source(n) = if n is ts_property then n.value.node ?? n else n
|
|
90
|
+
fn promise_property_site(n) = {
|
|
91
|
+
value n: unparenthesised_node(n)
|
|
92
|
+
value if n is (ts_arrow, ts_function, ts_method) then n.return_type.node ?? function_head_site(n) else n
|
|
93
|
+
}
|
|
94
|
+
in ts_node let promise_builtin_instance ts_value_builtin_type(node, ["Promise"]) == true
|
|
95
|
+
fn promise_finally_member(n) = n is (ts_member, ts_index)
|
|
96
|
+
&& (n.static_name.nonempty ?? ts_runtime_string(n.index.node)) == "finally"
|
|
97
|
+
&& n.object.promise_builtin_instance
|
|
98
|
+
in ts_call let promise_finally_callback { value callee.node; unparenthesised_node; promise_finally_member }
|
|
99
|
+
|
|
100
|
+
fn promise_async_policy(n, names) = {
|
|
101
|
+
value source: if n is ts_function && n.name != null then name_binding(n.name.node).node ?? n.name.node else n
|
|
102
|
+
value signatures: value_signatures(source)
|
|
103
|
+
value headers: flat_map(signatures ?? [], arm => arm)
|
|
104
|
+
value returns: map(headers, header => {
|
|
105
|
+
value arms: if header.result == null then null else value_parts(header.result)
|
|
106
|
+
value matches: map(arms ?? [], arm => {
|
|
107
|
+
value parts: filter(arm.parts, part => !contains(part.traits, "intersection"))
|
|
108
|
+
value named: map(parts, part => part.carrier != null
|
|
109
|
+
&& any(ts_value_ancestry(part).parts, base => contains(names, base.carrier.description.name)))
|
|
110
|
+
value {every: count(named) > 0 && all(named, found => found), some: any(named, found => found)}
|
|
111
|
+
})
|
|
112
|
+
value {
|
|
113
|
+
known: arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known)),
|
|
114
|
+
dynamic: any(arms ?? [], arm => any(arm.parts, part =>
|
|
115
|
+
contains(part.traits, "any") || contains(part.traits, "unknown"))),
|
|
116
|
+
every: count(matches) > 0 && all(matches, result => result.every),
|
|
117
|
+
some: any(matches, result => result.some),
|
|
118
|
+
mixed: count(arms) > 1 && !all(matches, result => result.every),
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
value {
|
|
122
|
+
known: signatures != null && count(returns) > 0 && all(returns, result => result.known),
|
|
123
|
+
dynamic: any(returns, result => result.dynamic),
|
|
124
|
+
required: all(returns, result => if n.return_type != null then result.every else result.some),
|
|
125
|
+
mixed: any(returns, result => result.mixed),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-misused-promises:void-arguments {
|
|
130
|
+
when parent not ts_call || node.index != 0 || !parent.promise_finally_callback
|
|
131
|
+
when promise_void_context(node)
|
|
132
|
+
when !promise_void_overload_accepts(node)
|
|
133
|
+
report at unparenthesised_node(node)
|
|
134
|
+
warn "Promise returned in function argument where a void return was expected."
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
in ts_call.argument rule es-no-misused-promises:predicates {
|
|
138
|
+
when node.index == 0 && condition_array_callback(parent)
|
|
139
|
+
when promise_returns_thenable(promise_return_arms(value_signatures(node)))
|
|
140
|
+
report at unparenthesised_node(node)
|
|
141
|
+
warn "Expected a non-Promise value to be returned."
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
in ts_declarator.init, ts_parameter.init, ts_assignment.right rule es-no-misused-promises:void-variables {
|
|
145
|
+
# An unannotated declarator gives its initializer no expected type.
|
|
146
|
+
when ((!(is ts_declarator.init) || parent.type != null) && promise_void_context(node))
|
|
147
|
+
|| promise_dispose_void_context(node)
|
|
148
|
+
report at unparenthesised_node(node)
|
|
149
|
+
warn "Promise-returning function provided to variable where a void return was expected."
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
in ts_property, ts_method, ts_class_property.init rule es-no-misused-promises:void-properties {
|
|
153
|
+
when not ts_method || parent is ts_object
|
|
154
|
+
when not ts_class_property.init || node.parent.type != null
|
|
155
|
+
let promise_source promise_property_source(node)
|
|
156
|
+
when promise_void_context(promise_source)
|
|
157
|
+
report at promise_property_site(promise_source)
|
|
158
|
+
warn "Promise-returning function provided to property where a void return was expected."
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
in ts_return.argument, ts_arrow.body rule es-no-misused-promises:void-returns {
|
|
162
|
+
when promise_void_context(node)
|
|
163
|
+
report at unparenthesised_node(node)
|
|
164
|
+
warn "Promise-returning function provided to return value where a void return was expected."
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
in ts_jsx_expression.expression rule es-no-misused-promises:void-attributes {
|
|
168
|
+
when parent.parent is ts_jsx_attribute
|
|
169
|
+
when promise_void_context(node)
|
|
170
|
+
report at parent.node
|
|
171
|
+
warn "Promise-returning function provided to attribute where a void return was expected."
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn promise_inherited_void_targets(n) = {
|
|
175
|
+
value owner: n.parent.node
|
|
176
|
+
value declared: owner.implements.nodes (if owner is ts_interface then owner.extends.nodes else [])
|
|
177
|
+
value constructed: if owner is ts_class && owner.extends != null then {
|
|
178
|
+
value headers: value_constructors(owner.extends.node)
|
|
179
|
+
value map(flat_map(headers ?? [], arm => arm), header => {heritage: owner.extends.node, value: header.result})
|
|
180
|
+
} else []
|
|
181
|
+
# An interface extending a constructor type (`typeof C`) is checked
|
|
182
|
+
# against the instances it constructs too, as TypeScript ESLint does.
|
|
183
|
+
value instances: if owner is ts_interface then flat_map(owner.extends.nodes, heritage => {
|
|
184
|
+
value headers: value_constructors(heritage)
|
|
185
|
+
value map(flat_map(headers ?? [], arm => arm), header => {heritage: heritage, value: header.result})
|
|
186
|
+
}) else []
|
|
187
|
+
value targets: map(declared, heritage => {heritage: heritage, value: heritage}) constructed instances
|
|
188
|
+
value filter(targets, target => target.value != null && {
|
|
189
|
+
value lookup: value_lookup(target.value, n.static_name)
|
|
190
|
+
value lookup.member != null && any(lookup.alternatives, alternative => alternative.matched
|
|
191
|
+
&& any(alternative.sources, source => source.kind == "named"))
|
|
192
|
+
&& promise_returns_void(promise_return_arms(value_signatures(lookup.member)))
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
in ts_method, ts_method_signature, ts_class_property, ts_property_signature rule es-no-misused-promises:void-inherited-methods {
|
|
196
|
+
when !is_static
|
|
197
|
+
when static_name != null && parent is (ts_class, ts_interface)
|
|
198
|
+
when promise_returns_thenable(promise_return_arms(value_signatures(node)))
|
|
199
|
+
each target in promise_inherited_void_targets(node) {
|
|
200
|
+
report at { start: offset, end: if byte_slice(source, end_offset, end_offset + 1) == ";" then end_offset + 1 else end_offset }
|
|
201
|
+
warn "Promise-returning method provided where a void return was expected by extended/implemented type '" target.heritage.text "'."
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
define {
|
|
2
|
+
fn qualifier_receiver(n) = if n is ts_qualified_name then n.left.node else n.object.node
|
|
3
|
+
fn qualifier_key(n) = if n is ts_qualified_name then n.right.node else n.property.node
|
|
4
|
+
fn qualifier_entity(n) = {
|
|
5
|
+
value terminal: {
|
|
6
|
+
value n
|
|
7
|
+
walk (current) if current is (ts_member, ts_qualified_name) && current.optional == null
|
|
8
|
+
then qualifier_receiver(current) else null
|
|
9
|
+
last
|
|
10
|
+
}
|
|
11
|
+
value terminal is ts_identifier
|
|
12
|
+
}
|
|
13
|
+
fn qualifier_owner_path(n, names, index) = {
|
|
14
|
+
value name: { value names; drop index; first }
|
|
15
|
+
value if index == 0 then {
|
|
16
|
+
value identity: declaration_identity(name)
|
|
17
|
+
value identity != null && qualifier_identity(n) == identity
|
|
18
|
+
} else n is (ts_member, ts_qualified_name)
|
|
19
|
+
&& qualifier_key(n).identifier_name == name.identifier_name
|
|
20
|
+
&& qualifier_owner_path(qualifier_receiver(n), names, index - 1)
|
|
21
|
+
}
|
|
22
|
+
fn qualifier_module_import(n, owner) = {
|
|
23
|
+
value declaration: qualifier_receiver(n).scope_declaration
|
|
24
|
+
value imports: {
|
|
25
|
+
value declaration
|
|
26
|
+
walk (item) item.parent.node
|
|
27
|
+
filter (item) item is ts_import
|
|
28
|
+
}
|
|
29
|
+
value own: first(imports)
|
|
30
|
+
value source: own.from.node
|
|
31
|
+
value matches: own != null && own.namespace.node == declaration && source is ts_string && owner is ts_namespace && owner.name is ts_string && source.literal_value.value == owner.name.literal_value.value
|
|
32
|
+
value matches
|
|
33
|
+
}
|
|
34
|
+
fn qualifier_ambient_module(n, owner) = owner is ts_namespace && owner.name is ts_string && ts_module_program(owner).ts_module_external_file && qualifier_module_import(n, owner)
|
|
35
|
+
fn qualifier_ambient_owner(owner) = owner is ts_namespace && owner.name is ts_string && ts_module_program(owner).ts_module_external_file
|
|
36
|
+
fn qualifier_owner(n) = {
|
|
37
|
+
value owners: {
|
|
38
|
+
value n
|
|
39
|
+
walk (item) item.parent.node
|
|
40
|
+
filter (item) item is (ts_namespace, ts_enum)
|
|
41
|
+
}
|
|
42
|
+
value find(owners, owner => {
|
|
43
|
+
value names: if owner is ts_namespace then ts_module_names(owner) else [owner.name.node]
|
|
44
|
+
value count(names) > 0 && qualifier_owner_path(qualifier_receiver(n), names, count(names) - 1) || qualifier_ambient_module(n, owner)
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
fn qualifier_owned_identity(n) = {
|
|
48
|
+
value owner: qualifier_owner(n)
|
|
49
|
+
value scopes: if owner == null then [] else name_scopes(owner.body.node ?? owner, "lexical")
|
|
50
|
+
value declarations: {
|
|
51
|
+
value scopes
|
|
52
|
+
filter (scope) scope.owner == owner || scope.owner == owner.body.node
|
|
53
|
+
flat_map (scope) name_scope_declarations(n, scope.id, qualifier_key(n).text)
|
|
54
|
+
}
|
|
55
|
+
value identities: map(declarations, declaration => declaration_identity(declaration.node))
|
|
56
|
+
value if count(identities) > 0 && all(identities, identity => identity == first(identities))
|
|
57
|
+
then first(identities) else null
|
|
58
|
+
}
|
|
59
|
+
fn qualifier_identity(n) = if n is (ts_member, ts_qualified_name) then {
|
|
60
|
+
value lookup: value_lookup(qualifier_receiver(n), qualifier_key(n).text)
|
|
61
|
+
value rows: lookup.alternatives
|
|
62
|
+
value sources: if count(rows) == 1 then first(rows).sources else []
|
|
63
|
+
value if lookup != null && first(rows).matched && count(sources) == 1 then first(sources).site
|
|
64
|
+
else qualifier_owned_identity(n)
|
|
65
|
+
} else declaration_identity(n)
|
|
66
|
+
fn qualifier_local(n, key) = {
|
|
67
|
+
value scopes: name_scopes(n, "lexical")
|
|
68
|
+
value rows: map(scopes, scope => {
|
|
69
|
+
value declarations: name_scope_declarations(n, scope.id, key)
|
|
70
|
+
value { known: declarations != null, declarations: filter(declarations, declaration =>
|
|
71
|
+
if n is ts_qualified_name then contains(declaration.classes, "type")
|
|
72
|
+
else contains(declaration.classes, "value") || contains(scope.classes, "statics") && contains(declaration.classes, "member")) }
|
|
73
|
+
})
|
|
74
|
+
value row: find(rows, row => !row.known || count(row.declarations) > 0)
|
|
75
|
+
value if scopes == null || row == null || !row.known then null
|
|
76
|
+
else map(row.declarations, declaration => declaration_identity(declaration.node))
|
|
77
|
+
}
|
|
78
|
+
fn qualifier_unnecessary(n) = {
|
|
79
|
+
value receiver: qualifier_receiver(n)
|
|
80
|
+
value owner: qualifier_owner(n)
|
|
81
|
+
value if !qualifier_entity(receiver) || n.optional != null || owner == null then false else {
|
|
82
|
+
value accessed: qualifier_identity(n)
|
|
83
|
+
value local: qualifier_local(n, qualifier_key(n).text)
|
|
84
|
+
value matches: if local == null then qualifier_ambient_owner(owner) else count(local) > 0 && all(local, declaration => declaration == accessed)
|
|
85
|
+
value accessed != null && matches
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
fn qualifier_outer(n) = if n.parent is (ts_member, ts_qualified_name) && qualifier_receiver(n.parent.node) == n then n.parent.node else null
|
|
89
|
+
in ts_member, ts_qualified_name rule es-no-unnecessary-qualifier {
|
|
90
|
+
when qualifier_unnecessary(node)
|
|
91
|
+
when !{
|
|
92
|
+
value qualifier_outer(node)
|
|
93
|
+
walk (item) qualifier_outer(item)
|
|
94
|
+
any (item) qualifier_unnecessary(item)
|
|
95
|
+
}
|
|
96
|
+
let qualifier_node qualifier_receiver(node)
|
|
97
|
+
let qualifier_key qualifier_key(node)
|
|
98
|
+
let qualifier_comments {
|
|
99
|
+
value descendants(ts_comment)
|
|
100
|
+
filter (comment) comment.offset >= qualifier_node.offset && comment.end_offset <= qualifier_key.offset
|
|
101
|
+
map (comment) comment.text (if starts_with(comment.text, "//") then "\n" else " ")
|
|
102
|
+
join ""
|
|
103
|
+
}
|
|
104
|
+
report at qualifier_node
|
|
105
|
+
fix at { start: qualifier_node.offset, end: qualifier_key.offset } as qualifier_comments
|
|
106
|
+
warn "Qualifier is unnecessary since '{qualifier_key}' is in scope."
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Private member reassignment and fixes that preserve constructor types.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
group readonly_write_access ts_member, ts_index
|
|
6
|
+
in ts_program let readonly_write_index {
|
|
7
|
+
value accesses: {
|
|
8
|
+
value descendants(readonly_write_access)
|
|
9
|
+
filter (access) access.write_site != null
|
|
10
|
+
}
|
|
11
|
+
# A numeric index writes an element, never a member: only numeric member keys
|
|
12
|
+
# can be targeted that way, and resolving element types is expensive.
|
|
13
|
+
value numeric_keys: {
|
|
14
|
+
value descendants(ts_class_property)
|
|
15
|
+
filter (property) property.key is ts_number
|
|
16
|
+
map (property) ts_value_key(property.key.node)
|
|
17
|
+
unique
|
|
18
|
+
}
|
|
19
|
+
value relevant: filter(accesses, access => !(access is ts_index && access.index is ts_number
|
|
20
|
+
&& !contains(numeric_keys, access.readonly_write_name)))
|
|
21
|
+
# A constructor write never blocks `readonly`: a readonly member may be assigned there.
|
|
22
|
+
value blocking: {
|
|
23
|
+
value relevant
|
|
24
|
+
filter (access) !access.readonly_write_constructor
|
|
25
|
+
}
|
|
26
|
+
value if any(blocking, access => access.readonly_write_name == null && access.readonly_write_sources == null)
|
|
27
|
+
then { unnamable: true, unknown_writes: [], writes: [] } else {
|
|
28
|
+
value unknown_writes: {
|
|
29
|
+
value blocking
|
|
30
|
+
filter (access) access.readonly_write_sources == null
|
|
31
|
+
map (access) { name: access.readonly_write_name, class: access.readonly_write_class }
|
|
32
|
+
}
|
|
33
|
+
value writes: {
|
|
34
|
+
value relevant
|
|
35
|
+
flat_map (access) {
|
|
36
|
+
value access.readonly_write_sources
|
|
37
|
+
map (source) source.site
|
|
38
|
+
unique
|
|
39
|
+
map (site) { site: site, access: access }
|
|
40
|
+
}
|
|
41
|
+
group_by (write) write.site
|
|
42
|
+
}
|
|
43
|
+
value { unnamable: false, unknown_writes: unknown_writes, writes: writes }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
in ts_member, ts_index {
|
|
47
|
+
let readonly_write_key if is ts_member then ts_value_key(property.node) else index.node
|
|
48
|
+
let readonly_write_name if is ts_member then ts_value_key(property.node)
|
|
49
|
+
else if index is (ts_string, ts_number, ts_bigint) then ts_value_key(index.node) else null
|
|
50
|
+
let readonly_write_sources {
|
|
51
|
+
value lookup: value_lookup(object.node, readonly_write_key)
|
|
52
|
+
value if lookup.alternatives == null then null
|
|
53
|
+
else if readonly_write_name != null
|
|
54
|
+
&& any(lookup.alternatives, alternative => !alternative.matched && count(alternative.sources) == 0) then null
|
|
55
|
+
else flat_map(lookup.alternatives, alternative => alternative.sources)
|
|
56
|
+
}
|
|
57
|
+
let readonly_write_function {
|
|
58
|
+
value walk(node, item => item.parent.node)
|
|
59
|
+
find (item) item is (ts_function, ts_arrow, ts_method)
|
|
60
|
+
}
|
|
61
|
+
let readonly_write_constructor {
|
|
62
|
+
value owner: readonly_write_function
|
|
63
|
+
value owner is ts_method && owner.calls_constructor
|
|
64
|
+
}
|
|
65
|
+
let readonly_write_class {
|
|
66
|
+
value walk(node, item => item.parent.node)
|
|
67
|
+
find (item) item is ts_class
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
fn readonly_writes(n) = {
|
|
71
|
+
value identity: declaration_identity(n)
|
|
72
|
+
value raw_key: if n is ts_parameter then n.name.node else n.key.node
|
|
73
|
+
value key: if raw_key is ts_computed_key then raw_key.expression.node else raw_key
|
|
74
|
+
value name: ts_value_key(key)
|
|
75
|
+
value program: last(walk(n, item => item.parent.node))
|
|
76
|
+
value index: program.readonly_write_index
|
|
77
|
+
value class: find(walk(n, item => item.parent.node), item => item is ts_class)
|
|
78
|
+
# A write whose receiver type is unknown can only target a member of the class it sits in.
|
|
79
|
+
value unknown: index.unnamable || any(index.unknown_writes,
|
|
80
|
+
write => write.name == name && (write.class == null || write.class == class))
|
|
81
|
+
value if unknown then null else map(find(index.writes, group => group.key == identity).items ?? [], write => write.access)
|
|
82
|
+
}
|
|
83
|
+
fn readonly_constructor_write(n, access) = {
|
|
84
|
+
value owner: access.readonly_write_function
|
|
85
|
+
value class: find(walk(n, item => item.parent.node), item => item is ts_class)
|
|
86
|
+
value owner is ts_method && owner.calls_constructor && owner.parent.node == class
|
|
87
|
+
&& !ts_value_static(n.modifier.nodes)
|
|
88
|
+
}
|
|
89
|
+
in ts_class_property, ts_parameter {
|
|
90
|
+
let readonly_writes readonly_writes(node)
|
|
91
|
+
let readonly_constructor_writes filter(readonly_writes, access => readonly_constructor_write(node, access))
|
|
92
|
+
let readonly_member_writes count(readonly_writes) - count(readonly_constructor_writes)
|
|
93
|
+
let readonly_constructor_indirect_writes count(filter(readonly_constructor_writes, access => access.object not ts_this))
|
|
94
|
+
}
|
|
95
|
+
fn readonly_member_key(key) =
|
|
96
|
+
key is (ts_identifier, ts_private_name, ts_string, ts_number)
|
|
97
|
+
|| key is ts_template && count(key.expression) == 0
|
|
98
|
+
|| key is ts_computed_key && (key.expression is ts_number
|
|
99
|
+
|| key.expression is ts_member && key.expression.object == "Symbol")
|
|
100
|
+
# An enum member reference is not trait-literal, but it is a literal initializer.
|
|
101
|
+
fn readonly_enum_literal(initializer) = ts_value_has_enum(initializer)
|
|
102
|
+
&& any(value_constituents(initializer) ?? [], arm => any(arm, part => part.identity_known && part.identity.kind == "value"))
|
|
103
|
+
fn readonly_primitive_annotation(initializer) = {
|
|
104
|
+
value names: filter(["string", "number", "boolean", "bigint"], name => ts_value_has_trait(initializer, name) == true)
|
|
105
|
+
value if count(names) > 0 then join(names, " | ") else null
|
|
106
|
+
}
|
|
107
|
+
fn readonly_enum_annotation(initializer) = {
|
|
108
|
+
value parts: value_constituents(initializer)
|
|
109
|
+
value carriers: {
|
|
110
|
+
value parts
|
|
111
|
+
flat_map (arm) arm
|
|
112
|
+
filter (part) part.carrier_known && part.carrier.description.kind == "enum"
|
|
113
|
+
map (part) part.carrier
|
|
114
|
+
unique
|
|
115
|
+
}
|
|
116
|
+
value carrier: first(carriers)
|
|
117
|
+
value scopes: if count(carriers) == 1 then name_scopes(initializer, "lexical") else null
|
|
118
|
+
value rows: map(scopes, scope => {declarations: name_scope_declarations(initializer, scope.id, carrier.description.name)})
|
|
119
|
+
value row: find(rows, row => row.declarations == null || count(row.declarations) > 0)
|
|
120
|
+
value identity: {file: carrier.file, node: carrier.node}
|
|
121
|
+
value if parts != null && scopes != null && row.declarations != null && count(row.declarations) > 0
|
|
122
|
+
&& all(row.declarations, declaration => declaration_identity(declaration.node) == identity)
|
|
123
|
+
then carrier.description.name else null
|
|
124
|
+
}
|
|
125
|
+
fn readonly_initializer_annotation(initializer) = {
|
|
126
|
+
value if ts_value_has_trait(initializer, "any") == true then "any"
|
|
127
|
+
else if ts_value_has_enum(initializer) then readonly_enum_annotation(initializer)
|
|
128
|
+
else readonly_primitive_annotation(initializer)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
in ts_class_property rule es-prefer-readonly {
|
|
132
|
+
option onlyInlineLambdas boolean = false "Limit initialised members to direct arrow functions"
|
|
133
|
+
when !onlyInlineLambdas || init == null || init is ts_arrow
|
|
134
|
+
when (contains([] modifier, "private") || key is ts_private_name)
|
|
135
|
+
&& !contains([] modifier, "readonly") && !contains([] modifier, "accessor")
|
|
136
|
+
&& readonly_member_key(key.node)
|
|
137
|
+
when readonly_writes != null && readonly_member_writes == 0
|
|
138
|
+
report at {start: style_head_offset, end: key.end_offset}
|
|
139
|
+
let readonly_message_name if key is ts_computed_key then key.expression.text else key.text
|
|
140
|
+
warn "Member '{readonly_message_name}' is never reassigned; mark it as `readonly`."
|
|
141
|
+
let preserve_inference type.node == null && init != null && count(readonly_constructor_writes) > 0
|
|
142
|
+
let literal_initializer init != null && (ts_value_has_trait(init.node, "literal") == true || readonly_enum_literal(init.node))
|
|
143
|
+
let needs_annotation preserve_inference && literal_initializer && key is ts_identifier && ts_value_has_trait(node, "literal") != true
|
|
144
|
+
let annotation if needs_annotation then readonly_initializer_annotation(init.node) else null
|
|
145
|
+
let fixable readonly_constructor_indirect_writes == 0 && (!preserve_inference || !ts_value_opaque(init.node) && ts_value_has_trait(init.node, "unknown") == false)
|
|
146
|
+
fix at key.node if fixable as "readonly " key (if annotation != null then ": " annotation else "")
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
in ts_parameter rule es-prefer-readonly:parameter {
|
|
150
|
+
option onlyInlineLambdas boolean = false "Limit initialised members to direct arrow functions"
|
|
151
|
+
when !onlyInlineLambdas || init == null || init is ts_arrow
|
|
152
|
+
when contains([] modifier, "private") && !contains([] modifier, "readonly")
|
|
153
|
+
when readonly_writes != null && readonly_member_writes == 0
|
|
154
|
+
report at {start: style_head_offset, end: name.end_offset}
|
|
155
|
+
warn "Member '{name}' is never reassigned; mark it as `readonly`."
|
|
156
|
+
fix at name.node if readonly_constructor_indirect_writes == 0 as "readonly " name
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import .ts/type-specifiers
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn readonly_parameter_marker(part) = count(part.traits) == 1 && contains(part.traits, "intersection")
|
|
6
|
+
fn readonly_parameter_uncertain(part) = !readonly_parameter_marker(part)
|
|
7
|
+
&& any(part.uncertain_traits, trait => !contains(["thenable", "nonthenable"], trait))
|
|
8
|
+
fn readonly_parameter_array(part) = contains(["Array", "ReadonlyArray"], part.carrier.description.name)
|
|
9
|
+
fn readonly_parameter_scalar(part) =
|
|
10
|
+
!any(part.traits, trait => contains(["object", "structural", "callable", "constructible"], trait))
|
|
11
|
+
&& any(part.traits, trait => contains(["any", "unknown", "undefined", "null", "boolean", "number", "string", "bigint", "symbol", "void"], trait))
|
|
12
|
+
fn readonly_parameter_allowed(part, settings) = {
|
|
13
|
+
value carrier: part.carrier
|
|
14
|
+
value name: carrier.description.name
|
|
15
|
+
value origin: if part.carrier_known && name != null && count(settings.allow) > 0
|
|
16
|
+
then declaration_origin(settings.node, {file: carrier.file, node: carrier.node}) else null
|
|
17
|
+
value gate: {name: name, from: if origin.kind == "library" then "lib" else origin.kind,
|
|
18
|
+
path: origin.path, package: origin.package}
|
|
19
|
+
value any(settings.allow, entry => type_specifier_matches(entry, gate, settings.directory))
|
|
20
|
+
}
|
|
21
|
+
fn readonly_parameter_annotation_allowed(annotation, allow) = if annotation == null || count(allow) == 0 then false else {
|
|
22
|
+
value sources: value_sources(annotation)
|
|
23
|
+
value alias: if count(sources.sites) == 1 then first(sources.sites) else null
|
|
24
|
+
value origin: if alias.kind == "ts_type_alias" then declaration_origin(annotation, alias) else null
|
|
25
|
+
value gate: {name: alias.name, from: if origin.kind == "library" then "lib" else origin.kind,
|
|
26
|
+
path: origin.path, package: origin.package}
|
|
27
|
+
value alias.kind == "ts_type_alias"
|
|
28
|
+
&& any(allow, entry => type_specifier_matches(entry, gate, program_directory(annotation)))
|
|
29
|
+
}
|
|
30
|
+
fn readonly_parameter_indexer_sources(part) =
|
|
31
|
+
flat_map([{domain: "text"}, {domain: "ordinal"}], key => {
|
|
32
|
+
value lookup: value_lookup(part.handle, key)
|
|
33
|
+
value if lookup == null then [] else filter(flat_map(lookup.alternatives, arm => arm.sources), source => source.kind == "indexer")
|
|
34
|
+
})
|
|
35
|
+
fn readonly_parameter_indexer_mutable(part) =
|
|
36
|
+
any(readonly_parameter_indexer_sources(part), source => !contains(source.flags, "readonly"))
|
|
37
|
+
fn readonly_parameter_branded(arm) = {
|
|
38
|
+
value parts: filter(arm.parts, part => !readonly_parameter_marker(part))
|
|
39
|
+
value count(parts) > 1
|
|
40
|
+
&& !any(parts, part => readonly_parameter_uncertain(part))
|
|
41
|
+
&& any(parts, part => any(part.traits, trait => contains(["string", "number", "bigint"], trait)))
|
|
42
|
+
&& any(parts, part => contains(part.traits, "object_literal"))
|
|
43
|
+
&& all(parts, part =>
|
|
44
|
+
any(part.traits, trait => contains(["string", "number", "bigint"], trait))
|
|
45
|
+
|| contains(part.traits, "object_literal")
|
|
46
|
+
&& !contains(part.traits, "callable") && !contains(part.traits, "constructible"))
|
|
47
|
+
}
|
|
48
|
+
fn readonly_parameter_children(handle, members, arguments, unchecked) = {
|
|
49
|
+
value children: value_children(handle, members, arguments)
|
|
50
|
+
value {
|
|
51
|
+
answer: if children == null then null else true,
|
|
52
|
+
children: map(children ?? [], edge => if unchecked then {kind: "source", source: edge.handle, root: false}
|
|
53
|
+
else {kind: "edge", edge: edge}),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
fn readonly_parameter_step(entry, settings) = if entry.kind == "source" then {
|
|
57
|
+
value arms: value_parts(entry.source)
|
|
58
|
+
value skipped: entry.root && count(arms) > 0 && all(arms, arm => readonly_parameter_branded(arm))
|
|
59
|
+
value {
|
|
60
|
+
answer: if arms == null then null else true,
|
|
61
|
+
children: if skipped then [] else map(arms ?? [], arm => {kind: "arm", arm: arm}),
|
|
62
|
+
}
|
|
63
|
+
} else if entry.kind == "edge" then {
|
|
64
|
+
value edge: entry.edge
|
|
65
|
+
value writable: edge.member != null && !any(edge.member.sources, source => contains(source.flags, "readonly")
|
|
66
|
+
|| contains(source.flags, "private") || settings.methods && contains(source.flags, "method"))
|
|
67
|
+
value {answer: !writable, children: if writable then [] else [{kind: "source", source: edge.handle, root: false}]}
|
|
68
|
+
} else if entry.kind == "part" then {
|
|
69
|
+
value part: entry.part
|
|
70
|
+
value branch: if readonly_parameter_marker(part) then {answer: true, children: []}
|
|
71
|
+
else if contains(part.traits, "error") then {answer: null, children: []}
|
|
72
|
+
else if readonly_parameter_array(part) then
|
|
73
|
+
if part.carrier.description.name == "Array" then {answer: false, children: []}
|
|
74
|
+
else readonly_parameter_children(part.handle, false, map(part.carrier.arguments, (argument, index) => index), false)
|
|
75
|
+
else if part.product != null then
|
|
76
|
+
if part.product.readonly then readonly_parameter_children(part.handle, true, [], true)
|
|
77
|
+
else {answer: false, children: []}
|
|
78
|
+
else readonly_parameter_children(part.handle, true, [], false)
|
|
79
|
+
value {answer: if branch.answer == false then false else if readonly_parameter_uncertain(part) then null else branch.answer,
|
|
80
|
+
children: branch.children}
|
|
81
|
+
} else {
|
|
82
|
+
value arm: entry.arm
|
|
83
|
+
value allowed: !any(arm.parts, part => contains(part.traits, "error"))
|
|
84
|
+
&& any(arm.parts, part => readonly_parameter_allowed(part, settings))
|
|
85
|
+
value branch: if allowed then {answer: true, children: []}
|
|
86
|
+
else if any(arm.parts, part => contains(part.traits, "error")) then {answer: null, children: []}
|
|
87
|
+
else if any(arm.parts, part => part.carrier.description.kind == "parameter")
|
|
88
|
+
&& !any(arm.parts, part => readonly_parameter_marker(part))
|
|
89
|
+
|| all(arm.parts, part => readonly_parameter_scalar(part)) then {answer: true, children: []}
|
|
90
|
+
else if any(arm.parts, part => readonly_parameter_indexer_mutable(part)) then {answer: false, children: []}
|
|
91
|
+
else if any(arm.parts, part => readonly_parameter_array(part) || part.product != null) then
|
|
92
|
+
{answer: true, children: map(arm.parts, part => {kind: "part", part: part})}
|
|
93
|
+
else readonly_parameter_children(arm.handle, true, [], false)
|
|
94
|
+
value {answer: if allowed then true else if branch.answer == false then false
|
|
95
|
+
else if any(arm.parts, part => readonly_parameter_uncertain(part)) then null else branch.answer,
|
|
96
|
+
children: branch.children}
|
|
97
|
+
}
|
|
98
|
+
fn readonly_parameter_has_mutable(source, methods, allow) = {
|
|
99
|
+
value settings: {node: source, methods: methods, allow: allow,
|
|
100
|
+
directory: if count(allow) > 0 then program_directory(source) else null}
|
|
101
|
+
value states: walk({kind: "source", source: source, root: true}, entry => readonly_parameter_step(entry, settings).children)
|
|
102
|
+
value any(states, entry => readonly_parameter_step(entry, settings).answer == false)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
in ts_parameter rule es-prefer-readonly-parameter-types {
|
|
106
|
+
option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Allowed type names and declaration origins"
|
|
107
|
+
option checkParameterProperties boolean = true "Check constructor parameter properties"
|
|
108
|
+
option ignoreInferredTypes boolean = false "Check only parameters with written types"
|
|
109
|
+
option treatMethodsAsReadonly boolean = false "Ignore method writability"
|
|
110
|
+
when parent is (ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature)
|
|
111
|
+
when checkParameterProperties || !any([] modifier, modifier => contains(["private", "public", "protected", "readonly"], modifier))
|
|
112
|
+
when !ignoreInferredTypes || type != null
|
|
113
|
+
when type not ts_type_keyword
|
|
114
|
+
|| !contains(["any", "unknown", "undefined", "null", "void", "never", "boolean", "number", "string", "bigint", "symbol"], type.text)
|
|
115
|
+
let readonly_parameter_source type.node ?? init.node ?? node
|
|
116
|
+
when !readonly_parameter_annotation_allowed(type.node, allow)
|
|
117
|
+
when readonly_parameter_has_mutable(readonly_parameter_source, treatMethodsAsReadonly, allow)
|
|
118
|
+
report at { start: name.offset, end: end_offset }
|
|
119
|
+
warn "Parameter should be a read only type."
|
|
120
|
+
}
|
|
121
|
+
}
|