@inthedark/dxlint 0.1.14 → 0.1.17

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 (71) hide show
  1. package/README.md +16 -9
  2. package/bin/dxlint-darwin-arm64 +0 -0
  3. package/bin/dxlint-linux-x64 +0 -0
  4. package/bin/dxlint-win32-x64.exe +0 -0
  5. package/esbuild.mjs +134 -0
  6. package/package.json +20 -2
  7. package/rules/bugs.dx +1 -0
  8. package/std/ts/arithmetic.dx +10 -0
  9. package/std/ts/assertions.dx +56 -4
  10. package/std/ts/calls.dx +45 -15
  11. package/std/ts/conditions.dx +2 -0
  12. package/std/ts/constants.dx +2 -1
  13. package/std/ts/content-mapper.dx +48 -0
  14. package/std/ts/deprecation.dx +38 -18
  15. package/std/ts/effects.dx +8 -1
  16. package/std/ts/enums.dx +32 -4
  17. package/std/ts/format.dx +1 -1
  18. package/std/ts/grammar.dx +29 -15
  19. package/std/ts/import-no-deprecated.dx +4 -2
  20. package/std/ts/javascript.dx +496 -0
  21. package/std/ts/layout/literals.dx +14 -7
  22. package/std/ts/layout/separators.dx +6 -1
  23. package/std/ts/layout/shared.dx +36 -37
  24. package/std/ts/layout/types.dx +4 -0
  25. package/std/ts/literals.dx +1 -0
  26. package/std/ts/methods.dx +3 -0
  27. package/std/ts/modules.dx +60 -2
  28. package/std/ts/names.dx +30 -13
  29. package/std/ts/narrowing.dx +20 -14
  30. package/std/ts/parentheses.dx +27 -0
  31. package/std/ts/problems/control-flow.dx +1 -0
  32. package/std/ts/problems/numeric-precision.dx +1 -0
  33. package/std/ts/problems/regexp.dx +1 -0
  34. package/std/ts/regex/checks.dx +1 -0
  35. package/std/ts/regex/findings.dx +10 -6
  36. package/std/ts/regex/values.dx +4 -15
  37. package/std/ts/restrictions.dx +76 -0
  38. package/std/ts/scoping/member-uses.dx +5 -3
  39. package/std/ts/scoping/shadowing.dx +1 -0
  40. package/std/ts/scoping/usage.dx +3 -3
  41. package/std/ts/spreads.dx +1 -1
  42. package/std/ts/structure/padding.dx +10 -8
  43. package/std/ts/style/expressions.dx +8 -4
  44. package/std/ts/style/returns.dx +3 -1
  45. package/std/ts/style/signatures.dx +5 -0
  46. package/std/ts/style/type-exports.dx +1 -0
  47. package/std/ts/suggestions/builtins.dx +1 -1
  48. package/std/ts/suggestions/comparisons.dx +1 -0
  49. package/std/ts/suggestions/expressions.dx +1 -0
  50. package/std/ts/suggestions/object-spread.dx +1 -1
  51. package/std/ts/tokens.dx +14 -5
  52. package/std/ts/type-specifiers.dx +19 -3
  53. package/std/ts/typed/awaitability.dx +1 -0
  54. package/std/ts/typed/collection-iteration.dx +24 -0
  55. package/std/ts/typed/floating-promises.dx +45 -14
  56. package/std/ts/typed/includes.dx +3 -4
  57. package/std/ts/typed/promises.dx +16 -13
  58. package/std/ts/typed/readonly-parameters.dx +11 -2
  59. package/std/ts/typed/string-boundaries.dx +1 -0
  60. package/std/ts/typed/switches.dx +1 -0
  61. package/std/ts/typed/transfers.dx +54 -7
  62. package/std/ts/typed/type-arguments.dx +19 -20
  63. package/std/ts/typed/type-conversions.dx +1 -0
  64. package/std/ts/typed/type-parameters.dx +1 -1
  65. package/std/ts/typed/using-disposable.dx +98 -0
  66. package/std/ts/typed.dx +22 -9
  67. package/std/ts/values/arguments.dx +1 -1
  68. package/std/ts/values/enums.dx +5 -1
  69. package/std/ts/values/inspection.dx +26 -7
  70. package/std/ts/values.dx +98 -39
  71. package/std/ts.dx +10 -4
@@ -116,6 +116,8 @@ in ts_open_angle, ts_close_angle let layout_generic_edge parent is layout_generi
116
116
  when layout_generic_edge
117
117
  }
118
118
 
