@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,187 @@
1
+ import .ts/globals
2
+ import .ts/constants
3
+ import .ts/scoping/bindings
4
+ import .ts/scoping/receivers
5
+
6
+ define {
7
+
8
+ in ts_identifier let ts_global_reference {
9
+ value classes: name_role_classes(node, "reference")
10
+ value classes != null && contains(classes, "value") && scope_declaration == null
11
+ }
12
+ fn ts_global_enabled_reference(n) = n.ts_global_reference == true && n.global != "off"
13
+ fn ts_global_visible(n, key) = {
14
+ value scopes: name_scopes(n, "lexical")
15
+ value if scopes == null then null else any(scopes, scope => {
16
+ value declarations: name_scope_declarations(n, scope.id, key)
17
+ value declarations != null && any(declarations, declaration => contains(declaration.classes, "value"))
18
+ })
19
+ }
20
+ fn ts_global_alias(n) = {
21
+ value declaration: n.declaration.node
22
+ value declarator: declaration.parent.node
23
+ value writes: any([] declaration.references, reference => contains(["write", "read_write"], reference.access))
24
+ value if n not ts_identifier || declaration not ts_declarator.name || writes then null else declarator.init.node
25
+ }
26
+ fn ts_global_value_compute(n) =
27
+ if n is (ts_parenthesized, ts_non_null, ts_as, ts_satisfies, ts_type_assertion) then n.expression.global_value
28
+ else if n is ts_identifier then
29
+ if contains(["readonly", "writable"], n.global) then n.text
30
+ else if contains(["const", "let", "var"], n.declaration.parent.parent.kind) then {
31
+ value alias: ts_global_alias(n)
32
+ value alias.global_value
33
+ } else null
34
+ else if n is ts_conditional then n.consequent.global_value ?? n.alternate.global_value
35
+ else if n is ts_logical then n.left.global_value ?? n.right.global_value
36
+ else if n is (ts_member, ts_index) && contains(["globalThis", "global", "self", "window"], n.object.global_value) then n.static_name
37
+ else null
38
+ in ts_node let global_value ts_global_value_compute(node)
39
+
40
+ in ts_program let ts_global_written {
41
+ value descendants(ts_identifier)
42
+ filter (reference) reference.ts_global_reference && contains(["write", "read_write"], reference.access)
43
+ map (reference) reference.text
44
+ unique
45
+ }
46
+ fn ts_global_method_name(n) = if n is ts_index then ts_runtime_string(n.index.node) else n.static_name
47
+ fn ts_global_method_candidate(n, name) = {
48
+ value callee: unparenthesised_node(n.callee.node)
49
+ value callee is ts_identifier || callee is (ts_member, ts_index) && ts_global_method_name(callee) == name
50
+ }
51
+ fn ts_global_method_compute(n) = {
52
+ value bare: unparenthesised_node(n)
53
+ value program: ts_module_program(bare)
54
+ value if bare is (ts_member, ts_index) then {
55
+ value global: bare.object.global_value
56
+ value name: ts_global_method_name(bare)
57
+ value if global == null || name == null || contains(program.ts_global_written, global) then null else global "." name
58
+ } else if bare is ts_identifier then { value alias: ts_global_alias(bare); value alias.ts_reference_method }
59
+ else null
60
+ }
61
+ in ts_node let ts_reference_method ts_global_method_compute(node)
62
+ fn ts_global_callee(n) = {
63
+ value callee: unparenthesised_node(n.callee.node)
64
+ value if callee is ts_identifier && ts_global_enabled_reference(callee) then callee.text else null
65
+ }
66
+ in ts_call, ts_new let global_callee ts_global_callee(node)
67
+ in ts_call, ts_new let global_method_callee callee.ts_reference_method
68
+ fn ts_global_extended(n) = {
69
+ value callee: unparenthesised_node(n.callee.node)
70
+ value object: unparenthesised_node(callee.object.node)
71
+ value left: unparenthesised_node(n.left.node)
72
+ value target: if n is ts_assignment && left is (ts_member, ts_index) then left.object.node
73
+ else if n is ts_call && contains(["defineProperty", "defineProperties"], callee.static_name)
74
+ && object is ts_identifier && object.text == "Object" then first(n.argument.nodes) else null
75
+ value prototype: unparenthesised_node(target)
76
+ value constructor: unparenthesised_node(prototype.object.node)
77
+ value program: ts_module_program(n)
78
+ value if prototype.static_name == "prototype" && constructor is ts_identifier
79
+ && re_test(constructor.text, /^\p{Uppercase}/)
80
+ && ts_global_builtin_setting(program, constructor.text) == "readonly"
81
+ && ts_global_enabled_reference(constructor) then constructor.text else null
82
+ }
83
+ in ts_assignment, ts_call let extended_native ts_global_extended(node)
84
+ fn ts_global_own_property(n) = {
85
+ value callee: unparenthesised_node(n.callee.node)
86
+ value method: unparenthesised_node(callee.object.node)
87
+ value object: unparenthesised_node(method.object.node)
88
+ value empty: object is ts_object && count(object.property.nodes) == 0
89
+ value constructor: if object is (ts_member, ts_index) && object.static_name == "prototype"
90
+ then unparenthesised_node(object.object.node) else object
91
+ value visible: ts_global_visible(n, "Object")
92
+ value program: ts_module_program(n)
93
+ value setting: ts_global_setting(program, "Object")
94
+ value callee is (ts_member, ts_index) && callee.static_name == "call"
95
+ && method is (ts_member, ts_index) && method.static_name == "hasOwnProperty"
96
+ && (empty || constructor is ts_identifier && constructor.text == "Object") && visible == false
97
+ && contains(["readonly", "writable"], setting)
98
+ }
99
+ in ts_call let own_property_call ts_global_own_property(node)
100
+ fn ts_global_label(n) = {
101
+ value key: n.label.text
102
+ value program: ts_module_program(n)
103
+ value visible: ts_global_visible(n, key)
104
+ value setting: ts_global_setting(program, key)
105
+ value key != null && (visible == true || contains(["readonly", "writable"], setting))
106
+ }
107
+ in ts_labeled let label_binding ts_global_label(node)
108
+ fn ts_global_function_destination(n) = if n.parent is (ts_parenthesized, ts_logical, ts_conditional)
109
+ then n.parent.node else null
110
+ fn ts_global_default_receiver(function) = {
111
+ value anonymous: function.name == null
112
+ value current: { value function; walk (n) ts_global_function_destination(n); last }
113
+ value parent: current.parent.node
114
+ value left: unparenthesised_node(parent.left.node)
115
+ value callee: unparenthesised_node(parent.callee.node)
116
+ value object: unparenthesised_node(callee.object.node)
117
+ value factory: object is ts_identifier && object.text == "Array"
118
+ && contains(["from", "fromAsync"], callee.static_name)
119
+ value callback_receiver: scoping_receiver_callback(parent, current, factory)
120
+ value if scoping_receiver_uppercase(function.name.node)
121
+ || any([] function.parameter.nodes, parameter => parameter.name == "this") then false
122
+ else if parent is (ts_property, ts_class_property) then current not (ts_property.value, ts_class_property.init)
123
+ else if parent is (ts_assignment, ts_assignment_pattern) then
124
+ left not (ts_member, ts_index) && !(anonymous && scoping_receiver_uppercase(parent.left.node))
125
+ else if parent is ts_declarator || current is ts_parameter.init then
126
+ !anonymous || !scoping_receiver_uppercase(parent.name.node)
127
+ else if parent is (ts_member, ts_index) then {
128
+ value call: if parent.parent is ts_call && parent.parent.callee.node == parent then parent.parent.node else null
129
+ value receiver: first([] call.argument.nodes)
130
+ value !(contains(["bind", "call", "apply"], parent.static_name)
131
+ && current is (ts_member.object, ts_index.object) && receiver != null && !scoping_receiver_nullish(receiver))
132
+ } else parent not ts_call || callback_receiver == null || scoping_receiver_nullish(callback_receiver)
133
+ }
134
+ in ts_program let ts_global_this {
135
+ value source_type: ts_source_type(node)
136
+ value source_type != "module"
137
+ && !(source_type == "script" && source_environment.root_function && scoping_strict_code)
138
+ }
139
+ in ts_node let ts_global_this
140
+ if parent is ts_function then parent.scoping_strict_code != true && ts_global_default_receiver(parent.node)
141
+ else if parent is ts_method && not ts_method.key || is ts_class_property.init || parent is ts_static_block then false
142
+ else parent.ts_global_this
143
+ fn ts_global_eval_object(n, key) = {
144
+ value terminal: {
145
+ value unparenthesised_node(n)
146
+ walk (current) if current is (ts_member, ts_index) && current.static_name == key
147
+ then unparenthesised_node(current.object.node) else null
148
+ last
149
+ }
150
+ value terminal is ts_identifier && terminal.text == key && terminal.ts_global_reference
151
+ && contains(["readonly", "writable"], terminal.global)
152
+ }
153
+ fn ts_global_eval(n) = {
154
+ value outer: n.scoping_outer_parens
155
+ value call: if outer.parent is ts_call && outer.parent.callee.node == outer then outer.parent.node else null
156
+ value object: unparenthesised_node(n.object.node)
157
+ value if n is ts_identifier && n.text == "eval" then
158
+ if call == null && !ts_global_enabled_reference(n) then null
159
+ else { direct: call != null, optional: call.optional != null, offset: n.offset, end: n.end_offset }
160
+ else if n is (ts_member, ts_index) && n.static_name == "eval" then {
161
+ value global: if object is ts_this then object.ts_global_this
162
+ else any(["global", "window", "globalThis"], key => ts_global_eval_object(object, key))
163
+ value property: if n is ts_index then n.index.node else n.property.node
164
+ value if global != true || property == null then null else { direct: false, optional: false, offset: property.offset, end: property.end_offset }
165
+ } else null
166
+ }
167
+ in ts_identifier, ts_member, ts_index let eval_usage ts_global_eval(node)
168
+ fn ts_global_catch_next(n) = if n is ts_try.handler || n.parent is (ts_function, ts_method, ts_arrow, ts_static_block)
169
+ then null else n.parent.node
170
+ fn ts_global_caught_throw(n) = {
171
+ value call: unparenthesised_node(n.argument.node)
172
+ value callee: unparenthesised_node(call.callee.node)
173
+ value handler: { value n; walk (current) ts_global_catch_next(current); last }
174
+ value catch: handler.parent.node
175
+ value target: if callee is ts_member then callee.property.node else callee
176
+ value if call not (ts_call, ts_new) || handler not ts_try.handler then null else {
177
+ name: if target is ts_identifier then target.text else null,
178
+ global: callee is ts_identifier && ts_global_enabled_reference(callee),
179
+ parameter: catch.parameter.node,
180
+ catch_offset: catch.kind.offset ?? n.offset,
181
+ throw_offset: n.offset,
182
+ arguments: call.argument.nodes
183
+ }
184
+ }
185
+ in ts_throw let catch_throw ts_global_caught_throw(node)
186
+
187
+ }
@@ -0,0 +1,17 @@
1
+ # Shared state and value helpers for the `.ts/suggestions` topic files.
2
+
3
+ define {
4
+
5
+ in ts_block let statement_count count([] body)
6
+
7
+ in ts_parenthesized let unwrapped_node expression.unwrapped_node.nonempty ?? expression.node
8
+ in ts_parenthesized let unwrapped_text expression.unwrapped_text.nonempty ?? expression
9
+
10
+ in ts_parenthesized let inner_comparison_tokens expression.unparenthesised_tokens
11
+ in ts_node let unparenthesised_tokens if is ts_parenthesized then inner_comparison_tokens else token_texts
12
+
13
+ in ts_block let uncommented_empty_block count(body) == 0 && { value descendants(ts_comment); count } == 0
14
+
15
+ fn enabled_global_identifier(n, name) = n is ts_identifier && n.text == name && n.resolved == "global" && n.global != "off"
16
+
17
+ }
@@ -0,0 +1,148 @@
1
+ # Object member policy and source-preserving short/long syntax conversions.
2
+
3
+ import .ts/helpers
4
+ import .ts/names
5
+ import .ts/literals
6
+
7
+ define {
8
+
9
+ fn shorthand_arrow_captures(n) = {
10
+ value n.children.nodes
11
+ walk (current) if current is (ts_function, ts_method, ts_class) then [] else current.children.nodes
12
+ any (current) current is (ts_this, ts_super) || current is ts_meta_property && current.text == "new.target"
13
+ || current is ts_identifier && current.text == "arguments"
14
+ && (current.resolved == "global" || ts_arguments_owner(current) != null
15
+ || current.access != "type" && current.declaration != null
16
+ && (current.declaration.offset < n.offset || current.declaration.end_offset > n.end_offset))
17
+ }
18
+ fn shorthand_member_info(n) = {
19
+ value key: n.key.node
20
+ value name: n.static_name
21
+ value proto: key not ts_computed_key && name == "__proto__"
22
+ value expression: if n.value.node != null then unparenthesised_node(n.value.node) else null
23
+ value method: n is ts_method
24
+ value function: expression is ts_function && expression.name.node == null
25
+ value arrow: expression is ts_arrow && expression.body is ts_block && !shorthand_arrow_captures(expression)
26
+ value quoted_key: key.expression.node ?? key
27
+ value comments: overlapping(ts_comment, n.offset, n.end_offset)
28
+ value if n.parent not ts_object || n.parent.write_site != null || n not (ts_property, ts_method)
29
+ || contains(["get", "set"], n.kind) || key == null then null else {
30
+ name: name,
31
+ concise: method || expression == null,
32
+ method: method,
33
+ function: function,
34
+ arrow: arrow,
35
+ quoted: quoted_key is ts_string,
36
+ redundant: !proto && (function || expression is ts_identifier && name == expression.text),
37
+ constructor: re_test(name, /^[_$0-9]*\p{Uppercase}/),
38
+ annotated: any(comments, comment => starts_with(comment.text, "/*")
39
+ && re_test(comment.text, /^\/\*\s*\*/) && contains(comment.text, "@type")),
40
+ proto: proto,
41
+ computed: key is ts_computed_key
42
+ }
43
+ }
44
+ in ts_property, ts_method let shorthand_member shorthand_member_info(node)
45
+ in ts_object let shorthand_consistency {
46
+ value members: filter(map(property.nodes, member => member.shorthand_member), member => member != null)
47
+ value concise: count(filter(members, member => member.concise))
48
+ value if write_site != null then null
49
+ else if concise > 0 && concise < count(members) then "mixed"
50
+ else if concise == 0 && count(members) > 0 && all(members, member => member.redundant) then "redundant"
51
+ else null
52
+ }
53
+
54
+ fn shorthand_modifiers(n) =
55
+ (if contains(n.modifier, "async") then "async " else "") (if contains(n.modifier, "*") then "*" else "")
56
+
57
+ fn shorthand_parameter_open(n) = {
58
+ value type_end: last(n.type_parameter.nodes)
59
+ value first_parameter: first(n.parameter.nodes)
60
+ value start: type_end.end_offset ?? n.offset
61
+ value end: first_parameter.offset ?? n.body.offset
62
+ value overlapping(ts_open_paren, start, end)
63
+ find (token) token.paired_token.end <= n.body.offset
64
+ }
65
+
66
+ fn shorthand_header_token(n, open) = if count(n.type_parameter) == 0 then open else {
67
+ value first_type: first(n.type_parameter.nodes)
68
+ value overlapping(ts_open_angle, n.offset, first_type.offset)
69
+ first
70
+ }
71
+
72
+ fn shorthand_callable_tail(source, n) = {
73
+ value open: shorthand_parameter_open(n)
74
+ value first_parameter: first(n.parameter.nodes)
75
+ value header_token: shorthand_header_token(n, open)
76
+ value start: header_token.offset
77
+ value if n is ts_arrow then {
78
+ value header: if open != null then byte_slice(source, start, n.operator.offset)
79
+ else "(" first_parameter.text ")" byte_slice(source, first_parameter.end_offset, n.operator.offset)
80
+ value header
81
+ trim_end
82
+ then (parameters) parameters " " n.body.text
83
+ } else (header_token.token_neighbours.before.gap ?? "") byte_slice(source, start, n.end_offset)
84
+ }
85
+
86
+ fn shorthand_span_retains(comment, start, end) =
87
+ start != null && start <= comment.offset && comment.end_offset <= end
88
+
89
+ fn shorthand_fix_allowed(n, info, longform, method) = {
90
+ value comments: overlapping(ts_comment, n.offset, n.end_offset)
91
+ value if longform then !info.method || all(comments, comment => n.key.offset <= comment.offset)
92
+ else if !method then count(comments) == 0
93
+ else {
94
+ value callable: n.value.unwrapped_node ?? n.value.node
95
+ value open: shorthand_parameter_open(callable)
96
+ value first_parameter: first(callable.parameter.nodes)
97
+ value header_token: shorthand_header_token(callable, open)
98
+ value start: header_token.offset ?? first_parameter.offset
99
+ value comments
100
+ all (comment) shorthand_span_retains(comment, n.key.offset, n.key.end_offset)
101
+ || (if callable is ts_arrow then
102
+ shorthand_span_retains(comment, start, callable.operator.offset)
103
+ || shorthand_span_retains(comment, callable.body.offset, callable.body.end_offset)
104
+ else shorthand_span_retains(comment, start, callable.end_offset))
105
+ }
106
+ }
107
+
108
+ fn shorthand_member_fix(source, n, info, longform, method) =
109
+ if longform then
110
+ if info.method then {
111
+ value suffix: byte_slice(source, n.key.end_offset, n.end_offset)
112
+ value n.key.text ": " (if contains(n.modifier, "async") then "async " else "") "function" (if contains(n.modifier, "*") then "*" else "") suffix
113
+ } else n.text ": " n.text
114
+ else if method then {
115
+ value callable: n.value.unwrapped_node ?? n.value.node
116
+ value tail: shorthand_callable_tail(source, callable)
117
+ value shorthand_modifiers(callable) n.key.text tail
118
+ } else n.value.text
119
+
120
+ in ts_property, ts_method rule es-object-shorthand {
121
+ option mode ("always" | "never" | "methods" | "properties" | "consistent" | "consistent-as-needed") = "always" "object shorthand policy"
122
+ option avoidQuotes boolean = false "keep string-literal keys in long form"
123
+ option ignoreConstructors boolean = false "ignore capitalised method names"
124
+ option methodsIgnorePattern string = "" "method names to ignore"
125
+ option avoidExplicitReturnArrows boolean = false "prefer methods to block-bodied arrows without lexical captures"
126
+ let shorthand_info shorthand_member
127
+ when shorthand_info != null && !shorthand_info.proto
128
+ let needs_longform shorthand_info.concise && (mode == "never" || shorthand_info.method && avoidQuotes && shorthand_info.quoted)
129
+ let needs_method !shorthand_info.concise && (mode == "always" || mode == "methods") && (shorthand_info.function || avoidExplicitReturnArrows && shorthand_info.arrow) && !(ignoreConstructors && shorthand_info.constructor) && !(avoidQuotes && shorthand_info.quoted) && !(methodsIgnorePattern != "" && shorthand_info.name != null && re_test(shorthand_info.name, methodsIgnorePattern))
130
+ let needs_property !shorthand_info.concise && (mode == "always" || mode == "properties") && !shorthand_info.computed && shorthand_info.redundant && !shorthand_info.function && !shorthand_info.annotated && !(avoidQuotes && shorthand_info.quoted)
131
+ when needs_longform || needs_method || needs_property
132
+ fix at node if shorthand_fix_allowed(node, shorthand_info, needs_longform, needs_method) as shorthand_member_fix(source, node, shorthand_info, needs_longform, needs_method)
133
+ warn if needs_longform then
134
+ if shorthand_info.method then
135
+ if mode == "never" then "Expected longform method syntax." else "Expected longform method syntax for string literal keys."
136
+ else "Expected longform property syntax."
137
+ else if needs_method then "Expected method shorthand."
138
+ else "Expected property shorthand."
139
+ }
140
+
141
+ in ts_object rule es-object-shorthand:consistent {
142
+ option mode ("always" | "never" | "methods" | "properties" | "consistent" | "consistent-as-needed") = "always" "object shorthand policy"
143
+ when mode == "consistent" || mode == "consistent-as-needed"
144
+ when shorthand_consistency == "mixed" || mode == "consistent-as-needed" && shorthand_consistency == "redundant"
145
+ warn if shorthand_consistency == "mixed" then "Unexpected mix of shorthand and non-shorthand properties." else "Expected shorthand for all properties."
146
+ }
147
+
148
+ }
@@ -0,0 +1,96 @@
1
+ # Object.assign policy and source-preserving object spread fixes.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ in ts_object let assign_source_accessor any([] property.accessor_kind, k => k == "get" || k == "set")
7
+ in ts_object let assign_source_proto contains([] property.static_name, "__proto__")
8
+ in ts_parenthesized forward assign_source_accessor, assign_source_proto from expression
9
+
10
+ fn object_spread_call_open(n) = {
11
+ value overlapping(ts_open_paren, n.callee.end_offset, n.end_offset)
12
+ find (token) token.paired_token.end == n.end_offset
13
+ }
14
+ fn object_spread_wrap(n) = n.parent not ts_parenthesized
15
+ && (if n.parent is ts_assignment then n is ts_assignment.left
16
+ else n.parent not (ts_declarator, ts_array, ts_return, ts_property) && n not ts_call.argument)
17
+ fn object_spread_argument_edges(n, bare) = {
18
+ value overlapping(ts_token, n.offset, n.end_offset)
19
+ filter (token) token is (ts_open_paren, ts_close_paren, ts_open_brace, ts_close_brace)
20
+ && (token.offset <= bare.offset || token.offset >= bare.end_offset - 1)
21
+ }
22
+ fn object_spread_object_edits(source, n, bare) = {
23
+ value edges: object_spread_argument_edges(n, bare)
24
+ value left: first(edges)
25
+ value right: last(edges)
26
+ value after: byte_slice(source, left.end_offset, right.offset)
27
+ value leading_spaces: re_find(after, /^\s*/)
28
+ value left_end: left.end_offset + byte_len(leading_spaces)
29
+ value before: byte_slice(source, left_end, right.offset)
30
+ value trailing_spaces: re_find(before, /\s*$/)
31
+ value line_comments: overlapping(ts_line_comment, left_end, right.offset)
32
+ value line: last(line_comments)
33
+ value keep_newline: line != null && line.end_offset >= right.token_neighbours.before.end
34
+ value right_start: if keep_newline then right.offset else right.offset - byte_len(trailing_spaces)
35
+ value inner: {
36
+ value edges
37
+ slice 1 -1
38
+ map (token) { start: token.offset, end: token.end_offset, text: "" }
39
+ }
40
+ value close: { value edges; find (token) token.offset == bare.end_offset - 1 }
41
+ value comma: right.token_neighbours.after
42
+ value duplicate_comma: comma.text == "," && (count(bare.property) == 0 || close.token_neighbours.before.text == ",")
43
+ value comma_edits: if duplicate_comma then [{ start: comma.start, end: comma.end, text: "" }] else []
44
+ value inner
45
+ concat [
46
+ { start: left.offset, end: left_end, text: "" },
47
+ { start: right_start, end: right.end_offset, text: "" }
48
+ ]
49
+ concat comma_edits
50
+ }
51
+ fn object_spread_argument_edits(source, n) = {
52
+ value bare: unparenthesised_node(n)
53
+ value if bare is ts_object then object_spread_object_edits(source, n, bare)
54
+ else {
55
+ value parens: n not ts_parenthesized && bare is (ts_assignment, ts_arrow, ts_conditional)
56
+ value [
57
+ { start: n.offset, end: n.offset, text: if parens then "...(" else "..." },
58
+ { start: n.end_offset, end: n.end_offset, text: if parens then ")" else "" }
59
+ ]
60
+ }
61
+ }
62
+ fn object_spread_fix(source, n) = {
63
+ value open: object_spread_call_open(n)
64
+ value close: open.paired_token
65
+ value wrapped: object_spread_wrap(n)
66
+ value wrappers: [
67
+ { start: n.offset, end: open.offset, text: "" },
68
+ { start: open.offset, end: open.end_offset, text: if wrapped then "({{" else "{{" },
69
+ { start: close.start, end: close.end, text: if wrapped then "}})" else "}}" }
70
+ ]
71
+ value edits: {
72
+ value n.argument.nodes
73
+ flat_map (argument) object_spread_argument_edits(source, argument)
74
+ concat wrappers
75
+ }
76
+ value replacement: expression_fix_edits(n.text, n.offset, edits)
77
+ value statement: expression_fix_statement(n)
78
+ value owners: expression_fix_before_nodes(statement)
79
+ value semicolon: if !wrapped || statement == null then false else expression_fix_asi(statement, statement.token_neighbours.before, owners, replacement)
80
+ value if open == null || semicolon == null || !expression_fix_retains_comments(n, replacement) then null
81
+ else (if semicolon then ";" else "") replacement
82
+ }
83
+
84
+ in ts_call rule es-prefer-object-spread {
85
+ when ts_global_method_candidate(node, "assign")
86
+ let first_argument { value argument.nodes; first; unparenthesised_node }
87
+ when first_argument is ts_object
88
+ when global_method_callee == "Object.assign"
89
+ when !any(argument.nodes, arg => unparenthesised_node(arg) is ts_spread)
90
+ when count(argument) == 1 || !any([] argument.assign_source_accessor, a => a == true) && !{ value [] argument.assign_source_proto; slice 1; any (p) p == true }
91
+ let replacement object_spread_fix(source, node)
92
+ fix at node if replacement != null as expression_fix_edges(node, replacement)
93
+ warn if count(argument) == 1 then "Use an object literal instead of `Object.assign`. eg: `{{ foo: bar }}`." else "Use an object spread instead of `Object.assign` eg: `{{ ...foo }}`."
94
+ }
95
+
96
+ }
@@ -0,0 +1,59 @@
1
+ # Regular expression literal preferences.
2
+
3
+ define {
4
+
5
+ in ts_template let static_template count(expression) == 0
6
+ in ts_parenthesized forward static_template from expression
7
+
8
+ in ts_regex, ts_call, ts_new rule es-prefer-named-capture-group {
9
+ each capture in regexp_unnamed_captures {
10
+ warn "Capture group '{capture}' should be converted to a named or non-capturing group."
11
+ }
12
+ }
13
+
14
+ in ts_regex rule es-no-div-regex {
15
+ when re_test(text, "^/=")
16
+ fix at { start: offset + 1, end: offset + 2 } as "[=]"
17
+ warn "A regular expression literal can be confused with '/='."
18
+ }
19
+
20
+ fn regex_raw_tag(tag) = tag is (ts_member, ts_index) && tag.static_name == "raw" && { value tag.object.node; unparenthesised_node; enabled_global_identifier "String" }
21
+ fn regex_static_argument(n) = n is ts_string || n is ts_template && n.static_template == true || n is ts_tagged_template && n.quasi.static_template == true && { value n.tag.node; unparenthesised_node; regex_raw_tag }
22
+ fn regex_construction_kind(args) =
23
+ when {
24
+ len(args) < 1 || len(args) > 2 then null
25
+ all(args, arg => { value arg; unparenthesised_node; regex_static_argument }) then "static"
26
+ { value args; first; unparenthesised_node } is ts_regex && (len(args) == 1 || {
27
+ value args
28
+ last
29
+ unparenthesised_node
30
+ regex_static_argument
31
+ }) then (if len(args) == 1 then "wrapped" else "flags")
32
+ else then null
33
+ }
34
+
35
+ in ts_call, ts_new rule es-prefer-regex-literals {
36
+ option disallowRedundantWrapping boolean = false "disallow constructors wrapping regex literals"
37
+ when callee.global_value == "RegExp"
38
+ let regex_preference regex_construction_kind(argument.nodes)
39
+ when regex_preference != null && (regex_preference == "static" || disallowRedundantWrapping)
40
+ warn when {
41
+ regex_preference == "static" then "Use a regular expression literal instead of the 'RegExp' constructor."
42
+ regex_preference == "wrapped" then "Regular expression literal is unnecessarily wrapped within a 'RegExp' constructor."
43
+ else then "Use regular expression literal with flags instead of the 'RegExp' constructor."
44
+ }
45
+ }
46
+
47
+ in ts_regex, ts_call, ts_new rule es-require-unicode-regexp {
48
+ option requireFlag ("u" | "v" | null) = null "require a particular unicode mode"
49
+ let unicode_info regexp_unicode_info
50
+ when unicode_info != null
51
+ when (when {
52
+ requireFlag == "u" then !re_test(unicode_info.flags, /u/)
53
+ requireFlag == "v" then !re_test(unicode_info.flags, /v/)
54
+ else then !re_test(unicode_info.flags, /[uv]/)
55
+ })
56
+ warn if requireFlag == "v" then "Use the 'v' flag." else "Use the 'u' flag."
57
+ }
58
+
59
+ }
@@ -0,0 +1,50 @@
1
+ # Redundant aliases; retain replacement spelling and comments.
2
+ import .ts/helpers
3
+
4
+ define {
5
+
6
+ in ts_identifier let module_name_info { name: identifier_name }
7
+ in ts_string let module_name_info { name: get(literal_value, "value") }
8
+ in ts_parenthesized forward module_name_info, module_default_info from expression
9
+ in ts_assignment_pattern, ts_assignment let module_default_info left.module_name_info
10
+ fn aliases_same(a, b) = a != null && b != null && a.name == b.name
11
+
12
+ fn rename_fix_allowed(n, replacement) = replacement != null
13
+ && !(replacement is (ts_assignment_pattern, ts_assignment) && replacement.left is ts_parenthesized)
14
+ && {
15
+ value overlapping(ts_comment, n.offset, n.end_offset)
16
+ all (comment) replacement.offset <= comment.offset && comment.end_offset <= replacement.end_offset
17
+ }
18
+
19
+ group useless_rename_import ts_import_specifier
20
+ in useless_rename_import rule es-no-useless-rename:import {
21
+ let original_name imported.module_name_info
22
+ when aliases_same(original_name, local.module_name_info)
23
+ let replacement local.node
24
+ fix at node if rename_fix_allowed(node, replacement) as byte_slice(source, offset, imported.offset) replacement.text
25
+ warn "Import {original_name.name} unnecessarily renamed."
26
+ }
27
+
28
+ group useless_rename_export ts_export_specifier
29
+ in useless_rename_export rule es-no-useless-rename:export {
30
+ let original_name local.module_name_info
31
+ when aliases_same(original_name, exported.module_name_info)
32
+ let replacement local.node
33
+ fix at node if rename_fix_allowed(node, replacement) as byte_slice(source, offset, local.offset) replacement.text
34
+ warn "Export {original_name.name} unnecessarily renamed."
35
+ }
36
+
37
+ group useless_rename_destructuring ts_pattern_property, ts_property
38
+ in useless_rename_destructuring rule es-no-useless-rename:destructuring {
39
+ let original_name key.module_name_info
40
+ when is ts_pattern_property || parent.flow_destructuring_target == true
41
+ when {
42
+ value default: value.module_default_info.nonempty ?? value.module_name_info
43
+ value aliases_same(original_name, default)
44
+ }
45
+ let replacement unparenthesised_node(value.node)
46
+ fix at node if rename_fix_allowed(node, replacement) as replacement.text
47
+ warn "Destructuring assignment {original_name.name} unnecessarily renamed."
48
+ }
49
+
50
+ }
@@ -0,0 +1,47 @@
1
+ # String content: escapes, script urls and line continuation.
2
+
3
+ define {
4
+
5
+ in ts_string rule es-no-nonoctal-decimal-escape {
6
+ each decimal_escape in {
7
+ value overlapping(ts_literal_piece, offset, end_offset)
8
+ filter (piece) re_test(piece.text, /^\\[89]$/)
9
+ unique_by (piece) {start: piece.offset, end: piece.end_offset}
10
+ } {
11
+ report at decimal_escape.node
12
+ warn "Don't use '{decimal_escape.text}' escape sequence."
13
+ }
14
+ }
15
+
16
+ in ts_string, ts_template rule es-no-script-url {
17
+ when literal_value.type == "string" && re_test(literal_value.value, /(?i)^javascript:/)
18
+ when parent not ts_tagged_template
19
+ warn "Script URL is a form of eval."
20
+ }
21
+
22
+ in ts_string let octal_escape {
23
+ value text
24
+ re_find /(?:^|[^\\])(?:\\\\)*\\(?:[0-3][0-7]{1,2}|[4-7][0-7]|0[89]|[1-7])/
25
+ re_find /\\(?:[0-3][0-7]{1,2}|[4-7][0-7]|0[89]|[1-7])$/
26
+ }
27
+ in ts_string rule es-no-octal-escape {
28
+ when octal_escape != null
29
+ warn "Don't use octal: '{if re_test(octal_escape, /^\\0[89]$/) then '\\0' else octal_escape}'. Use '\\u....' instead."
30
+ }
31
+
32
+ in ts_string rule es-no-multi-str {
33
+ when re_test(text, /[\r\n\u{2028}\u{2029}]/) && parent not (ts_jsx_attribute, ts_jsx_expression, ts_jsx_element)
34
+ warn "Multiline support is limited to browsers supporting ES5 only."
35
+ }
36
+
37
+ in ts_string, ts_template, ts_regex rule es-no-useless-escape {
38
+ option allowRegexCharacters string[] = [] "allow otherwise unnecessary regex escapes"
39
+ when contains(text, "\\")
40
+ each escape_info in regexp_useless_escapes {
41
+ when !escape_info.regex || !contains(allowRegexCharacters, escape_info.character)
42
+ report at { start: escape_info.offset, end: escape_info.offset + 1 }
43
+ warn "Unnecessary escape character: \\{escape_info.character}."
44
+ }
45
+ }
46
+
47
+ }