@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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,104 @@
1
+ # String concatenation policy and source-preserving template fixes.
2
+ import .ts/expression-fixes
3
+ import .ts/template-fixes
4
+
5
+ define {
6
+
7
+ fn concatenation_string(n) = unparenthesised_node(n) is (ts_string, ts_template)
8
+ in ts_binary let additive operator == "+"
9
+ in ts_binary let string_addition additive && (concatenation_string(left.node) || left.string_addition == true || concatenation_string(right.node) || right.string_addition == true)
10
+ in ts_binary let nonstring_addition additive && ((if left.additive == true then left.nonstring_addition == true else !concatenation_string(left.node)) || (if right.additive == true then right.nonstring_addition == true else !concatenation_string(right.node)))
11
+ in ts_binary, ts_parenthesized let inside_addition parent is ts_binary && parent.additive == true || parent is ts_parenthesized && parent.inside_addition == true
12
+ in ts_parenthesized forward additive, string_addition, nonstring_addition from expression
13
+
14
+ fn concatenation_starts_curly(n) = {
15
+ value bare: {
16
+ value unparenthesised_node(n)
17
+ walk (current) if current is ts_binary then unparenthesised_node(current.left.node) else null
18
+ last
19
+ }
20
+ value if bare is ts_template then count(bare.expression) > 0 && first([] bare.quasi.text) == "`${{"
21
+ else bare not ts_string
22
+ }
23
+ fn concatenation_ends_curly(n) = {
24
+ value bare: unparenthesised_node(n)
25
+ value if bare is ts_binary then concatenation_starts_curly(bare.right.node)
26
+ else if bare is ts_template then count(bare.expression) > 0 && last([] bare.quasi.text) == "}}`"
27
+ else bare not ts_string
28
+ }
29
+ fn concatenation_legacy_escape(n) = {
30
+ value unparenthesised_node(n)
31
+ walk (bare) if bare.additive == true then [unparenthesised_node(bare.left.node), unparenthesised_node(bare.right.node)] else []
32
+ any (bare) bare is ts_string && re_test(bare.text, /(^|[^\\])(?:\\\\)*\\(?:[1-9]|0[0-9])/)
33
+ }
34
+ fn concatenation_gap(source, start, end) = {
35
+ value overlapping(ts_token, start, end)
36
+ filter (token) start <= token.offset && token.end_offset <= end
37
+ pieces: fold ({ text: "", cursor: start }) (state, token) {
38
+ text: state.text byte_slice(source, state.cursor, token.offset),
39
+ cursor: token.end_offset
40
+ }
41
+ value pieces.text byte_slice(source, pieces.cursor, end)
42
+ }
43
+ fn concatenation_template(source, n, before, after) = {
44
+ # Unlike trivia around ordinary operands, pure whitespace around an existing
45
+ # template is outside its value. Only comments need moving into its curly.
46
+ value prefix: if before != null && re_test(before, /\/[/*]/) then before else ""
47
+ value suffix: if after != null && re_test(after, /\/[/*]/) then after else ""
48
+ value if prefix == "" && suffix == "" then n.text else {
49
+ value first_quasi: first(n.quasi.nodes)
50
+ value last_quasi: last(n.quasi.nodes)
51
+ value leading: byte_slice(source, n.offset, first_quasi.end_offset)
52
+ value middle: byte_slice(source, first_quasi.end_offset, last_quasi.offset)
53
+ value trailing: byte_slice(source, last_quasi.offset, n.end_offset)
54
+ value leading prefix middle suffix trailing
55
+ }
56
+ }
57
+ fn concatenation_text(source, n, before, after) = {
58
+ value bare: unparenthesised_node(n)
59
+ value if bare is ts_string then {
60
+ value raw: slice(bare.text, 1, -1)
61
+ value quote: slice(bare.text, 0, 1)
62
+ value "`" template_literal_raw(raw, [quote]) "`"
63
+ }
64
+ else if bare is ts_template then concatenation_template(source, bare, before, after)
65
+ else if bare.string_addition == true then {
66
+ value left: unparenthesised_node(bare.left.node)
67
+ value right: unparenthesised_node(bare.right.node)
68
+ value leading: concatenation_gap(source, left.end_offset, bare.operator.offset)
69
+ value trailing: concatenation_gap(source, bare.operator.end_offset, right.offset)
70
+ value gap: leading trailing
71
+ value if concatenation_ends_curly(left) then {
72
+ value left_text: concatenation_text(source, left, before, gap)
73
+ value right_text: concatenation_text(source, right, null, after)
74
+ value slice(left_text, 0, -1) slice(right_text, 1)
75
+ } else if concatenation_starts_curly(right) then {
76
+ value left_text: concatenation_text(source, left, before, null)
77
+ value right_text: concatenation_text(source, right, gap, after)
78
+ value slice(left_text, 0, -1) slice(right_text, 1)
79
+ } else {
80
+ value left_text: concatenation_text(source, left, before, null)
81
+ value right_text: concatenation_text(source, right, after, null)
82
+ value left_text leading "+" trailing right_text
83
+ }
84
+ }
85
+ else "`${{" (before ?? "") bare.text (after ?? "") "}}`"
86
+ }
87
+ fn concatenation_fix(source, n) = if concatenation_legacy_escape(n) then null else {
88
+ value statement: expression_fix_statement(n)
89
+ value owners: expression_fix_before_nodes(statement)
90
+ value replacement: concatenation_text(source, n, null, null)
91
+ value semicolon: if statement == null then false else expression_fix_asi(statement, statement.token_neighbours.before, owners, replacement)
92
+ # Comments inside removed operand parentheses have no safe interpolation slot.
93
+ value retained: expression_fix_retains_comments(n, replacement)
94
+ value if semicolon == null || !retained then null else (if semicolon then ";" else "") replacement
95
+ }
96
+
97
+ in ts_binary rule es-prefer-template {
98
+ when string_addition && nonstring_addition && !inside_addition
99
+ let replacement concatenation_fix(source, node)
100
+ fix at node if replacement != null as expression_fix_edges(node, replacement)
101
+ warn "Unexpected string concatenation."
102
+ }
103
+
104
+ }
@@ -0,0 +1,24 @@
1
+ # .ts/suggestions: eslint core "suggestion" rules (style and best
2
+ # practice, not likely bugs) on the native TypeScript layers.
3
+
4
+ import .ts/expressions
5
+ import .ts/suggestions/object-shorthand
6
+ import .ts/suggestions/curly
7
+ import .ts/suggestions/renames
8
+ import .ts/suggestions/comparisons
9
+ import .ts/suggestions/templates
10
+ import .ts/suggestions/object-spread
11
+ import .ts/suggestions/computed-keys
12
+ import .ts/expression-fixes
13
+ import .ts/literal-text
14
+ import .ts/literals
15
+ import .ts/suggestions/helpers
16
+ import .ts/suggestions/control-flow
17
+ import .ts/suggestions/functions
18
+ import .ts/suggestions/constructors
19
+ import .ts/suggestions/expressions
20
+ import .ts/suggestions/errors
21
+ import .ts/suggestions/strings
22
+ import .ts/suggestions/regexp
23
+ import .ts/suggestions/builtins
24
+ import .ts/suggestions/directives
@@ -0,0 +1,22 @@
1
+ # Source-preserving string escapes for template fixes.
2
+ define {
3
+
4
+ fn template_literal_raw(raw, unescape_quotes) = {
5
+ value split(raw, "")
6
+ cooked: fold ({ text: "", escape: false, dollar: false }) (state, char) if state.escape then {
7
+ text: state.text (if contains(unescape_quotes, char) then "" else "\\") char,
8
+ escape: false,
9
+ dollar: false
10
+ } else if char == "\\" then {
11
+ text: state.text (if state.dollar then "$" else ""),
12
+ escape: true,
13
+ dollar: false
14
+ } else {
15
+ text: state.text (if state.dollar then (if char == "{{" then "\\$" else "$") else "") (if char == "$" then "" else if char == "`" then "\\`" else char),
16
+ escape: false,
17
+ dollar: char == "$"
18
+ }
19
+ value cooked.text (if cooked.dollar then "$" else "") (if cooked.escape then "\\" else "")
20
+ }
21
+
22
+ }
@@ -0,0 +1,124 @@
1
+ import .ts/comments
2
+
3
+ define {
4
+
5
+ fn ts_token_breaks(text) = re_count(text, /\r\n|[\r\n\u{2028}\u{2029}]/)
6
+
7
+ fn ts_token_kind(text) = {
8
+ value punctuation: {
9
+ ",": "ts_comma", ";": "ts_semicolon", ":": "ts_colon", ".": "ts_dot",
10
+ "?": "ts_question", "...": "ts_ellipsis", "=": "ts_equals",
11
+ "(": "ts_open_paren", ")": "ts_close_paren",
12
+ "[": "ts_open_bracket", "]": "ts_close_bracket",
13
+ "{": "ts_open_brace", "}": "ts_close_brace",
14
+ "<": "ts_open_angle", ">": "ts_close_angle",
15
+ }
16
+ value get(punctuation, text) ?? if re_test(text, /^[A-Za-z]/) then "ts_keyword"
17
+ else if contains(["?.", "??=", "??", "===", "==", "=>", "!==", "!=", "!",
18
+ "**=", "**", "*=", "*", "&&=", "&&", "&=", "&", "||=", "||", "|=", "|",
19
+ "<<=", "<<", "<=", "++", "+=", "+", "--", "-=", "-", "%=", "%", "^=", "^",
20
+ "/=", "/", "~", "@", "#"], text) then "ts_punctuator" else null
21
+ }
22
+
23
+ fn ts_token_record(entry) = if entry == null then null else {
24
+ text: entry.text,
25
+ kind: if entry.node != null then kind_name(entry.node) else ts_token_kind(entry.text),
26
+ start: entry.start,
27
+ end: entry.end,
28
+ }
29
+
30
+ fn ts_token_node(entry) = if entry == null then null else {
31
+ value overlapping(ts_token, entry.start, entry.end)
32
+ filter (item) item.offset == entry.start && item.end_offset == entry.end
33
+ first
34
+ }
35
+
36
+ fn ts_token_adjacent(n, before) = if before then token(n, "before") else token(n, "after")
37
+
38
+ fn ts_token_gaps(source, start, end, text) = if !contains(text, "/") then [text] else {
39
+ value comments: {
40
+ value overlapping(ts_comment, start, end)
41
+ filter (comment) comment.offset >= start && comment.end_offset <= end
42
+ sort_by (comment) comment.offset
43
+ }
44
+ value starts: [start] map(comments, comment => comment.end_offset)
45
+ value starts
46
+ map (from, index) byte_slice(source, from, first(slice(comments, index, index + 1)).offset ?? end)
47
+ }
48
+
49
+ fn ts_token_gap(source, start, end) = {
50
+ value text: byte_slice(source, start, end)
51
+ value breaks: if text == "" then 0 else ts_token_breaks(text)
52
+ value gaps: if text == "" then [] else ts_token_gaps(source, start, end, text)
53
+ value {
54
+ text: text,
55
+ start: start,
56
+ end: end,
57
+ spaced: any(gaps, gap => re_test(gap, /\s/)),
58
+ line_break: breaks > 0,
59
+ blank_lines: if breaks < 2 then 0 else {
60
+ value lines: split(re_replace(text, /\r\n|[\r\u{2028}\u{2029}]/, "\n"), "\n")
61
+ value count(filter(slice(lines, 1, -1), line => trim(line) == ""))
62
+ },
63
+ blank_gaps: if breaks < 2 then 0 else if !contains(text, "/") then breaks - 1
64
+ else sum(map(gaps, gap => max(ts_token_breaks(gap) - 1, 0))),
65
+ comments: text != "" && trim(text) != "",
66
+ }
67
+ }
68
+
69
+ fn ts_token_space(source, n, before) = {
70
+ value adjacent: ts_token_adjacent(n, before)
71
+ value if adjacent == null then null
72
+ else ts_token_gap(source, if before then adjacent.end else n.end_offset,
73
+ if before then n.offset else adjacent.start)
74
+ }
75
+
76
+ fn ts_token_gap_text(source, n, before) = {
77
+ value adjacent: ts_token_adjacent(n, before)
78
+ value if adjacent == null then null
79
+ else byte_slice(source, if before then adjacent.end else n.end_offset,
80
+ if before then n.offset else adjacent.start)
81
+ }
82
+
83
+ fn ts_token_neighbour(source, n, before) = {
84
+ value adjacent: ts_token_adjacent(n, before)
85
+ value if adjacent == null then null else {
86
+ value start: if before then adjacent.end else n.end_offset
87
+ value end: if before then n.offset else adjacent.start
88
+ value text: byte_slice(source, start, end)
89
+ value line_break: text != "" && re_test(text, /[\r\n\u{2028}\u{2029}]/)
90
+ value spaced: text != "" && any(ts_token_gaps(source, start, end, text), gap => re_test(gap, /\s/))
91
+ value {
92
+ text: adjacent.text,
93
+ start: adjacent.start,
94
+ end: adjacent.end,
95
+ gap: text,
96
+ spaced: spaced,
97
+ same_line: !line_break,
98
+ line_break: line_break,
99
+ }
100
+ }
101
+ }
102
+
103
+ fn ts_token_neighbours(source, n) = {
104
+ before: ts_token_neighbour(source, n, true),
105
+ after: ts_token_neighbour(source, n, false),
106
+ parent_start: n.parent.offset,
107
+ }
108
+
109
+ @macro ts_token_context {
110
+ let first_token ts_token_record(token(node, "first"))
111
+ let last_token ts_token_record(token(node, "last"))
112
+ let previous_token ts_token_record(token(node, "before")).kind
113
+ let next_token ts_token_record(token(node, "after")).kind
114
+ let token_texts token_list(node)
115
+ let paired_token ts_token_record(token_pair(node, [["(", ")"], ["[", "]"], ["{{", "}}"]]))
116
+ let space_before ts_token_space(source, node, true)
117
+ let space_after ts_token_space(source, node, false)
118
+ let token_neighbours ts_token_neighbours(source, node)
119
+ }
120
+
121
+ group ts_token_query_node ts_node, ts_token, ts_comment
122
+ in ts_token_query_node { @ts_token_context }
123
+
124
+ }
@@ -0,0 +1,93 @@
1
+ define {
2
+
3
+ schema type_specifier_file { from: "file"; name: string | string[]; path?: string }
4
+ schema type_specifier_lib { from: "lib"; name: string | string[] }
5
+ schema type_specifier_package { from: "package"; name: string | string[]; package: string }
6
+
7
+ fn type_specifier_path(path) = {
8
+ value slashes: replace(path, "\\", "/")
9
+ value rooted: starts_with(slashes, "/")
10
+ value parts: fold(split(slashes, "/"), [], (parts, part) =>
11
+ if part == "" || part == "." then parts
12
+ else if part == ".." then
13
+ if count(parts) > 0 && !ends_with(last(parts), ":") then slice(parts, 0, count(parts) - 1)
14
+ else parts
15
+ else concat(parts, [part]))
16
+ value (if rooted then "/" else "") join(parts, "/")
17
+ }
18
+ fn type_specifier_matches(entry, gate, directory) =
19
+ gate.name != null && (if type_of(entry) == "str" then gate.name == entry else {
20
+ value matches_origin: when {
21
+ entry.from == "lib" then gate.from == "lib"
22
+ entry.from == "package" then gate.from == "package"
23
+ && (gate.package == entry.package
24
+ || gate.package == "@types/" replace(replace(entry.package, "@", ""), "/", "__"))
25
+ entry.from == "file" then gate.path != null && directory != null && {
26
+ value base: type_specifier_path(directory)
27
+ value path: type_specifier_path(gate.path)
28
+ value if entry.path == null then starts_with(path,
29
+ base (if ends_with(base, "/") then "" else "/"))
30
+ else {
31
+ value allowed: type_specifier_path(entry.path)
32
+ value absolute: starts_with(allowed, "/") || re_test(allowed, /^[A-Za-z]:\//)
33
+ value path == (if absolute then allowed else type_specifier_path(base "/" entry.path))
34
+ }
35
+ }
36
+ else then false
37
+ }
38
+ value contains(if type_of(entry.name) == "str" then [entry.name] else entry.name, gate.name) && matches_origin
39
+ })
40
+ fn type_is_allowed(entries, groups, node) = {
41
+ value directory: if any(entries, entry => entry.from == "file") then program_directory(node) else null
42
+ value count(groups) > 0 && any(entries, entry =>
43
+ all(groups, group => any(group, gate => type_specifier_matches(entry, gate, directory))))
44
+ }
45
+ fn type_specifier_site(entry, n, site, name, directory) = {
46
+ value origin: declaration_origin(n, site)
47
+ value if origin == null || name == null || entry.from == "file" && directory == null then null else type_specifier_matches(entry, {
48
+ name: name,
49
+ from: if origin.kind == "library" then "lib" else if origin.kind == "package" then "package" else "file",
50
+ path: origin.path,
51
+ package: origin.package
52
+ }, directory)
53
+ }
54
+ fn type_specifier_part(entry, n, part, directory) = {
55
+ value scalar: find(["any", "unknown", "never", "void", "undefined", "null", "string", "number",
56
+ "boolean", "bigint", "symbol", "object"], name => contains(part.traits, name))
57
+ value if !part.carrier_known then null
58
+ else if part.carrier != null && contains(["named", "enum"], part.carrier.description.kind)
59
+ then type_specifier_site(entry, n, part.carrier, part.carrier.description.name, directory)
60
+ else if scalar != null then type_specifier_matches(entry, {name: scalar, from: "lib"}, directory)
61
+ else if contains(part.traits, "parameter") then null
62
+ else false
63
+ }
64
+ fn type_value_allowed(entries, n) = if count(entries) == 0 then false else {
65
+ value arms: value_parts(n)
66
+ value roots: value_sources(n)
67
+ value sites: walk(roots.sites ?? [], site => site_sources(n, site).sites ?? [])
68
+ value complete: roots != null && all(sites, site =>
69
+ contains(["ts_class", "ts_interface", "ts_enum", "ts_function", "ts_method", "ts_method_signature", "ts_array_type", "ts_tuple_type"], site.kind)
70
+ || site_sources(n, site) != null)
71
+ # Arms can be truthiness/enum splits of one declared type; a union source means distinct types.
72
+ value named: if count(arms) == 1 || !any(sites, site => site.kind == "ts_union") then filter(sites, site => site.name != null
73
+ && contains(["ts_type_alias", "ts_function", "ts_method", "ts_method_signature"], site.kind)) else []
74
+ value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
75
+ value states: map(entries, entry => {
76
+ value named_states: map(named, site => type_specifier_site(entry, n, site, site.name, directory))
77
+ value alternatives: map(arms ?? [], arm => {
78
+ value parts: map(arm.parts, part => type_specifier_part(entry, n, part, directory))
79
+ value if any(parts, state => state == true) || any(named_states, state => state == true) then true
80
+ else if !complete || any(parts, state => state == null) || any(named_states, state => state == null) then null
81
+ else false
82
+ })
83
+ value if arms == null then null
84
+ else if count(arms) == 0 then type_specifier_matches(entry, {name: "never", from: "lib"}, directory)
85
+ else if all(alternatives, state => state == true) then true
86
+ else if any(alternatives, state => state == false) then false
87
+ else null
88
+ })
89
+ value if any(states, state => state == true) then true
90
+ else if any(states, state => state == null) then null
91
+ else false
92
+ }
93
+ }
@@ -0,0 +1,154 @@
1
+ import .ts/values/inspection
2
+
3
+ define {
4
+
5
+ fn assignment_carrier(parts) = {
6
+ value arm: first(parts)
7
+ value carriers: filter(arm, part => part.carrier_known && part.carrier != null)
8
+ value carrier: first(carriers).carrier
9
+ value if parts != null && len(parts) == 1 && len(carriers) > 0
10
+ && all(carriers, part => part.carrier.description == carrier.description && part.carrier.arguments == carrier.arguments)
11
+ && all(carriers, part => part.product_known && part.product == first(carriers).product)
12
+ then carrier else null
13
+ }
14
+ fn assignment_array(parts, source) = {
15
+ value shape: ts_value_array_parts(parts, source)
16
+ value shape.known && shape.array
17
+ }
18
+ fn assignment_unsafe(sender, receiver, n) = {
19
+ value {sender: sender, receiver: receiver}
20
+ walk (pair) if ts_value_trait(pair.sender, "any") || pair.sender == null || pair.receiver == null
21
+ || len(pair.sender) != 1 || len(pair.receiver) != 1 then [] else {
22
+ value first(pair.sender)
23
+ flat_map (source) {
24
+ value first(pair.receiver)
25
+ flat_map (target) {
26
+ value assignment_carrier_arguments(source, target, n)
27
+ map (arguments) {sender: first(arguments), receiver: last(arguments)}
28
+ }
29
+ }
30
+ }
31
+ any (pair) ts_value_trait(pair.sender, "any") && pair.receiver != null && len(pair.receiver) > 0
32
+ && !ts_value_trait(pair.receiver, "any") && !ts_value_trait(pair.receiver, "unknown")
33
+ }
34
+ fn assignment_carrier_arguments(source, target, n) = {
35
+ value a: source.carrier
36
+ value b: target.carrier
37
+ value tuples: a.description.kind == "tuple" && b.description.kind == "tuple"
38
+ && source.product_known && target.product_known && source.product != null
39
+ && source.product == target.product
40
+ value source_readonly: if a.description.kind == "array" then source.product.readonly == true
41
+ else a.description.name == "ReadonlyArray"
42
+ value target_readonly: if b.description.kind == "array" then target.product.readonly == true
43
+ else b.description.name == "ReadonlyArray"
44
+ value arrays: ts_value_array_part(source, n).array && ts_value_array_part(target, n).array
45
+ && source_readonly == target_readonly
46
+ value same: arrays || tuples || a.description.kind == "named" && b.description.kind == "named"
47
+ && a.file == b.file && a.node == b.node
48
+ value arguments: zip(a.arguments ?? [], b.arguments ?? [])
49
+ value if source.carrier_known && target.carrier_known && a != null && b != null
50
+ && same && len(a.arguments) == len(b.arguments) then arguments else []
51
+ }
52
+ fn assignment_finding(site, mode, parts, receiver, this_any) = {
53
+ site: site, mode: mode, error: ts_value_category(parts) == "error", this_any: this_any,
54
+ sender: if mode == "generic" then ts_value_print(parts) else "any",
55
+ receiver: if mode == "generic" then ts_value_print(receiver) else null
56
+ }
57
+ # `this` typed as any, with `noImplicitThis` off, gets its own message variant.
58
+ fn assignment_this_any(source, n) = source != null && ts_compiler_option(n, "noImplicitThis") == false
59
+ && (source is ts_this && ts_value_category(value_constituent_traits(source, ["error", "any"])) == "any"
60
+ || any(filter(descendants(ts_this) ?? [], item => item.offset >= source.offset && item.end_offset <= source.end_offset),
61
+ item => ts_value_category(value_constituent_traits(item, ["error", "any"])) == "any"))
62
+ fn assignment_destructure_step(entry, source) = {
63
+ value receiver: entry.receiver
64
+ value sender: value_at(source, entry.path)
65
+ value if entry.mode != null && ts_value_category(sender) != null then
66
+ {finding: assignment_finding(receiver, entry.mode, sender, null, false), children: []}
67
+ else {
68
+ value carrier: assignment_carrier(sender)
69
+ value array: assignment_array(sender, source)
70
+ value element: first(carrier.arguments)
71
+ value if receiver is (ts_array, ts_array_pattern) then
72
+ if array && ts_value_trait(element, "any") then
73
+ {finding: assignment_finding(receiver, "array", element, null, false), children: []}
74
+ else if carrier.description.kind == "tuple" then {
75
+ value items: receiver.element.nodes
76
+ value selected: filter(items, item => item not (ts_hole, ts_rest))
77
+ value {finding: null, children: map(selected, item => {
78
+ value position: index_of(items, item)
79
+ value {receiver: item, path: [] entry.path position, mode: "tuple"}
80
+ })}
81
+ } else {finding: null, children: []}
82
+ else if receiver is (ts_object, ts_object_pattern) && ts_value_category(sender) == null
83
+ && !ts_value_trait(sender, "unknown") then {
84
+ value properties: filter(receiver.property.nodes, property => property not ts_rest && ts_value_key(property.key.node) != null)
85
+ value {finding: null, children: map(properties, property => {
86
+ value key: ts_value_key(property.key.node)
87
+ value target: property.value.node ?? property.key.node
88
+ value {receiver: target, path: [] entry.path key, mode: "object"}
89
+ })}
90
+ } else {finding: null, children: []}
91
+ }
92
+ }
93
+ fn assignment_destructure(receiver, source, path) = {
94
+ value {receiver: receiver, path: path, mode: null}
95
+ walk (entry) assignment_destructure_step(entry, source).children
96
+ map (entry) assignment_destructure_step(entry, source).finding
97
+ filter (finding) finding != null
98
+ sort_by (finding) finding.site.offset
99
+ }
100
+ fn assignment_empty_map(source) = {
101
+ value bare: unparenthesised_node(source)
102
+ value bare is ts_new && bare.callee is ts_identifier && bare.callee.text == "Map"
103
+ && count(bare.argument.nodes) == 0 && count(bare.type_argument.nodes) == 0
104
+ }
105
+ fn assignment_receiver(n) = if n is (ts_declarator, ts_parameter) then n.name.node
106
+ else if n is (ts_class_property, ts_property) then n.key.node
107
+ else if n is (ts_assignment, ts_assignment_pattern) then n.left.node
108
+ else if n is ts_jsx_attribute then n.name.node else null
109
+ fn assignment_sender(n) = if n is (ts_declarator, ts_parameter, ts_class_property) then n.init.node
110
+ else if n is (ts_assignment, ts_assignment_pattern) then n.right.node
111
+ else if n is ts_property then n.value.node ?? n.key.node
112
+ else if n is ts_jsx_attribute && n.value is ts_jsx_expression then n.value.expression.node else null
113
+ fn assignment_transfers_value(n) = {
114
+ value source: assignment_sender(n)
115
+ value target: assignment_receiver(n)
116
+ # Compound assignments and shorthand properties never transfer a value, so
117
+ # nothing below them is asked.
118
+ value if source == null || target == null || n is ts_assignment && n.operator != "="
119
+ || n is ts_property && (n.parent.flow_destructuring_target || source is ts_assignment_pattern) then [] else {
120
+ value compare: n not (ts_declarator, ts_parameter, ts_class_property) || n.type != null
121
+ value report: if n is ts_parameter then { start: target.offset, end: source.end_offset }
122
+ else if n is ts_jsx_attribute then source else n
123
+ value sender: value_constituents(source)
124
+ value receiver: if n.type != null then value_constituents(n.type.node)
125
+ else if n is ts_property then {
126
+ value key: ts_value_key(n.key.node)
127
+ value path: [key]
128
+ value value_expected_at(n.parent.node, path) ?? value_constituents(target)
129
+ } else value_constituents(target)
130
+ value category: ts_value_category(sender)
131
+ value known_unknown: ts_value_trait(receiver, "unknown")
132
+ && (n.type != null || target not (ts_array, ts_array_pattern, ts_object, ts_object_pattern))
133
+ value if category != null && !known_unknown then [assignment_finding(report, "any", sender, null, assignment_this_any(source, n))]
134
+ else if compare && !assignment_empty_map(source) && assignment_unsafe(sender, receiver, n)
135
+ then [assignment_finding(report, "generic", sender, receiver, false)]
136
+ else assignment_destructure(target, source, [])
137
+ }
138
+ }
139
+ fn assignment_spread(n) = {
140
+ value source: n.argument.node
141
+ value sender: value_constituents(source)
142
+ value carrier: assignment_carrier(sender)
143
+ value element: first(carrier.arguments)
144
+ value if n.parent is ts_array && (ts_value_category(sender) != null
145
+ || assignment_array(sender, source) && ts_value_trait(element, "any"))
146
+ then [assignment_finding(n, "spread", sender, null, false)] else []
147
+ }
148
+
149
+ in ts_declarator, ts_parameter, ts_class_property, ts_assignment, ts_assignment_pattern, ts_property, ts_jsx_attribute {
150
+ let assignment_value_transfers assignment_transfers_value(node)
151
+ }
152
+ in ts_spread { let assignment_value_transfers assignment_spread(node) }
153
+
154
+ }