@inthedark/dxlint 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,43 +1,43 @@
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
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
4
  # operator bans.
5
5
 
6
6
  import .ts/restrictions/comments
7
7
  import .ts/helpers
8
8
 
9
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
- }
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
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
34
  value result: len(xs) - 1
35
35
  value slice(xs, 0, result)
36
36
  map (x) "'" x "'"
37
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 "")
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
41
  @macro restriction_import_glob(path, glob, sensitive) (!starts_with(glob, "#") && {
42
42
  value result: (if sensitive then "" else "(?i)") (if { value glob; trim_start "!"; contains "/" } then "^" else "(?:^|/)") {
43
43
  value glob
@@ -50,214 +50,214 @@ schema restriction_import_pattern {
50
50
  replace "\\?" "[^/]"
51
51
  } "(?:/|$)"
52
52
  value re_test(path, result)
53
- })
53
+ })
54
54
  @macro restriction_import_group(path, entry) ({
55
55
  value entry.group.nonempty ?? []
56
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)))
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
60
  fn restriction_import_parent(path) = {
61
61
  value path
62
62
  split: split "/"
63
63
  value result: { value path; split "/"; len } - 1
64
64
  value slice(split, 0, result)
65
65
  join "/"
66
- }
66
+ }
67
67
  fn restriction_import_parents(path) = {
68
68
  value path
69
69
  walk (current) if contains(current, "/") && restriction_import_parent(current) != "" then restriction_import_parent(current) else null
70
70
  drop 1
71
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
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
83
  let restriction_import_star_offset offset + {
84
84
  value text
85
85
  split "*"
86
86
  first
87
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
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
97
  let restriction_import_star_offset offset + {
98
98
  value text
99
99
  split "*"
100
100
  first
101
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
- }
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
146
  in ts_import, ts_export, ts_import_equals rule es-no-restricted-imports {
147
147
  @restriction_import_options
148
148
  when len(paths) > 0
149
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
- }
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
154
  in ts_import_specifier, ts_import.default_import, ts_import.namespace, ts_export_specifier, ts_export rule es-no-restricted-imports:names {
155
155
  @restriction_import_options
156
156
  when len(paths) > 0
157
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
- }
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
164
  in ts_import, ts_export, ts_import_equals rule es-no-restricted-imports:patterns {
165
165
  @restriction_import_options
166
166
  when len(patterns) > 0
167
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
- }
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
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
174
  @restriction_import_options
175
175
  when len(patterns) > 0
176
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 {
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
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
- }
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
261
  group restriction_declaration ts_import, ts_export
262
262
  in ts_program let restriction_declarations {
263
263
  value descendants(restriction_declaration)
@@ -283,25 +283,25 @@ fn restriction_sorted_imports(members, insensitive) = {
283
283
  value restriction_sorted_items(members, ordered)
284
284
  }
285
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
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
291
  let preceding {
292
292
  value ts_program.restriction_declarations
293
293
  filter (entry) !entry.export && entry.offset < offset
294
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
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
305
  when parent is ts_import && parent.restriction_declaration_entry != null && unsorted && previous.encountered != true
306
306
  report at local.node ?? imported.node
307
307
  let members parent.specifier.nodes
@@ -310,26 +310,26 @@ in ts_import_specifier rule es-sort-imports:members {
310
310
  all (member) !member.space_before.comments && !member.space_after.comments
311
311
  } as restriction_sorted_imports(members, ignoreCase)
312
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
-
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
333
  in ts_variable_declaration let restriction_sort_bindings {
334
334
  value declaration.nodes
335
335
  filter (item) item.name is ts_identifier
@@ -342,10 +342,10 @@ fn restriction_sorted_variables(bindings, insensitive) = {
342
342
  value restriction_sorted_items(bindings, ordered)
343
343
  }
344
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
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
349
  report at name.node
350
350
  warn "Variables within the same declaration block should be sorted alphabetically."
351
351
  let bindings parent.restriction_sort_bindings
@@ -358,113 +358,113 @@ in ts_declarator rule es-sort-vars {
358
358
  && item.index == first_binding.index + i
359
359
  } as restriction_sorted_variables(bindings, ignoreCase)
360
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
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
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)
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
468
  in ts_function_like, ts_static_block let max_nested_callbacks_counted restriction_call_argument
469
469
  in ts_class let max_classes_counted true
470
470
  in ts_program let restriction_classes {
@@ -487,27 +487,27 @@ in ts_node {
487
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
488
  let restriction_subtree_modified restriction_branch_total(restriction_flow_branch, is ts_switch, children.restriction_modified_contribution)
489
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
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
508
  report at restriction_function_head(node)
509
- warn "{@restriction_function_name} has too many parameters ({amount}). Maximum allowed is {max}."
510
- }
509
+ warn "{@restriction_function_name} has too many parameters ({amount}). Maximum allowed is {max}."
510
+ }
511
511
  in ts_function_like {
512
512
  let restriction_subtree_statements {
513
513
  value owner: node
@@ -518,62 +518,62 @@ in ts_function_like {
518
518
  }
519
519
  let max_statements_checked true
520
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
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
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
527
  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"
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
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
542
  when amount > max
543
543
  report at if is ts_static_block then { start: first_token.start, end: first_token.end } else node
544
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)
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
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)
549
+ option max int(0..) = 4 "maximum control-flow nesting"
550
+ when !(is ts_if && is ts_if.alternate)
551
551
  when restriction_nesting > max
552
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
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
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
- }
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
570
  fn restriction_line_previous(items, item) = {
571
571
  value result: index_of(items, item) - 1
572
572
  value items
573
573
  index_of: index_of item
574
574
  value slice(items, result, index_of)
575
575
  last
576
- }
576
+ }
577
577
  fn restriction_line_comment_gap(source, items, item, start, end) = {
578
578
  value default: { value restriction_line_previous(items, item).end_offset; nonempty } ?? start
579
579
  value max: max(start, default)
@@ -581,85 +581,85 @@ fn restriction_line_comment_gap(source, items, item, start, end) = {
581
581
  min: min item.offset
582
582
  value max_2: max(start, min)
583
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) == ""
584
+ }
585
+ in ts_line {
586
+ let restriction_line_comments overlapping(ts_comment)
587
+ let restriction_line_blank trim(text) == ""
588
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
589
  value end_offset_2: last(restriction_line_comments).end_offset
590
590
  value max: max(offset, end_offset_2)
591
591
  value min: min(end_offset, max)
592
592
  value byte_slice(source, min, end_offset)
593
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
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
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)
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
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)
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
649
  in ts_member let restriction_name_mutation parent not ts_update && !(parent is ts_unary && parent.operator == "delete")
650
650
  in ts_identifier let restriction_identifier_name text
651
651
  in ts_private_name let restriction_identifier_name re_replace(text, /^#*/, "")
652
652
  in ts_identifier, ts_private_name {
653
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
- }
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
663
  in ts_identifier, ts_private_name rule es-id-denylist {
664
664
  option names string[] = [] "restricted identifiers"
665
665
  when len(names) > 0 && contains(names, restriction_identifier_name)
@@ -667,145 +667,145 @@ in ts_identifier, ts_private_name rule es-id-denylist {
667
667
  when parent not (ts_call, ts_new, ts_meta_property) && !(is ts_member.property && !restriction_name_assignment)
668
668
  warn "Identifier '{text}' is restricted."
669
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"
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
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
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
705
  @macro restriction_global_options {
706
706
  option globals (restriction_global | string)[] = [] "restricted globals"
707
707
  option checkGlobalObject boolean = false "check global-object properties"
708
708
  option globalObjects string[] = [] "additional global-object names"
709
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
- }
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
739
  fn restriction_property_match(object, name, options) = {
740
740
  value options
741
741
  find_last (o) o.object != null && o.object == object && (o.property == name || o.property == null) && !{ value o.allowProperties.nonempty ?? []; contains name }
742
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
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
750
  let restriction_property_pattern is ts_object_pattern || flow_destructuring_target == true
751
- }
751
+ }
752
752
  @macro restriction_property_body {
753
753
  option options restriction_property[] = [] "restricted object properties"
754
754
  when len(options) > 0
755
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
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
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
- }
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
787
  in ts_identifier rule es-no-restricted-exports:declarations {
788
788
  @restriction_export_options
789
789
  when len(restrictedNamedExports) > 0 || restrictedNamedExportsPattern != ""
790
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)
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
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
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
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
801
  warn if restricted_name then "'{export_name}' is restricted from being used as an exported name." else "Exporting 'default' is restricted."
809
- }
810
-
811
- }
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
+ }