119
+ # A function declaration, never an expression (an arrow's body is one).
120
+ fn layout_function_declaration(n) = n is ts_function && n is (ts_node.body, ts_node.declaration, ts_if.consequent, ts_if.alternate) && n.parent not ts_arrow
119
121
  in ts_open_angle rule es-type-generic-spacing {
120
122
  @layout_generic_policy
121
123
  when parent not (ts_arrow, ts_function_type, ts_call_signature)
@@ -123,6 +125,8 @@ in ts_open_angle rule es-type-generic-spacing {
123
125
  when previous_token != null
124
126
  let generic_space layout_comma_space(space_before.text, true)
125
127
  when before != (generic_space != "")
128
+ # Without `before`, a function declaration may space its `<` either way.
129
+ when before || !layout_function_declaration(parent.node)
126
130
  report at { start: offset - byte_len(generic_space), end: offset }
127
131
  fix at { start: offset - byte_len(generic_space), end: offset } as if before then " " else ""
128
132
  warn "Generic spaces mismatch"
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/names
2
3
  import .ts/import-attributes
3
4
 
package/std/ts/methods.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # String and collection method policies.
2
3
  import .ts/constants
3
4
 
@@ -18,6 +19,8 @@ fn collection_value(n) =
18
19
  fn collection_static_value(n) = { value n; collection_value_node; collection_value }
19
20
  fn collection_method(n, name) = n is ts_member && n.property == name || n is ts_index && collection_static_value(n.index.node).type == "string" && collection_static_value(n.index.node).value == name
20
21
 
22
+ in ts_call let collection_test_call count(argument) == 1 && collection_method(unparenthesised_node(callee.node), "test")
23
+
21
24
  fn collection_arrayish(n) = {
22
25
  value arms: value_parts(n)
23
26
  value parts: map(arms ?? [], arm => filter(arm.parts, part => !contains(part.traits, "parameter")
package/std/ts/modules.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/comments
2
3
 
3
4
  @macro ts_module_profile(profile, order, condition) {
@@ -10,6 +11,8 @@ import .ts/comments
10
11
  exclude "exclude"
11
12
  extends "extends"
12
13
  references "references[].path"
14
+ # Content mappers make TypeScript read other extensions as its own.
15
+ extensions "contentMappers[].extensions[]"
13
16
  options "compilerOptions"
14
17
  paths "paths"
15
18
  base "baseUrl"
@@ -22,6 +25,13 @@ import .ts/comments
22
25
  ".cjs" [".cts", ".d.cts"]
23
26
  }
24
27
  candidates ["{{dir}}/{{name}}{{ext}}", "{{dir}}/{{name}}/index{{ext}}"]
28
+ references {
29
+ ts_import ["from"]
30
+ ts_export ["from"]
31
+ ts_import_equals ["value.argument"]
32
+ ts_import_call ["argument"]
33
+ ts_import_type ["argument"]
34
+ }
25
35
  packages {
26
36
  directories ["node_modules"]
27
37
  metadata "package.json"
@@ -31,6 +41,7 @@ import .ts/comments
31
41
  scoped_prefix "@"
32
42
  type_directory "@types"
33
43
  type_join "__"
44
+ preserve_links_option "preserveSymlinks"
34
45
  }
35
46
  conditions ["types", condition, "default"]
36
47
  ambient_scopes [ambient_module]
@@ -50,17 +61,44 @@ import .ts/comments
50
61
  ]
51
62
  # `/// <reference lib="x" />` asks for `lib:x`, wherever the release keeps it.
52
63
  library_prefix "lib:"
64
+ # `/// <reference types="x" />` asks for `types:x`: the program's type
65
+ # roots first, then the packages seen from the file.
66
+ type_reference_prefix "types:"
53
67
  library_option "lib"
54
68
  default_libraries ["es2023", "dom"]
55
69
  types_option "types"
56
70
  type_roots_option "typeRoots"
57
71
  type_directories ["node_modules/@types"]
58
- project_files ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"]
72
+ # Without `include`: every file whose extension the program reads.
73
+ project_files ["**/*"]
59
74
  references true
60
75
  # A file publishes a global only by spelling its name (or escaping it,
61
76
  # or forwarding through a reference); ambient modules need a keyword.
62
77
  name_hints ["\\u", "<reference"]
63
78
  ambient_hints ["module", "namespace"]
79
+ # What pulls another file into the program, as tsc follows imports and
80
+ # references; a cheap text scan, so imports in comments count too.
81
+ pulls {
82
+ "{found}" [
83
+ "(?-u:\\b)(?:from|import)\\s*[\"']([^\"'\\n]+)[\"']",
84
+ "(?-u:\\b)(?:require|import)\\s*\\(\\s*[\"']([^\"'\\n]+)[\"']",
85
+ ]
86
+ }
87
+ # A file only pulls reach joins the program when it may publish globals
88
+ # or ambient modules; a plain module is reached through its importers.
89
+ pulled_publishers ["(?m)^\\s*declare\\s+(?:global(?-u:\\b)|module\\s+[\"'])"]
90
+ pulled_modules ["(?m)^\\s*(?:import|export)(?-u:\\b)"]
91
+ # Global lookups skip modules: a top-level `import`/`export` (column 0;
92
+ # a namespace's indented `export` keeps its script global).
93
+ global_modules ["(?m)^(?:import|export)(?-u:\\b)"]
94
+ # Reference directives count only in the file's leading comments, never
95
+ # inside a block comment (matched whole, so it holds no directive).
96
+ leading "\\A(?:[\\s\\uFEFF]+|//[^\\n]*|/\\*[\\s\\S]*?\\*/)*"
97
+ leading_pulls {
98
+ "./{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+path\\s*=\\s*[\"']([^\"']+)[\"']"]
99
+ "types:{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+types\\s*=\\s*[\"']([^\"']+)[\"']"]
100
+ "lib:{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+lib\\s*=\\s*[\"']([^\"']+)[\"']"]
101
+ }
64
102
  }
65
103
  when resolution.config.module == "commonjs" || resolution.config.module == 1 {
66
104
  conditions ["types", "require", "default"]
@@ -130,6 +168,16 @@ in ts_import {
130
168
  resolves type, query namespace.node in module js_string(from.text) as "*" asking all
131
169
  }
132
170
  }
