@inthedark/dxlint 0.0.0-stage → 0.1.0

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 +29 -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,169 @@
1
+ # .ts/naming: declaration and member names, ordinary context policies.
2
+
3
+ define {
4
+
5
+ group naming_name_node ts_identifier, ts_private_name, ts_string, ts_number
6
+ group naming_enum_member naming_name_node
7
+ group naming_class_method naming_name_node
8
+ group naming_object_method naming_name_node
9
+ group naming_type_method naming_name_node
10
+ group naming_class_property naming_name_node
11
+ group naming_object_property naming_name_node
12
+ group naming_type_property naming_name_node
13
+ group naming_classic_accessor naming_name_node
14
+ group naming_auto_accessor naming_name_node
15
+
16
+ in ts_node let naming_binding_owner if is (ts_declarator, ts_parameter, ts_function, ts_class, ts_interface, ts_type_alias, ts_enum, ts_type_parameter, ts_import, ts_import_specifier, ts_program) then node else parent.naming_binding_owner
17
+ in naming_name_node let naming_binding declaration_kinds != null
18
+ in naming_name_node let naming_member is (ts_method.key, ts_class_property.key, ts_property.key, ts_method_signature.key, ts_property_signature.key, ts_enum_member.name)
19
+ in naming_name_node let naming_owner if naming_binding then parent.naming_binding_owner else if naming_member then parent.node else null
20
+ # Empty names are strings; default() and null equality also match them.
21
+ in naming_name_node let naming_name if is ts_private_name then re_replace(text, /^#+/, "") else if is (ts_string, ts_number) then if type_of(static_name) == "str" then static_name else text else text
22
+ # Resolve the written field on its signature; dynamic `.type` reads the type fact.
23
+ in ts_property_signature let naming_type_member_context if type is ts_function_type then "type-method" else "type-property"
24
+ in naming_name_node let naming_context if naming_owner == null then null else if naming_binding then (
25
+ if naming_owner is ts_declarator then "variable"
26
+ else if naming_owner is ts_parameter then if any([] naming_owner.modifier, m => contains(["public", "protected", "private", "readonly"], m)) then "parameter-property" else "parameter"
27
+ else if naming_owner is ts_function then "function"
28
+ else if naming_owner is ts_class then "class"
29
+ else if naming_owner is ts_interface then "interface"
30
+ else if naming_owner is ts_type_alias then "type-alias"
31
+ else if naming_owner is ts_enum then "enum"
32
+ else if naming_owner is ts_type_parameter then "type-parameter"
33
+ else if naming_owner is ts_import || naming_owner is ts_import_specifier && naming_owner.imported == "default" then "import"
34
+ else null
35
+ ) else (
36
+ if naming_owner is ts_enum_member then "enum-member"
37
+ else if naming_owner is ts_method then
38
+ if contains(["get", "set"], naming_owner.kind) then "classic-accessor"
39
+ else if naming_owner.parent is ts_class then if naming_name == "constructor" then null else "class-method"
40
+ else "object-method"
41
+ else if naming_owner is ts_class_property then
42
+ if contains([] naming_owner.modifier, "accessor") then "auto-accessor"
43
+ else if naming_owner.init is (ts_function, ts_arrow) then "class-method" else "class-property"
44
+ else if naming_owner is ts_property then if naming_owner.value is (ts_function, ts_arrow) then "object-method" else "object-property"
45
+ else if naming_owner is ts_method_signature then "type-method"
46
+ else if naming_owner is ts_property_signature then naming_owner.naming_type_member_context
47
+ else null
48
+ )
49
+ in naming_name_node let naming_requires_quotes naming_member && !re_test(naming_name, /^[\p{Alphabetic}_$][\p{Alphabetic}\p{N}_$]*$/)
50
+ in naming_name_node let naming_convention_checked naming_context != null
51
+
52
+ fn naming_underscore_required(policy) = if policy == "requireDouble" then 2 else if policy == "require" then 1 else 0
53
+ fn naming_underscore_trim(count, policy) = (
54
+ if contains(["requireDouble", "allowDouble"], policy) && count >= 2 then 2
55
+ else if policy == "allowSingleOrDouble" then min(count, 2)
56
+ else if contains(["require", "allow"], policy) then min(count, 1) else 0
57
+ )
58
+ fn naming_underscore(name, policy, trailing) = {
59
+ value { value result: if trailing then /_+$/ else /^_+/; value re_find(name, result); nonempty } ?? ""
60
+ count: len
61
+ value naming_underscore_count(name, policy, trailing, count)
62
+ }
63
+ fn naming_underscore_count(name, policy, trailing, count) = {
64
+ name: if trailing then { value result: len(name) - naming_underscore_trim(count, policy); value slice(name, 0, result) } else { value count; naming_underscore_trim: naming_underscore_trim policy; value slice(name, naming_underscore_trim) },
65
+ error: policy == "forbid" && count > 0 || count < naming_underscore_required(policy)
66
+ }
67
+ fn naming_affix(name, choices, suffix) = {
68
+ value choices
69
+ filter (p) if suffix then ends_with(name, p) else starts_with(name, p)
70
+ matched: first
71
+ value naming_affix_match(name, choices, suffix, matched)
72
+ }
73
+ fn naming_affix_match(name, choices, suffix, matched) = {
74
+ name: if suffix then { value result: len(name) - len(matched); value slice(name, 0, result) } else { value matched; len: len; value slice(name, len) },
75
+ error: len(choices) > 0 && matched == null
76
+ }
77
+ fn naming_format(name, format) = name == ""
78
+ || format == "camelCase" && slice(name, 0, 1) == { value name; slice 0 1; lower } && !contains(name, "_")
79
+ || format == "PascalCase" && slice(name, 0, 1) == { value name; slice 0 1; upper } && !contains(name, "_")
80
+ || format == "strictCamelCase" && slice(name, 0, 1) == { value name; slice 0 1; lower } && !contains(name, "_") && !re_test(name, /\p{Lu}\p{Lu}/)
81
+ || format == "StrictPascalCase" && slice(name, 0, 1) == { value name; slice 0 1; upper } && !contains(name, "_") && !re_test(name, /\p{Lu}\p{Lu}/)
82
+ || format == "snake_case" && name == lower(name) && !re_test(name, /^_|_$|__/)
83
+ || format == "UPPER_CASE" && name == upper(name) && !re_test(name, /^_|_$|__/)
84
+ fn naming_failure(name, requires_quotes, settings) = {
85
+ value name
86
+ step: naming_underscore settings.leadingUnderscore false
87
+ value naming_leading_failure(requires_quotes, settings, step)
88
+ }
89
+ fn naming_leading_failure(quotes, settings, step) = (
90
+ if step.error then {kind: "leading", settings: settings}
91
+ else {
92
+ value step.name
93
+ step_2: naming_underscore settings.trailingUnderscore true
94
+ value naming_trailing_failure(quotes, settings, step_2)
95
+ }
96
+ )
97
+ fn naming_trailing_failure(quotes, settings, step) = (
98
+ if step.error then {kind: "trailing", settings: settings}
99
+ else { value step.name; step_2: naming_affix settings.prefix false; value naming_prefix_failure(quotes, settings, step_2) }
100
+ )
101
+ fn naming_prefix_failure(quotes, settings, step) = (
102
+ if step.error then {kind: "prefix", settings: settings}
103
+ else { value step.name; step_2: naming_affix settings.suffix true; value naming_suffix_failure(quotes, settings, step_2) }
104
+ )
105
+ fn naming_suffix_failure(quotes, settings, step) = (
106
+ if step.error then {kind: "suffix", settings: settings}
107
+ else if type_of(settings.customRegex) == "str" && re_test(step.name, settings.customRegex) != (settings.customMatch.nonempty ?? true) then {kind: "custom", settings: settings}
108
+ else if len(settings.formats) > 0 && (quotes || !any(settings.formats, format => naming_format(step.name, format))) then {kind: "format", settings: settings, processed: step.name}
109
+ else null
110
+ )
111
+ fn naming_site(n) = if n.parent is ts_parameter then { start: n.offset, end: n.parent.end_offset }
112
+ else if n.parent is ts_declarator && n.parent.type != null then { start: n.offset, end: n.parent.type.end_offset }
113
+ else n
114
+ fn naming_message(name, failure) = "name `" name "` " (
115
+ if failure.kind == "leading" || failure.kind == "trailing" then {
116
+ value result: if failure.kind == "leading" then "leadingUnderscore" else "trailingUnderscore"
117
+ value get(failure.settings, result)
118
+ naming_underscore_message failure.kind
119
+ }
120
+ else if failure.kind == "prefix" then "must have one of the following prefixes: " join(failure.settings.prefix, ", ")
121
+ else if failure.kind == "suffix" then "must have one of the following suffixes: " join(failure.settings.suffix, ", ")
122
+ else if failure.kind == "custom" then "must " (if failure.settings.customMatch.nonempty ?? true then "match" else "not match") " the RegExp: /" replace(failure.settings.customRegex, "/", "\\/") "/u"
123
+ else (if name != failure.processed then "trimmed as `" failure.processed "` " else "") "must match one of the following formats: " join(failure.settings.formats, ", ")
124
+ )
125
+ fn naming_underscore_message(policy, side) = if policy == "forbid" then "must not have a " side " underscore." else "must have " (if naming_underscore_required(policy) == 2 then "two" else "one") " " side " underscore(s)."
126
+
127
+ @macro naming_rule(rule-id, context, targets, default-formats, underscore-policy, label) {
128
+ in targets rule rule-id {
129
+ option formats enum(camelCase, PascalCase, strictCamelCase, StrictPascalCase, snake_case, UPPER_CASE)[] = default-formats
130
+ option leadingUnderscore enum(allow, allowDouble, allowSingleOrDouble, forbid, require, requireDouble) | null = underscore-policy
131
+ option trailingUnderscore enum(allow, allowDouble, allowSingleOrDouble, forbid, require, requireDouble) | null = underscore-policy
132
+ option prefix string[] = []
133
+ option suffix string[] = []
134
+ option customRegex regex | null = null
135
+ option customMatch boolean = true
136
+ when naming_context == context
137
+ when naming_convention_checked
138
+ let failure naming_failure(naming_name, naming_requires_quotes, {
139
+ formats: formats, leadingUnderscore: leadingUnderscore, trailingUnderscore: trailingUnderscore,
140
+ prefix: prefix, suffix: suffix, customRegex: customRegex, customMatch: customMatch
141
+ })
142
+ when failure != null
143
+ let naming_label label
144
+ report at naming_site(node)
145
+ warn naming_label " " naming_message(naming_name, failure)
146
+ }
147
+ }
148
+
149
+ @naming_rule(es-naming-convention:variable, "variable", ts_identifier, ["camelCase", "UPPER_CASE"], allow, "Variable")
150
+ @naming_rule(es-naming-convention:function, "function", ts_identifier, ["camelCase"], allow, "Function")
151
+ @naming_rule(es-naming-convention:parameter, "parameter", ts_identifier, ["camelCase"], allow, "Parameter")
152
+ @naming_rule(es-naming-convention:parameter-property, "parameter-property", ts_identifier, ["camelCase"], allow, "Parameter Property")
153
+ @naming_rule(es-naming-convention:import, "import", ts_identifier, ["camelCase", "PascalCase"], null, "Import")
154
+ @naming_rule(es-naming-convention:class, "class", ts_identifier, ["PascalCase"], null, "Class")
155
+ @naming_rule(es-naming-convention:interface, "interface", ts_identifier, ["PascalCase"], null, "Interface")
156
+ @naming_rule(es-naming-convention:type-alias, "type-alias", ts_identifier, ["PascalCase"], null, "Type Alias")
157
+ @naming_rule(es-naming-convention:enum, "enum", ts_identifier, ["PascalCase"], null, "Enum")
158
+ @naming_rule(es-naming-convention:type-parameter, "type-parameter", ts_identifier, ["PascalCase"], null, "Type Parameter")
159
+ @naming_rule(es-naming-convention:enum-member, "enum-member", naming_enum_member, ["camelCase"], allow, "Enum Member")
160
+ @naming_rule(es-naming-convention:class-method, "class-method", naming_class_method, ["camelCase"], allow, "Class Method")
161
+ @naming_rule(es-naming-convention:object-method, "object-method", naming_object_method, ["camelCase"], allow, "Object Literal Method")
162
+ @naming_rule(es-naming-convention:type-method, "type-method", naming_type_method, ["camelCase"], allow, "Type Method")
163
+ @naming_rule(es-naming-convention:class-property, "class-property", naming_class_property, ["camelCase"], allow, "Class Property")
164
+ @naming_rule(es-naming-convention:object-property, "object-property", naming_object_property, ["camelCase"], allow, "Object Literal Property")
165
+ @naming_rule(es-naming-convention:type-property, "type-property", naming_type_property, ["camelCase"], allow, "Type Property")
166
+ @naming_rule(es-naming-convention:classic-accessor, "classic-accessor", naming_classic_accessor, ["camelCase"], allow, "Classic Accessor")
167
+ @naming_rule(es-naming-convention:auto-accessor, "auto-accessor", naming_auto_accessor, ["camelCase"], allow, "Auto Accessor")
168
+
169
+ }
@@ -0,0 +1,96 @@
1
+ define {
2
+
3
+ in ts_if, ts_conditional {
4
+ in consequent propagates test
5
+ in alternate propagates !test
6
+ }
7
+ in ts_while, ts_for {
8
+ in body propagates test
9
+ }
10
+ in ts_for { in update propagates test }
11
+ in ts_logical {
12
+ when operator == "&&" { in right propagates left }
13
+ when operator == "||" { in right propagates !left }
14
+ when operator == "??" { in right propagates !(left \ nullable) }
15
+ when operator == "&&" { unwraps truthy left & right }
16
+ when operator == "||" { unwraps truthy left | right }
17
+ }
18
+ in ts_assignment {
19
+ when operator == "&&=" { in right propagates left }
20
+ when operator == "||=" { in right propagates !left }
21
+ when operator == "??=" { in right propagates !(left \ nullable) }
22
+ }
23
+ in ts_parenthesized { unwraps truthy expression }
24
+ in ts_non_null { unwraps truthy expression }
25
+ in ts_unary {
26
+ when operator == "!" { unwraps truthy !argument }
27
+ }
28
+ in ts_identifier, ts_this, ts_member, ts_index {
29
+ unwraps truthy excludes nullable, falsy
30
+ }
31
+ in ts_conditional {
32
+ unwraps truthy (test & consequent) | (!test & alternate)
33
+ }
34
+ in ts_type_predicate {
35
+ resolves value name
36
+ when modifier == null { unwraps truthy includes type from name }
37
+ }
38
+ in ts_binary {
39
+ when operator == "===" && (right always literal || right always undefined || right.structure_unwrapped is ts_null) { unwraps truthy includes right from left }
40
+ when operator == "===" && (left always literal || left always undefined || left.structure_unwrapped is ts_null) { unwraps truthy includes left from right }
41
+ # Unequal to a literal rules that value out; unequal to some `number` proves nothing.
42
+ when operator == "!==" && (right always literal || right always undefined || right.structure_unwrapped is ts_null) {
43
+ unwraps truthy !includes right from left
44
+ }
45
+ when operator == "!==" && (left always literal || left always undefined || left.structure_unwrapped is ts_null) {
46
+ unwraps truthy !includes left from right
47
+ }
48
+ when operator == "==" && (right.structure_unwrapped is ts_null || right always undefined) {
49
+ unwraps truthy !(left \ nullable)
50
+ }
51
+ when operator == "==" && (left.structure_unwrapped is ts_null || left always undefined) {
52
+ unwraps truthy !(right \ nullable)
53
+ }
54
+ when operator == "!=" && (right.structure_unwrapped is ts_null || right always undefined) {
55
+ unwraps truthy left \ nullable
56
+ }
57
+ when operator == "!=" && (left.structure_unwrapped is ts_null || left always undefined) {
58
+ unwraps truthy right \ nullable
59
+ }
60
+ when operator == "in" { unwraps truthy has left from right }
61
+ when operator == "instanceof" {
62
+ unwraps truthy includes right.["prototype"] from left
63
+ }
64
+ }
65
+
66
+ @macro narrowing_typeof(tag, expected, available) {
67
+ in ts_binary {
68
+ when left.structure_unwrapped is ts_unary && left.structure_unwrapped.operator == "typeof" && right.literal_value.value == tag && available {
69
+ when operator == "===" || operator == "==" {
70
+ unwraps truthy includes expected from left.structure_unwrapped.argument
71
+ }
72
+ when operator == "!==" || operator == "!=" {
73
+ unwraps truthy !includes expected from left.structure_unwrapped.argument
74
+ }
75
+ }
76
+ when right.structure_unwrapped is ts_unary && right.structure_unwrapped.operator == "typeof" && left.literal_value.value == tag && available {
77
+ when operator == "===" || operator == "==" {
78
+ unwraps truthy includes expected from right.structure_unwrapped.argument
79
+ }
80
+ when operator == "!==" || operator == "!=" {
81
+ unwraps truthy !includes expected from right.structure_unwrapped.argument
82
+ }
83
+ }
84
+ }
85
+ }
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)
95
+
96
+ }
@@ -0,0 +1,262 @@
1
+ # Expression/type parentheses and IIFE wrapping; structure imports this policy.
2
+ define {
3
+
4
+ in ts_call, ts_new, ts_import_call let structure_argument_count count(argument)
5
+ in ts_function, ts_call let structure_iife_wrapped parent is ts_parenthesized
6
+ || is (ts_if.test, ts_while.test, ts_do_while.test)
7
+ || is ts_switch.discriminant
8
+ || is ts_with.object
9
+ || is (ts_call.argument, ts_new.argument, ts_import_call.argument) && parent.structure_argument_count == 1 && next_token is ts_close_paren
10
+ in ts_function let structure_iife_callee { direct: true, prototype: false, wrapped: structure_iife_wrapped, node: node }
11
+ in ts_parenthesized let structure_iife_callee expression.structure_iife_callee
12
+ in ts_member, ts_index let structure_iife_callee if (object.structure_iife_callee).direct && contains(["call", "apply"], static_name) then { direct: false, prototype: true, wrapped: (object.structure_iife_callee).wrapped, node: (object.structure_iife_callee).node } else null
13
+
14
+ in ts_call rule es-wrap-iife {
15
+ option mode "outside" | "inside" | "any" = "outside"
16
+ option functionPrototypeMethods boolean = false "include function call/apply invocations"
17
+ let info callee.structure_iife_callee
18
+ when info != null && (functionPrototypeMethods || !info.prototype)
19
+ when !structure_iife_wrapped && !info.wrapped || mode == "inside" && !info.wrapped || mode == "outside" && !structure_iife_wrapped
20
+ let target if mode == "inside" then info.node else node
21
+ let closing if mode == "inside" then token_neighbours.after else info.node.token_neighbours.after
22
+ let edit if !structure_iife_wrapped && !info.wrapped then { start: target.offset, end: target.end_offset, text: "(" target.text ")" }
23
+ else if mode == "inside" then (
24
+ if parent is ts_parenthesized && parent not ts_new.callee then
25
+ { start: info.node.end_offset, end: closing.end, text: ")" byte_slice(source, info.node.end_offset, closing.start) }
26
+ else { start: info.node.offset, end: info.node.end_offset, text: "(" info.node.text ")" }
27
+ ) else {
28
+ start: closing.start, end: end_offset, text: byte_slice(source, closing.end, end_offset) ")"
29
+ }
30
+ fix at edit as edit.text
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
+ }
33
+
34
+ group structure_parens ts_parenthesized, ts_parenthesized_type
35
+ group structure_assertion ts_as, ts_satisfies, ts_type_assertion
36
+ group structure_binary ts_binary, ts_logical
37
+ group structure_member ts_member, ts_index
38
+ group structure_conditional_owner ts_function, ts_arrow, ts_program, ts_return, ts_if, ts_while, ts_do_while, ts_conditional, ts_for
39
+ group structure_return_owner ts_return, ts_arrow, ts_function, ts_program
40
+ group no_extra_parens_inner ts_node
41
+ group no_extra_parens_spread_allowed
42
+
43
+ in structure_parens let no_extra_parens_checked true
44
+
45
+ in ts_parenthesized let structure_parens_inner expression.node
46
+ in ts_parenthesized_type let structure_parens_inner type.node
47
+ in ts_node let structure_unwrapped if is structure_parens then structure_parens_inner.structure_unwrapped else node
48
+ in structure_parens let structure_parens_outer if parent is structure_parens then parent.structure_parens_outer else node
49
+ in structure_parens let no_extra_parens_owner {
50
+ value outer: structure_parens_outer
51
+ value owner: outer.parent.node
52
+ value if owner is ts_computed_key || outer is (ts_property.value, ts_pattern_property.value) && owner.parent is (ts_object, ts_object_pattern) then owner.parent.node else owner
53
+ }
54
+ in structure_parens let structure_parens_count if parent is structure_parens then 1 + parent.structure_parens_count else 1
55
+ in ts_node let structure_parens_optional node.optional != null || is (structure_member, ts_call)
56
+ && (if is ts_call then callee.structure_parens_optional else object.structure_parens_optional) == true
57
+ in ts_node let structure_parens_has_call is (ts_call, ts_import_call)
58
+ || is structure_member && object.structure_parens_has_call == true
59
+ || is (ts_non_null, ts_parenthesized) && expression.structure_parens_has_call == true
60
+ || is ts_tagged_template && tag.structure_parens_has_call == true
61
+ in ts_node let structure_parens_has_in node.operator == "in"
62
+ || any(children.nodes, child =>
63
+ if child is structure_parens then structure_parens_required(source, child.parent.node, child.structure_parens_inner.structure_unwrapped, child) == 0
64
+ && child.structure_parens_inner.structure_unwrapped.structure_parens_has_in == true
65
+ else !structure_parens_enclosed_in(child) && child.structure_parens_has_in == true)
66
+ in ts_call let structure_parens_iife (callee.structure_iife_callee).direct == true && !(callee.structure_iife_callee).wrapped
67
+ in ts_node let structure_parens_prototype if is ts_call then (callee.structure_iife_callee).prototype == true || (callee.structure_unwrapped).structure_parens_prototype == true
68
+ else if is structure_member then (object.structure_unwrapped).structure_parens_prototype == true else false
69
+ in ts_node let structure_parens_leading_child if is (ts_call, ts_new) then callee.node
70
+ else if is structure_member then object.node
71
+ else if is (structure_binary, ts_assignment) then left.node
72
+ else if is ts_conditional then test.node
73
+ else if is (ts_sequence, ts_as, ts_satisfies, ts_non_null) then first(expression.nodes) else null
74
+ in ts_node let structure_parens_leading_object is ts_object
75
+ || structure_parens_leading_child != null && structure_parens_leading_child.offset == offset
76
+ && structure_parens_leading_child not ts_parenthesized && structure_parens_leading_child.structure_parens_leading_object == true
77
+ in ts_node let structure_parens_hazard is (ts_object, ts_function, ts_class)
78
+ || is ts_index && node.object == "let"
79
+ || structure_parens_leading_child != null && structure_parens_leading_child not ts_parenthesized && structure_parens_leading_child.structure_parens_hazard == true
80
+ in ts_node let structure_parens_leads is (ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
81
+ ts_member.object, ts_index.object, ts_call.callee, ts_binary.left, ts_logical.left, ts_assignment.left, ts_conditional.test)
82
+ || is ts_sequence.expression && index == 0 || is ts_update.argument && offset == parent.offset
83
+ in ts_node let structure_parens_context if parent is (ts_expression_statement, ts_export_assignment, ts_return)
84
+ || is (ts_arrow.body, ts_export.expression, ts_for.init, ts_for_in.left, ts_for_of.left) then parent.node
85
+ else if structure_parens_leads && !(parent is structure_parens && parent.structure_parens_context is ts_return) then parent.structure_parens_context else null
86
+ in ts_node let structure_parens_for_init if parent is ts_for then is ts_for.init
87
+ else if structure_parens_enclosed_in(node) then false else parent.structure_parens_for_init == true
88
+ in ts_node let structure_parens_conditional_owner if is structure_conditional_owner then node else parent.structure_parens_conditional_owner
89
+ in ts_node let structure_parens_return_owner if is structure_return_owner then node else parent.structure_parens_return_owner
90
+ in ts_node let structure_parens_leading_let if is ts_identifier then text == "let"
91
+ else structure_parens_leading_child != null && structure_parens_leading_child not ts_parenthesized && structure_parens_leading_child.structure_parens_leading_let == true
92
+ in ts_node let structure_parens_new_receiver if is ts_new then node
93
+ else if is (ts_new.callee, ts_member.object, ts_index.object, ts_call.callee, ts_tagged_template.tag, ts_non_null.expression) then parent.structure_parens_new_receiver else null
94
+
95
+ # These child fields use +In grammar even inside a for initialiser.
96
+ fn structure_parens_enclosed_in(child) = child.parent is (ts_array, ts_object, ts_array_pattern, ts_object_pattern, ts_function, ts_block, ts_template, structure_parens)
97
+ || child is (ts_call.argument, ts_new.argument, ts_import_call.argument, ts_index.index, ts_computed_key.expression, ts_arrow.parameter, ts_parameter.init, ts_conditional.consequent)
98
+
99
+ fn structure_parens_need(required) = if required then 1 else 0
100
+ fn structure_parens_required(source, p, inner, outer) =
101
+ if p is structure_binary then {
102
+ value p.operator == "??" && contains(["&&", "||"], inner.operator) || contains(["&&", "||"], p.operator) && inner.operator == "??"
103
+ || inner is structure_assertion || outer is ts_node.left && p.operator == "**" && inner is (ts_unary, ts_await)
104
+ || inner.precedence < p.precedence || inner.precedence == p.precedence && (outer is ts_node.right) != (p.operator == "**")
105
+ structure_parens_need
106
+ }
107
+ else if p is ts_conditional then {
108
+ value inner is structure_assertion || inner.precedence < (if outer is ts_node.test then 4 else 1)
109
+ structure_parens_need
110
+ }
111
+ else if outer is (ts_call.callee, ts_new.callee) then {
112
+ value p is ts_call && inner is ts_function
113
+ || inner.precedence < (if p is ts_new && p.callee.end_offset == p.end_offset then 18 else 19) || inner.structure_parens_optional
114
+ || p is ts_new && inner.structure_parens_has_call
115
+ structure_parens_need
116
+ }
117
+ else if p is (ts_call, ts_new, ts_import_call, ts_jsx_expression) then structure_parens_need(inner.precedence < 1)
118
+ else if outer is (ts_member.object, ts_index.object) then (if inner is ts_regex then null else {
119
+ value inner is ts_number && p is ts_member && re_test(inner.text, /^[0-9_]+$/) && !re_test(inner.text, /^0[0-7]+$/)
120
+ || inner.structure_parens_optional && p.optional == null || inner.precedence < 19
121
+ || p.structure_parens_new_receiver != null && inner.structure_parens_has_call
122
+ structure_parens_need
123
+ })
124
+ else if p is structure_member then 0
125
+ else if p is ts_arrow then (if inner is structure_assertion then null else structure_parens_need(inner.structure_parens_leading_object == true || inner.precedence < 1))
126
+ else if outer is ts_declarator.init then (if inner is ts_regex then null else structure_parens_need(inner.precedence < 1))
127
+ else if p is (ts_return, ts_throw, ts_yield) then (if inner is ts_regex && p is ts_return then null else {
128
+ value { value source; byte_slice p.offset inner.offset; re_test /[\n\r\u{2028}\u{2029}]/ } || p is ts_yield && inner.precedence < 1
129
+ structure_parens_need
130
+ })
131
+ else if p is (ts_expression_statement, ts_export_assignment) then structure_parens_need(inner.structure_parens_hazard || inner.structure_parens_iife)
132
+ else if p is (ts_if, ts_while, ts_do_while, ts_switch, ts_with, ts_case) then (if p is ts_case && inner is structure_assertion then null else 0)
133
+ else if outer is ts_export.expression then structure_parens_need(inner.precedence < 1)
134
+ else if p is ts_for then (if inner is structure_assertion && outer not ts_for.init then null else structure_parens_need(outer is ts_for.init && inner.structure_parens_has_in))
135
+ else if p is ts_for_in then 0
136
+ else if p is ts_for_of then structure_parens_need(outer is ts_for_of.right && inner.precedence < 1)
137
+ else if outer is ts_assignment.left then (if inner not (ts_identifier, structure_member) then null else structure_parens_need(
138
+ inner is ts_identifier && (p.right.structure_unwrapped) is (ts_function, ts_class, ts_arrow) && (p.right.structure_unwrapped).name == null))
139
+ else if p is ts_spread && inner is structure_assertion then null
140
+ else if p is (ts_assignment, ts_assignment_pattern, ts_array, ts_spread, ts_property, ts_class_property, ts_parameter, ts_computed_key) then structure_parens_need(inner.precedence < 1)
141
+ else if p is (ts_unary, ts_await) then structure_parens_need(inner.precedence < 15)
142
+ else if p is ts_update then structure_parens_need(inner.precedence < 17)
143
+ else if p is (ts_sequence, ts_type_alias, ts_parenthesized_type, ts_template) then 0
144
+ else if outer is ts_tagged_template.tag then structure_parens_need(inner.precedence < 19 || inner.structure_parens_optional)
145
+ else if outer is ts_class.extends then structure_parens_need(inner.precedence < 19)
146
+ else if outer is ts_enum_member.init then structure_parens_need(inner.precedence < 1)
147
+ else if p is ts_array_type then structure_parens_need(inner.precedence < 20)
148
+ else if p is ts_type_operator then structure_parens_need(inner.precedence < p.precedence)
149
+ else if p is (ts_union, ts_intersection) then structure_parens_need(inner.precedence < p.precedence)
150
+ else if outer is (ts_type_assertion.expression, ts_as.expression, ts_satisfies.expression) then structure_parens_need(inner.precedence < 15)
151
+ else if outer is (ts_node.type, ts_node.return_type) then 0 else null
152
+
153
+ # Removing a delimiter must not join identifiers, operators or comment openers.
154
+ fn structure_boundary_uncertain(left, right) = left != "" && right != ""
155
+ && !re_test(left, /\s/) && !re_test(right, /\s/)
156
+ && re_test(left right, /[\\\u{80}-\u{10ffff}]/)
157
+ fn structure_boundary_space(left, right) =
158
+ re_test(left, /[a-zA-Z0-9_$]/) && re_test(right, /[a-zA-Z0-9_$]/)
159
+ || left == "/" && re_test(right, /[a-zA-Z0-9_$/*]/)
160
+ || re_test(left right, /^(?:\+\+|--|\*\*|&&|\|\||\?\?|\?\.|<<|>>|<=|>=|==|!=|=>|[+\-*%&|^]=)$/)
161
+
162
+ fn structure_parens_comment(pair, source) = {
163
+ value start: (pair.space_before).start.nonempty ?? 0
164
+ value overlapping(ts_comment, start, pair.offset + 1)
165
+ find_last (c) c.end_offset <= pair.offset && { value source; byte_slice c.end_offset pair.offset; trim } == ""
166
+ }
167
+ fn structure_parens_cast(pair, source) = {
168
+ value comment: structure_parens_comment(pair, source)
169
+ value comment is ts_block_comment && contains(comment.structure_comment_body, "@type")
170
+ }
171
+ fn structure_parens_replacement(source, pairs) = {
172
+ value pairs
173
+ fold (null) (body, pair) {
174
+ start: pair.offset, end: pair.end_offset,
175
+ text: if body == null then byte_slice(source, pair.offset + 1, pair.end_offset - 1)
176
+ else byte_slice(source, pair.offset + 1, body.start) body.text byte_slice(source, body.end, pair.end_offset - 1),
177
+ }
178
+ }
179
+
180
+ in structure_parens rule es-no-extra-parens {
181
+ option conditionalAssign boolean = true "enforce assignments in conditions"
182
+ option ternaryOperandBinaryExpressions boolean = true "enforce binary ternary operands"
183
+ option nestedBinaryExpressions boolean = true "enforce nested binary operands"
184
+ option returnAssign boolean = true "enforce assignments in returns"
185
+ option nestedConditionalExpressions boolean = true "enforce nested conditionals"
186
+ option enforceForArrowConditionals boolean = true "enforce conditional arrow bodies"
187
+ option enforceForSequenceExpressions boolean = true "enforce sequence expressions"
188
+ option enforceForNewInMemberExpressions boolean = true "enforce new expressions in member access"
189
+ option enforceForFunctionPrototypeMethods boolean = true "enforce function prototype receivers"
190
+ option allowParensAfterCommentPattern string = "" "allow parentheses after matching comments"
191
+ let inner structure_parens_inner
192
+ when inner != null && inner not structure_parens
193
+ when no_extra_parens_checked && inner is no_extra_parens_inner
194
+ let outer structure_parens_outer
195
+ let owner outer.parent.node
196
+ when owner != null && outer not ts_node.type_argument
197
+ let required structure_parens_required(source, owner, inner, outer)
198
+ when required != null
199
+ let context outer.structure_parens_context
200
+ let protected inner.structure_parens_iife
201
+ || inner is structure_assertion && owner is (ts_for, ts_for_in, ts_for_of, ts_throw, ts_yield, ts_unary, ts_await)
202
+ || context != null && context not ts_return && (inner.structure_parens_hazard
203
+ || context is ts_for_of && inner.structure_parens_leading_let || inner is ts_identifier && inner.text == "async" && context is ts_for_of)
204
+ || context is (ts_expression_statement, ts_for, ts_for_in) && inner is ts_identifier && inner.text == "let" && outer.next_token is (ts_identifier, ts_open_bracket)
205
+ || context is ts_return && { value source; byte_slice context.offset inner.offset; re_test /[\n\r\u{2028}\u{2029}]/ }
206
+ || outer.structure_parens_for_init && inner.structure_parens_has_in
207
+ || owner is ts_update && { value source; byte_slice inner.end_offset owner.end_offset; re_test /[\n\r]/ }
208
+ || owner is ts_await && inner is ts_non_null
209
+ when structure_parens_count > (if protected && required < 1 then 1 else required)
210
+ let comment structure_parens_comment(outer, source)
211
+ let typed_inner {
212
+ value overlapping(ts_block_comment, outer.offset, inner.offset)
213
+ any (c) c.offset > outer.offset && c.end_offset <= inner.offset && contains(c.structure_comment_body, "@type")
214
+ }
215
+ when !(comment is ts_block_comment && contains(comment.structure_comment_body, "@type") && (structure_parens_count == 1 || typed_inner))
216
+ when conditionalAssign || !(inner is ts_assignment && owner.structure_parens_conditional_owner is (ts_if, ts_while, ts_do_while, ts_conditional, ts_for))
217
+ when ternaryOperandBinaryExpressions || !(inner is structure_binary && owner is ts_conditional)
218
+ when nestedBinaryExpressions || !(inner is structure_binary && owner is structure_binary || structure_parens_count == 1 && inner is (ts_union, ts_intersection) && owner is (ts_union, ts_intersection))
219
+ when returnAssign || !(inner is ts_assignment && owner.structure_parens_return_owner is (ts_return, ts_arrow))
220
+ when nestedConditionalExpressions || !(inner is ts_conditional && owner is ts_conditional)
221
+ when enforceForArrowConditionals || !(inner is ts_conditional && owner is ts_arrow)
222
+ when enforceForSequenceExpressions || inner not ts_sequence
223
+ when enforceForNewInMemberExpressions || !(inner is ts_new && owner is structure_member)
224
+ when enforceForFunctionPrototypeMethods || !(inner.structure_parens_prototype || inner is ts_function && owner.parent.structure_parens_prototype == true)
225
+ when owner not ts_spread || inner not no_extra_parens_spread_allowed
226
+ when allowParensAfterCommentPattern == "" || comment == null || !re_test(comment.structure_comment_body, allowParensAfterCommentPattern)
227
+ warn "Unnecessary parentheses around expression."
228
+ let directive owner is ts_expression_statement && (owner.parent is ts_program
229
+ || owner.parent is ts_block && owner.parent.parent is (ts_function, ts_arrow, ts_method))
230
+ let minimum if directive && inner is ts_string
231
+ || !nestedBinaryExpressions && inner is (ts_union, ts_intersection) && owner is (ts_union, ts_intersection)
232
+ || protected && required < 1 then 1 else required
233
+ let chain {
234
+ value node
235
+ walk (pair) if pair.parent is structure_parens then pair.parent.node else null
236
+ }
237
+ let batch is ts_parenthesized_type || any(chain, pair => structure_parens_cast(pair, source))
238
+ let removable if batch then structure_parens_count - minimum else 1
239
+ let pairs {
240
+ value chain
241
+ filter (pair) !structure_parens_cast(pair, source)
242
+ slice 0 removable
243
+ }
244
+ let replacement structure_parens_replacement(source, pairs)
245
+ let pair { value pairs; last }
246
+ let body replacement.text
247
+ let before if {
248
+ value 0
249
+ max: max (pair.offset - 1)
250
+ value overlapping(ts_block_comment, max, pair.offset)
251
+ any (c) c.end_offset == pair.offset
252
+ } then "" else byte_char(source, pair.offset, true)
253
+ let after byte_char(source, pair.end_offset)
254
+ let first byte_char(body, 0)
255
+ let last { value body; length: byte_len; value byte_char(body, length, true) }
256
+ let fixable pair != null && byte_char(source, pair.offset) == "(" && byte_char(source, pair.end_offset, true) == ")"
257
+ && !structure_boundary_uncertain(before, first) && !structure_boundary_uncertain(last, after)
258
+ && !(structure_parens_count == 1 && directive && inner is ts_string)
259
+ fix at replacement if fixable as (if structure_boundary_space(before, first) then " " else "") body (if structure_boundary_space(last, after) then " " else "")
260
+ }
261
+
262
+ }
@@ -0,0 +1,25 @@
1
+ define {
2
+
3
+ in ts_node let precedence 21
4
+ in ts_sequence let precedence 0
5
+ in ts_assignment, ts_arrow let precedence 1
6
+ in ts_yield let precedence 2
7
+ in ts_conditional, ts_conditional_type, ts_function_type let precedence 3
8
+ in ts_union let precedence 4
9
+ in ts_intersection let precedence 5
10
+ in ts_binary, ts_logical let precedence get({
11
+ "||": 4, "??": 4, "&&": 5, "|": 6, "^": 7, "&": 8,
12
+ "==": 9, "!=": 9, "===": 9, "!==": 9,
13
+ "<": 10, ">": 10, "<=": 10, ">=": 10, "in": 10, "instanceof": 10,
14
+ "<<": 11, ">>": 11, ">>>": 11, "+": 12, "-": 12,
15
+ "*": 13, "/": 13, "%": 13, "**": 14,
16
+ }, operator.text) ?? 21
17
+ in ts_as, ts_satisfies let precedence 10
18
+ in ts_unary, ts_type_assertion, ts_type_operator let precedence 15
19
+ in ts_await let precedence 16
20
+ in ts_update let precedence 17
21
+ in ts_call, ts_tagged_template, ts_import_call let precedence 19
22
+ in ts_new let precedence if callee.end_offset < end_offset then 19 else 18
23
+ in ts_member, ts_index, ts_non_null, ts_array_type let precedence 20
24
+
25
+ }
@@ -0,0 +1,34 @@
1
+ # Calls and constructions that cannot work, and borrowed prototype methods.
2
+
3
+ define {
4
+
5
+ # `new Promise(async () => ...)`, also inside parentheses.
6
+ in ts_new let promise_constructor callee.is_real_promise == true
7
+ in ts_identifier let is_real_promise @is_global("Promise")
8
+ in ts_parenthesized let promise_executor_slot parent is ts_new && index == 0 && parent.promise_constructor || parent is ts_parenthesized && parent.promise_executor_slot == true
9
+ in ts_arrow, ts_function rule es-no-async-promise-executor {
10
+ when contains(modifier, "async")
11
+ when parent is ts_new && index == 0 && parent.promise_constructor || parent is ts_parenthesized && parent.promise_executor_slot == true
12
+ let async_token find(modifier.nodes, modifier => modifier.text == "async")
13
+ report at { start: async_token.offset, end: async_token.end_offset }
14
+ warn "Promise executor functions should not be async."
15
+ }
16
+
17
+ in ts_call, ts_new rule es-no-obj-calls {
18
+ when contains(["Atomics", "JSON", "Math", "Reflect", "Intl", "Temporal"], callee.global_value)
19
+ let called { value callee; re_replace /^[\s(]+|[\s)!]+$/ ""; re_replace /^.*[.\[]\s*['"`]?|['"`]?\s*\]$/ "" }
20
+ warn if called == callee.global_value then "'{called}' is not a function." else "'{called}' is reference to '{callee.global_value}', which is not a function."
21
+ }
22
+ in ts_new rule es-no-new-native-nonconstructor {
23
+ when (callee == "Symbol" || callee == "BigInt") && callee.is_real_global == true
24
+ warn "`{callee}` cannot be called as a constructor."
25
+ }
26
+
27
+ # `x.hasOwnProperty(k)` borrows a method the object may not have.
28
+ in ts_member.property, ts_index.index rule es-no-prototype-builtins {
29
+ when parent.called_member == true && contains(["hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable"], parent.static_name)
30
+ warn "Do not access Object.prototype method '{parent.static_name}' from target object."
31
+ }
32
+ in ts_member, ts_index, ts_parenthesized let called_member is ts_call.callee || parent is ts_parenthesized && parent.called_member == true
33
+
34
+ }
@@ -0,0 +1,39 @@
1
+ # Class construction order and accessor returns.
2
+
3
+ define {
4
+
5
+ in ts_method let problem_super_calls {
6
+ value missing: flow_complete_without(body.node, "constructed", ["returned"])
7
+ value called: flow_complete_with(body.node, "constructed", ["returned"])
8
+ value if missing == false then "all" else if missing == true then if called == true then "some" else if called == false then "none" else null else null
9
+ }
10
+
11
+ in ts_method.key rule es-constructor-super {
12
+ when text == "constructor" && ts_class.extends != null
13
+ when parent.problem_super_calls == "none" || parent.problem_super_calls == "some"
14
+ warn if parent.problem_super_calls == "none" then "Expected to call 'super()'." else "Lacked a call of 'super()' in some code paths."
15
+ }
16
+ in ts_call rule es-constructor-super:call {
17
+ when callee is ts_super && flow_constructor != null && reachable(node) == true && (flow_prior_event(node, "constructed") == true || flow_constructor.parent.extends.flow_constructor_heritage == false)
18
+ warn if flow_prior_event(node, "constructed") == true then "Unexpected duplicate 'super()'." else "Unexpected 'super()' because 'super' is not a constructor."
19
+ }
20
+ in ts_this rule es-no-this-before-super {
21
+ when flow_constructor != null && reachable(node) == true && flow_required(node, "constructed") == false
22
+ warn "'this' is not allowed before 'super()'."
23
+ }
24
+ in ts_member, ts_index rule es-no-this-before-super:super {
25
+ when object is ts_super && flow_constructor != null && reachable(node) == true && flow_required(node, "constructed") == false
26
+ warn "'super' is not allowed before 'super()'."
27
+ }
28
+
29
+ in ts_return rule es-no-setter-return {
30
+ when argument != null && enclosing_accessor == "set"
31
+ warn "Setter cannot return a value."
32
+ }
33
+ # `{ set: v => value }`: the arrow's value is its return.
34
+ in ts_arrow.body rule es-no-setter-return:arrow {
35
+ when parent.accessor == "set" && not ts_block
36
+ warn "Setter cannot return a value."
37
+ }
38
+
39
+ }