@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,153 @@
|
|
|
1
|
+
# String prefix/suffix tests and safe replacements.
|
|
2
|
+
import .ts/methods
|
|
3
|
+
import .ts/expression-fixes
|
|
4
|
+
import .ts/values/inspection
|
|
5
|
+
import .ts/constants
|
|
6
|
+
|
|
7
|
+
define {
|
|
8
|
+
|
|
9
|
+
fn string_boundary_same(a, b) = a.reference_key != null && a.reference_key == b.reference_key
|
|
10
|
+
fn string_boundary_length_member(n, subject) = {
|
|
11
|
+
value bare: unparenthesised_node(n)
|
|
12
|
+
value bare is (ts_member, ts_index) && collection_method(bare, "length") && string_boundary_same(bare.object.node, subject)
|
|
13
|
+
}
|
|
14
|
+
fn string_boundary_length(n, subject) = {
|
|
15
|
+
value bare: unparenthesised_node(n)
|
|
16
|
+
value ts_runtime_string(subject) != null && ts_runtime_number(bare) != null && ts_runtime_number(bare) == utf16_len(ts_runtime_string(subject))
|
|
17
|
+
|| string_boundary_length_member(bare, subject)
|
|
18
|
+
}
|
|
19
|
+
fn string_boundary_tail(n, receiver, needle) = {
|
|
20
|
+
value bare: unparenthesised_node(n)
|
|
21
|
+
value bare is ts_binary && bare.operator == "-" && string_boundary_length_member(bare.left.node, receiver) && string_boundary_length(bare.right.node, needle)
|
|
22
|
+
}
|
|
23
|
+
fn string_boundary_last_character(n, receiver) = {
|
|
24
|
+
value bare: unparenthesised_node(n)
|
|
25
|
+
value bare is ts_binary && bare.operator == "-" && string_boundary_length_member(bare.left.node, receiver) && ts_runtime_number(bare.right.node) == 1
|
|
26
|
+
}
|
|
27
|
+
fn string_boundary_negative_length(n, needle) = {
|
|
28
|
+
value bare: unparenthesised_node(n)
|
|
29
|
+
value bare is ts_unary && bare.operator == "-" && string_boundary_length(bare.argument.node, needle)
|
|
30
|
+
}
|
|
31
|
+
fn string_boundary_receiver(n, optional) = if ts_value_string_like(n) then true
|
|
32
|
+
else if !optional then false else {
|
|
33
|
+
value arms: value_parts(n)
|
|
34
|
+
value arms != null && len(arms) > 0
|
|
35
|
+
&& any(arms, arm => any(arm.parts, part => contains(part.traits, "string")))
|
|
36
|
+
&& all(arms, arm => any(arm.parts, part =>
|
|
37
|
+
contains(part.traits, "string") || contains(part.traits, "null") || contains(part.traits, "undefined"))
|
|
38
|
+
&& all(arm.parts, part => !contains(part.traits, "any") && !contains(part.traits, "unknown")
|
|
39
|
+
&& !contains(part.uncertain_traits, "string")))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn string_boundary_access(n) = {
|
|
43
|
+
value bare: unparenthesised_node(n)
|
|
44
|
+
value method: if bare is ts_call then unparenthesised_node(bare.callee.node) else bare
|
|
45
|
+
value if bare not (ts_call, ts_index) || method not (ts_member, ts_index) then null else {
|
|
46
|
+
receiver: method.object.node,
|
|
47
|
+
optional: method.optional != null || bare.optional != null,
|
|
48
|
+
method: if bare is ts_index then "index" else method.static_name.nonempty ?? ts_runtime_string(method.index.node),
|
|
49
|
+
args: if bare is ts_call then bare.argument.nodes else [bare.index.node]
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
fn string_boundary_comparison(n, needle) = {
|
|
53
|
+
value access: string_boundary_access(n)
|
|
54
|
+
value arguments: access.args
|
|
55
|
+
value first: first(arguments)
|
|
56
|
+
value last: last(arguments)
|
|
57
|
+
value if access == null
|
|
58
|
+
|| !(contains(["indexOf", "lastIndexOf", "index", "charAt"], access.method) && len(arguments) == 1
|
|
59
|
+
|| contains(["slice", "substring"], access.method) && contains([1, 2], len(arguments))) then null
|
|
60
|
+
else if !string_boundary_receiver(access.receiver, access.optional) then null
|
|
61
|
+
else if access.method == "indexOf" && len(arguments) == 1 && ts_runtime_number(needle) == 0 then {
|
|
62
|
+
kind: "start", receiver: access.receiver, needle: first, optional: access.optional, safe: true
|
|
63
|
+
}
|
|
64
|
+
else if access.method == "lastIndexOf" && len(arguments) == 1 && string_boundary_tail(needle, access.receiver, first) then {
|
|
65
|
+
kind: "end", receiver: access.receiver, needle: first, optional: access.optional, safe: true
|
|
66
|
+
}
|
|
67
|
+
else if contains(["index", "charAt"], access.method) && len(arguments) == 1 && (ts_runtime_number(first) == 0 || string_boundary_last_character(first, access.receiver)) then {
|
|
68
|
+
kind: if ts_runtime_number(first) == 0 then "start" else "end",
|
|
69
|
+
receiver: access.receiver, needle: unparenthesised_node(needle), optional: access.optional,
|
|
70
|
+
safe: ts_runtime_string(needle) != null && utf16_len(ts_runtime_string(needle)) == 1
|
|
71
|
+
}
|
|
72
|
+
else if contains(["slice", "substring"], access.method) && len(arguments) == 2 && ts_runtime_number(first) == 0 && string_boundary_length(last, needle) then {
|
|
73
|
+
kind: "start", receiver: access.receiver, needle: unparenthesised_node(needle), optional: access.optional, safe: true
|
|
74
|
+
}
|
|
75
|
+
else if contains(["slice", "substring"], access.method) && (len(arguments) == 1 && (string_boundary_negative_length(first, needle) || string_boundary_tail(first, access.receiver, needle))
|
|
76
|
+
|| len(arguments) == 2 && string_boundary_tail(first, access.receiver, needle) && string_boundary_length_member(last, access.receiver)) then {
|
|
77
|
+
kind: "end", receiver: access.receiver, needle: unparenthesised_node(needle), optional: access.optional,
|
|
78
|
+
safe: access.method != "substring" || !string_boundary_negative_length(first, needle)
|
|
79
|
+
} else null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fn string_boundary_regex(n) = {
|
|
83
|
+
value pattern: collection_value_node(n)
|
|
84
|
+
value syntax: pattern.regexp_syntax
|
|
85
|
+
value root: first(syntax)
|
|
86
|
+
value flags: pattern.regexp_input.flags
|
|
87
|
+
value alternatives: filter(syntax, part => part.kind == "alternative" && part.parent == root.id)
|
|
88
|
+
value alternative: first(alternatives)
|
|
89
|
+
value terms: filter(syntax, term => term.parent == alternative.id)
|
|
90
|
+
value from_start: first(terms).kind == "assertion" && first(terms).raw == "^"
|
|
91
|
+
value from_end: last(terms).kind == "assertion" && last(terms).raw == "$"
|
|
92
|
+
value characters: if from_start then drop(terms, 1) else slice(terms, 0, -1)
|
|
93
|
+
value if syntax == null || type_of(flags) != "str" || re_test(flags, /[gimy]/)
|
|
94
|
+
|| len(alternatives) != 1 || from_start == from_end || len(characters) == 0
|
|
95
|
+
|| !all(characters, term => term.kind == "character" && term.character != null) then null else {
|
|
96
|
+
kind: if from_start then "start" else "end",
|
|
97
|
+
text: { value characters; map (term) term.character; join "" }
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
fn string_boundary_quote(text) = '"' {
|
|
101
|
+
value text
|
|
102
|
+
re_replace /\\/ "\\\\"
|
|
103
|
+
re_replace /"/ '\\"'
|
|
104
|
+
re_replace /\n/ "\\n"
|
|
105
|
+
re_replace /\r/ "\\r"
|
|
106
|
+
re_replace /\u{2028}/ "\\u2028"
|
|
107
|
+
re_replace /\u{2029}/ "\\u2029"
|
|
108
|
+
} '"'
|
|
109
|
+
fn string_boundary_regex_call(n, method_name) = {
|
|
110
|
+
value bare: unparenthesised_node(n)
|
|
111
|
+
value method: unparenthesised_node(bare.callee.node)
|
|
112
|
+
value if bare not ts_call || count(bare.argument) != 1 || !collection_method(method, method_name) then null else {
|
|
113
|
+
value match: method_name == "match"
|
|
114
|
+
value receiver: if match then method.object.node else first(bare.argument.nodes)
|
|
115
|
+
value pattern: if match then first(bare.argument.nodes) else method.object.node
|
|
116
|
+
value info: string_boundary_regex(pattern)
|
|
117
|
+
value optional: method.optional != null || bare.optional != null
|
|
118
|
+
value if info == null || match && !string_boundary_receiver(receiver, optional) then null else {
|
|
119
|
+
kind: info.kind, receiver: receiver, argument: string_boundary_quote(info.text), optional: optional, safe: true
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
fn string_boundary_replacement(n, info, negative) = {
|
|
124
|
+
value receiver: expression_fix_wrap(info.receiver.text, info.receiver.precedence < 19)
|
|
125
|
+
value result: (if negative then "!" else "") receiver (if info.optional then "?." else ".") (if info.kind == "end" then "endsWith" else "startsWith") "(" (info.argument.nonempty ?? info.needle.text) ")"
|
|
126
|
+
value if !expression_fix_clean(n) then null else expression_fix_edges(n, result)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
in ts_binary rule es-prefer-string-starts-ends-with {
|
|
130
|
+
option allowSingleElementEquality enum(never, always) = never
|
|
131
|
+
when re_test(operator, /^[!=]==?$/)
|
|
132
|
+
let comparison string_boundary_comparison(left.node, right.node)
|
|
133
|
+
let match if right.literal_value.type == "null" then string_boundary_regex_call(left.node, "match") else null
|
|
134
|
+
let info comparison ?? match
|
|
135
|
+
let access string_boundary_access(left.node)
|
|
136
|
+
when info != null
|
|
137
|
+
when allowSingleElementEquality != "always" || !contains(["index", "charAt"], access.method)
|
|
138
|
+
report at if match != null then unparenthesised_node(left.node) else node
|
|
139
|
+
warn if info.kind == "end" then "Use the 'String#endsWith' method instead." else "Use 'String#startsWith' method instead."
|
|
140
|
+
let safe info.safe && (match != null || len(operator) == 3 || ts_value_string_like(info.needle) || ts_runtime_string(info.needle) != null || comparison != null && contains(["indexOf", "lastIndexOf"], access.method))
|
|
141
|
+
let negative if match != null then starts_with(operator, "=") else starts_with(operator, "!")
|
|
142
|
+
let replacement if safe then string_boundary_replacement(node, info, negative) else null
|
|
143
|
+
fix at node if replacement != null as replacement
|
|
144
|
+
}
|
|
145
|
+
in ts_call rule es-prefer-string-starts-ends-with:regex {
|
|
146
|
+
let info string_boundary_regex_call(node, "test")
|
|
147
|
+
when info != null
|
|
148
|
+
warn if info.kind == "end" then "Use the 'String#endsWith' method instead." else "Use 'String#startsWith' method instead."
|
|
149
|
+
let replacement string_boundary_replacement(node, info, false)
|
|
150
|
+
fix if replacement != null as replacement
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
define {
|
|
2
|
+
fn switch_part_identity(part) =
|
|
3
|
+
when {
|
|
4
|
+
contains(part.traits, "undefined") then { kind: "nullish", value: "undefined" }
|
|
5
|
+
contains(part.traits, "null") then { kind: "nullish", value: "null" }
|
|
6
|
+
part.identity_known then part.identity
|
|
7
|
+
else then null
|
|
8
|
+
}
|
|
9
|
+
fn switch_enum_outer(n, site) = {
|
|
10
|
+
value parent: site_parent(n, site)
|
|
11
|
+
value if parent == null then ""
|
|
12
|
+
else if contains(["ts_namespace", "ts_module"], parent.kind) then {
|
|
13
|
+
value outer: first(site_field(n, parent, "name") ?? [])
|
|
14
|
+
value prefix: switch_enum_outer(n, parent)
|
|
15
|
+
value if outer == null then prefix else if prefix == "" then outer.name else prefix "." outer.name
|
|
16
|
+
} else switch_enum_outer(n, parent)
|
|
17
|
+
}
|
|
18
|
+
fn switch_enum_name(n, carrier) = {
|
|
19
|
+
value outer: switch_enum_outer(n, { file: carrier.file, node: carrier.node })
|
|
20
|
+
value if outer == "" then carrier.description.name else outer "." carrier.description.name
|
|
21
|
+
}
|
|
22
|
+
fn switch_enum_members(n, carrier) =
|
|
23
|
+
site_field(n, { file: carrier.file, node: carrier.node }, "member") ?? []
|
|
24
|
+
fn switch_enum_values(n, members) = {
|
|
25
|
+
value states: {
|
|
26
|
+
value { index: 0, literal: null }
|
|
27
|
+
walk (state) if state.index >= len(members) then null else {
|
|
28
|
+
value member: get(members, state.index)
|
|
29
|
+
value init: first(site_field(n, member, "init") ?? [])
|
|
30
|
+
value text: if init == null then null else trim(site_text(n, init).text)
|
|
31
|
+
value literal: when {
|
|
32
|
+
text == null && state.index == 0 then 0
|
|
33
|
+
text == null && type_of(state.literal) == "num" then state.literal + 1
|
|
34
|
+
text == null then null
|
|
35
|
+
starts_with(text, "'") || starts_with(text, "\"") then js_string(text)
|
|
36
|
+
type_of(num(text)) == "num" then num(text)
|
|
37
|
+
else then null
|
|
38
|
+
}
|
|
39
|
+
value { index: state.index + 1, literal: literal }
|
|
40
|
+
}
|
|
41
|
+
drop 1
|
|
42
|
+
}
|
|
43
|
+
value map(states, state => state.literal)
|
|
44
|
+
}
|
|
45
|
+
fn switch_enum_label(enum_name, member_name) =
|
|
46
|
+
if re_test(member_name, /^[A-Za-z_$][A-Za-z0-9_$]*$/) then enum_name "." member_name
|
|
47
|
+
else "(typeof " enum_name ")[" json_string(member_name) "]"
|
|
48
|
+
fn switch_literal(n, arm) = {
|
|
49
|
+
value part: find(arm, part => {
|
|
50
|
+
value identity: switch_part_identity(part)
|
|
51
|
+
value identity != null && (contains(["value", "nullish"], identity.kind) || contains(part.traits, "symbol"))
|
|
52
|
+
})
|
|
53
|
+
value enum_part: find(arm, part => part.carrier.description.kind == "enum")
|
|
54
|
+
value enum_members: if enum_part == null then [] else switch_enum_members(n, enum_part.carrier)
|
|
55
|
+
value enum_values: switch_enum_values(n, enum_members)
|
|
56
|
+
value enum_member: if part == null || enum_part == null then null else {
|
|
57
|
+
value index: index_of(enum_values, part.identity.value)
|
|
58
|
+
value if index == null then null else get(enum_members, index)
|
|
59
|
+
}
|
|
60
|
+
value symbol_site: if part == null || !contains(part.traits, "symbol") || part.identity.kind != "declaration"
|
|
61
|
+
then null else { file: part.identity.file, node: part.identity.node }
|
|
62
|
+
value symbol_name: if symbol_site == null then null else {
|
|
63
|
+
value name: first(site_field(n, symbol_site, "name") ?? [])
|
|
64
|
+
value if name == null then null else name.name
|
|
65
|
+
}
|
|
66
|
+
value label: when {
|
|
67
|
+
symbol_name != null then "typeof " symbol_name
|
|
68
|
+
enum_member != null then switch_enum_label(switch_enum_name(n, enum_part.carrier), enum_member.name)
|
|
69
|
+
contains(part.traits, "symbol") then "symbol"
|
|
70
|
+
switch_part_identity(part).kind == "nullish" then switch_part_identity(part).value
|
|
71
|
+
else then ts_value_print_part(part)
|
|
72
|
+
}
|
|
73
|
+
value if part == null then null else {
|
|
74
|
+
key: { identity: switch_part_identity(part), enum_file: enum_part.carrier.file, enum_node: enum_part.carrier.node },
|
|
75
|
+
label: label,
|
|
76
|
+
sort: if symbol_name != null then "unique symbol" else label,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
fn switch_arm_literals(n, arm) = {
|
|
80
|
+
value literal: switch_literal(n, arm)
|
|
81
|
+
value when {
|
|
82
|
+
literal != null then [literal]
|
|
83
|
+
any(arm, part => contains(part.traits, "boolean")) then map([false, true], value => {
|
|
84
|
+
key: { identity: { kind: "value", value: value }, enum_file: null, enum_node: null },
|
|
85
|
+
label: "" value,
|
|
86
|
+
sort: "" value,
|
|
87
|
+
})
|
|
88
|
+
else then [null]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
fn switch_case_literal(n, parts) = {
|
|
92
|
+
value arm: first(parts)
|
|
93
|
+
value literal: switch_literal(n, arm)
|
|
94
|
+
value if count(parts) != 1 || literal == null || any(arm, part =>
|
|
95
|
+
switch_part_identity(part) != literal.key.identity && part.carrier.description.kind != "enum") then null else literal
|
|
96
|
+
}
|
|
97
|
+
fn switch_sort_literals(literals) = fold(literals, [], (sorted, literal) => {
|
|
98
|
+
value before: filter(sorted, other => compare_text(other.sort, literal.sort, false, true) <= 0)
|
|
99
|
+
value after: filter(sorted, other => compare_text(other.sort, literal.sort, false, true) > 0)
|
|
100
|
+
value concat(concat(before, [literal]), after)
|
|
101
|
+
})
|
|
102
|
+
in ts_switch {
|
|
103
|
+
# Arms one by one: an enum type enumerates its members here.
|
|
104
|
+
let switch_arms value_alternatives(discriminant.node)
|
|
105
|
+
let switch_arm_parts map(switch_arms ?? [], arm => value_constituents(arm))
|
|
106
|
+
let switch_parts if switch_arms == null || any(switch_arm_parts, parts => parts == null) then null
|
|
107
|
+
else flat_map(switch_arm_parts, parts => parts)
|
|
108
|
+
let switch_literals flat_map(switch_parts, arm => switch_arm_literals(node, arm))
|
|
109
|
+
let switch_tests filter(case.nodes, branch => branch.test != null)
|
|
110
|
+
let switch_test_parts map(switch_tests, branch => value_constituents(branch.test.node))
|
|
111
|
+
let switch_test_literals map(switch_test_parts, parts => switch_case_literal(node, parts))
|
|
112
|
+
let switch_missing {
|
|
113
|
+
value filter(switch_literals, literal => literal != null
|
|
114
|
+
&& !any(switch_test_literals, branch => branch != null && branch.key == literal.key))
|
|
115
|
+
unique
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
in ts_switch rule es-switch-exhaustiveness-check {
|
|
119
|
+
option allowDefaultCaseForExhaustiveSwitch boolean = true "allow a default in an exhaustive switch"
|
|
120
|
+
option considerDefaultExhaustiveForUnions boolean = false "let a default cover missing union members"
|
|
121
|
+
option requireDefaultForNonUnion boolean = false "require a default for non-literal types"
|
|
122
|
+
option defaultCaseCommentPattern regex | null = null "comments that count as an omitted default"
|
|
123
|
+
when switch_parts != null && all(switch_test_parts, parts => parts != null)
|
|
124
|
+
let switch_default find(case.nodes, branch => branch.test == null) ?? (
|
|
125
|
+
if switch_default_comment != null && re_test(
|
|
126
|
+
{ value switch_default_comment.structure_comment_body; trim },
|
|
127
|
+
defaultCaseCommentPattern.nonempty ?? "(?i)^no default$"
|
|
128
|
+
) then switch_default_comment else null)
|
|
129
|
+
let switch_broad any(switch_literals, literal => literal == null)
|
|
130
|
+
let switch_missing_labels join(map(switch_sort_literals(switch_missing), literal => literal.label), " | ")
|
|
131
|
+
let switch_reports {
|
|
132
|
+
value [
|
|
133
|
+
{ node: discriminant.node, message: "Switch is not exhaustive. Cases not matched: " switch_missing_labels,
|
|
134
|
+
enabled: count(switch_missing) > 0 && !(considerDefaultExhaustiveForUnions && switch_default != null) },
|
|
135
|
+
{ node: discriminant.node, message: "Switch is not exhaustive. Cases not matched: default",
|
|
136
|
+
enabled: requireDefaultForNonUnion && switch_broad && switch_default == null },
|
|
137
|
+
{ node: switch_default, message: "The switch statement is exhaustive, so the default case is unnecessary.",
|
|
138
|
+
enabled: !allowDefaultCaseForExhaustiveSwitch && !switch_broad && count(switch_missing) == 0 && switch_default != null },
|
|
139
|
+
]
|
|
140
|
+
filter (item) item.enabled
|
|
141
|
+
}
|
|
142
|
+
each switch_report in switch_reports {
|
|
143
|
+
report at switch_report.node
|
|
144
|
+
warn "{switch_report.message}"
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Unnecessary template interpolations and raw-escape-preserving fixes.
|
|
2
|
+
import .ts/expression-fixes
|
|
3
|
+
import .ts/template-fixes
|
|
4
|
+
import .ts/values/inspection
|
|
5
|
+
|
|
6
|
+
define {
|
|
7
|
+
|
|
8
|
+
fn template_expression_subject(n) = if n is ts_literal_type then n.value.node else unparenthesised_node(n)
|
|
9
|
+
fn template_expression_whitespace(n) = if n is ts_string then n.literal_value.type == "string" && re_test(n.literal_value.value, /^[\t\v\f \r\n\uFEFF\p{Zs}\u2028\u2029]*$/) else n is (ts_template, ts_template_type) && count(n.quasi) == 1 && {
|
|
10
|
+
value n.quasi
|
|
11
|
+
first_quasi: first
|
|
12
|
+
value result: { value n.quasi; first; byte_len } - 1
|
|
13
|
+
value byte_slice(first_quasi, 1, result)
|
|
14
|
+
re_test /^[\t\v\f \r\n\uFEFF\p{Zs}\u2028\u2029]*$/
|
|
15
|
+
}
|
|
16
|
+
fn template_expression_literal(n, type_syntax) = n is ts_literal || n is (ts_template, ts_template_type) || (if type_syntax then n is ts_type_keyword && contains(["null", "undefined"], n.text) else n is ts_identifier && contains(["undefined", "Infinity", "NaN"], n.text))
|
|
17
|
+
fn template_expression_global_string(n) = n is ts_call && n.callee is ts_identifier && n.callee.text == "String" && n.callee.resolved == "global"
|
|
18
|
+
fn template_expression_string(n, single, type_syntax) = single && (ts_value_string_like(n) || template_expression_global_string(n)) && (!type_syntax || !ts_value_parameter(n) && !ts_value_has_enum(n))
|
|
19
|
+
|
|
20
|
+
fn template_regex_text(n) = {
|
|
21
|
+
value flags: re_find(n.text, /[a-z]*$/)
|
|
22
|
+
value body: re_replace(n.text, /[a-z]*$/, "")
|
|
23
|
+
value split("dgimsuvy", "")
|
|
24
|
+
filter (flag) contains(flags, flag)
|
|
25
|
+
canonical_flags: join ""
|
|
26
|
+
value body canonical_flags
|
|
27
|
+
}
|
|
28
|
+
fn template_interpolation_raw(n) = when {
|
|
29
|
+
n is ts_string then { value slice(n.text, 1, -1); template_literal_raw (["'", '"']) }
|
|
30
|
+
n is (ts_template, ts_template_type) then slice(n.text, 1, -1)
|
|
31
|
+
n is ts_regex then { value template_regex_text(n); re_replace /\\/ "\\\\"; template_literal_raw (["'", '"']) }
|
|
32
|
+
else then n.literal_value.value.nonempty ?? n.text
|
|
33
|
+
}
|
|
34
|
+
fn template_quasi_text(quasi) = slice(quasi.text, 1, if ends_with(quasi.text, "`") then -1 else -2)
|
|
35
|
+
fn template_unescaped_dollar(text) = re_test(text, /(^|[^\\])(?:\\\\)*\$$/)
|
|
36
|
+
fn template_interpolation_eligible(interpolation, comments, type_syntax) = !any(comments, comment => comment.offset >= interpolation.previous.end_offset && comment.end_offset <= interpolation.following.offset)
|
|
37
|
+
&& (interpolation.string_only || template_expression_literal(interpolation.subject, type_syntax) && !(re_test(interpolation.following.text, /^}[\r\n\u2028\u2029]/) && template_expression_whitespace(interpolation.subject)))
|
|
38
|
+
fn template_interpolation_fix(source, n, interpolation, next_opens, post_opens) = when {
|
|
39
|
+
interpolation.string_only then {
|
|
40
|
+
value operand: interpolation.expression
|
|
41
|
+
value parens: expression_fix_parens(source, n, operand)
|
|
42
|
+
value wrapped: expression_fix_wrap(operand.text, parens > 0)
|
|
43
|
+
value if parens == null then null else {
|
|
44
|
+
target: n,
|
|
45
|
+
text: expression_fix_edges(n, wrapped)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
interpolation.subject is ts_string && re_test(interpolation.subject.text, /(^|[^\\])(?:\\\\)*\\(?:[1-9]|0[0-9])/) then null
|
|
49
|
+
else then {
|
|
50
|
+
value raw_text: template_interpolation_raw(interpolation.subject)
|
|
51
|
+
value previous: byte_slice(source, interpolation.previous.offset, interpolation.previous.end_offset - 2)
|
|
52
|
+
value following: slice(interpolation.following.text, 1)
|
|
53
|
+
# A valid string NUL escape must not become a legacy octal escape on joining.
|
|
54
|
+
value raw: if re_test(raw_text, /(^|[^\\])(?:\\\\)*\\0$/) && re_test(following, /^[0-9]/) then slice(raw_text, 0, -2) "\\x00" else raw_text
|
|
55
|
+
value protect_previous: post_opens && template_unescaped_dollar(previous)
|
|
56
|
+
value protect_next: next_opens && template_unescaped_dollar(raw)
|
|
57
|
+
value fixed_raw: if protect_next then slice(raw, 0, -1) "\\$" else raw
|
|
58
|
+
value {
|
|
59
|
+
target: { start: interpolation.previous.end_offset - 2 - (if protect_previous then 1 else 0), end: interpolation.following.offset + 1 },
|
|
60
|
+
text: (if protect_previous then "\\$" else "") fixed_raw
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
in ts_template let template_expression_nodes expression.nodes
|
|
66
|
+
in ts_template_type let template_expression_nodes type.nodes
|
|
67
|
+
in ts_template, ts_template_type rule es-no-unnecessary-template-expression {
|
|
68
|
+
when parent not ts_tagged_template
|
|
69
|
+
let single count(template_expression_nodes) == 1 && first([] quasi.text) == "`${{" && last([] quasi.text) == "}}`"
|
|
70
|
+
let comments descendants(ts_comment)
|
|
71
|
+
let type_syntax is ts_template_type
|
|
72
|
+
let interpolation_pairs {
|
|
73
|
+
value quasi.nodes
|
|
74
|
+
following_quasi: drop 1
|
|
75
|
+
value quasi.nodes
|
|
76
|
+
quasi_pairs: zip following_quasi
|
|
77
|
+
value template_expression_nodes
|
|
78
|
+
zip quasi_pairs
|
|
79
|
+
map (pair) {
|
|
80
|
+
expression: { value pair; first; unparenthesised_node },
|
|
81
|
+
subject: { value pair; first; template_expression_subject },
|
|
82
|
+
previous: { value pair; last; first },
|
|
83
|
+
following: { value pair; last; last },
|
|
84
|
+
string_only: {
|
|
85
|
+
value pair
|
|
86
|
+
first
|
|
87
|
+
unparenthesised_node
|
|
88
|
+
template_expression_string single type_syntax
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
reverse
|
|
92
|
+
result: fold ({ opens: false, items: [] }) (state, interpolation) {
|
|
93
|
+
value following: slice(interpolation.following.text, 1, if ends_with(interpolation.following.text, "`") then -1 else -2)
|
|
94
|
+
value next_opens: if following == "" then state.opens else starts_with(following, "{{")
|
|
95
|
+
value eligible: template_interpolation_eligible(interpolation, comments, type_syntax)
|
|
96
|
+
value raw: if eligible then template_interpolation_raw(interpolation.subject) else null
|
|
97
|
+
value subject_quasis: if interpolation.subject is (ts_template, ts_template_type) then interpolation.subject.quasi.nodes else []
|
|
98
|
+
value first_quasi: first(subject_quasis)
|
|
99
|
+
value first_raw: if first_quasi == null then "" else template_quasi_text(first_quasi)
|
|
100
|
+
value post_opens: when {
|
|
101
|
+
interpolation.subject is (ts_template, ts_template_type) then if count(subject_quasis) == 1 && first_raw != "" then starts_with(first_raw, "{{") else next_opens
|
|
102
|
+
else then if raw == "" then next_opens else starts_with(raw, "{{")
|
|
103
|
+
}
|
|
104
|
+
value {
|
|
105
|
+
opens: if eligible then post_opens else state.opens,
|
|
106
|
+
items: {
|
|
107
|
+
value state.items
|
|
108
|
+
concat [{
|
|
109
|
+
data: interpolation,
|
|
110
|
+
eligible: eligible,
|
|
111
|
+
fix: if eligible then template_interpolation_fix(source, node, interpolation, next_opens, post_opens) else null
|
|
112
|
+
}]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
value result.items
|
|
117
|
+
reverse
|
|
118
|
+
}
|
|
119
|
+
each interpolation in interpolation_pairs {
|
|
120
|
+
when interpolation.eligible
|
|
121
|
+
report at if interpolation.data.string_only then {start: interpolation.data.expression.offset - 2, end: interpolation.data.expression.end_offset + 1} else {start: interpolation.data.previous.end_offset - 2, end: interpolation.data.following.offset + 1}
|
|
122
|
+
warn "Template literal expression is unnecessary and can be simplified."
|
|
123
|
+
fix at interpolation.fix.target if interpolation.fix != null as interpolation.fix.text
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import .ts/values/inspection
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn typed_template_gate(n, site, name) = {
|
|
6
|
+
value origin: declaration_origin(n, site)
|
|
7
|
+
value {name: name, from: when {
|
|
8
|
+
origin.kind == "library" then "lib"
|
|
9
|
+
origin.kind == "package" then "package"
|
|
10
|
+
else then "file"
|
|
11
|
+
}, path: origin.path, package: origin.package}
|
|
12
|
+
}
|
|
13
|
+
fn typed_template_named(part, n) = {
|
|
14
|
+
value named: part.carrier.description.kind == "named" || contains(part.traits, "enum")
|
|
15
|
+
value ancestry: if named then ts_value_ancestry(part).parts else []
|
|
16
|
+
value candidates: filter(ancestry, candidate => contains(["named", "enum"], candidate.carrier.description.kind))
|
|
17
|
+
value unique(map(candidates, candidate =>
|
|
18
|
+
typed_template_gate(n, candidate.carrier, candidate.carrier.description.name)))
|
|
19
|
+
}
|
|
20
|
+
fn typed_template_append(clauses, clause) = if clauses == null then null else {
|
|
21
|
+
value gates: unique(clause)
|
|
22
|
+
value present: any(clauses, previous => count(previous) == count(gates)
|
|
23
|
+
&& all(gates, gate => contains(previous, gate)))
|
|
24
|
+
value if present then clauses else if count(clauses) >= 256 then null else [] clauses [gates]
|
|
25
|
+
}
|
|
26
|
+
fn typed_template_disjoin(left, right) = if left == null || right == null then null else
|
|
27
|
+
fold(unique(left), [], (clauses, a) =>
|
|
28
|
+
fold(unique(right), clauses, (clauses, b) => typed_template_append(clauses, [] a b)))
|
|
29
|
+
fn typed_template_part(part, n, depth) = {
|
|
30
|
+
value gates: typed_template_named(part, n)
|
|
31
|
+
value primitive: find([
|
|
32
|
+
["string", "string"], ["any", "any"], ["null", "nullish"], ["undefined", "nullish"],
|
|
33
|
+
["boolean", "boolean"], ["number", "number"], ["bigint", "number"]
|
|
34
|
+
], pair => contains(part.traits, first(pair)))
|
|
35
|
+
value regexp: part.carrier.description.kind == "named" && part.carrier.description.name == "RegExp"
|
|
36
|
+
value shape: ts_value_array_part(part, n)
|
|
37
|
+
value elements: if shape.tuple then part.carrier.arguments
|
|
38
|
+
else if shape.array then slice(part.carrier.arguments, 0, 1) else []
|
|
39
|
+
value children: if shape.array || shape.tuple then
|
|
40
|
+
map(elements ?? [], element => typed_template_value(element, n, depth + 1)) else []
|
|
41
|
+
value clauses: when {
|
|
42
|
+
first(primitive) == "string" then []
|
|
43
|
+
primitive != null then [[{option: last(primitive)}]]
|
|
44
|
+
shape.array || shape.tuple then
|
|
45
|
+
if elements == null || any(children, child => child == null) then null
|
|
46
|
+
else [] [[{option: "array"}]] flat_map(children, child => child)
|
|
47
|
+
regexp then [[{option: "regexp"}]]
|
|
48
|
+
else then [[]]
|
|
49
|
+
}
|
|
50
|
+
value if clauses == null then null else map(clauses, clause => [] clause gates)
|
|
51
|
+
}
|
|
52
|
+
fn typed_template_value(handle, n, depth) = {
|
|
53
|
+
value arms: value_parts(handle)
|
|
54
|
+
value results: if depth > 64 then null else map(arms ?? [], arm =>
|
|
55
|
+
fold(arm.parts, [[]], (clauses, part) =>
|
|
56
|
+
typed_template_disjoin(clauses, typed_template_part(part, n, depth))))
|
|
57
|
+
value clauses: flat_map(results ?? [], result => result ?? [])
|
|
58
|
+
value when {
|
|
59
|
+
arms == null || results == null || any(results, result => result == null) then null
|
|
60
|
+
count(arms) == 0 then [[{option: "never"}]]
|
|
61
|
+
count(clauses) > 256 then null
|
|
62
|
+
else then clauses
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
fn typed_template_annotation(n) = {
|
|
66
|
+
value bare: unparenthesised_node(n)
|
|
67
|
+
value if bare is ts_identifier then bare.declaration.parent.type.node else null
|
|
68
|
+
}
|
|
69
|
+
fn typed_template_alias(n) = {
|
|
70
|
+
value annotation: typed_template_annotation(n)
|
|
71
|
+
value arms: value_parts(n)
|
|
72
|
+
value parts: filter(first(arms).parts ?? [], part =>
|
|
73
|
+
!contains(part.traits, "intersection") && !contains(part.traits, "parameter"))
|
|
74
|
+
value structural: count(arms) == 1 && count(parts) == 1
|
|
75
|
+
&& contains(first(parts).traits, "object_literal")
|
|
76
|
+
value roots: if structural && annotation is ts_type_reference then value_sources(annotation) else null
|
|
77
|
+
value aliases: filter(roots.sites ?? [], site => site.kind == "ts_type_alias")
|
|
78
|
+
value map(aliases, site => typed_template_gate(n, site, site.name))
|
|
79
|
+
}
|
|
80
|
+
fn typed_template_checks(n) = {
|
|
81
|
+
value clauses: typed_template_value(n, n, 0)
|
|
82
|
+
value aliases: typed_template_alias(n)
|
|
83
|
+
value if clauses == null then null else map(clauses, clause => [] clause aliases)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
in ts_node let template_type_checks typed_template_checks(node)
|
|
87
|
+
|
|
88
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import .ts/values/inspection
|
|
2
|
+
import .ts/typed/assignments
|
|
3
|
+
import .ts/typed/awaitability
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
fn argument_value_finding(site, mode, sender, receiver, display) = {
|
|
8
|
+
site: {start: site.offset, end: site.end_offset}, mode: mode, error: ts_value_category(sender) == "error",
|
|
9
|
+
sender: ts_value_print(display ?? sender), receiver: ts_value_print(receiver)
|
|
10
|
+
}
|
|
11
|
+
# An unsafe transfer needs an `any` somewhere the walk can reach: its own parts,
|
|
12
|
+
# or the arguments of their carriers (the only children the walk descends into).
|
|
13
|
+
fn assignment_may_be_unsafe(parts) = parts != null && any(parts, arm => any(arm, part =>
|
|
14
|
+
contains(part.traits, "any")
|
|
15
|
+
|| part.carrier_known && part.carrier != null && count(part.carrier.arguments ?? []) > 0))
|
|
16
|
+
fn argument_value_findings(call) = if call not (ts_call, ts_new, ts_tagged_template) then [] else {
|
|
17
|
+
value if call is ts_tagged_template then call.quasi.expression.nodes else call.argument.nodes
|
|
18
|
+
flat_map (argument) {
|
|
19
|
+
value spread: argument is ts_spread
|
|
20
|
+
value source: if spread then argument.argument.node else argument
|
|
21
|
+
value sender: value_constituents(source)
|
|
22
|
+
value expected: if spread || !assignment_may_be_unsafe(sender) then null else value_expected(argument)
|
|
23
|
+
value receiver: expected.value
|
|
24
|
+
value carrier: assignment_carrier(sender)
|
|
25
|
+
value element: first(carrier.arguments)
|
|
26
|
+
value when {
|
|
27
|
+
spread && ts_value_category(sender) != null then
|
|
28
|
+
[argument_value_finding(argument, "spread", sender, null, null)]
|
|
29
|
+
spread && assignment_array(sender, source) && ts_value_category(element) != null then
|
|
30
|
+
[argument_value_finding(argument, "array", element, null, sender)]
|
|
31
|
+
!spread && expected.present == true && receiver != null
|
|
32
|
+
&& !assignment_empty_map(source) && assignment_unsafe(sender, receiver, call) then
|
|
33
|
+
[argument_value_finding(argument, "argument", sender, receiver, null)]
|
|
34
|
+
else then []
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
fn return_value_owner(n) = {
|
|
39
|
+
value n.parent.node
|
|
40
|
+
walk (current) current.parent.node
|
|
41
|
+
find (current) current is (ts_function, ts_arrow, ts_method)
|
|
42
|
+
}
|
|
43
|
+
fn return_value_finding(n) = {
|
|
44
|
+
value source: if n is ts_return then n.argument.node
|
|
45
|
+
else if n.parent is ts_arrow && n is ts_arrow.body && n not ts_block then n else null
|
|
46
|
+
value owner: if source != null then return_value_owner(n) else null
|
|
47
|
+
value sender: if source != null then value_constituents(source) else null
|
|
48
|
+
value expected: if source != null then value_expected(source) else null
|
|
49
|
+
value receiver: if owner.return_type != null then value_constituents(owner.return_type.node) else expected.value
|
|
50
|
+
value known: owner != null && (owner.return_type != null && receiver != null
|
|
51
|
+
|| expected != null && (!expected.present || receiver != null))
|
|
52
|
+
value carrier: assignment_carrier(sender)
|
|
53
|
+
value target_carrier: assignment_carrier(receiver)
|
|
54
|
+
value element: first(carrier.arguments)
|
|
55
|
+
value target_element: first(target_carrier.arguments)
|
|
56
|
+
value asynchronous: contains([] owner.modifier, "async")
|
|
57
|
+
value promise: source != null && source could thenable && ts_value_category(sender) == null
|
|
58
|
+
&& !assignment_array(sender, source)
|
|
59
|
+
value awaited_handle: if promise then ts_awaited_value(source, source) else null
|
|
60
|
+
value awaited: if awaited_handle != null then value_constituents(awaited_handle) else null
|
|
61
|
+
value async_target: asynchronous && owner.return_type != null && owner.return_type.node could thenable
|
|
62
|
+
value target_handle: if promise && owner.return_type == null && expected.present == true
|
|
63
|
+
then value_expected_handle(source) else null
|
|
64
|
+
value target_awaited_handle: if !promise && !async_target || receiver == null then null
|
|
65
|
+
else if owner.return_type != null then ts_awaited_value(owner.return_type.node, owner.return_type.node)
|
|
66
|
+
else if target_handle != null then ts_awaited_value(target_handle, target_handle) else null
|
|
67
|
+
value target_awaited: if target_awaited_handle != null then value_constituents(target_awaited_handle) else null
|
|
68
|
+
value mode: when {
|
|
69
|
+
ts_value_category(sender) != null then "value"
|
|
70
|
+
assignment_array(sender, source) && ts_value_trait(element, "any") then "array"
|
|
71
|
+
ts_value_trait(awaited, "any") then "promise"
|
|
72
|
+
else then "generic"
|
|
73
|
+
}
|
|
74
|
+
value allowed: ts_value_trait(receiver, "unknown") || owner.return_type != null
|
|
75
|
+
&& ts_value_trait(receiver, "any")
|
|
76
|
+
|| async_target && (ts_value_trait(target_awaited, "any") || ts_value_trait(target_awaited, "unknown"))
|
|
77
|
+
|| mode == "promise" && (ts_value_trait(target_awaited, "unknown")
|
|
78
|
+
|| owner.return_type != null && ts_value_trait(target_awaited, "any"))
|
|
79
|
+
|| mode == "array" && assignment_array(receiver, source)
|
|
80
|
+
&& (ts_value_trait(target_element, "unknown") || owner.return_type != null && ts_value_trait(target_element, "any"))
|
|
81
|
+
value if !known || sender == null || allowed || source == null
|
|
82
|
+
|| async_target && target_awaited == null
|
|
83
|
+
|| promise && (awaited == null || receiver != null && target_awaited == null)
|
|
84
|
+
|| mode == "promise" && !asynchronous then null
|
|
85
|
+
else if mode != "generic" then assignment_finding(n, mode, sender, receiver, assignment_this_any(source, n))
|
|
86
|
+
else if !assignment_empty_map(source) && assignment_unsafe(sender, receiver, source)
|
|
87
|
+
then assignment_finding(n, mode, sender, receiver, false) else null
|
|
88
|
+
}
|
|
89
|
+
in ts_call, ts_new, ts_tagged_template { let argument_value_transfers argument_value_findings(node) }
|
|
90
|
+
in ts_node { let return_value_transfer return_value_finding(node) }
|
|
91
|
+
|
|
92
|
+
fn typed_accessor_key(method) = ts_value_key(method.key.node) ?? join(token_list(method.key.node), "")
|
|
93
|
+
fn typed_accessor_type_mismatch(annotation) = {
|
|
94
|
+
value getter: annotation.parent.node
|
|
95
|
+
value owner: getter.parent.node
|
|
96
|
+
value key: typed_accessor_key(getter)
|
|
97
|
+
value setter: if getter is (ts_method, ts_method_signature) && getter.kind == "get"
|
|
98
|
+
&& owner is (ts_class, ts_interface, ts_type_literal)
|
|
99
|
+
then find_last(owner.member.nodes, member => member.kind == "set" && typed_accessor_key(member) == key)
|
|
100
|
+
else null
|
|
101
|
+
value parameter: first(setter.parameter.nodes)
|
|
102
|
+
value setter != null && count(setter.parameter.nodes) == 1 && parameter.type != null
|
|
103
|
+
&& value_accepts(parameter.type.node, annotation, "value", "value") == false
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
}
|