171
+ # `import("./m")` (a written specifier) reads the module's namespace, as `import * as`.
172
+ fn ts_module_dynamic_specifier(n) = {
173
+ value bare: last(walk([first(n.argument.nodes)], item => if item is ts_parenthesized then [item.expression.node] else []))
174
+ value if bare is ts_string then js_string(bare.text)
175
+ else if bare is ts_template && count(bare.expression.nodes) == 0 then re_replace(bare.text, /^`|`$/, "") else null
176
+ }
177
+ in ts_import_call {
178
+ let ts_module_dynamic_source ts_module_dynamic_specifier(node)
179
+ when ts_module_dynamic_source != null { resolves value node in module ts_module_dynamic_source as "*" }
180
+ }
133
181
  in ts_import_specifier {
134
182
  when kind != "type" && parent.kind != "type" {
135
183
  resolves value, type, query local.node ?? imported.node in module js_string(parent.from.text) as ts_module_name(imported.node)
@@ -198,6 +246,16 @@ in ts_export {
198
246
  }
199
247
  }
200
248
  in ts_export_assignment { exports value, type, query expression.node as "=" }
249
+ # `export = Outer.Public`: a qualified target reads the namespace member, as
250
+ # `import X = Outer.Public` does.
251
+ fn ts_module_export_target(n) = n.parent is ts_export_assignment
252
+ || n.parent is ts_member && n == n.parent.object.node && ts_module_export_target(n.parent.node)
253
+ in ts_member {
254
+ let ts_module_member_key ts_module_name(property.node)
255
+ when ts_module_export_target(node) {
256
+ defines (scope module, namespace from object.definition).[ts_module_member_key]
257
+ }
258
+ }
201
259
 
202
260
  in ts_variable_declaration {
203
261
  when ts_module_public(node) { exports value, query declaration.nodes }
@@ -267,7 +325,7 @@ in ts_reference_path let ts_module_path_reference {
267
325
  value if starts_with(path, "./") || starts_with(path, "../") || starts_with(path, "/")
268
326
  || re_test(path, /^[A-Za-z]:\//) then path else "./{path}"
269
327
  }
270
- in ts_reference_types let ts_module_types_reference "@types/{ts_module_reference}"
328
+ in ts_reference_types let ts_module_types_reference "types:{ts_module_reference}"
271
329
  in ts_reference_lib let ts_module_lib_reference "lib:{ts_module_reference}"
272
330
  in ts_reference_path {
273
331
  globals all value, type, query from module ts_module_path_reference
package/std/ts/names.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/names: JavaScript and TypeScript scopes, declarations and references.
2
3
  import .ts/directives
3
4
  import .ts/effects
@@ -85,9 +86,9 @@ in ts_rest { targets argument }
85
86
  in ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature {
86
87
  declares value, query parameter in nearest function hoisted
87
88
  }
88
- # Other files see these bodies only by entering them: the declared return
89
- # type (or a constructor's or setter's role) stands for what they do, so an
90
- # edit inside one concerns only the files that stepped in.
89
+ # Other files never read inside these bodies: the declared return type (or a
90
+ # constructor's or setter's role) stands for what they do. An edit inside one
91
+ # concerns nobody else, and other files' view of this file leaves it empty.
91
92
  in ts_function, ts_arrow { opaque body when return_type != null }
92
93
  in ts_method { opaque body when return_type != null || kind == "constructor" || kind == "set" }
93
94
  in ts_parameter { targets name }
@@ -138,22 +139,29 @@ in ts_decorator let ts_name_decorator_method when {
138
139
  }
139
140
  in ts_identifier {
140
141
  let ts_name_outside_method containing(ts_decorator).ts_name_decorator_method
141
- when !(is (ts_member.property, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key,
142
+ let ts_name_resolution if is (ts_member.property, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key,
142
143
  ts_enum_member.name, ts_pattern_property.key, ts_import_specifier.imported, ts_export_specifier.local,
143
144
  ts_export_specifier.exported, ts_labeled.label, ts_break.label, ts_continue.label, ts_jsx_attribute.name,
144
145
  ts_tuple_member.name, ts_qualified_name.right, ts_type_parameter.name, ts_type_alias.name,
145
146
  ts_interface.name, ts_import.default_import, ts_import.namespace, ts_export.namespace, ts_import_specifier.local, ts_import_equals.name)
146
147
  || is ts_property.key && parent.value != null && !parent.flow_shorthand_default
147
148
  # `[key: string]: T` names nothing.
148
- || is ts_parameter.name && parent is ts_index_signature.parameter) {
149
- when ts_name_outside_method != null { resolves value outside ts_name_outside_method }
150
- when ts_name_outside_method == null && !ts_name_type_key { resolves value }
149
+ || is ts_parameter.name && parent is ts_index_signature.parameter then null
150
+ else if ts_name_outside_method != null then "outside"
151
+ # `import X = A.B`, `export = A.B`: `A` names a namespace in every meaning.
152
+ else if is ts_member.object && ts_name_entity_path(parent.node) then "entity"
151
153
  # A computed key inside type syntax reads like `typeof`: type-only
152
154
  # imports count.
153
- when ts_name_outside_method == null && ts_name_type_key { resolves query }
154
- }
155
+ else if ts_name_type_key then "query"
156
+ else "value"
157
+ when ts_name_resolution == "outside" { resolves value outside ts_name_outside_method }
158
+ when ts_name_resolution == "value" { resolves value }
159
+ when ts_name_resolution == "query" { resolves query }
160
+ when ts_name_resolution == "entity" { resolves value, type }
155
161
  }
156
- in ts_identifier let ts_name_type_key name_type_access && containing(ts_computed_key) != null
162
+ fn ts_name_entity_path(n) = n is (ts_export_assignment.expression, ts_import_equals.value)
163
+ || n is ts_member.object && ts_name_entity_path(n.parent.node)
164
+ in ts_identifier let ts_name_type_key containing(ts_computed_key) != null && name_type_access
157
165
  in ts_type_reference { resolves type name }
158
166
  # `export { x }` uses `x`; `export { x } from "m"` does not.
159
167
  in ts_export_specifier { when parent.from == null { resolves value, type local } }
@@ -178,6 +186,13 @@ group name_type_syntax ts_type_reference, ts_type_keyword, ts_type_parameter, ts
178
186
  ts_type_query, ts_import_type, ts_literal_type, ts_template_type, ts_type_predicate, ts_mapped_type
179
187
  in ts_node let name_type_access is name_type_syntax || parent.name_type_access == true
180
188
  in ts_node let name_write_owner when {
189
+ # Most nodes sit in no field that can write.
190
+ !(is (ts_for_in.left, ts_for_of.left, ts_declarator.name, ts_assignment.left, ts_update.argument, ts_unary.argument,
191
+ ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
192
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
193
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
194
+ ts_type_assertion.expression, ts_instantiation.expression, ts_property.value, ts_pattern_property.value,
195
+ ts_property.key, ts_pattern_property.key)) then null
181
196
  is (ts_for_in.left, ts_for_of.left) then parent.node
182
197
  is ts_declarator.name then when {
183
198
  parent.init != null then parent.node
@@ -272,7 +287,7 @@ in ts_node let reference_kind null
272
287
  in ts_identifier let declaration ts_name_binding.node
273
288
  in ts_identifier let declarations ts_name_binding.declarations ?? []
274
289
  in ts_identifier let references
275
- if ts_name_declared && ts_name_binding.node == node then ts_name_binding.references else []
290
+ if ts_name_declared && ts_name_binding.node == node then name_references(node) else []
276
291
  in ts_identifier let scope when {
277
292
  ts_name_declared then first(name_scopes(node, "declaration")).owner
278
293
  ts_name_referenced then {
@@ -310,8 +325,10 @@ fn ts_name_self_owner(n) = when {
310
325
  }
311
326
  in ts_identifier let ts_name_usage_references {
312
327
  value binding: if ts_name_declared then ts_name_binding else null
313
- value refs: filter(binding.references, reference => reference is ts_identifier)
314
- value if binding == null || binding.node != node || any(refs, reference => reference.access == null) then null else refs
328
+ value if binding == null || binding.node != node then null else {
329
+ value refs: filter(name_references(node), reference => reference is ts_identifier)
330
+ value if any(refs, reference => reference.access == null) then null else refs
331
+ }
315
332
  }
316
333
  in ts_node {
317
334
  let uses null
@@ -33,8 +33,10 @@ in ts_conditional {
33
33
  }
34
34
  in ts_type_predicate {
35
35
  resolves value name
36
- when modifier == null { unwraps truthy includes type from name }
36
+ when modifier == null { unwraps truthy includes type from name dynamic }
37
37
  }
38
+ # Equality keeps `any` as it is (TypeScript); predicates, typeof and
39
+ # instanceof narrow it (`dynamic`), except to `Object`/`Function`.
38
40
  in ts_binary {
39
41
  when operator == "===" && (right always literal || right always undefined || right.structure_unwrapped is ts_null) { unwraps truthy includes right from left }
40
42
  when operator == "===" && (left always literal || left always undefined || left.structure_unwrapped is ts_null) { unwraps truthy includes left from right }
@@ -58,16 +60,20 @@ in ts_binary {
58
60
  unwraps truthy right \ nullable
59
61
  }
60
62
  when operator == "in" { unwraps truthy has left from right }
61
- when operator == "instanceof" {
63
+ when operator == "instanceof" && (right not ts_identifier || right.text != "Object" && right.text != "Function") {
64
+ unwraps truthy includes right.["prototype"] from left dynamic
65
+ }
66
+ when operator == "instanceof" && right is ts_identifier && (right.text == "Object" || right.text == "Function") {
62
67
  unwraps truthy includes right.["prototype"] from left
63
68
  }
64
69
  }
65
70
 
66
- @macro narrowing_typeof(tag, expected, available) {
71
+ # `mode`: `dynamic` when the test narrows `any` (all but "function"/"object").
72
+ @macro narrowing_typeof(tag, expected, available, mode) {
67
73
  in ts_binary {
68
74
  when left.structure_unwrapped is ts_unary && left.structure_unwrapped.operator == "typeof" && right.literal_value.value == tag && available {
69
75
  when operator == "===" || operator == "==" {
70
- unwraps truthy includes expected from left.structure_unwrapped.argument
76
+ unwraps truthy includes expected from left.structure_unwrapped.argument mode
71
77
  }
72
78
  when operator == "!==" || operator == "!=" {
73
79
  unwraps truthy !includes expected from left.structure_unwrapped.argument
@@ -75,7 +81,7 @@ in ts_binary {
75
81
  }
76
82
  when right.structure_unwrapped is ts_unary && right.structure_unwrapped.operator == "typeof" && left.literal_value.value == tag && available {
77
83
  when operator == "===" || operator == "==" {
78
- unwraps truthy includes expected from right.structure_unwrapped.argument
84
+ unwraps truthy includes expected from right.structure_unwrapped.argument mode
79
85
  }
80
86
  when operator == "!==" || operator == "!=" {
81
87
  unwraps truthy !includes expected from right.structure_unwrapped.argument
@@ -83,14 +89,14 @@ in ts_binary {
83
89
  }
84
90
  }
85
91
  }
86
- @narrowing_typeof("string", (trait string, truthy | trait string, falsy), true)
87
- @narrowing_typeof("number", (trait number, truthy | trait number, falsy), true)
88
- @narrowing_typeof("boolean", (trait boolean, truthy | trait boolean, falsy), true)
89
- @narrowing_typeof("bigint", (trait bigint, truthy | trait bigint, falsy), true)
90
- @narrowing_typeof("symbol", (trait symbol, truthy), true)
91
- @narrowing_typeof("undefined", (trait undefined, nullable, falsy, nonthenable), true)
92
- @narrowing_typeof("function", (instance "Function"), global_declarations(node, "Function", "type") != null && count(global_declarations(node, "Function", "type")) > 0)
93
- @narrowing_typeof("function", (trait callable, object, truthy), global_declarations(node, "Function", "type") == [])
94
- @narrowing_typeof("object", (trait object, truthy | trait null, nullable, falsy, nonthenable), true)
92
+ @narrowing_typeof("string", (trait string, truthy | trait string, falsy), true, dynamic)
93
+ @narrowing_typeof("number", (trait number, truthy | trait number, falsy), true, dynamic)
94
+ @narrowing_typeof("boolean", (trait boolean, truthy | trait boolean, falsy), true, dynamic)
95
+ @narrowing_typeof("bigint", (trait bigint, truthy | trait bigint, falsy), true, dynamic)
96
+ @narrowing_typeof("symbol", (trait symbol, truthy), true, dynamic)
97
+ @narrowing_typeof("undefined", (trait undefined, nullable, falsy, nonthenable), true, dynamic)
98
+ @narrowing_typeof("function", (instance "Function"), global_declarations(node, "Function", "type") != null && count(global_declarations(node, "Function", "type")) > 0, static)
99
+ @narrowing_typeof("function", (trait callable, object, truthy), global_declarations(node, "Function", "type") == [], static)
100
+ @narrowing_typeof("object", (trait object, truthy | trait null, nullable, falsy, nonthenable), true, static)
95
101
 
96
102
  }
@@ -31,6 +31,32 @@ in ts_call rule es-wrap-iife {
31
31
  warn if !structure_iife_wrapped && !info.wrapped then "Wrap an immediate function invocation in parentheses." else if mode == "inside" then "Wrap only the function expression in parens." else "Move the invocation into the parens that contain the function."
32
32
  }
33
33
 
34
+ fn wrap_breaks(source, from, to) = re_test(byte_slice(source, from, to), /[\n\r\u{2028}\u{2029}]/)
35
+ # A line break between the parts an operator joins, never inside a bracketed child.
36
+ fn wrap_multiline(source, n) =
37
+ if n is structure_binary then wrap_breaks(source, n.left.end_offset, n.right.offset) || wrap_multiline(source, n.left.node) || wrap_multiline(source, n.right.node)
38
+ else if n is ts_conditional then wrap_breaks(source, n.test.end_offset, n.consequent.offset) || wrap_breaks(source, n.consequent.end_offset, n.alternate.offset)
39
+ || wrap_multiline(source, n.test.node) || wrap_multiline(source, n.consequent.node) || wrap_multiline(source, n.alternate.node)
40
+ else if n is structure_member then re_test(n.structure_chain_info.gap ?? "", /[\n\r\u{2028}\u{2029}]/) || wrap_multiline(source, n.object.node)
41
+ else if n is ts_call then wrap_multiline(source, n.callee.node)
42
+ else if n is (ts_as, ts_satisfies, ts_non_null) then wrap_multiline(source, n.expression.node)
43
+ else if n is (ts_unary, ts_await) then wrap_multiline(source, n.argument.node)
44
+ else false
45
+ # An operand of an expression `wrap_multiline` looks into.
46
+ fn wrap_joined(n) =
47
+ n.parent is (ts_binary, ts_logical, ts_conditional, ts_as, ts_satisfies, ts_non_null, ts_unary, ts_await)
48
+ || n.parent is structure_member && n.parent.object.node == n
49
+ || n.parent is ts_call && n.parent.callee.node == n
50
+
51
+ in ts_binary, ts_logical, ts_conditional, ts_member, ts_index, ts_call, ts_as, ts_satisfies, ts_non_null, ts_unary, ts_await rule es-wrap-multiline-expression {
52
+ when !wrap_joined(node) && !(parent is (ts_expression_statement, ts_parenthesized))
53
+ when wrap_multiline(source, node)
54
+ when next_token != null && !space_after.line_break
55
+ let indent { value byte_slice(source, position(offset).js_line_start, offset); re_find /^[ \t]*/; nonempty } ?? ""
56
+ fix at node as "(" text "\n" indent ")"
57
+ warn "Wrap this multiline expression in parentheses, with `)` on its own line."
58
+ }
59
+
34
60
  group structure_parens ts_parenthesized, ts_parenthesized_type
35
61
  group structure_assertion ts_as, ts_satisfies, ts_type_assertion
36
62
  group structure_binary ts_binary, ts_logical
@@ -206,6 +232,7 @@ in structure_parens rule es-no-extra-parens {
206
232
  || outer.structure_parens_for_init && inner.structure_parens_has_in
207
233
  || owner is ts_update && { value source; byte_slice inner.end_offset owner.end_offset; re_test /[\n\r]/ }
208
234
  || owner is ts_await && inner is ts_non_null
235
+ || wrap_multiline(source, inner) && wrap_breaks(source, inner.end_offset, outer.end_offset)
209
236
  when structure_parens_count > (if protected && required < 1 then 1 else required)
210
237
  let comment structure_parens_comment(outer, source)
211
238
  let typed_inner {
@@ -82,6 +82,7 @@ fn problem_duplicate_condition(branch) = {
82
82
  && any(problem_condition_parts(condition, "&&"), part => problem_condition_covered(part, previous))
83
83
  }
84
84
  in ts_if.test rule es-no-dupe-else-if {
85
+ when parent.parent is ts_if && parent.parent.alternate.node == parent.node
85
86
  when problem_duplicate_condition(parent.node)
86
87
  report at unparenthesised_node(node)
87
88
  warn "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain."
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  define {
2
3
 
3
4
  fn problem_number_significand(text, as_float) = {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # Regular expression validation rules.
2
3
 
3
4
  define {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/constants
2
3
 
3
4
  define {
@@ -186,17 +186,21 @@ fn regexp_checked_by_call(n) = {
186
186
  value first: first(arguments)
187
187
  value n is ts_regex && owner is (ts_call, ts_new) && first == n && len(arguments) > 1 && regexp_input(owner) != null
188
188
  }
189
- in regexp_site let regexp_control_findings if regexp_tree.ok
189
+ # What a literal's own spelling rules out, before anything parses it.
190
+ in regexp_site let regexp_may_hold_control node not ts_regex || re_test(text, /\\[xuc0-9]|[\u{0}-\u{1f}]/)
191
+ in regexp_site let regexp_may_hold_spaces node not ts_regex || contains(text, " ")
192
+ in regexp_site let regexp_may_mislead node not ts_regex || contains(text, "[") && re_test(text, /[^\u{0}-\u{7f}]|\\[uxc0-9]/)
193
+ in regexp_site let regexp_control_findings if regexp_may_hold_control && regexp_tree.ok
190
194
  then regexp_controls(regexp_nodes, regexp_input) else []
191
- in regexp_site let regexp_empty_class_findings if node is ts_regex && regexp_tree.ok && regexp_error == null
195
+ in regexp_site let regexp_empty_class_findings if node is ts_regex && contains(text, "[]") && regexp_tree.ok && regexp_error == null
192
196
  then regexp_empty_classes(regexp_view_nodes, regexp_input) else []
193
- in regexp_site let regexp_space_findings if regexp_tree.ok && regexp_error == null
197
+ in regexp_site let regexp_space_findings if regexp_may_hold_spaces && regexp_tree.ok && regexp_error == null
194
198
  then regexp_spaces(regexp_view_nodes, regexp_input) else []
195
- in regexp_site let regexp_backreference_findings if re_test(regexp_input.text, /\\(?:[1-9]|k)/)
199
+ in regexp_site let regexp_backreference_findings if re_test(if node is ts_regex then text else regexp_input.text, /\\(?:[1-9]|k)/)
196
200
  && regexp_tree.ok && regexp_error == null then regexp_backreferences(regexp_nodes, regexp_input) else []
197
- in regexp_site let regexp_misleading_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
201
+ in regexp_site let regexp_misleading_findings if regexp_may_mislead && regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
198
202
  then regexp_misleading(regexp_view_nodes, regexp_input, false) else []
199
- in regexp_site let regexp_misleading_escaped_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
203
+ in regexp_site let regexp_misleading_escaped_findings if regexp_may_mislead && regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
200
204
  then regexp_misleading(regexp_view_nodes, regexp_input, true) else []
201
205
  in regexp_site let regexp_findings if regexp_tree.ok != true then null else {
202
206
  value flat([
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/expression-fixes
2
3
  import .ts/constants
3
4
  import .ts/suggestions/globals
@@ -35,21 +36,9 @@ fn regexp_literal_input(n, override_flags) = {
35
36
  value flags: if override_flags == null then written_flags else override_flags.value
36
37
  value end: byte_len(n.text) - byte_len(written_flags) - 1
37
38
  value pattern: byte_slice(n.text, 1, end)
38
- value characters: split(pattern, "")
39
- value decoded: js_string_parts(json_string(pattern))
40
- value positions: regexp_positions(characters, n.offset + 1)
41
- value spans: map(positions, position => {
42
- offset: position.offset, end_offset: position.offset + byte_len(get(characters, position.index))
43
- })
44
- value units: flat(map(positions, position => {
45
- value span: get(spans, position.index)
46
- value count: if span.end_offset - span.offset == 4 then 2 else 1
47
- value map(slice(decoded.units, position.unit, position.unit + count), unit => {
48
- value: unit.value, offset: span.offset, end_offset: span.end_offset
49
- })
50
- }))
39
+ value mapped: text_units(pattern, n.offset + 1)
51
40
  value {
52
- text: pattern, source: pattern, flags: flags, spans: spans, units: units,
41
+ text: pattern, source: pattern, flags: flags, spans: mapped.spans, units: mapped.units,
53
42
  offset: n.offset + 1, end_offset: n.offset + end, written: "regex", pattern_node: n
54
43
  }
55
44
  }
@@ -91,7 +80,7 @@ fn regexp_initialised_binding(n) = {
91
80
  in ts_program let regexp_mentioned contains(source, "RegExp")
92
81
  in ts_call, ts_new let regexp_candidate {
93
82
  value callee: unparenthesised_node(node.callee.node)
94
- value callee is ts_identifier && (callee.text == "RegExp" || ts_module_program(node).regexp_mentioned && regexp_initialised_binding(callee))
83
+ value callee is ts_identifier && (callee.text == "RegExp" || ts_program.regexp_mentioned && regexp_initialised_binding(callee))
95
84
  || callee is (ts_member, ts_index) && ts_global_method_name(callee) == "RegExp"
96
85
  || callee is (ts_non_null, ts_as, ts_satisfies, ts_type_assertion, ts_conditional, ts_logical)
97
86
  }
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/restrictions: eslint core rules that limit or forbid things by
2
3
  # configuration: sizes (max-*, complexity), restricted names, imports,
3
4
  # properties and syntax, identifier shapes, sort orders, comment and
@@ -5,9 +6,84 @@
5
6
 
6
7
  import .ts/restrictions/comments
7
8
  import .ts/helpers
9
+ import .ts/modules
8
10
 
9
11
  define {
10
12
 
13
+ in ts_program rule es-module-only-used-from {
14
+ default option from string[] = [] "the files that alone may use a module this way: globs, relative to the config"
15
+ option ignore string[] = [] "files whose imports don't count: globs, relative to the config"
16
+ when len(from) > 0
17
+ let use module_importers(node, from, ignore)
18
+ when use.reaches_stop && !use.reaches_entry && !use.is_stop
19
+ report at { start: 0, end: 1 }
20
+ warn "Only {join(from, ', ')} use this module: move it there."
21
+ }
22
+
23
+ # One name's sites (overloads, merges) decide together, and report once.
24
+ fn export_usage_site(owner, name, at, before) = { owner: owner, name: name, at: at, before: before }
25
+ fn export_usage_tagged(text, tags) = text != null && any(tags, tag => re_test(text, "(?:^|[\\s*/])" re_escape(tag) "(?:$|[^\\w-])"))
26
+ fn export_usage_bound(declarator) = if declarator.name.node is ts_identifier then [declarator.name.node] else {
27
+ value [declarator.name.node]
28
+ walk (current) current.children.nodes
29
+ filter (item) item is ts_identifier && item.declaration_kind != null
30
+ }
31
+ in ts_export_specifier let export_usage_before byte_slice(source, previous.end_offset ?? parent.offset, offset)
32
+ fn export_usage_statement_sites(statement, source_text) = {
33
+ # From the code before it: comments are children only under some configs.
34
+ value lead: token(statement, "before")
35
+ value before: byte_slice(source_text, if lead == null then 0 else lead.end, statement.offset)
36
+ value when {
37
+ statement is ts_export_assignment then [export_usage_site(statement, "=", statement, before)]
38
+ statement.kind == "default" then [export_usage_site(statement, "default", statement.declaration.name.node ?? statement, before)]
39
+ statement.operator == "*" then if statement.namespace == null then [] else [export_usage_site(statement, ts_module_name(statement.namespace.node), statement.namespace.node, before)]
40
+ count(statement.specifier) > 0 then map(statement.specifier.nodes, item => export_usage_site(item, ts_module_name(item.exported.node ?? item.local.node), item.exported.node ?? item.local.node, before item.export_usage_before))
41
+ statement.declaration.node is ts_variable_declaration then flat_map(statement.declaration.declaration.nodes, declarator => map(export_usage_bound(declarator), bound => export_usage_site(declarator, bound.text, bound, before)))
42
+ statement.declaration != null then [export_usage_site(statement, "{statement.declaration.name}", statement.declaration.name.node, before)]
43
+ else then []
44
+ }
45
+ }
46
+ in ts_program let export_usage_sites {
47
+ value body.nodes
48
+ filter (statement) statement is (ts_export, ts_export_assignment)
49
+ flat_map (statement) export_usage_statement_sites(statement, source)
50
+ }
51
+ fn export_usage_reports(sites, names, tags) = {
52
+ value sites
53
+ reported: filter (site) contains(names, site.name)
54
+ && find(sites, other => other.name == site.name).owner == site.owner
55
+ && !any(sites, other => other.name == site.name && export_usage_tagged(other.before, tags))
56
+ unique_by (site) site.owner
57
+ map (site) { at: site.at, names: map(filter(reported, other => other.owner == site.owner), other => other.name) }
58
+ }
59
+ @macro export_usage_options {
60
+ option ignore string[] = [] "files whose imports don't count: globs, relative to the config"
61
+ option keepTags string[] = [] "comment tags before an export that keep it, such as @public"
62
+ }
63
+ # Per program, not per export: the graph answers with the whole file's table.
64
+ in ts_program rule es-no-unused-exports {
65
+ @export_usage_options
66
+ let use module_export_users(node, [], ignore)
67
+ when use != null && !use.is_entry && !use.is_ignored
68
+ let unused { value use.exports; filter (row) count(row.importers) == 0; map (row) row.name }
69
+ each found in export_usage_reports(export_usage_sites, unused, keepTags) {
70
+ report at found.at
71
+ warn "Nothing imports this export: {join(found.names, ', ')}."
72
+ }
73
+ }
74
+ in ts_program rule es-export-only-used-from {
75
+ default option from string[] = [] "the files that alone may use an export this way: globs, relative to the config"
76
+ @export_usage_options
77
+ when len(from) > 0
78
+ let use module_export_users(node, from, ignore)
79
+ when use != null && !use.is_stop && !use.is_ignored
80
+ let only { value use.exports; filter (row) row.reaches_stop && !row.reaches_entry; map (row) row.name }
81
+ each found in export_usage_reports(export_usage_sites, only, keepTags) {
82
+ report at found.at
83
+ warn "Only {join(from, ', ')} use this export: {join(found.names, ', ')}. Move it there."
84
+ }
85
+ }
86
+
11
87
  schema restriction_import_path {
12
88
  name: string
13
89
  message?: string
@@ -2,6 +2,7 @@ define {
2
2
 
3
3
  group member_usage_access ts_member, ts_index, ts_pattern_property, ts_property
4
4
  in ts_class let member_usage_accesses descendants(member_usage_access)
5
+ in ts_class let member_usage_access_groups { value member_usage_accesses; filter (access) access.member_usage_name != null; group_by (access) access.member_usage_name }
5
6
  fn member_usage_class(n) = find(walk(n, item => item.parent.node), item => item is ts_class)
6
7
  fn member_usage_key(n) = if n is ts_parameter then n.name.node else n.key.node
7
8
  in ts_class let member_usage_members {
@@ -64,7 +65,7 @@ fn member_usage_receiver(access) = {
64
65
  else if named != null then { class: named, static: true }
65
66
  else if declaration is ts_declarator then
66
67
  if declaration.init is ts_this
67
- && all(binding.references, reference => reference.name_write_owner == null || reference.name_write_owner == declaration)
68
+ && all(name_references(object), reference => reference.name_write_owner == null || reference.name_write_owner == declaration)
68
69
  then member_usage_this(access) else null
69
70
  else if declaration is ts_parameter then member_usage_annotation(declaration.type.node, access)
70
71
  else null
@@ -80,8 +81,9 @@ fn member_usage_used(declaration) = {
80
81
  value class: member_usage_class(declaration)
81
82
  value accessor: declaration is ts_method && contains(["get", "set"], declaration.kind)
82
83
  || contains([] declaration.modifier, "accessor")
83
- value any(class.member_usage_accesses, access =>
84
- access.member_usage_name == name && (accessor || access.member_usage_read)
84
+ value named: find(class.member_usage_access_groups, group => group.key == name).items ?? []
85
+ value any(named, access =>
86
+ (accessor || access.member_usage_read)
85
87
  && (if access is ts_member && access.property is ts_private_name
86
88
  then member_usage_hash_owner(access) == class
87
89
  else {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  define {
2
3
 
3
4
  fn scoping_typed(program) = re_test(program.file, /\.(ts|tsx|mts|cts)$/)
@@ -204,7 +204,7 @@ fn ts_usage_before_parameter(n, typed, declaration_file) = {
204
204
  slice (index + 1)
205
205
  any (later) {
206
206
  value binding: name_binding(later)
207
- value binding != null && (count(binding.references) > 0
207
+ value binding != null && (count(name_references(later)) > 0
208
208
  || any(binding.declarations, site => ts_usage_initialized(site) || ts_usage_marked(site, typed, declaration_file)))
209
209
  }
210
210
  }
@@ -252,7 +252,7 @@ fn ts_usage_used(binding, sites, references, typed, variable, value_binding, dec
252
252
  }
253
253
  in ts_identifier let ts_usage_binding name_binding(node)
254
254
  in ts_program let ts_usage_typed
255
- any([".ts", ".tsx", ".mts", ".cts"], extension => ends_with(file, extension))
255
+ !any([".js", ".jsx", ".mjs", ".cjs"], extension => ends_with(file, extension))
256
256
  in ts_program let ts_usage_declaration_file ts_usage_typed && contains(last(split(file, "/")), ".d.")
257
257
  in ts_node let ts_usage_typed scoping_program.ts_usage_typed
258
258
  in ts_node let ts_usage_declaration_file scoping_program.ts_usage_declaration_file
@@ -269,7 +269,7 @@ fn ts_usage_summary(n) = if !n.scoping_lexical_declaration || n.parent is ts_qua
269
269
  value site: ts_usage_site(n)
270
270
  value typed: n.ts_usage_typed
271
271
  value declaration_file: n.ts_usage_declaration_file
272
- value real: map(binding.references, reference => ts_usage_reference(reference))
272
+ value real: map(name_references(n), reference => ts_usage_reference(reference))
273
273
  value initial: {
274
274
  value sites
275
275
  filter (site) ts_usage_initialized(site)
package/std/ts/spreads.dx CHANGED
@@ -14,7 +14,7 @@ fn spread_written_properties(n) = {
14
14
  value binding: name_binding(n)
15
15
  value if binding == null then if n is (ts_function, ts_arrow) then false else null
16
16
  else if spread_declared_contract(n) then false
17
- else any(binding.references, reference => reference.parent is (ts_member, ts_index)
17
+ else any(name_references(n), reference => reference.parent is (ts_member, ts_index)
18
18
  && reference.parent.object.node == reference && reference.parent.write_site != null)
19
19
  }
20
20
  fn spread_class_value(n, arms) = {