@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,337 @@
|
|
|
1
|
+
# Type argument removal and reduce assertion relocation.
|
|
2
|
+
import .ts/assertions
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn type_argument_target(n) = unparenthesised_node(if n is ts_class then n.extends.node
|
|
7
|
+
else if n is (ts_type_reference, ts_jsx_opening) then n.name.node
|
|
8
|
+
else if n is ts_tagged_template then n.tag.node else n.callee.node)
|
|
9
|
+
fn type_argument_local(n) = {
|
|
10
|
+
value target: type_argument_target(n)
|
|
11
|
+
value binding: if target is ts_identifier then name_binding(target) else null
|
|
12
|
+
value owners: map(binding.declarations ?? [], declaration => declaration.parent.node)
|
|
13
|
+
value preferred: if n is ts_type_reference then
|
|
14
|
+
concat(concat(filter(owners, owner => owner is ts_interface), filter(owners, owner => owner is ts_type_alias)), filter(owners, owner => owner is ts_class))
|
|
15
|
+
else concat(concat(filter(owners, owner => owner is ts_declarator), filter(owners, owner => owner is ts_class)), filter(owners, owner => owner is (ts_function, ts_arrow)))
|
|
16
|
+
value candidates: if count(preferred) > 0 then preferred else owners
|
|
17
|
+
value parameterized: filter(candidates, owner => count(owner.type_parameter.nodes) > 0)
|
|
18
|
+
value chosen: first(parameterized)
|
|
19
|
+
value declarator: find(candidates, owner => owner is ts_declarator && owner.type != null)
|
|
20
|
+
value construct: if declarator != null then first(declarator.type.descendants(ts_call_signature)) else null
|
|
21
|
+
value owner: if binding != null then binding.node.parent else null
|
|
22
|
+
value if target is (ts_function, ts_arrow, ts_class) then target
|
|
23
|
+
else if chosen != null then chosen
|
|
24
|
+
else if construct != null && count(construct.type_parameter.nodes) > 0 then construct
|
|
25
|
+
else if count(candidates) != 1 then null
|
|
26
|
+
else if owner is (ts_function, ts_class, ts_interface, ts_type_alias) then owner
|
|
27
|
+
else if owner is ts_declarator && owner.type == null && owner.parent.kind == "const"
|
|
28
|
+
&& unparenthesised_node(owner.init.node) is (ts_function, ts_arrow, ts_class)
|
|
29
|
+
then unparenthesised_node(owner.init.node)
|
|
30
|
+
else null
|
|
31
|
+
}
|
|
32
|
+
# Names from another module stand for a declaration site: follow imports and
|
|
33
|
+
# re-exports rather than only the local binding.
|
|
34
|
+
fn type_argument_defined(target, classes) = if count(classes) == 0 then []
|
|
35
|
+
else { value found: name_definitions(target, first(classes))
|
|
36
|
+
value if count(found) > 0 then found else type_argument_defined(target, slice(classes, 1)) }
|
|
37
|
+
# An export wrapper holds the declaration; anything else (an import specifier
|
|
38
|
+
# whose module is unknown) is not one.
|
|
39
|
+
fn type_argument_declared(n, site, depth) = if site == null || depth > 3 then null
|
|
40
|
+
else if contains(["ts_class", "ts_interface", "ts_type_alias", "ts_function", "ts_arrow", "ts_declarator", "ts_method", "ts_namespace"], site.kind) then site
|
|
41
|
+
else { value declaration: first(site_field(n, site, "declaration") ?? [])
|
|
42
|
+
value type_argument_declared(n, declaration, depth + 1) }
|
|
43
|
+
# A member of a resolved object: class members, or a namespace body's own
|
|
44
|
+
# declarations (its `body` field lists them).
|
|
45
|
+
fn type_argument_member_sites(n, site) = if site == null then []
|
|
46
|
+
else if site.kind == "ts_namespace"
|
|
47
|
+
then flat_map(site_field(n, site, "body") ?? [], block => site_field(n, block, "body") ?? [])
|
|
48
|
+
else site_field(n, site, "member") ?? []
|
|
49
|
+
fn type_argument_member(n) = {
|
|
50
|
+
value callee: unparenthesised_node(type_argument_target(n))
|
|
51
|
+
value object: if callee is ts_member then unparenthesised_node(callee.object.node) else null
|
|
52
|
+
value key: if callee is ts_member then callee.static_name else null
|
|
53
|
+
value found: if object is ts_identifier && key != null then name_definitions(object, "value") else []
|
|
54
|
+
value sites: filter(map(found, item => type_argument_declared(n, site_parent(n, item), 0)), item => item != null)
|
|
55
|
+
value members: filter(flat_map(sites, site => type_argument_member_sites(n, site)), member => member.name == key)
|
|
56
|
+
value parameterized: filter(members, member => count(site_field(n, member, "type_parameter") ?? []) > 0)
|
|
57
|
+
value if count(parameterized) > 0 then first(parameterized)
|
|
58
|
+
else if count(members) == 1 then first(members)
|
|
59
|
+
else null
|
|
60
|
+
}
|
|
61
|
+
fn type_argument_named(n) = {
|
|
62
|
+
value target: type_argument_target(n)
|
|
63
|
+
value classes: if n is ts_type_reference then ["type", "value"] else ["value"]
|
|
64
|
+
value found: if target is ts_identifier then type_argument_defined(target, classes) else []
|
|
65
|
+
value sites: filter(map(found, site => type_argument_declared(n, site_parent(n, site), 0)), site => site != null)
|
|
66
|
+
# A type reference reads the type side; a class heritage reads the value side.
|
|
67
|
+
value ordered: if n is ts_type_reference then
|
|
68
|
+
concat(concat(filter(sites, site => site.kind == "ts_interface"), filter(sites, site => site.kind == "ts_type_alias")), filter(sites, site => site.kind == "ts_class"))
|
|
69
|
+
else sites
|
|
70
|
+
value parameterized: filter(ordered, site => count(site_field(n, site, "type_parameter") ?? []) > 0)
|
|
71
|
+
value if count(parameterized) > 0 then first(parameterized)
|
|
72
|
+
else if count(sites) == 1 then first(sites)
|
|
73
|
+
else null
|
|
74
|
+
}
|
|
75
|
+
# The declaration a call or type reference finally stands for: the selected
|
|
76
|
+
# signature for calls, otherwise the named declaration, otherwise a local one.
|
|
77
|
+
fn type_argument_signature(n) = if n is (ts_call, ts_new) then {
|
|
78
|
+
value binding: call_binding(n)
|
|
79
|
+
value if binding != null && count(binding.choices) == 1 && first(binding.choices).site != null
|
|
80
|
+
then first(binding.choices) else null
|
|
81
|
+
} else null
|
|
82
|
+
fn type_argument_site(n, signature) = {
|
|
83
|
+
value named: type_argument_named(n)
|
|
84
|
+
value member: type_argument_member(n)
|
|
85
|
+
value local: {
|
|
86
|
+
value declaration: type_argument_local(n)
|
|
87
|
+
value if declaration != null then node_site(declaration) else null
|
|
88
|
+
}
|
|
89
|
+
value if signature != null then signature.site
|
|
90
|
+
else if local != null then local
|
|
91
|
+
else if named != null then named
|
|
92
|
+
else member
|
|
93
|
+
}
|
|
94
|
+
fn type_argument_formals(n, site) = {
|
|
95
|
+
value formals: if site != null then site_field(n, site, "type_parameter") else null
|
|
96
|
+
value named: if formals != null && count(formals) > 0 then null else type_argument_named(n)
|
|
97
|
+
value if formals != null && count(formals) > 0 then formals
|
|
98
|
+
else if named != null then site_field(n, named, "type_parameter")
|
|
99
|
+
else formals
|
|
100
|
+
}
|
|
101
|
+
fn type_argument_known(parts) = parts != null && all(parts, arm => all(arm, part =>
|
|
102
|
+
part.carrier_known && !any(["any", "unknown", "error", "parameter"],
|
|
103
|
+
trait => contains(part.traits, trait) || contains(part.uncertain_traits, trait))
|
|
104
|
+
&& all(part.carrier.arguments ?? [], argument => type_argument_known(argument))))
|
|
105
|
+
fn type_argument_parts(parts) = flat_map(parts ?? [], arm => arm)
|
|
106
|
+
fn type_argument_special_part(part) =
|
|
107
|
+
contains(part.traits, "any")
|
|
108
|
+
|| contains(part.traits, "structural") && !contains(part.traits, "object")
|
|
109
|
+
&& !contains(part.traits, "callable") && !contains(part.traits, "parameter")
|
|
110
|
+
fn type_argument_special(parts) =
|
|
111
|
+
parts != null && any(type_argument_parts(parts), part => type_argument_special_part(part))
|
|
112
|
+
fn type_argument_numeric_part(part) =
|
|
113
|
+
contains(part.traits, "number") && !contains(part.traits, "string")
|
|
114
|
+
fn type_argument_exact_numeric_part(part) =
|
|
115
|
+
type_argument_numeric_part(part) && (!part.identity_known || part.identity == null)
|
|
116
|
+
fn type_argument_numeric(parts) = parts != null && count(parts) > 0
|
|
117
|
+
&& all(parts, arm => count(arm) > 0 && all(arm, part => type_argument_exact_numeric_part(part)))
|
|
118
|
+
fn type_argument_widened_numeric(parts) = parts != null && count(parts) > 0
|
|
119
|
+
&& all(parts, arm => count(arm) > 0 && all(arm, part => type_argument_numeric_part(part)))
|
|
120
|
+
fn type_argument_enum_carriers(parts) = flat_map(parts ?? [],
|
|
121
|
+
arm => map(filter(arm, part => contains(part.traits, "enum")), part => part.carrier))
|
|
122
|
+
# TypeScript assigns `number` to numeric enums (and unions of them); distinct
|
|
123
|
+
# numeric enums stay distinct.
|
|
124
|
+
fn type_argument_numeric_carriers_same(a, b) = {
|
|
125
|
+
value ca: type_argument_enum_carriers(a)
|
|
126
|
+
value cb: type_argument_enum_carriers(b)
|
|
127
|
+
value count(ca) == 0 || count(cb) == 0
|
|
128
|
+
|| count(ca) == count(cb) && all(ca, carrier => contains(cb, carrier))
|
|
129
|
+
}
|
|
130
|
+
fn type_argument_numeric_same(a, b) = type_argument_numeric(a) && type_argument_numeric(b)
|
|
131
|
+
&& type_argument_numeric_carriers_same(a, b)
|
|
132
|
+
fn type_argument_numeric_widened_same(a, b) =
|
|
133
|
+
type_argument_numeric(a) && type_argument_widened_numeric(b)
|
|
134
|
+
&& type_argument_numeric_carriers_same(a, b)
|
|
135
|
+
fn type_argument_product(parts) = if parts != null && count(parts) == 1 && count(first(parts)) == 1
|
|
136
|
+
then first(first(parts)).product else null
|
|
137
|
+
fn type_argument_element_count(parts) = {
|
|
138
|
+
value product: type_argument_product(parts)
|
|
139
|
+
value if product != null && product.elements != null then count(product.elements) else null
|
|
140
|
+
}
|
|
141
|
+
fn type_argument_element(handle, index) = {
|
|
142
|
+
value rows: value_children(handle, false, [index])
|
|
143
|
+
value if rows != null && count(rows) > 0 then first(rows).handle else null
|
|
144
|
+
}
|
|
145
|
+
fn type_argument_elements_same(a, b, count, index) =
|
|
146
|
+
if index >= count then true
|
|
147
|
+
else {
|
|
148
|
+
value left: type_argument_element(a, index)
|
|
149
|
+
value right: type_argument_element(b, index)
|
|
150
|
+
value left != null && right != null && type_argument_same_handles(left, right)
|
|
151
|
+
&& type_argument_elements_same(a, b, count, index + 1)
|
|
152
|
+
}
|
|
153
|
+
fn type_argument_equivalent(a, b, depth) = {
|
|
154
|
+
value ca: value_constituents(a)
|
|
155
|
+
value cb: value_constituents(b)
|
|
156
|
+
value elements_a: type_argument_element_count(ca)
|
|
157
|
+
value elements_b: type_argument_element_count(cb)
|
|
158
|
+
value if depth < 3 && elements_a != null && elements_a == elements_b && elements_a > 0
|
|
159
|
+
then type_argument_elements_same(a, b, elements_a, 0)
|
|
160
|
+
else type_argument_numeric_same(ca, cb)
|
|
161
|
+
|| type_argument_known(ca) && type_argument_known(cb)
|
|
162
|
+
&& value_accepts(a, b, "value", "value") == true
|
|
163
|
+
&& value_accepts(b, a, "value", "value") == true
|
|
164
|
+
}
|
|
165
|
+
fn type_argument_same_handles(a, b) = {
|
|
166
|
+
value ca: value_constituents(a)
|
|
167
|
+
value cb: value_constituents(b)
|
|
168
|
+
value if ca == null || cb == null then false
|
|
169
|
+
else if type_argument_special(ca) || type_argument_special(cb) then ca == cb
|
|
170
|
+
else if value_same(a, b) == true then true
|
|
171
|
+
else type_argument_equivalent(a, b, 0)
|
|
172
|
+
}
|
|
173
|
+
fn type_argument_enum_type(parts) = if parts != null && count(parts) == 1 then {
|
|
174
|
+
value carriers: type_argument_enum_carriers(parts)
|
|
175
|
+
value if count(carriers) > 0 && all(carriers, carrier => carrier == first(carriers))
|
|
176
|
+
then first(carriers) else null
|
|
177
|
+
} else null
|
|
178
|
+
fn type_argument_enum_value(parts, carrier) = parts != null && carrier != null
|
|
179
|
+
&& any(type_argument_parts(parts), part => contains(part.traits, "enum") && part.carrier == carrier)
|
|
180
|
+
# Widening an enum member gives its enum type, so the argument repeats the type
|
|
181
|
+
# argument even though the member itself is narrower.
|
|
182
|
+
fn type_argument_same_widened(n, argument) = {
|
|
183
|
+
value ca: value_constituents(n)
|
|
184
|
+
value cb: value_widened(argument)
|
|
185
|
+
value raw: value_constituents(argument)
|
|
186
|
+
value if ca == null || cb == null then false
|
|
187
|
+
else if type_argument_special(ca) || type_argument_special(cb) then ca == cb
|
|
188
|
+
else if type_argument_numeric_widened_same(ca, cb) then true
|
|
189
|
+
else if type_argument_enum_value(raw, type_argument_enum_type(ca)) then true
|
|
190
|
+
else value_accepts(n, argument, "value", "widened") == true
|
|
191
|
+
&& value_accepts(argument, n, "widened", "value") == true
|
|
192
|
+
}
|
|
193
|
+
fn type_argument_same(n, other, widened) = if widened then {
|
|
194
|
+
value source: value_union(n, [])
|
|
195
|
+
value raw: value_union(other, [])
|
|
196
|
+
value source != null && raw != null && type_argument_same_handles(source, raw)
|
|
197
|
+
|| type_argument_same_widened(n, other)
|
|
198
|
+
} else {
|
|
199
|
+
value source: value_union(n, [])
|
|
200
|
+
value source != null && other != null && type_argument_same_handles(source, other)
|
|
201
|
+
}
|
|
202
|
+
fn type_argument_default_site(n, site) = {
|
|
203
|
+
value formal: get(type_argument_formals(n, site), count(n.type_argument.nodes) - 1)
|
|
204
|
+
value defaults: if formal != null then site_field(n, formal, "default") else null
|
|
205
|
+
value if defaults != null && count(defaults) == 1 then first(defaults) else null
|
|
206
|
+
}
|
|
207
|
+
fn type_argument_default(n, site) = {
|
|
208
|
+
value default: type_argument_default_site(n, site)
|
|
209
|
+
value if default != null then declaration_value(n, default) else null
|
|
210
|
+
}
|
|
211
|
+
# An unresolved type reference (`F<number>` from a missing module) says
|
|
212
|
+
# nothing about which arguments it carries; only its written ones can tell
|
|
213
|
+
# it apart from a default written with another shape.
|
|
214
|
+
fn type_argument_written_same(n, default_site) = if n is ts_type_reference && default_site != null
|
|
215
|
+
then count(n.type_argument.nodes) == count(site_field(n, default_site, "type_argument") ?? [])
|
|
216
|
+
else true
|
|
217
|
+
fn type_argument_unresolved(n) = {
|
|
218
|
+
value parts: value_constituents(n)
|
|
219
|
+
value parts != null && !type_argument_known(parts)
|
|
220
|
+
}
|
|
221
|
+
fn type_argument_parameter_direct(n, parameter, formal) = {
|
|
222
|
+
value annotation: first(site_field(n, parameter, "type") ?? [])
|
|
223
|
+
value candidates: if annotation != null && annotation.kind == "ts_union"
|
|
224
|
+
then site_field(n, annotation, "type") ?? [] else [annotation]
|
|
225
|
+
value any(candidates, candidate => candidate != null && candidate.kind == "ts_type_reference"
|
|
226
|
+
&& count(site_field(n, candidate, "type_argument") ?? []) == 0
|
|
227
|
+
&& { value name: first(site_field(n, candidate, "name") ?? [])
|
|
228
|
+
value found: if name != null then site_text(n, name) else null
|
|
229
|
+
value found != null && found.text == formal.name })
|
|
230
|
+
}
|
|
231
|
+
fn type_argument_parameter_sites(n, site, signature) = if signature != null then
|
|
232
|
+
map(signature.parameters ?? [], parameter => parameter.site)
|
|
233
|
+
else if site == null then []
|
|
234
|
+
else if site.kind == "ts_class" then {
|
|
235
|
+
value constructor: find(site_field(n, site, "member") ?? [], member => member.name == "constructor")
|
|
236
|
+
value if constructor != null then site_field(n, constructor, "parameter") ?? [] else []
|
|
237
|
+
} else site_field(n, site, "parameter") ?? []
|
|
238
|
+
fn type_argument_inferred(n, site, signature, formal) = {
|
|
239
|
+
value owner: n.parent.node
|
|
240
|
+
value pairs: zip(type_argument_parameter_sites(n, site, signature), owner.argument.nodes)
|
|
241
|
+
value pair: find(pairs, pair => pair != null && first(pair) != null && last(pair) != null
|
|
242
|
+
&& last(pair) not (ts_this, ts_spread)
|
|
243
|
+
&& type_argument_parameter_direct(n, first(pair), formal))
|
|
244
|
+
value pair != null && type_argument_same(n, last(pair), true)
|
|
245
|
+
}
|
|
246
|
+
fn type_argument_redundancy(n) = {
|
|
247
|
+
value owner: n.parent.node
|
|
248
|
+
value arguments: owner.type_argument.nodes
|
|
249
|
+
value signature: type_argument_signature(owner)
|
|
250
|
+
value site: type_argument_site(owner, signature)
|
|
251
|
+
value formal: get(type_argument_formals(owner, site), count(arguments) - 1)
|
|
252
|
+
value default: type_argument_default(owner, site)
|
|
253
|
+
value default_site: type_argument_default_site(owner, site)
|
|
254
|
+
value if last(arguments) != n then null
|
|
255
|
+
else if default != null && type_argument_same(n, default, false)
|
|
256
|
+
&& (!type_argument_unresolved(n) || type_argument_written_same(n, default_site)) then "default"
|
|
257
|
+
else if formal != null && type_argument_inferred(n, site, signature, formal) then "inferred"
|
|
258
|
+
else null
|
|
259
|
+
}
|
|
260
|
+
fn reduce_type_argument_fresh(initializer, annotation) = {
|
|
261
|
+
value bare: unparenthesised_node(initializer)
|
|
262
|
+
value bare not ts_object || count(value_parts(annotation)) == 1 && all(bare.property.nodes, property =>
|
|
263
|
+
property is (ts_property, ts_method) && {
|
|
264
|
+
value key: ts_value_key(property.key.node)
|
|
265
|
+
value lookup: if key != null then value_lookup(annotation, key) else null
|
|
266
|
+
value source: if property is ts_method then property else property.value.node ?? property.key.node
|
|
267
|
+
value arms: value_parts(source)
|
|
268
|
+
value lookup != null && count(lookup.alternatives) > 0
|
|
269
|
+
&& all(lookup.alternatives, alternative => alternative.matched == true)
|
|
270
|
+
&& lookup.member != null && value_constituents(lookup.member) != null && arms != null
|
|
271
|
+
&& all(arms, arm => value_accepts(lookup.member, arm.handle, "value", "value") == true)
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
fn reduce_type_argument(n) = {
|
|
275
|
+
value call: n.parent.node
|
|
276
|
+
value callee: unparenthesised_node(call.callee.node)
|
|
277
|
+
value key: if callee is ts_member then callee.static_name
|
|
278
|
+
else if callee is ts_index then ts_runtime_string(callee.index.node) else null
|
|
279
|
+
value call is ts_call && get(call.argument.nodes, 1) == n && key == "reduce"
|
|
280
|
+
&& ts_value_array_like(callee.object.node)
|
|
281
|
+
&& reduce_type_argument_signature(call)
|
|
282
|
+
&& value_accepts(n.type.node, n.expression.node, "value", "value") == true
|
|
283
|
+
&& reduce_type_argument_fresh(n.expression.node, n.type.node)
|
|
284
|
+
}
|
|
285
|
+
fn reduce_type_argument_signature(call) = {
|
|
286
|
+
value selection: call_selection(call)
|
|
287
|
+
value signatures: if selection == null then value_signatures(call.callee.node) else null
|
|
288
|
+
value sites: if selection != null && selection.uncertain == false then [selection.selected]
|
|
289
|
+
else if signatures != null then map(signatures, arm => map(arm, header => header.site)) else null
|
|
290
|
+
value declarations: concat(global_declarations(call, "Array", "type"),
|
|
291
|
+
global_declarations(call, "ReadonlyArray", "type"))
|
|
292
|
+
value sites != null && count(sites) > 0
|
|
293
|
+
&& all(sites, arm => count(arm) > 0 && all(arm, selected => selected != null && any(declarations, declaration =>
|
|
294
|
+
any(site_field(call, declaration, "member"), member =>
|
|
295
|
+
member.name == "reduce" && member.file == selected.file && member.node == selected.node))))
|
|
296
|
+
}
|
|
297
|
+
fn type_argument_fix(n) = {
|
|
298
|
+
value owner: n.parent.node
|
|
299
|
+
value arguments: owner.type_argument.nodes
|
|
300
|
+
value previous: { value arguments; slice 0 -1; last }
|
|
301
|
+
value opening: last(overlapping(ts_open_angle, owner.offset, first(arguments).offset))
|
|
302
|
+
value closing: first(overlapping(ts_close_angle, n.end_offset, owner.end_offset))
|
|
303
|
+
value span: if count(arguments) == 1 then { start: opening.offset, end: closing.end_offset }
|
|
304
|
+
else { start: previous.end_offset, end: n.end_offset }
|
|
305
|
+
value if last(arguments) != n || opening == null || closing == null
|
|
306
|
+
|| count(overlapping(ts_comment, span.start, span.end)) > 0 then null else span
|
|
307
|
+
}
|
|
308
|
+
fn reduce_type_argument_fix(source, n) = {
|
|
309
|
+
value call: n.parent.node
|
|
310
|
+
value assertion: assertion_fix(source, n)
|
|
311
|
+
value insertion: if call.optional != null then call.optional.end_offset else call.callee.end_offset
|
|
312
|
+
value edits: {
|
|
313
|
+
value if count(call.type_argument.nodes) == 0 then [{ start: insertion, end: insertion, text: "<" n.type.text ">" }] else []
|
|
314
|
+
concat [{ start: assertion.target.offset, end: assertion.target.end_offset, text: assertion.text }]
|
|
315
|
+
}
|
|
316
|
+
value replacement: if assertion == null then null else expression_fix_edits(call.text, call.offset, edits)
|
|
317
|
+
value if replacement == null || !expression_fix_retains_comments(call, replacement) then null
|
|
318
|
+
else { target: call, text: replacement }
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
in ts_as, ts_type_assertion rule es-prefer-reduce-type-parameter {
|
|
322
|
+
when reduce_type_argument(node)
|
|
323
|
+
warn "Unnecessary assertion: Array#reduce accepts a type parameter for the default value."
|
|
324
|
+
let edit reduce_type_argument_fix(source, node)
|
|
325
|
+
fix at edit.target if edit != null as edit.text
|
|
326
|
+
}
|
|
327
|
+
in ts_call.type_argument, ts_new.type_argument, ts_type_reference.type_argument, ts_class.type_argument, ts_jsx_opening.type_argument, ts_tagged_template.type_argument rule es-no-unnecessary-type-arguments {
|
|
328
|
+
# Only the last type argument can be redundant (TypeScript ESLint checks no other).
|
|
329
|
+
when last(parent.type_argument.nodes) == node
|
|
330
|
+
let redundancy type_argument_redundancy(node)
|
|
331
|
+
when redundancy != null
|
|
332
|
+
warn if redundancy == "default" then "This is the default value for this type parameter, so it can be omitted." else "This value can be trivially inferred for this type parameter, so it can be omitted."
|
|
333
|
+
let edit type_argument_fix(node)
|
|
334
|
+
fix at edit if edit != null as ""
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Type identity selects redundant components; source tokens own removal.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
group duplicate_type_constituent ts_union, ts_intersection
|
|
6
|
+
in duplicate_type_constituent let duplicate_type_constituents_checked true
|
|
7
|
+
fn constituent_arm_key(arm) = {
|
|
8
|
+
value part: if count(arm.parts) == 1 then first(arm.parts) else null
|
|
9
|
+
value scalar: find(["void", "null", "undefined", "string", "number", "boolean", "bigint"],
|
|
10
|
+
trait => contains(part.traits, trait))
|
|
11
|
+
value if part != null && scalar != null && part.identity_known && part.identity.kind == "value"
|
|
12
|
+
then {scalar: scalar, identity: part.identity} else arm.handle
|
|
13
|
+
}
|
|
14
|
+
fn constituent_explicit_undefined(arms) = {
|
|
15
|
+
value part: if count(arms) == 1 && count(first(arms).parts) == 1 then first(first(arms).parts) else null
|
|
16
|
+
value part != null && part.identity_known && part.identity.kind == "value"
|
|
17
|
+
&& part.identity.value.type == "undefined" && contains(part.traits, "undefined")
|
|
18
|
+
&& !contains(part.traits, "void") && part.carrier.description.kind != "parameter"
|
|
19
|
+
}
|
|
20
|
+
fn constituent_collect(n, group, union, entries) = {
|
|
21
|
+
value candidates: {
|
|
22
|
+
value {node: n, group: group}
|
|
23
|
+
walk (item) when {
|
|
24
|
+
item.node is ts_parenthesized_type then {node: item.node.type.node, group: item.group}
|
|
25
|
+
union && item.node is ts_union || !union && item.node is ts_intersection then
|
|
26
|
+
map(item.node.type.nodes, child => {node: child, group: item.node})
|
|
27
|
+
else then null
|
|
28
|
+
}
|
|
29
|
+
filter (item) item.node not ts_parenthesized_type
|
|
30
|
+
sort_by (item) item.node.offset
|
|
31
|
+
}
|
|
32
|
+
value fold(candidates, entries, (state, item) => {
|
|
33
|
+
value bare: item.node
|
|
34
|
+
value arms: value_parts(bare)
|
|
35
|
+
value keys: if arms == null then null else unique(map(arms, arm => constituent_arm_key(arm)))
|
|
36
|
+
value complete: arms != null && !any(arms, arm => any(arm.parts, part => contains(part.traits, "error")))
|
|
37
|
+
value tokens: bare.token_texts
|
|
38
|
+
value previous_syntax: find(state, entry => entry.complete && entry.tokens == tokens).node
|
|
39
|
+
value previous_identity: if complete then find(state, entry => entry.complete
|
|
40
|
+
&& count(entry.keys) == count(keys) && all(keys, key => contains(entry.keys, key))).node else null
|
|
41
|
+
value state [{
|
|
42
|
+
node: bare, group: item.group, previous_syntax: previous_syntax, previous_identity: previous_identity,
|
|
43
|
+
complete: complete, tokens: tokens, keys: keys, explicit_undefined: constituent_explicit_undefined(arms),
|
|
44
|
+
}]
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
fn constituent_components(n, union) = fold(n.type.nodes, [],
|
|
48
|
+
(entries, child) => constituent_collect(child, n, union, entries))
|
|
49
|
+
|
|
50
|
+
fn constituent_wrappers(source, n) = {
|
|
51
|
+
value wrappers: walk(n, current => if current.parent is ts_parenthesized_type then current.parent.node else null)
|
|
52
|
+
value fold(wrappers, {node: n, before: "", after: ""}, (state, current) => {
|
|
53
|
+
value parent: current.parent
|
|
54
|
+
value if parent not ts_parenthesized_type then state else {
|
|
55
|
+
node: parent.node,
|
|
56
|
+
before: byte_slice(source, parent.first_token.end, current.offset) state.before,
|
|
57
|
+
after: state.after byte_slice(source, current.end_offset, parent.last_token.start),
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
fn constituent_equivalent(part) = part.previous_syntax ?? part.previous_identity
|
|
62
|
+
fn constituent_optional(part, optional) = {
|
|
63
|
+
value optional && part.explicit_undefined
|
|
64
|
+
}
|
|
65
|
+
fn constituent_removed(part, optional) =
|
|
66
|
+
part.complete && (constituent_equivalent(part) != null || constituent_optional(part, optional))
|
|
67
|
+
# Removing every constituent of one group would leave it empty; ESLint keeps the last one.
|
|
68
|
+
fn constituent_emptied(parts, optional) = {
|
|
69
|
+
value parts
|
|
70
|
+
group_by (part) part.group
|
|
71
|
+
flat_map (group) {
|
|
72
|
+
value removed: filter(group.items, part => constituent_removed(part, optional))
|
|
73
|
+
value if count(removed) == count(group.items) then [last(group.items)] else []
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
fn constituent_kept(part, emptied) = any(emptied, other => other.node == part.node)
|
|
77
|
+
fn constituent_hidden(part, parts) =
|
|
78
|
+
any(parts, other => other.complete && constituent_equivalent(other) != null
|
|
79
|
+
&& other.node.offset <= part.node.offset && other.node.end_offset >= part.node.end_offset
|
|
80
|
+
&& (other.node.offset != part.node.offset || other.node.end_offset != part.node.end_offset))
|
|
81
|
+
fn constituent_replacement(source, part, wrapped, parts, optional, emptied) = {
|
|
82
|
+
value before: wrapped.node.token_neighbours.before
|
|
83
|
+
value after: wrapped.node.token_neighbours.after
|
|
84
|
+
value remains: any(parts, other => other.group.offset == part.group.offset
|
|
85
|
+
&& (!constituent_removed(other, optional) || constituent_kept(other, emptied)))
|
|
86
|
+
value when {
|
|
87
|
+
!remains then null
|
|
88
|
+
contains(["|", "&"], before.text) && before.start >= part.group.offset then {
|
|
89
|
+
value gap: byte_slice(source, before.end, wrapped.node.offset)
|
|
90
|
+
value {start: before.start, end: wrapped.node.end_offset, text: gap wrapped.before wrapped.after}
|
|
91
|
+
}
|
|
92
|
+
contains(["|", "&"], after.text) && after.end <= part.group.end_offset then {
|
|
93
|
+
value gap: byte_slice(source, wrapped.node.end_offset, after.start)
|
|
94
|
+
value {start: wrapped.node.offset, end: after.end, text: wrapped.before wrapped.after gap}
|
|
95
|
+
}
|
|
96
|
+
else then null
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
fn constituent_candidate(source, part, parts, optional, emptied) = {
|
|
100
|
+
value wrapped: constituent_wrappers(source, part.node)
|
|
101
|
+
value previous: constituent_equivalent(part)
|
|
102
|
+
value replacement: constituent_replacement(source, part, wrapped, parts, optional, emptied)
|
|
103
|
+
value {node: part.node, wrapped: wrapped.node, previous: previous, replacement: replacement,
|
|
104
|
+
kept: constituent_kept(part, emptied)}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
in duplicate_type_constituent {
|
|
108
|
+
let constituent_root constituent_wrappers(source, node)
|
|
109
|
+
let constituent_top if is ts_union then constituent_root.node.parent not ts_union
|
|
110
|
+
else constituent_root.node.parent not ts_intersection
|
|
111
|
+
let constituent_kind if is ts_union then "Union" else "Intersection"
|
|
112
|
+
let constituent_parts if constituent_top then constituent_components(node, is ts_union) else []
|
|
113
|
+
let constituent_optional_parameter is ts_union
|
|
114
|
+
&& constituent_root.node.parent is ts_parameter
|
|
115
|
+
&& constituent_root.node.parent.optional != null
|
|
116
|
+
let constituent_emptied constituent_emptied(constituent_parts, constituent_optional_parameter)
|
|
117
|
+
let constituent_candidates {
|
|
118
|
+
value selected: filter(constituent_parts, part => constituent_removed(part, constituent_optional_parameter)
|
|
119
|
+
&& !constituent_hidden(part, constituent_parts))
|
|
120
|
+
value map(selected, part => constituent_candidate(source, part, constituent_parts, constituent_optional_parameter, constituent_emptied))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
rule es-no-duplicate-type-constituents {
|
|
124
|
+
when duplicate_type_constituents_checked
|
|
125
|
+
each part in constituent_candidates {
|
|
126
|
+
when part.previous != null
|
|
127
|
+
report at {start: part.node.offset, end: part.wrapped.end_offset}
|
|
128
|
+
warn "{constituent_kind} type constituent is duplicated with {part.previous}."
|
|
129
|
+
fix at part.replacement if part.replacement != null && !part.kept as part.replacement.text
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
rule es-no-duplicate-type-constituents:optional {
|
|
133
|
+
when duplicate_type_constituents_checked && constituent_optional_parameter
|
|
134
|
+
each part in constituent_candidates {
|
|
135
|
+
when part.previous == null
|
|
136
|
+
report at {start: part.node.offset, end: part.wrapped.end_offset}
|
|
137
|
+
warn "Explicit undefined is unnecessary on an optional parameter."
|
|
138
|
+
fix at part.replacement if part.replacement != null && !part.kept as part.replacement.text
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import .ts/values/inspection
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn conversion_primitive(n, primitive, constrained) = {
|
|
6
|
+
value graph: value_parts(n)
|
|
7
|
+
value arms: if graph == null then null else map(graph, arm => arm.parts)
|
|
8
|
+
value arms != null && count(arms) > 0 && all(arms, arm =>
|
|
9
|
+
any(arm, part => contains(part.traits, primitive))
|
|
10
|
+
&& all(arm, part =>
|
|
11
|
+
!any(["any", "error", "unknown", "intersection", "structural"], trait => contains(part.traits, trait))
|
|
12
|
+
&& (contains(part.traits, primitive) && part.carrier == null
|
|
13
|
+
|| part.carrier.description.kind == "enum"
|
|
14
|
+
|| constrained && part.carrier.description.kind == "parameter")))
|
|
15
|
+
}
|
|
16
|
+
fn conversion_integers(n) = {
|
|
17
|
+
value graph: value_parts(n)
|
|
18
|
+
value arms: if graph == null then null else map(graph, arm => arm.parts)
|
|
19
|
+
value arms != null && count(arms) > 0 && all(arms, arm =>
|
|
20
|
+
!any(arm, part => contains(part.traits, "intersection") || contains(part.traits, "parameter"))
|
|
21
|
+
&& any(arm, part => contains(part.traits, "number") && part.identity_known
|
|
22
|
+
&& part.identity.kind == "value" && type_of(part.identity.value) == "num"
|
|
23
|
+
&& part.identity.value % 1 == 0)
|
|
24
|
+
&& all(arm, part => part.carrier == null || part.carrier.description.kind == "enum"))
|
|
25
|
+
}
|
|
26
|
+
fn conversion_empty(n) = n is ts_string && js_string(n.text) == ""
|
|
27
|
+
fn conversion_finding(type, violation, offset) = { type: type, violation: violation, offset: offset }
|
|
28
|
+
fn conversion_global(callee) = {
|
|
29
|
+
value identity: declaration_identity(callee)
|
|
30
|
+
value globals: global_declarations(callee, callee.text, "value")
|
|
31
|
+
value callee.scope_declaration == null || identity != null && globals != null && any(globals, global => global == identity)
|
|
32
|
+
}
|
|
33
|
+
fn conversion_call(n) = {
|
|
34
|
+
value callee: n.callee.node
|
|
35
|
+
value primitive: get({ String: "string", Boolean: "boolean", Number: "number", BigInt: "bigint" }, callee.text)
|
|
36
|
+
value first: first(n.argument.nodes)
|
|
37
|
+
value if callee is ts_identifier && conversion_global(callee) && primitive != null
|
|
38
|
+
&& first != null && conversion_primitive(first, primitive, true) then
|
|
39
|
+
conversion_finding(primitive, "Passing a " primitive " to " callee.text "()", callee.offset)
|
|
40
|
+
else if callee is ts_member && callee.property.text == "toString"
|
|
41
|
+
&& conversion_primitive(callee.object.node, "string", true) && !ts_value_has_enum(callee.object.node) then
|
|
42
|
+
conversion_finding("string", "Calling a string's .toString() method", callee.property.offset)
|
|
43
|
+
else null
|
|
44
|
+
}
|
|
45
|
+
fn conversion_operator(n) = if n is (ts_binary, ts_assignment) then {
|
|
46
|
+
value if (n.operator == "+" || n.operator == "+=") && conversion_empty(n.right.node)
|
|
47
|
+
&& conversion_primitive(n.left.node, "string", false) then
|
|
48
|
+
conversion_finding("string", "Concatenating a string with ''",
|
|
49
|
+
if n is ts_assignment then n.offset else n.left.end_offset)
|
|
50
|
+
else if n is ts_binary && n.operator == "+" && conversion_empty(n.left.node)
|
|
51
|
+
&& conversion_primitive(n.right.node, "string", false) then
|
|
52
|
+
conversion_finding("string", "Concatenating '' with a string", n.offset)
|
|
53
|
+
else null
|
|
54
|
+
} else {
|
|
55
|
+
value inner: unparenthesised_node(n.argument.node)
|
|
56
|
+
value if n.operator == "+" && conversion_primitive(n.argument.node, "number", false) then
|
|
57
|
+
conversion_finding("number", "Using the unary + operator on a number", n.offset)
|
|
58
|
+
else if inner is ts_unary && inner.operator == n.operator then
|
|
59
|
+
if n.operator == "!" && conversion_primitive(inner.argument.node, "boolean", false) then
|
|
60
|
+
conversion_finding("boolean", "Using !! on a boolean", n.offset)
|
|
61
|
+
else if n.operator == "~" && conversion_integers(inner.argument.node) then
|
|
62
|
+
conversion_finding("number", "Using ~~ on an integer", n.offset)
|
|
63
|
+
else null
|
|
64
|
+
else null
|
|
65
|
+
}
|
|
66
|
+
fn type_conversion_site(n, start) = {
|
|
67
|
+
value end: if n is ts_call then (if n.callee is ts_identifier then n.callee.end_offset else n.end_offset)
|
|
68
|
+
else if n is ts_assignment then n.end_offset
|
|
69
|
+
else if n is ts_binary then (if start == n.offset then unparenthesised_node(n.right.node).offset else n.end_offset)
|
|
70
|
+
else if n.operator == "+" then n.operator.end_offset
|
|
71
|
+
else unparenthesised_node(n.argument.node).operator.end_offset
|
|
72
|
+
value { start: start, end: end }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
in ts_call, ts_unary, ts_binary, ts_assignment rule es-no-unnecessary-type-conversion {
|
|
76
|
+
let conversion if is ts_call then conversion_call(node) else conversion_operator(node)
|
|
77
|
+
when conversion != null
|
|
78
|
+
report at type_conversion_site(node, conversion.offset)
|
|
79
|
+
warn "{conversion.violation} does not change the type or value of the {conversion.type}."
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
}
|