@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,811 @@
1
+ # .ts/restrictions: eslint core rules that limit or forbid things by
2
+ # configuration: sizes (max-*, complexity), restricted names, imports,
3
+ # properties and syntax, identifier shapes, sort orders, comment and
4
+ # operator bans.
5
+
6
+ import .ts/restrictions/comments
7
+ import .ts/helpers
8
+
9
+ define {
10
+
11
+ schema restriction_import_path {
12
+ name: string
13
+ message?: string
14
+ importNames?: string[]
15
+ allowImportNames?: string[]
16
+ allowTypeImports: boolean = false
17
+ }
18
+ schema restriction_import_pattern {
19
+ group?: string[]
20
+ regex?: js_regex
21
+ message?: string
22
+ caseSensitive: boolean = false
23
+ importNames?: string[]
24
+ allowImportNames?: string[]
25
+ importNamePattern?: js_regex
26
+ allowImportNamePattern?: js_regex
27
+ allowTypeImports: boolean = false
28
+ }
29
+ @macro restriction_import_options {
30
+ option paths restriction_import_path[] = [] "restricted import paths"
31
+ option patterns restriction_import_pattern[] = [] "restricted import pattern groups"
32
+ }
33
+ @macro restriction_import_list(xs) (if len(xs) < 2 then { value xs; map (x) "'" x "'"; join "" } else if len(xs) == 2 then { value xs; map (x) "'" x "'"; join " and " } else {
34
+ value result: len(xs) - 1
35
+ value slice(xs, 0, result)
36
+ map (x) "'" x "'"
37
+ join ", "
38
+ } ", and '" last(xs) "'")
39
+ @macro restriction_import_plural(xs) (if len(xs) == 1 then "is" else "are")
40
+ @macro restriction_import_custom(entry) (if entry.message != null && entry.message != "" then " " entry.message else "")
41
+ @macro restriction_import_glob(path, glob, sensitive) (!starts_with(glob, "#") && {
42
+ value result: (if sensitive then "" else "(?i)") (if { value glob; trim_start "!"; contains "/" } then "^" else "(?:^|/)") {
43
+ value glob
44
+ trim_start "!/"
45
+ replace "\\#" "#"
46
+ replace "\\!" "!"
47
+ re_escape
48
+ replace "\\*\\*/" "(?:.*/)?"
49
+ replace "\\*" "[^/]*"
50
+ replace "\\?" "[^/]"
51
+ } "(?:/|$)"
52
+ value re_test(path, result)
53
+ })
54
+ @macro restriction_import_group(path, entry) ({
55
+ value entry.group.nonempty ?? []
56
+ find_last (import_glob_pattern) @restriction_import_glob(path, import_glob_pattern, entry.caseSensitive)
57
+ })
58
+ @macro restriction_import_group_blocked(path, entry) ((@restriction_import_group(path, entry)) != null && !{ value @restriction_import_group(path, entry); starts_with "!" })
59
+ @macro restriction_import_pattern_matches(entry) (if entry.regex != null then { value result: if entry.caseSensitive then "u" else "iu"; value js_re_test(info.source, entry.regex, result) } else @restriction_import_group_blocked(info.source, entry) || any(info.parents, p => @restriction_import_group_blocked(p, entry)))
60
+ fn restriction_import_parent(path) = {
61
+ value path
62
+ split: split "/"
63
+ value result: { value path; split "/"; len } - 1
64
+ value slice(split, 0, result)
65
+ join "/"
66
+ }
67
+ fn restriction_import_parents(path) = {
68
+ value path
69
+ walk (current) if contains(current, "/") && restriction_import_parent(current) != "" then restriction_import_parent(current) else null
70
+ drop 1
71
+ }
72
+ fn restriction_import_source(value) = if type_of(value) == "str" then trim(value) else null
73
+ in ts_import_specifier, ts_export_specifier let restriction_import_all_members_type kind == "type" && (previous == null || previous.restriction_import_all_members_type == true)
74
+ in ts_call let restriction_import_argument first([] argument.static_name)
75
+ in ts_import {
76
+ let restriction_import_source restriction_import_source(from.static_name)
77
+ let restriction_import_type kind == "type"
78
+ let restriction_import_all_type kind == "type" || default_import == null && namespace == null && count(specifier) > 0 && last([] specifier.restriction_import_all_members_type) == true
79
+ let restriction_import_name ""
80
+ let restriction_import_item false
81
+ let restriction_import_offset offset
82
+ let restriction_import_end end_offset
83
+ let restriction_import_star_offset offset + {
84
+ value text
85
+ split "*"
86
+ first
87
+ byte_len
88
+ }
89
+ }
90
+ in ts_export {
91
+ let restriction_import_source restriction_import_source(from.static_name)
92
+ let restriction_import_type kind == "type"
93
+ let restriction_import_all_type kind == "type" || namespace == null && count(specifier) > 0 && last([] specifier.restriction_import_all_members_type) == true
94
+ let restriction_import_star count(specifier) == 0 && declaration == null
95
+ let restriction_import_name if restriction_import_star then "*" else ""
96
+ let restriction_import_item restriction_import_star
97
+ let restriction_import_star_offset offset + {
98
+ value text
99
+ split "*"
100
+ first
101
+ byte_len
102
+ }
103
+ let restriction_import_offset if restriction_import_star then restriction_import_star_offset else offset
104
+ let restriction_import_end if restriction_import_star then restriction_import_star_offset + 1 else end_offset
105
+ }
106
+ in ts_import_equals {
107
+ let restriction_import_source restriction_import_source(value.restriction_import_argument)
108
+ let restriction_import_type kind == "type"
109
+ let restriction_import_all_type kind == "type"
110
+ let restriction_import_name ""
111
+ let restriction_import_item false
112
+ let restriction_import_offset offset
113
+ let restriction_import_end end_offset
114
+ }
115
+ in ts_import_specifier let restriction_import_name if type_of(imported.static_name) == "str" then imported.static_name else imported.nonempty ?? local
116
+ in ts_export_specifier let restriction_import_name if type_of(local.static_name) == "str" then local.static_name else local
117
+ in ts_import_specifier, ts_export_specifier {
118
+ let restriction_import_source parent.restriction_import_source
119
+ let restriction_import_type kind == "type" || parent.restriction_import_type
120
+ let restriction_import_all_type parent.restriction_import_all_type
121
+ let restriction_import_item true
122
+ let restriction_import_offset offset
123
+ let restriction_import_end end_offset
124
+ }
125
+ in ts_identifier {
126
+ let restriction_import_source parent.restriction_import_source
127
+ let restriction_import_type parent.restriction_import_type
128
+ let restriction_import_all_type parent.restriction_import_all_type
129
+ let restriction_import_name if is ts_node.namespace then "*" else "default"
130
+ let restriction_import_item true
131
+ let restriction_import_offset if is ts_node.namespace then parent.restriction_import_star_offset else offset
132
+ let restriction_import_end end_offset
133
+ }
134
+ @macro restriction_import_info {
135
+ let info if type_of(restriction_import_source) == "str" then {
136
+ source: restriction_import_source,
137
+ name: restriction_import_name,
138
+ item: restriction_import_item,
139
+ type: restriction_import_type,
140
+ all_type: restriction_import_all_type,
141
+ offset: restriction_import_offset,
142
+ end: restriction_import_end,
143
+ parents: restriction_import_parents(restriction_import_source)
144
+ } else null
145
+ }
146
+ in ts_import, ts_export, ts_import_equals rule es-no-restricted-imports {
147
+ @restriction_import_options
148
+ when len(paths) > 0
149
+ @restriction_import_info
150
+ @each entry in paths
151
+ when info != null && entry.name == info.source && entry.importNames == null && entry.allowImportNames == null && !(entry.allowTypeImports && info.all_type)
152
+ warn "'{info.source}' import is restricted from being used." @restriction_import_custom(entry)
153
+ }
154
+ in ts_import_specifier, ts_import.default_import, ts_import.namespace, ts_export_specifier, ts_export rule es-no-restricted-imports:names {
155
+ @restriction_import_options
156
+ when len(paths) > 0
157
+ @restriction_import_info
158
+ when info.item && info != null
159
+ @each entry in paths
160
+ when entry.name == info.source && !(entry.allowTypeImports && (info.type || info.all_type)) && (entry.importNames != null && (info.name == "*" || contains(entry.importNames, info.name)) || entry.allowImportNames != null && !contains(entry.allowImportNames, info.name))
161
+ report at { start: info.offset, end: info.end }
162
+ warn (if info.name == "*" then "* import is invalid because " (if entry.allowImportNames != null then "only " @restriction_import_list(entry.allowImportNames) " from '{info.source}' " @restriction_import_plural(entry.allowImportNames) " allowed." else @restriction_import_list(entry.importNames) " from '{info.source}' " @restriction_import_plural(entry.importNames) " restricted.") else "'{info.name}' import from '{info.source}' is restricted" (if entry.allowImportNames != null then " because only " @restriction_import_list(entry.allowImportNames) " " @restriction_import_plural(entry.allowImportNames) " allowed." else ".")) @restriction_import_custom(entry)
163
+ }
164
+ in ts_import, ts_export, ts_import_equals rule es-no-restricted-imports:patterns {
165
+ @restriction_import_options
166
+ when len(patterns) > 0
167
+ @restriction_import_info
168
+ when info != null
169
+ @each entry in patterns
170
+ when @restriction_import_pattern_matches(entry) && !(entry.allowTypeImports && info.all_type) && entry.importNames == null && entry.allowImportNames == null && entry.importNamePattern == null && entry.allowImportNamePattern == null
171
+ warn "'{info.source}' import is restricted from being used by a pattern." @restriction_import_custom(entry)
172
+ }
173
+ in ts_import_specifier, ts_import.default_import, ts_import.namespace, ts_export_specifier, ts_export rule es-no-restricted-imports:pattern-names {
174
+ @restriction_import_options
175
+ when len(patterns) > 0
176
+ @restriction_import_info
177
+ when info.item && info != null
178
+ @each entry in patterns
179
+ when @restriction_import_pattern_matches(entry) && !(entry.allowTypeImports && (info.type || info.all_type)) && (entry.importNames != null && (info.name == "*" || contains(entry.importNames, info.name)) || entry.importNamePattern != null && (info.name == "*" || js_re_test(info.name, entry.importNamePattern, "u")) || entry.allowImportNames != null && !contains(entry.allowImportNames, info.name) || entry.allowImportNamePattern != null && (info.name == "*" || !js_re_test(info.name, entry.allowImportNamePattern, "u")))
180
+ report at { start: info.offset, end: info.end }
181
+ warn (if info.name == "*" then "* import is invalid because " (if entry.allowImportNames != null then "only " @restriction_import_list(entry.allowImportNames) " from '{info.source}' " @restriction_import_plural(entry.allowImportNames) " allowed." else if entry.allowImportNamePattern != null then "only imports that match the pattern '/{entry.allowImportNamePattern}/u' from '{info.source}' are allowed." else if entry.importNamePattern != null && entry.importNames == null then "import name matching '/{entry.importNamePattern}/u' pattern from '{info.source}' is restricted from being used." else @restriction_import_list(entry.importNames) " from '{info.source}' " @restriction_import_plural(entry.importNames) " restricted from being used by a pattern.") else "'{info.name}' import from '{info.source}' is restricted" (if entry.allowImportNames != null then " because only " @restriction_import_list(entry.allowImportNames) " " @restriction_import_plural(entry.allowImportNames) " allowed." else if entry.allowImportNamePattern != null then " because only imports that match the pattern '/{entry.allowImportNamePattern}/u' are allowed from '{info.source}'." else " from being used by a pattern.")) @restriction_import_custom(entry)
182
+ }
183
+
184
+ in ts_identifier let restriction_numeric_identifier true
185
+ in ts_parenthesized {
186
+ let restriction_numeric_identifier expression.restriction_numeric_identifier == true
187
+ let restriction_numeric_parse_int expression.restriction_numeric_parse_int == true
188
+ }
189
+ in ts_identifier let restriction_numeric_parse_int text == "parseInt"
190
+ in ts_member, ts_index let restriction_numeric_parse_int object == "Number" && static_name == "parseInt"
191
+ in ts_call let restriction_numeric_radix callee.restriction_numeric_parse_int == true
192
+ in ts_declarator let restriction_numeric_const parent.kind == "const"
193
+ in ts_assignment {
194
+ let restriction_numeric_default parent is ts_array && parent.flow_destructuring_target == true
195
+ let restriction_numeric_object left.restriction_numeric_identifier != true
196
+ }
197
+ in ts_node {
198
+ let restriction_numeric_type_owner if parent is (ts_union, ts_intersection, ts_parenthesized_type) then parent.restriction_numeric_type_owner else {alias: parent is ts_type_alias, indexed: parent is ts_indexed_access}
199
+ let restriction_numeric_parent if parent is ts_parenthesized then parent.restriction_numeric_parent else {
200
+ default: is (ts_assignment_pattern.right, ts_parameter.init) || is ts_assignment.right && parent.restriction_numeric_default,
201
+ class_field: is ts_class_property.init,
202
+ readonly: is ts_class_property.init && contains(parent.modifier, "readonly"),
203
+ enum: parent is ts_enum_member,
204
+ literal_type: parent is ts_literal_type && parent.restriction_numeric_type_owner.alias,
205
+ type_index: parent.restriction_numeric_type_owner.indexed == true,
206
+ declaration: parent is ts_declarator,
207
+ constant: parent.restriction_numeric_const == true,
208
+ object: parent is (ts_object, ts_property, ts_method) || parent is ts_assignment && parent.restriction_numeric_object,
209
+ radix: is ts_call.argument && index == 1 && parent.restriction_numeric_radix,
210
+ jsx: parent is ts_jsx_expression,
211
+ array_index: is ts_index.index
212
+ }
213
+ let restriction_numeric_full if parent is ts_parenthesized then parent.restriction_numeric_full else if parent is ts_unary && contains(["-", "+"], parent.operator) then {sign: parent.operator, start: parent.offset, context: parent.restriction_numeric_parent} else {sign: "", start: offset, context: restriction_numeric_parent}
214
+ }
215
+ in ts_number, ts_bigint {
216
+ let restriction_numeric_signed_type parent is ts_literal_type && starts_with(parent.text, "-")
217
+ let restriction_numeric_sign if restriction_numeric_full.sign != "" then restriction_numeric_full.sign else if restriction_numeric_signed_type then "-" else ""
218
+ let restriction_numeric_value if is ts_bigint then (if restriction_numeric_sign == "-" && literal_value.value != "0" then "-" else "") literal_value.value "n" else num(literal_value.value) * (if restriction_numeric_sign == "-" then -1 else 1)
219
+ let restriction_numeric_offset if restriction_numeric_signed_type then parent.offset else restriction_numeric_full.start
220
+ let restriction_numeric_ts_offset if restriction_numeric_sign == "+" then offset else restriction_numeric_offset
221
+ let restriction_numeric_raw restriction_numeric_sign text
222
+ let restriction_numeric_ts_raw if restriction_numeric_sign == "+" then text else restriction_numeric_raw
223
+ let restriction_numeric_array_index restriction_numeric_full.context.array_index && js_compare(literal_value, 0) >= 0 && js_compare(literal_value, 4294967295) < 0 && (is ts_bigint || restriction_numeric_value == round(restriction_numeric_value)) && (restriction_numeric_sign != "-" || num(literal_value.value) == 0)
224
+ let no_magic_numbers_checked !(restriction_numeric_full.context.object || restriction_numeric_full.context.radix || restriction_numeric_full.context.jsx)
225
+ }
226
+ in ts_number, ts_bigint rule es-no-magic-numbers {
227
+ option enforceConst boolean = false "require const for named number declarations"
228
+ option ignore (number | string)[] = [] "numbers and bigint strings to allow"
229
+ let info restriction_numeric_full.context
230
+ let typescript_specific re_test(file, /\.[cm]?tsx?$/) && (info.enum || info.literal_type || info.type_index || info.readonly)
231
+ when no_magic_numbers_checked
232
+ when !any(ignore, v => type_of(v) == type_of(restriction_numeric_value) && v == restriction_numeric_value)
233
+ when !info.declaration || enforceConst && !info.constant
234
+ report at {start: if typescript_specific then restriction_numeric_ts_offset else restriction_numeric_offset, end: end_offset}
235
+ warn if info.declaration then "Number constants declarations must use 'const'." else "No magic number: {if typescript_specific then restriction_numeric_ts_raw else restriction_numeric_raw}."
236
+ }
237
+
238
+ in ts_import_specifier let restriction_declaration_local local.nonempty ?? imported
239
+ in ts_export_specifier let restriction_declaration_local local
240
+ in ts_import {
241
+ let restriction_declaration_export false
242
+ let restriction_declaration_category if count(specifier) > 0 then "named" else if namespace != null then "namespace" else if default_import != null then "default" else "side"
243
+ let restriction_declaration_count count(specifier) + (if namespace != null then 1 else 0) + (if default_import != null then 1 else 0)
244
+ let restriction_declaration_syntax if restriction_declaration_count == 0 then "none" else if namespace != null && default_import == null then "all" else if restriction_declaration_count == 1 then "single" else "multiple"
245
+ let restriction_declaration_local default_import.nonempty ?? namespace.nonempty ?? { value [] specifier.restriction_declaration_local; first; nonempty } ?? ""
246
+ }
247
+ in ts_export {
248
+ let restriction_declaration_export true
249
+ let restriction_declaration_category if count(specifier) > 0 then "named" else if namespace != null then "namespace" else "all"
250
+ let restriction_declaration_count count(specifier) + (if namespace != null then 1 else 0)
251
+ let restriction_declaration_syntax if restriction_declaration_count == 0 then "none" else if namespace != null then "all" else if restriction_declaration_count == 1 then "single" else "multiple"
252
+ let restriction_declaration_local namespace.nonempty ?? { value [] specifier.restriction_declaration_local; first; nonempty } ?? ""
253
+ }
254
+ in ts_import, ts_export let restriction_declaration_entry if from == null then null else {
255
+ node: node,
256
+ offset: offset, end: end_offset, module: from.static_name.nonempty ?? "",
257
+ export: restriction_declaration_export, type_only: kind == "type",
258
+ category: restriction_declaration_category, syntax: restriction_declaration_syntax,
259
+ local: restriction_declaration_local
260
+ }
261
+ group restriction_declaration ts_import, ts_export
262
+ in ts_program let restriction_declarations {
263
+ value descendants(restriction_declaration)
264
+ map (declaration) declaration.restriction_declaration_entry
265
+ filter (entry) entry != null
266
+ }
267
+ in ts_import let sort_imports_rank if restriction_declaration_syntax == "none" then 0 else if restriction_declaration_syntax == "all" then 1 else if restriction_declaration_syntax == "multiple" then 2 else 3
268
+ fn restriction_sorted_items(items, ordered) = {
269
+ value final_item: last(items)
270
+ value items
271
+ zip ordered (original, replacement) {
272
+ value replacement.text (if original != final_item then
273
+ byte_slice(original.parent.text, original.end_offset - original.parent.offset, original.next.offset - original.parent.offset)
274
+ else "")
275
+ }
276
+ join ""
277
+ }
278
+ fn restriction_sorted_imports(members, insensitive) = {
279
+ value ordered: {
280
+ value members
281
+ sort_by (item) if insensitive then lower(item.restriction_declaration_local) else item.restriction_declaration_local
282
+ }
283
+ value restriction_sorted_items(members, ordered)
284
+ }
285
+ @macro restriction_sort_import_options {
286
+ option ignoreCase boolean = false "ignore letter case"
287
+ option allowSeparatedGroups boolean = false "reset declaration order after blank lines"
288
+ }
289
+ in ts_import rule es-sort-imports:declarations {
290
+ @restriction_sort_import_options
291
+ let preceding {
292
+ value ts_program.restriction_declarations
293
+ filter (entry) !entry.export && entry.offset < offset
294
+ fold null (nearest, entry) if nearest == null || entry.offset > nearest.offset then entry else nearest
295
+ }
296
+ let separated preceding != null && { value source; byte_slice preceding.end offset; re_count /\n/ } > 1
297
+ when restriction_declaration_entry != null && preceding != null && !(allowSeparatedGroups && separated)
298
+ when restriction_declaration_syntax != preceding.syntax && sort_imports_rank < preceding.node.sort_imports_rank || restriction_declaration_syntax == preceding.syntax && compare_text(restriction_declaration_local, preceding.local, false, ignoreCase) < 0
299
+ warn if restriction_declaration_syntax != preceding.syntax then "Expected '{restriction_declaration_syntax}' syntax before '{preceding.syntax}' syntax." else "Imports should be sorted alphabetically."
300
+ }
301
+ in ts_import_specifier rule es-sort-imports:members {
302
+ @restriction_sort_import_options
303
+ let unsorted previous != null && compare_text(previous.restriction_declaration_local, restriction_declaration_local, false, ignoreCase) > 0
304
+ let encountered previous.encountered == true || unsorted
305
+ when parent is ts_import && parent.restriction_declaration_entry != null && unsorted && previous.encountered != true
306
+ report at local.node ?? imported.node
307
+ let members parent.specifier.nodes
308
+ fix at {start: first(members).offset, end: last(members).end_offset} if {
309
+ value members
310
+ all (member) !member.space_before.comments && !member.space_after.comments
311
+ } as restriction_sorted_imports(members, ignoreCase)
312
+ warn "Member '{restriction_declaration_local}' of the import declaration should be sorted alphabetically."
313
+ }
314
+ fn restriction_import_mergeable(a, b) = !(a.type_only && b.type_only && (a.category == "default" && b.category == "named" || a.category == "named" && b.category == "default")) && !(a.category == "all" && contains(["named", "namespace", "default"], b.category) || b.category == "all" && contains(["named", "namespace", "default"], a.category) || a.category == "namespace" && b.category == "named" || a.category == "named" && b.category == "namespace")
315
+ group duplicate_imports_checked ts_import
316
+ @macro restriction_duplicate_import_body {
317
+ option allowSeparateTypeImports boolean = false "allow separate type and value imports"
318
+ let own restriction_declaration_entry
319
+ let module trim(own.module)
320
+ when own != null && module != ""
321
+ when any(ts_program.restriction_declarations, entry => entry.node is duplicate_imports_checked && entry.offset < offset && entry.export == previous_export && trim(entry.module) == module && (!allowSeparateTypeImports || entry.type_only == own.type_only) && restriction_import_mergeable(own, entry))
322
+ warn "'{module}' {if own.export then 'export' else 'import'} is duplicated{if own.export == previous_export then '' else if own.export then ' as import' else ' as export'}."
323
+ }
324
+ in duplicate_imports_checked rule es-no-duplicate-imports:imports {
325
+ let previous_export false
326
+ @restriction_duplicate_import_body
327
+ }
328
+ in duplicate_imports_checked rule es-no-duplicate-imports:exports {
329
+ let previous_export true
330
+ @restriction_duplicate_import_body
331
+ }
332
+
333
+ in ts_variable_declaration let restriction_sort_bindings {
334
+ value declaration.nodes
335
+ filter (item) item.name is ts_identifier
336
+ }
337
+ fn restriction_sorted_variables(bindings, insensitive) = {
338
+ value ordered: {
339
+ value bindings
340
+ sort_by (item) if insensitive then lower(item.name) else item.name.text
341
+ }
342
+ value restriction_sorted_items(bindings, ordered)
343
+ }
344
+ in ts_declarator rule es-sort-vars {
345
+ option ignoreCase boolean = false "ignore letter case"
346
+ let simple_name name is ts_identifier
347
+ let greatest if !simple_name then previous.greatest else if previous.greatest == null || compare_text(name, previous.greatest, false, ignoreCase) >= 0 then name else previous.greatest
348
+ when simple_name && previous.greatest != null && compare_text(name, previous.greatest, false, ignoreCase) < 0
349
+ report at name.node
350
+ warn "Variables within the same declaration block should be sorted alphabetically."
351
+ let bindings parent.restriction_sort_bindings
352
+ let first_binding first(bindings)
353
+ let last_binding last(bindings)
354
+ # Crossing a destructuring initializer can change reads and evaluation order.
355
+ fix at {start: first_binding.offset, end: last_binding.end_offset} if {
356
+ value bindings
357
+ all (item, i) (item.init == null || unparenthesised_node(item.init.node) is (ts_string, ts_number, ts_boolean, ts_null, ts_bigint, ts_regex))
358
+ && item.index == first_binding.index + i
359
+ } as restriction_sorted_variables(bindings, ignoreCase)
360
+ }
361
+ in ts_identifier let restriction_sort_key_name text
362
+ in ts_computed_key let restriction_sort_key_name if expression is ts_identifier then expression else null
363
+ in ts_property, ts_method {
364
+ let restriction_sort_computed key is ts_computed_key
365
+ let restriction_sort_key_fallback key.restriction_sort_key_name
366
+ }
367
+ in ts_object let restriction_sort_key_count count(property)
368
+ in ts_property, ts_method, ts_spread rule es-sort-keys {
369
+ option mode "asc" | "desc" = "asc" "key order"
370
+ option caseSensitive boolean = true "compare letter case"
371
+ option natural boolean = false "compare digit sequences numerically"
372
+ option minKeys int(2..) = 2 "minimum object properties"
373
+ option allowLineSeparatedGroups boolean = false "reset sorting after blank lines"
374
+ option ignoreComputedKeys boolean = false "reset sorting at computed keys"
375
+ let reset is ts_spread || ignoreComputedKeys && restriction_sort_computed
376
+ let name if reset then null else if type_of(static_name) == "str" then static_name else restriction_sort_key_fallback
377
+ let carried if reset then null else if type_of(name) == "str" then name else previous.carried
378
+ let blank_gap previous.space_after.blank_gaps > 0 || space_before.blank_gaps > 0
379
+ let pending_blank if reset then previous.pending_blank else allowLineSeparatedGroups && (previous.pending_blank == true || blank_gap) && type_of(name) != "str"
380
+ let separated previous.pending_blank == true || blank_gap
381
+ let order compare_text(previous.carried, name, natural, !caseSensitive)
382
+ when parent is ts_object && parent.restriction_sort_key_count >= minKeys
383
+ when !reset && type_of(name) == "str" && type_of(previous.carried) == "str" && !(allowLineSeparatedGroups && separated)
384
+ when if mode == "asc" then order > 0 else order < 0
385
+ report at key.node
386
+ warn "Expected object keys to be in {if natural then 'natural ' else ''}{if caseSensitive then '' else 'insensitive '}{mode}ending order. '{name}' should be before '{previous.carried}'."
387
+ }
388
+
389
+ in ts_conditional rule es-no-ternary warn "Ternary operator used."
390
+
391
+ in ts_conditional let restriction_ternary true
392
+ in ts_parenthesized let restriction_ternary expression.restriction_ternary == true
393
+ in ts_conditional rule es-no-nested-ternary {
394
+ when consequent.restriction_ternary == true || alternate.restriction_ternary == true
395
+ warn "Do not nest ternary expressions."
396
+ }
397
+ in ts_continue rule es-no-continue warn "Unexpected use of continue statement."
398
+ in ts_parenthesized, ts_unary let restriction_statement_position if parent is ts_parenthesized then parent.restriction_statement_position else parent is ts_expression_statement
399
+ in ts_parenthesized, ts_sequence, ts_update let restriction_for_update if parent is (ts_parenthesized, ts_sequence) then parent.restriction_for_update else is ts_for.update
400
+ in ts_unary let no_void_checked true
401
+ in ts_unary rule es-no-void {
402
+ when operator == "void" && no_void_checked
403
+ warn "Expected 'undefined' and instead saw 'void'."
404
+ }
405
+ in ts_update let no_plusplus_checked true
406
+ in ts_update rule es-no-plusplus {
407
+ when no_plusplus_checked
408
+ warn "Unary operator '{operator}' used."
409
+ }
410
+ in ts_binary, ts_assignment let restriction_bitwise_zero_or operator == "|" && right.literal_value.value == "0"
411
+ in ts_binary, ts_assignment, ts_unary let no_bitwise_checked true
412
+ in ts_binary, ts_assignment rule es-no-bitwise:binary {
413
+ when contains(["^", "|", "&", "<<", ">>", ">>>", "^=", "|=", "&=", "<<=", ">>=", ">>>=", "~"], operator)
414
+ when no_bitwise_checked
415
+ warn "Unexpected use of '{operator}'."
416
+ }
417
+ in ts_unary rule es-no-bitwise:unary {
418
+ when operator == "~" && no_bitwise_checked
419
+ warn "Unexpected use of '{operator}'."
420
+ }
421
+ in ts_identifier rule es-no-undefined {
422
+ when text == "undefined" && (declaration_kind != null || resolved == "local" || global == "readonly" || global == "writable")
423
+ warn "Unexpected use of undefined."
424
+ }
425
+ in ts_identifier {
426
+ let restriction_window_object resolved == "global" && (text == "window" || text == "globalThis" && restriction_name_external)
427
+ let restriction_called_name if resolved == "global" then text else null
428
+ }
429
+ in ts_this let restriction_window_object ts_function == null && ts_arrow == null && ts_method == null && ts_static_block == null
430
+ in ts_parenthesized {
431
+ let restriction_window_object expression.restriction_window_object == true
432
+ let restriction_called_name expression.restriction_called_name
433
+ }
434
+ in ts_member, ts_index let restriction_called_name if object.restriction_window_object == true then static_name else null
435
+ in ts_call rule es-no-alert {
436
+ let name callee.restriction_called_name
437
+ when contains(["alert", "confirm", "prompt"], name)
438
+ warn "Unexpected {name}."
439
+ }
440
+ in ts_labeled let restriction_target body.node
441
+ in ts_labeled let restriction_label_entry {name: label, target: restriction_target}
442
+ in ts_statement, ts_case let restriction_label_targets (parent.restriction_label_targets.nonempty ?? []) (if is ts_labeled then [restriction_label_entry] else [])
443
+ in ts_break, ts_continue let restriction_target (find_last(restriction_label_targets, target => target.name == label)).target
444
+ in ts_labeled, ts_break, ts_continue let no_labels_checked true
445
+ in ts_labeled rule es-no-labels {
446
+ when no_labels_checked
447
+ warn "Unexpected labeled statement."
448
+ }
449
+ in ts_break rule es-no-labels:break {
450
+ when label != null
451
+ when no_labels_checked
452
+ warn "Unexpected label in break statement."
453
+ }
454
+ in ts_continue rule es-no-labels:continue {
455
+ when label != null
456
+ when no_labels_checked
457
+ warn "Unexpected label in continue statement."
458
+ }
459
+
460
+ @macro restriction_function_name ({ value function_name.nonempty ?? "function"; slice 0 1; upper } { value function_name.nonempty ?? "function"; slice 1 })
461
+ group restriction_flow_control ts_conditional, ts_logical, ts_for, ts_for_in, ts_for_of, ts_if, ts_while, ts_do_while, ts_assignment_pattern
462
+ in ts_parameter let restriction_parameter_branch init != null
463
+ in ts_try let restriction_catch_branch handler != null
464
+ in ts_assignment let restriction_assignment_branch contains(["&&=", "||=", "??="], operator)
465
+ in ts_member, ts_index, ts_call let restriction_optional_branch optional != null
466
+ in ts_case let restriction_case_branch test != null
467
+ in ts_block let restriction_local_statements count(body)
468
+ in ts_function_like, ts_static_block let max_nested_callbacks_counted restriction_call_argument
469
+ in ts_class let max_classes_counted true
470
+ in ts_program let restriction_classes {
471
+ value descendants(ts_class)
472
+ filter (item) item.max_classes_counted == true
473
+ count
474
+ }
475
+ in ts_program let restriction_declared_classes {
476
+ value descendants(ts_class)
477
+ filter (item) item.max_classes_counted == true && item.parent is (ts_program, ts_block, ts_export)
478
+ count
479
+ }
480
+ in ts_program let restriction_program_span if count(body.nodes) == 0 then { start: offset, end: offset } else { start: first(body.nodes).offset, end: last(body.nodes).end_offset }
481
+ fn restriction_branch_total(branch, extra, child_totals) = (if branch then 1 else 0) + (if extra == true then 1 else 0) + sum(child_totals)
482
+ in ts_node {
483
+ let restriction_metric_boundary is (ts_function_like, ts_static_block) || is ts_class_property.init
484
+ let restriction_flow_branch is restriction_flow_control || (if is ts_parameter then restriction_parameter_branch == true else if is ts_try then restriction_catch_branch == true else if is ts_assignment then restriction_assignment_branch == true else if is (ts_member, ts_index, ts_call) then restriction_optional_branch == true else false)
485
+ let restriction_statement_owner if parent.node == null then null else if parent.restriction_metric_boundary then parent.node else parent.restriction_statement_owner
486
+ let restriction_subtree_classic restriction_branch_total(restriction_flow_branch, restriction_case_branch, children.restriction_classic_contribution)
487
+ let restriction_classic_contribution if restriction_metric_boundary then 0 else restriction_branch_total(restriction_flow_branch, restriction_case_branch, children.restriction_classic_contribution)
488
+ let restriction_subtree_modified restriction_branch_total(restriction_flow_branch, is ts_switch, children.restriction_modified_contribution)
489
+ let restriction_modified_contribution if restriction_metric_boundary then 0 else restriction_branch_total(restriction_flow_branch, is ts_switch, children.restriction_modified_contribution)
490
+ let restriction_function_depth (parent.restriction_function_depth.nonempty ?? 0) + (if parent is (ts_function_like, ts_static_block) then 1 else 0)
491
+ let restriction_top_functions (if is ts_function_like && restriction_function_depth == 0 then 1 else 0) + sum(children.restriction_top_functions)
492
+ let restriction_file_top_functions if parent.node == null then restriction_top_functions else (parent.restriction_file_top_functions.nonempty ?? 0)
493
+ let restriction_call_argument if parent is ts_parenthesized then parent.restriction_call_argument else is ts_call.argument
494
+ let restriction_constructor_argument if parent is ts_parenthesized then parent.restriction_constructor_argument else is (ts_call.argument, ts_new.argument)
495
+ let restriction_iife if parent is ts_parenthesized then parent.restriction_iife else is ts_call.callee
496
+ let restriction_callbacks (parent.restriction_callbacks.nonempty ?? 0) + (if max_nested_callbacks_counted == true then 1 else 0)
497
+ }
498
+ group restriction_parameter_function ts_function, ts_arrow, ts_method, ts_function_type
499
+ in ts_parameter {
500
+ let restriction_this_parameter name == "this"
501
+ let restriction_void_this_parameter name == "this" && type == "void"
502
+ let max_params_counted !restriction_void_this_parameter || node != first(parent.parameter.nodes)
503
+ }
504
+ in restriction_parameter_function rule es-max-params {
505
+ option max int(0..) = 3 "maximum parameters"
506
+ let amount { value parameter.nodes; filter (p) p.max_params_counted.nonempty ?? true; count }
507
+ when amount > max
508
+ report at restriction_function_head(node)
509
+ warn "{@restriction_function_name} has too many parameters ({amount}). Maximum allowed is {max}."
510
+ }
511
+ in ts_function_like {
512
+ let restriction_subtree_statements {
513
+ value owner: node
514
+ value descendants(ts_block)
515
+ filter (block) !block.restriction_metric_boundary && block.restriction_statement_owner == owner
516
+ map (block) block.restriction_local_statements
517
+ sum
518
+ }
519
+ let max_statements_checked true
520
+ }
521
+ in ts_function_like rule es-max-statements {
522
+ option max int(0..) = 10 "maximum statements"
523
+ option ignoreTopLevelFunctions boolean = false "skip top-level functions unless the file has only one"
524
+ when restriction_subtree_statements > max
525
+ when max_statements_checked
526
+ when !(ignoreTopLevelFunctions && restriction_function_depth == 0 && restriction_file_top_functions == 1)
527
+ report at restriction_function_head(node)
528
+ warn "{@restriction_function_name} has too many statements ({restriction_subtree_statements}). Maximum allowed is {max}."
529
+ }
530
+ in ts_function_like rule es-complexity {
531
+ option max int(0..) = 20 "maximum cyclomatic complexity"
532
+ option variant "classic" | "modified" = "classic" "how switches contribute"
533
+ let amount 1 + (if variant == "classic" then restriction_subtree_classic else restriction_subtree_modified)
534
+ when amount > max
535
+ report at restriction_function_head(node)
536
+ warn "{@restriction_function_name} has a complexity of {amount}. Maximum allowed is {max}."
537
+ }
538
+ in ts_static_block, ts_class_property.init rule es-complexity:implicit {
539
+ option max int(0..) = 20 "maximum cyclomatic complexity"
540
+ option variant "classic" | "modified" = "classic" "how switches contribute"
541
+ let amount if is (ts_function, ts_arrow) then 1 else 1 + (if variant == "classic" then restriction_subtree_classic else restriction_subtree_modified)
542
+ when amount > max
543
+ report at if is ts_static_block then { start: first_token.start, end: first_token.end } else node
544
+ warn "{if is ts_static_block then 'Class static block' else 'Class field initializer'} has a complexity of {amount}. Maximum allowed is {max}."
545
+ }
546
+ group restriction_depth_control ts_if, ts_switch, ts_try, ts_do_while, ts_while, ts_with, ts_for, ts_for_in, ts_for_of
547
+ in ts_statement, ts_arrow, ts_method, ts_static_block, ts_case let restriction_nesting if is (ts_function_like, ts_static_block) then 0 else (parent.restriction_nesting.nonempty ?? 0) + (if is restriction_depth_control && !(is ts_if && is ts_if.alternate) then 1 else 0)
548
+ in restriction_depth_control rule es-max-depth {
549
+ option max int(0..) = 4 "maximum control-flow nesting"
550
+ when !(is ts_if && is ts_if.alternate)
551
+ when restriction_nesting > max
552
+ report at { start: first_token.start, end: first_token.end }
553
+ warn "Blocks are nested too deeply ({restriction_nesting}). Maximum allowed is {max}."
554
+ }
555
+ in ts_function, ts_arrow rule es-max-nested-callbacks {
556
+ option max int(0..) = 10 "maximum callback nesting"
557
+ let amount restriction_callbacks
558
+ when max_nested_callbacks_counted && amount > max
559
+ report at function_warning_site(node)
560
+ warn "Too many nested callbacks ({amount}). Maximum allowed is {max}."
561
+ }
562
+ in ts_program rule es-max-classes-per-file {
563
+ option max int(1..) = 1 "maximum classes"
564
+ option ignoreExpressions boolean = false "count only class declarations"
565
+ let amount if ignoreExpressions then restriction_declared_classes else restriction_classes
566
+ when amount > max
567
+ report at { start: restriction_program_span.start, end: restriction_program_span.end }
568
+ warn "File has too many classes ({amount}). Maximum allowed is {max}."
569
+ }
570
+ fn restriction_line_previous(items, item) = {
571
+ value result: index_of(items, item) - 1
572
+ value items
573
+ index_of: index_of item
574
+ value slice(items, result, index_of)
575
+ last
576
+ }
577
+ fn restriction_line_comment_gap(source, items, item, start, end) = {
578
+ value default: { value restriction_line_previous(items, item).end_offset; nonempty } ?? start
579
+ value max: max(start, default)
580
+ value end
581
+ min: min item.offset
582
+ value max_2: max(start, min)
583
+ value byte_slice(source, max, max_2)
584
+ }
585
+ in ts_line {
586
+ let restriction_line_comments overlapping(ts_comment)
587
+ let restriction_line_blank trim(text) == ""
588
+ let restriction_line_comment count(restriction_line_comments) > 0 && all(restriction_line_comments, item => { value source; restriction_line_comment_gap restriction_line_comments item offset end_offset; trim } == "") && {
589
+ value end_offset_2: last(restriction_line_comments).end_offset
590
+ value max: max(offset, end_offset_2)
591
+ value min: min(end_offset, max)
592
+ value byte_slice(source, min, end_offset)
593
+ trim
594
+ } == ""
595
+ }
596
+ in ts_program, ts_function_like let restriction_line_rows overlapping(ts_line)
597
+ fn restriction_open_paren(n) = first(overlapping(ts_open_paren, n.offset, n.end_offset)).offset
598
+ fn restriction_function_type_arrow(n) = {
599
+ value from: { value last(n.parameter.nodes).end_offset; nonempty } ?? n.offset
600
+ value arrow: { value overlapping(ts_punctuator, from, n.return_type.offset); filter (t) t.text == "=>"; first }
601
+ value { start: arrow.offset, end: arrow.end_offset }
602
+ }
603
+ # ESLint reports a function's head: the callable start through its parameter paren, or the arrow token for a function type.
604
+ fn restriction_function_head(n) = when {
605
+ n.parent is (ts_property, ts_class_property) then { start: n.parent.node.offset, end: restriction_open_paren(n) }
606
+ n is ts_function_type then restriction_function_type_arrow(n)
607
+ n is ts_arrow then n.operator.node
608
+ else then { start: n.offset, end: restriction_open_paren(n) }
609
+ }
610
+ in ts_program rule es-max-lines {
611
+ option max int(0..) = 300 "maximum lines"
612
+ option skipComments boolean = false "exclude comment-only lines"
613
+ option skipBlankLines boolean = false "exclude blank lines"
614
+ let lines filter(restriction_line_rows, l => !(skipComments && l.restriction_line_comment || skipBlankLines && l.restriction_line_blank))
615
+ let amount if source == "" && !skipBlankLines then 1 else len(lines)
616
+ when amount > max
617
+ let exceed_line { value { value lines; slice max; first }; nonempty } ?? node
618
+ report at { start: position(exceed_line.offset).js_line_start, end: byte_len(source) }
619
+ warn "File has too many lines ({amount}). Maximum allowed is {max}."
620
+ }
621
+ in ts_function_like let max_lines_per_function_checked !restriction_iife
622
+ in ts_function_like rule es-max-lines-per-function {
623
+ option max int(0..) = 50 "maximum function lines"
624
+ option skipComments boolean = false "exclude comment-only lines"
625
+ option skipBlankLines boolean = false "exclude blank lines"
626
+ let lines filter(restriction_line_rows, l => !(skipComments && l.restriction_line_comment || skipBlankLines && l.restriction_line_blank))
627
+ when max_lines_per_function_checked
628
+ when len(lines) > max
629
+ report at restriction_function_head(node)
630
+ warn "{@restriction_function_name} has too many lines ({len(lines)}). Maximum allowed is {max}."
631
+ }
632
+
633
+ in ts_object {
634
+ let restriction_import_options parent is ts_import_call
635
+ let restriction_import_attributes parent.restriction_import_attribute_group == true
636
+ }
637
+ in ts_property, ts_pattern_property {
638
+ let restriction_import_options_key parent.restriction_import_options == true || parent.restriction_import_attributes == true
639
+ let restriction_import_attribute_group parent.restriction_import_options == true && contains(["with", "assert"], static_name)
640
+ let restriction_name_pattern is ts_pattern_property || parent.flow_destructuring_target == true
641
+ let restriction_name_alias restriction_name_pattern && value != null && (value not ts_assignment_pattern || value.left != null)
642
+ let restriction_name_same value != null && key == (value.left.nonempty ?? value)
643
+ }
644
+ in ts_export_specifier {
645
+ let restriction_name_reexport parent.from != null
646
+ let restriction_name_export_alias exported != null
647
+ }
648
+ in ts_class let restriction_name_declaration parent is (ts_program, ts_block, ts_export)
649
+ in ts_member let restriction_name_mutation parent not ts_update && !(parent is ts_unary && parent.operator == "delete")
650
+ in ts_identifier let restriction_identifier_name text
651
+ in ts_private_name let restriction_identifier_name re_replace(text, /^#*/, "")
652
+ in ts_identifier, ts_private_name {
653
+ let restriction_name_external contains(["readonly", "writable"], global)
654
+ let restriction_name_attributes is ts_property.key && parent.restriction_import_options_key == true
655
+ let restriction_name_pattern parent.restriction_name_pattern == true
656
+ let restriction_name_alias is ts_node.key && parent.restriction_name_alias == true
657
+ let restriction_name_same parent.restriction_name_same == true
658
+ let restriction_name_assignment is ts_member.property && parent.write_site != null && parent.restriction_name_mutation
659
+ let restriction_name_renamed is ts_import_specifier.imported && parent.local != null || is ts_export_specifier.local && parent.restriction_name_export_alias && parent.restriction_name_reexport
660
+ let restriction_name_property is ts_node.key && (if restriction_name_pattern then !restriction_name_alias || restriction_name_same else parent is ts_property) || restriction_name_assignment
661
+ let restriction_name_length if parent is (ts_declarator, ts_parameter) then is (ts_declarator.name, ts_parameter.name) else if parent is ts_assignment_pattern then is ts_assignment_pattern.left else if restriction_name_pattern then (if is ts_node.key then !restriction_name_alias || restriction_name_same else !restriction_name_same) else if parent is ts_property then is ts_property.key else if parent is ts_import_specifier then is ts_import_specifier.local && (parent.imported.static_name.nonempty ?? parent.imported) != restriction_identifier_name else if parent is ts_import then is (ts_import.default_import, ts_import.namespace) else if parent is (ts_function, ts_arrow, ts_method, ts_class_property, ts_rest, ts_array_pattern) then true else if parent is ts_class then parent.restriction_name_declaration else if parent is ts_try then is ts_try.parameter else if parent is ts_member then restriction_name_assignment else false
662
+ }
663
+ in ts_identifier, ts_private_name rule es-id-denylist {
664
+ option names string[] = [] "restricted identifiers"
665
+ when len(names) > 0 && contains(names, restriction_identifier_name)
666
+ when (!restriction_name_external || is ts_property.key && !restriction_name_pattern) && !restriction_name_attributes && !restriction_name_renamed && !restriction_name_alias
667
+ when parent not (ts_call, ts_new, ts_meta_property) && !(is ts_member.property && !restriction_name_assignment)
668
+ warn "Identifier '{text}' is restricted."
669
+ }
670
+ in ts_identifier, ts_private_name let id_length_checked true
671
+ in ts_identifier, ts_private_name rule es-id-length {
672
+ option min int = 2 "minimum identifier length"
673
+ option max int | null = null "maximum identifier length"
674
+ option exceptions string[] = [] "names exempt from length limits"
675
+ option exceptionPatterns regex[] = [] "name patterns exempt from length limits"
676
+ let amount graphemes(restriction_identifier_name)
677
+ when restriction_name_length && !restriction_name_attributes
678
+ when id_length_checked
679
+ when !contains(exceptions, restriction_identifier_name) && !any(exceptionPatterns, p => re_test(restriction_identifier_name, p))
680
+ when amount < min || max != null && amount > max
681
+ warn if amount < min then "Identifier name '{text}' is too short (< {min})." else "Identifier name '{text}' is too long (> {max})."
682
+ }
683
+ in ts_identifier, ts_private_name {
684
+ let restriction_name_match_context if is ts_private_name then (if parent is ts_class_property then "field" else "always")
685
+ else if parent is ts_member then (if is ts_member.object || restriction_name_assignment then "property" else null)
686
+ else if restriction_name_pattern then (if restriction_name_alias then null else "destructuring")
687
+ else if is ts_property.key then "property"
688
+ else if parent is ts_assignment_pattern then (if is ts_assignment_pattern.left then "default_property" else null)
689
+ else if parent is ts_import_specifier then (if is ts_import_specifier.local || is ts_import_specifier.imported && parent.local == null then "import" else null)
690
+ else if parent is ts_import then (if is (ts_import.default_import, ts_import.namespace) then "import" else null)
691
+ else if parent is ts_class_property then "field"
692
+ else if parent is (ts_call, ts_new, ts_meta_property) then null
693
+ else if parent is (ts_function, ts_declarator) then "declaration" else "reference"
694
+ let id_match_checked contains(["always", "destructuring", "import", "declaration", "reference"], restriction_name_match_context)
695
+ }
696
+ in ts_identifier, ts_private_name rule es-id-match {
697
+ option mode regex = "^.+$" "required identifier pattern"
698
+ when !restriction_name_attributes && id_match_checked && !restriction_name_external
699
+ when !re_test(restriction_identifier_name, mode)
700
+ warn "Identifier '{text}' does not match the pattern '{mode}'."
701
+ }
702
+ schema restriction_global {name: string; message?: string}
703
+ # ESLint accepts bare names as well as {name, message} records.
704
+ fn restriction_global_entry(g) = if type_of(g) == "str" then {name: g, message: null} else g
705
+ @macro restriction_global_options {
706
+ option globals (restriction_global | string)[] = [] "restricted globals"
707
+ option checkGlobalObject boolean = false "check global-object properties"
708
+ option globalObjects string[] = [] "additional global-object names"
709
+ when len(globals) > 0
710
+ }
711
+ in ts_identifier rule es-no-restricted-globals {
712
+ @restriction_global_options
713
+ let matched find_last(globals, g => restriction_global_entry(g).name == text)
714
+ when resolved == "global" && access != "type" && matched != null
715
+ when parent not (ts_type_reference, ts_qualified_name, ts_type_query)
716
+ warn "Unexpected use of '{text}'." (if restriction_global_entry(matched).message != null && restriction_global_entry(matched).message != "" then " " restriction_global_entry(matched).message else "")
717
+ }
718
+ in ts_identifier let restriction_global_root if resolved == "global" && contains(["readonly", "writable"], global) then text else null
719
+ in ts_parenthesized let restriction_global_root expression.restriction_global_root
720
+ in ts_member, ts_index let restriction_global_root if static_name == object.restriction_global_root then object.restriction_global_root else null
721
+ in ts_member let restriction_global_property_report property.node
722
+ in ts_index let restriction_global_property_report index.offset
723
+ in ts_member, ts_index rule es-no-restricted-globals:member {
724
+ @restriction_global_options
725
+ let root object.restriction_global_root
726
+ let name static_name
727
+ let matched find_last(globals, g => restriction_global_entry(g).name == name)
728
+ when checkGlobalObject && root != null && type_of(name) == "str" && contains(["globalThis", "self", "window"] globalObjects, root) && matched != null
729
+ report at restriction_global_property_report
730
+ warn "Unexpected use of '{name}'." (if restriction_global_entry(matched).message != null && restriction_global_entry(matched).message != "" then " " restriction_global_entry(matched).message else "")
731
+ }
732
+ schema restriction_property {
733
+ object?: string
734
+ property?: string
735
+ allowObjects?: string[]
736
+ allowProperties?: string[]
737
+ message?: string
738
+ }
739
+ fn restriction_property_match(object, name, options) = {
740
+ value options
741
+ find_last (o) o.object != null && o.object == object && (o.property == name || o.property == null) && !{ value o.allowProperties.nonempty ?? []; contains name }
742
+ nonempty
743
+ } ?? find_last(options, o => o.object == null && o.property == name && !{ value o.allowObjects.nonempty ?? []; contains object })
744
+ in ts_identifier let restriction_object_name text
745
+ in ts_parenthesized let restriction_object_name expression.restriction_object_name
746
+ in ts_declarator, ts_parameter let restriction_pattern_object init.restriction_object_name
747
+ in ts_assignment, ts_assignment_pattern let restriction_pattern_object right.restriction_object_name
748
+ in ts_object_pattern, ts_object {
749
+ let restriction_property_object parent.restriction_pattern_object
750
+ let restriction_property_pattern is ts_object_pattern || flow_destructuring_target == true
751
+ }
752
+ @macro restriction_property_body {
753
+ option options restriction_property[] = [] "restricted object properties"
754
+ when len(options) > 0
755
+ let matched restriction_property_match(object_name, property_name, options)
756
+ when type_of(property_name) == "str" && matched != null
757
+ warn (if matched.object != null then "'{object_name}.{property_name}' is restricted from being used." (if matched.allowProperties != null then " Only these properties are allowed: {join(matched.allowProperties, ', ')}." else "") else "'{property_name}' is restricted from being used." (if matched.allowObjects != null then " Property '{property_name}' is only allowed on these objects: {join(matched.allowObjects, ', ')}." else "")) (if matched.message != null && matched.message != "" then " " matched.message else "")
758
+ }
759
+ in ts_member, ts_index rule es-no-restricted-properties {
760
+ let object_name object.restriction_object_name
761
+ let property_name static_name
762
+ @restriction_property_body
763
+ }
764
+ in ts_pattern_property, ts_property rule es-no-restricted-properties:patterns {
765
+ let object_name parent.restriction_property_object
766
+ let property_name static_name
767
+ when parent is (ts_object_pattern, ts_object) && parent.restriction_property_pattern
768
+ report at key.node
769
+ @restriction_property_body
770
+ }
771
+ @macro restriction_export_options {
772
+ option restrictedNamedExports string[] = [] "restricted exported names"
773
+ option restrictedNamedExportsPattern js_regex = "" "restricted exported-name pattern"
774
+ }
775
+ fn restriction_export_forbidden(name, names, pattern) = contains(names, name) || name != "default" && pattern != "" && js_re_test(name, pattern, "u")
776
+ in ts_export let restriction_export_form if kind == "default" then "direct" else null
777
+ in ts_export_specifier let restriction_export_form if parent.from == null then "named" else if (local.static_name.nonempty ?? local) == "default" then "defaultFrom" else "namedFrom"
778
+ in ts_identifier, ts_string let restriction_export_form if is ts_export.namespace then "namespaceFrom" else null
779
+ in ts_export, ts_export_specifier, ts_identifier, ts_string let no_restricted_default_export_checked false
780
+ in ts_variable_declaration let restriction_exported_declaration parent is ts_export && parent.kind != "default"
781
+ in ts_declarator let restriction_exported_declaration parent.restriction_exported_declaration == true
782
+ in ts_export rule es-no-restricted-exports:direct {
783
+ @restriction_export_options
784
+ when kind == "default" && no_restricted_default_export_checked
785
+ warn "Exporting 'default' is restricted."
786
+ }
787
+ in ts_identifier rule es-no-restricted-exports:declarations {
788
+ @restriction_export_options
789
+ when len(restrictedNamedExports) > 0 || restrictedNamedExportsPattern != ""
790
+ let export_name text
791
+ when (exported && declaration_kind != null || is ts_declarator.name && parent.restriction_exported_declaration) && ts_export != null && ts_export.kind != "default"
792
+ when restriction_export_forbidden(export_name, restrictedNamedExports, restrictedNamedExportsPattern)
793
+ warn "'{export_name}' is restricted from being used as an exported name."
794
+ }
795
+ in ts_export_specifier rule es-no-restricted-exports:names {
796
+ @restriction_export_options
797
+ let export_name if exported == null then (if local is ts_string then local.static_name else local) else (if exported is ts_string then exported.static_name else exported)
798
+ let restricted_name restriction_export_forbidden(export_name, restrictedNamedExports, restrictedNamedExportsPattern)
799
+ when restricted_name || export_name == "default" && no_restricted_default_export_checked
800
+ report at exported.node ?? local.node
801
+ warn if restricted_name then "'{export_name}' is restricted from being used as an exported name." else "Exporting 'default' is restricted."
802
+ }
803
+ in ts_export.namespace rule es-no-restricted-exports:namespace {
804
+ @restriction_export_options
805
+ let export_name if is ts_string then static_name else text
806
+ let restricted_name restriction_export_forbidden(export_name, restrictedNamedExports, restrictedNamedExportsPattern)
807
+ when restricted_name || export_name == "default" && no_restricted_default_export_checked
808
+ warn if restricted_name then "'{export_name}' is restricted from being used as an exported name." else "Exporting 'default' is restricted."
809
+ }
810
+
811
+ }