@inthedark/dxlint 0.1.14 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/bin/dxlint-darwin-arm64 +0 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/esbuild.mjs +134 -0
- package/package.json +20 -2
- package/rules/bugs.dx +1 -0
- package/std/ts/arithmetic.dx +10 -0
- package/std/ts/assertions.dx +56 -4
- package/std/ts/calls.dx +45 -15
- package/std/ts/conditions.dx +2 -0
- package/std/ts/constants.dx +2 -1
- package/std/ts/content-mapper.dx +48 -0
- package/std/ts/deprecation.dx +38 -18
- package/std/ts/effects.dx +8 -1
- package/std/ts/enums.dx +32 -4
- package/std/ts/format.dx +1 -1
- package/std/ts/grammar.dx +29 -15
- package/std/ts/import-no-deprecated.dx +4 -2
- package/std/ts/javascript.dx +496 -0
- package/std/ts/layout/literals.dx +14 -7
- package/std/ts/layout/separators.dx +6 -1
- package/std/ts/layout/shared.dx +36 -37
- package/std/ts/layout/types.dx +4 -0
- package/std/ts/literals.dx +1 -0
- package/std/ts/methods.dx +3 -0
- package/std/ts/modules.dx +60 -2
- package/std/ts/names.dx +30 -13
- package/std/ts/narrowing.dx +20 -14
- package/std/ts/parentheses.dx +27 -0
- package/std/ts/problems/control-flow.dx +1 -0
- package/std/ts/problems/numeric-precision.dx +1 -0
- package/std/ts/problems/regexp.dx +1 -0
- package/std/ts/regex/checks.dx +1 -0
- package/std/ts/regex/findings.dx +10 -6
- package/std/ts/regex/values.dx +4 -15
- package/std/ts/restrictions.dx +76 -0
- package/std/ts/scoping/member-uses.dx +5 -3
- package/std/ts/scoping/shadowing.dx +1 -0
- package/std/ts/scoping/usage.dx +3 -3
- package/std/ts/spreads.dx +1 -1
- package/std/ts/structure/padding.dx +10 -8
- package/std/ts/style/expressions.dx +8 -4
- package/std/ts/style/returns.dx +3 -1
- package/std/ts/style/signatures.dx +5 -0
- package/std/ts/style/type-exports.dx +1 -0
- package/std/ts/suggestions/builtins.dx +1 -1
- package/std/ts/suggestions/comparisons.dx +1 -0
- package/std/ts/suggestions/expressions.dx +1 -0
- package/std/ts/suggestions/object-spread.dx +1 -1
- package/std/ts/tokens.dx +14 -5
- package/std/ts/type-specifiers.dx +19 -3
- package/std/ts/typed/awaitability.dx +1 -0
- package/std/ts/typed/collection-iteration.dx +24 -0
- package/std/ts/typed/floating-promises.dx +45 -14
- package/std/ts/typed/includes.dx +3 -4
- package/std/ts/typed/promises.dx +16 -13
- package/std/ts/typed/readonly-parameters.dx +11 -2
- package/std/ts/typed/string-boundaries.dx +1 -0
- package/std/ts/typed/switches.dx +1 -0
- package/std/ts/typed/transfers.dx +54 -7
- package/std/ts/typed/type-arguments.dx +19 -20
- package/std/ts/typed/type-conversions.dx +1 -0
- package/std/ts/typed/type-parameters.dx +1 -1
- package/std/ts/typed/using-disposable.dx +98 -0
- package/std/ts/typed.dx +22 -9
- package/std/ts/values/arguments.dx +1 -1
- package/std/ts/values/enums.dx +5 -1
- package/std/ts/values/inspection.dx +26 -7
- package/std/ts/values.dx +98 -39
- package/std/ts.dx +10 -4
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
# JavaScript values: how JavaScript reads, prints, compares and combines
|
|
2
|
+
# literals, over the engine's language-neutral text and number functions.
|
|
3
|
+
# `literal_value` records are `{type, value}`: `string`, `number`, `bigint`,
|
|
4
|
+
# `boolean`, `null`, `undefined`, `regex`.
|
|
5
|
+
|
|
6
|
+
define {
|
|
7
|
+
|
|
8
|
+
# ECMAScript white space and line terminators (StringNumericLiteral trimming).
|
|
9
|
+
fn js_trim(text) = trim("{text}", "\t\u{b}\u{c} \u{a0}\u{feff}\n\r\u{2028}\u{2029}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{202f}\u{205f}\u{3000}")
|
|
10
|
+
fn js_nan_unless(n) = if type_of(n) == "num" then n else num("NaN")
|
|
11
|
+
|
|
12
|
+
# A numeric literal's value: decimal, `0x`/`0o`/`0b`, legacy octal, `_` separators.
|
|
13
|
+
fn js_numeric_literal(text) = {
|
|
14
|
+
value t: replace("{text}", "_", "")
|
|
15
|
+
value prefix: slice(t, 0, 2)
|
|
16
|
+
value when {
|
|
17
|
+
prefix == "0x" || prefix == "0X" then parse_number(slice(t, 2), 16)
|
|
18
|
+
prefix == "0o" || prefix == "0O" then parse_number(slice(t, 2), 8)
|
|
19
|
+
prefix == "0b" || prefix == "0B" then parse_number(slice(t, 2), 2)
|
|
20
|
+
re_test(t, /^0[0-7]+$/) then parse_number(slice(t, 1), 8)
|
|
21
|
+
# A computed number's own text.
|
|
22
|
+
t == "NaN" || t == "Infinity" || t == "-Infinity" then num(t)
|
|
23
|
+
else then parse_number(t)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# `Number(text)`: runtime conversion, not literal syntax.
|
|
28
|
+
fn js_number_text(raw) = {
|
|
29
|
+
value text: js_trim(raw)
|
|
30
|
+
value prefix: slice(text, 0, 2)
|
|
31
|
+
value when {
|
|
32
|
+
text == "" then 0
|
|
33
|
+
text == "Infinity" || text == "+Infinity" then num("Infinity")
|
|
34
|
+
text == "-Infinity" then -num("Infinity")
|
|
35
|
+
prefix == "0x" || prefix == "0X" then js_nan_unless(parse_number(slice(text, 2), 16))
|
|
36
|
+
prefix == "0o" || prefix == "0O" then js_nan_unless(parse_number(slice(text, 2), 8))
|
|
37
|
+
prefix == "0b" || prefix == "0B" then js_nan_unless(parse_number(slice(text, 2), 2))
|
|
38
|
+
else then js_nan_unless(parse_number(text))
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# A bigint's digits (`n`, `_` and a radix prefix allowed) in decimal.
|
|
43
|
+
fn js_bigint_digits(text) = {
|
|
44
|
+
value digits: replace(trim_end(text, "n"), "_", "")
|
|
45
|
+
value prefix: slice(digits, 0, 2)
|
|
46
|
+
value when {
|
|
47
|
+
prefix == "0x" || prefix == "0X" then big_integer(slice(digits, 2), 16)
|
|
48
|
+
prefix == "0o" || prefix == "0O" then big_integer(slice(digits, 2), 8)
|
|
49
|
+
prefix == "0b" || prefix == "0B" then big_integer(slice(digits, 2), 2)
|
|
50
|
+
else then big_integer(digits)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
# A bigint value (`literal`: a bigint's own text; else a string converted
|
|
55
|
+
# to one) as signed decimal text; null when it is none.
|
|
56
|
+
fn js_bigint_value(text, literal) = {
|
|
57
|
+
value t: trim("{text}")
|
|
58
|
+
value signed: starts_with(t, "-") || starts_with(t, "+")
|
|
59
|
+
value body: if signed then slice(t, 1) else t
|
|
60
|
+
value when {
|
|
61
|
+
t == "" then if literal then null else "0"
|
|
62
|
+
!literal && (contains(body, "_") || contains(body, "n") || signed && !re_test(body, /^[0-9]+$/)) then null
|
|
63
|
+
trim_end(body, "n") == "" || re_test(body, /^0[xXoObB]$/) then null
|
|
64
|
+
else then {
|
|
65
|
+
value digits: js_bigint_digits(body)
|
|
66
|
+
value when {
|
|
67
|
+
digits == null then null
|
|
68
|
+
digits == "0" || !starts_with(t, "-") then digits
|
|
69
|
+
else then "-{digits}"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
# A bigint literal (`0x10n`) as exact decimal text; null for broken syntax.
|
|
76
|
+
fn js_bigint(text) = {
|
|
77
|
+
value t: "{text}"
|
|
78
|
+
value payload: if ends_with(t, "n") then slice(t, 0, -1) else ""
|
|
79
|
+
value prefix: slice(payload, 0, 2)
|
|
80
|
+
value radix: when {
|
|
81
|
+
prefix == "0x" || prefix == "0X" then 16
|
|
82
|
+
prefix == "0o" || prefix == "0O" then 8
|
|
83
|
+
prefix == "0b" || prefix == "0B" then 2
|
|
84
|
+
else then 10
|
|
85
|
+
}
|
|
86
|
+
value digits: if radix == 10 then payload else slice(payload, 2)
|
|
87
|
+
value valid: when {
|
|
88
|
+
radix == 16 then re_test(digits, /^[0-9a-fA-F]+(?:_[0-9a-fA-F]+)*$/)
|
|
89
|
+
radix == 8 then re_test(digits, /^[0-7]+(?:_[0-7]+)*$/)
|
|
90
|
+
radix == 2 then re_test(digits, /^[01]+(?:_[01]+)*$/)
|
|
91
|
+
else then re_test(digits, /^(?:0|[1-9][0-9]*(?:_[0-9]+)*)$/)
|
|
92
|
+
}
|
|
93
|
+
value if valid then big_integer(replace(digits, "_", ""), radix) else null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn js_literal_number(kind, raw) = when {
|
|
97
|
+
kind == "string" then js_number_text(raw)
|
|
98
|
+
kind == "boolean" then when {
|
|
99
|
+
"{raw}" == "true" then 1
|
|
100
|
+
"{raw}" == "false" then 0
|
|
101
|
+
else then null
|
|
102
|
+
}
|
|
103
|
+
kind == "null" then 0
|
|
104
|
+
kind == "undefined" || kind == "regex" then num("NaN")
|
|
105
|
+
kind == "number" then if type_of(raw) == "num" then raw else js_numeric_literal(raw)
|
|
106
|
+
kind == "bigint" then {
|
|
107
|
+
value digits: js_bigint_value(raw, true)
|
|
108
|
+
value if digits == null then null else parse_number(digits)
|
|
109
|
+
}
|
|
110
|
+
else then null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
# `Number(value)` of a plain value or `literal_value` record; NaN for
|
|
114
|
+
# non-numeric text, null for what has no number (symbols).
|
|
115
|
+
fn js_number(v) = when {
|
|
116
|
+
type_of(v) == "num" then v
|
|
117
|
+
type_of(v) == "bool" then if v then 1 else 0
|
|
118
|
+
type_of(v) == "null" then 0
|
|
119
|
+
type_of(v) == "str" || type_of(v) == "node" then js_number_text(v)
|
|
120
|
+
type_of(v) == "record" then js_literal_number("{v.type}", v.value)
|
|
121
|
+
else then null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# `String(n)` for a number (ECMAScript Number::toString).
|
|
125
|
+
fn js_number_to_string(n) = when {
|
|
126
|
+
n != n then "NaN"
|
|
127
|
+
n == 0 then "0"
|
|
128
|
+
n == num("Infinity") then "Infinity"
|
|
129
|
+
n == -num("Infinity") then "-Infinity"
|
|
130
|
+
else then {
|
|
131
|
+
value sign: if n < 0 then "-" else ""
|
|
132
|
+
value parts: number_digits(abs(n))
|
|
133
|
+
value digits: parts.digits
|
|
134
|
+
value k: len(digits)
|
|
135
|
+
value e: parts.exponent + 1
|
|
136
|
+
value body: when {
|
|
137
|
+
k <= e && e <= 21 then {
|
|
138
|
+
value zeros: repeat("0", e - k)
|
|
139
|
+
value "{digits}{zeros}"
|
|
140
|
+
}
|
|
141
|
+
0 < e && e <= 21 then {
|
|
142
|
+
value whole: slice(digits, 0, e)
|
|
143
|
+
value fraction: slice(digits, e)
|
|
144
|
+
value "{whole}.{fraction}"
|
|
145
|
+
}
|
|
146
|
+
e > -6 && e <= 0 then {
|
|
147
|
+
value zeros: repeat("0", -e)
|
|
148
|
+
value "0.{zeros}{digits}"
|
|
149
|
+
}
|
|
150
|
+
else then {
|
|
151
|
+
value x: e - 1
|
|
152
|
+
value exponent: if x >= 0 then "+{x}" else "{x}"
|
|
153
|
+
value head: slice(digits, 0, 1)
|
|
154
|
+
value tail: slice(digits, 1)
|
|
155
|
+
value if k == 1 then "{head}e{exponent}" else "{head}.{tail}e{exponent}"
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
value "{sign}{body}"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# `String(value)` of a plain value or `literal_value` record (bigints
|
|
163
|
+
# without `n`); null for other values.
|
|
164
|
+
fn js_to_string(v) = when {
|
|
165
|
+
type_of(v) == "num" then js_number_to_string(v)
|
|
166
|
+
type_of(v) == "bool" then if v then "true" else "false"
|
|
167
|
+
type_of(v) == "null" then "null"
|
|
168
|
+
type_of(v) == "str" || type_of(v) == "node" then v
|
|
169
|
+
type_of(v) == "record" then when {
|
|
170
|
+
v.type == "number" then {
|
|
171
|
+
value n: js_literal_number("number", v.value)
|
|
172
|
+
value if type_of(n) == "num" then js_number_to_string(n) else null
|
|
173
|
+
}
|
|
174
|
+
v.type == "string" || v.type == "bigint" || v.type == "boolean" then "{v.value}"
|
|
175
|
+
v.type == "null" then "null"
|
|
176
|
+
else then null
|
|
177
|
+
}
|
|
178
|
+
else then null
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
# `value.toPrecision(digits)` for whole `digits` 1..100; null otherwise.
|
|
182
|
+
fn js_number_to_precision(x, digits) = when {
|
|
183
|
+
type_of(x) != "num" || type_of(digits) != "num" || digits % 1 != 0 || digits < 1 || digits > 100 then null
|
|
184
|
+
x != x then "NaN"
|
|
185
|
+
x == num("Infinity") then "Infinity"
|
|
186
|
+
x == -num("Infinity") then "-Infinity"
|
|
187
|
+
else then {
|
|
188
|
+
value sign: if x < 0 then "-" else ""
|
|
189
|
+
value parts: if x == 0 then { digits: repeat("0", digits), exponent: 0 } else number_digits(abs(x), digits)
|
|
190
|
+
value m: parts.digits
|
|
191
|
+
value e: parts.exponent
|
|
192
|
+
value head: slice(m, 0, 1)
|
|
193
|
+
value tail: slice(m, 1)
|
|
194
|
+
value fraction: if tail == "" then "" else ".{tail}"
|
|
195
|
+
value mark: if e < 0 then "-" else "+"
|
|
196
|
+
value size: abs(e)
|
|
197
|
+
value whole: e + 1
|
|
198
|
+
value body: when {
|
|
199
|
+
e < -6 || e >= digits then "{head}{fraction}e{mark}{size}"
|
|
200
|
+
e >= 0 && whole == digits then m
|
|
201
|
+
e >= 0 then {
|
|
202
|
+
value left: slice(m, 0, whole)
|
|
203
|
+
value right: slice(m, whole)
|
|
204
|
+
value "{left}.{right}"
|
|
205
|
+
}
|
|
206
|
+
else then {
|
|
207
|
+
value zeros: repeat("0", -e - 1)
|
|
208
|
+
value "0.{zeros}{m}"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
value "{sign}{body}"
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
# A numeric binary operator on two numbers, with JavaScript's results
|
|
216
|
+
# (NaN, Infinity, ToInt32 for bitwise); null for other operators or operands.
|
|
217
|
+
fn js_number_binary(operator, x, y) = if type_of(x) != "num" || type_of(y) != "num" then null else when {
|
|
218
|
+
operator == "+" then x + y
|
|
219
|
+
operator == "-" then x - y
|
|
220
|
+
operator == "*" then x * y
|
|
221
|
+
operator == "/" then float_divide(x, y)
|
|
222
|
+
operator == "%" then if y == 0 then num("NaN") else x % y
|
|
223
|
+
# `Math.pow` differs from IEEE pow: `1 ** Infinity` and `NaN ** x` are NaN.
|
|
224
|
+
operator == "**" then if y != y || abs(x) == 1 && abs(y) == num("Infinity") then num("NaN") else pow(x, y)
|
|
225
|
+
operator == "&" then bits("and", x, y)
|
|
226
|
+
operator == "|" then bits("or", x, y)
|
|
227
|
+
operator == "^" then bits("xor", x, y)
|
|
228
|
+
operator == "<<" then bits("shift_left", x, uint32(y) % 32)
|
|
229
|
+
operator == ">>" then bits("shift_right", x, uint32(y) % 32)
|
|
230
|
+
operator == ">>>" then bits("shift_right_unsigned", x, uint32(y) % 32)
|
|
231
|
+
else then null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn js_number_unary(operator, x) = if type_of(x) != "num" then null else when {
|
|
235
|
+
operator == "-" then -x
|
|
236
|
+
operator == "+" then x
|
|
237
|
+
operator == "~" then bits("not", x)
|
|
238
|
+
else then null
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
# `text[index]`: the UTF-16 unit at `index`.
|
|
242
|
+
fn js_char_at(text, index) = utf16_at(text, index)
|
|
243
|
+
|
|
244
|
+
fn js_typed(v) = when {
|
|
245
|
+
type_of(v) == "record" then { type: "{v.type}", value: v.value }
|
|
246
|
+
type_of(v) == "num" then { type: "number", value: v }
|
|
247
|
+
type_of(v) == "bool" then { type: "boolean", value: v }
|
|
248
|
+
type_of(v) == "null" then { type: "null", value: "null" }
|
|
249
|
+
else then { type: "string", value: "{v}" }
|
|
250
|
+
}
|
|
251
|
+
fn js_number_of(literal) = js_nan_unless(js_literal_number(literal.type, literal.value))
|
|
252
|
+
fn js_order(x, y) = when {
|
|
253
|
+
x != x || y != y then null
|
|
254
|
+
x < y then -1
|
|
255
|
+
x > y then 1
|
|
256
|
+
else then 0
|
|
257
|
+
}
|
|
258
|
+
# A bigint (signed decimal text) against a number, exactly.
|
|
259
|
+
fn js_bigint_order(digits, n) = when {
|
|
260
|
+
n != n then null
|
|
261
|
+
n == num("Infinity") then -1
|
|
262
|
+
n == -num("Infinity") then 1
|
|
263
|
+
else then {
|
|
264
|
+
value whole: if n < 0 then ceil(n) else floor(n)
|
|
265
|
+
value order: compare_integers(digits, integer_radix(whole, 10))
|
|
266
|
+
value when {
|
|
267
|
+
order != 0 then order
|
|
268
|
+
n > whole then -1
|
|
269
|
+
n < whole then 1
|
|
270
|
+
else then 0
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
# -1, 0 or 1 as `<` orders two literal values: strings by UTF-16 unit,
|
|
276
|
+
# bigints exactly, otherwise as numbers; null when unordered.
|
|
277
|
+
fn js_compare(a, b) = {
|
|
278
|
+
value l: js_typed(a)
|
|
279
|
+
value r: js_typed(b)
|
|
280
|
+
value when {
|
|
281
|
+
l.type == "string" && r.type == "string" then compare_text(l.value, r.value)
|
|
282
|
+
l.type == "bigint" then {
|
|
283
|
+
value x: js_bigint_value(l.value, true)
|
|
284
|
+
value when {
|
|
285
|
+
x == null then null
|
|
286
|
+
r.type == "bigint" || r.type == "string" then {
|
|
287
|
+
value y: js_bigint_value(r.value, r.type == "bigint")
|
|
288
|
+
value if y == null then null else compare_integers(x, y)
|
|
289
|
+
}
|
|
290
|
+
else then js_bigint_order(x, js_number_of(r))
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
r.type == "bigint" then {
|
|
294
|
+
value y: js_bigint_value(r.value, true)
|
|
295
|
+
value when {
|
|
296
|
+
y == null then null
|
|
297
|
+
l.type == "string" then {
|
|
298
|
+
value x: js_bigint_value(l.value, false)
|
|
299
|
+
value if x == null then null else compare_integers(x, y)
|
|
300
|
+
}
|
|
301
|
+
else then {
|
|
302
|
+
value order: js_bigint_order(y, js_number_of(l))
|
|
303
|
+
value if order == null then null else 0 - order
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else then js_order(js_number_of(l), js_number_of(r))
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
# The code point one escape character stands for (`\n` is 10).
|
|
312
|
+
fn js_single_escape(c) = when {
|
|
313
|
+
c == "b" then 8
|
|
314
|
+
c == "f" then 12
|
|
315
|
+
c == "n" then 10
|
|
316
|
+
c == "r" then 13
|
|
317
|
+
c == "t" then 9
|
|
318
|
+
c == "v" then 11
|
|
319
|
+
else then code_of(c)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
# One token of a literal body with escapes: its code point record, `skip`
|
|
323
|
+
# for a line continuation, null when the literal is broken.
|
|
324
|
+
fn js_escape_part(token, quote, template) = {
|
|
325
|
+
value g: token.groups
|
|
326
|
+
value code: when {
|
|
327
|
+
get(g, 0) != null then parse_number(get(g, 0), 16)
|
|
328
|
+
get(g, 1) != null then {
|
|
329
|
+
value point: parse_number(get(g, 1), 16)
|
|
330
|
+
value if point > 1114111 then -1 else point
|
|
331
|
+
}
|
|
332
|
+
get(g, 2) != null then parse_number(get(g, 2), 16)
|
|
333
|
+
get(g, 3) != null then parse_number(get(g, 3), 8)
|
|
334
|
+
get(g, 4) != null then -2
|
|
335
|
+
get(g, 5) != null then -1
|
|
336
|
+
get(g, 6) != null then js_single_escape(get(g, 6))
|
|
337
|
+
# A backslash alone ends the text: its escape is cut off.
|
|
338
|
+
token.text == quote || token.text == "\\" || !template && (token.text == "\n" || token.text == "\r") then -1
|
|
339
|
+
else then code_of(token.text)
|
|
340
|
+
}
|
|
341
|
+
value when {
|
|
342
|
+
code == -1 then null
|
|
343
|
+
code == -2 then { skip: true }
|
|
344
|
+
else then { offset: token.offset + 1, end_offset: token.end_offset + 1, value: code }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
# A quoted string or substitution-free template as `{text, spans, units}`:
|
|
349
|
+
# the decoded text, each Unicode scalar's and each UTF-16 unit's byte span in
|
|
350
|
+
# the literal (units with their `value`); null for broken syntax. Lone
|
|
351
|
+
# surrogates become U+FFFD in `text`.
|
|
352
|
+
fn js_string_parts(text) = {
|
|
353
|
+
value t: "{text}"
|
|
354
|
+
value quote: slice(t, 0, 1)
|
|
355
|
+
value body: slice(t, 1, -1)
|
|
356
|
+
value template: quote == "`"
|
|
357
|
+
value closed: (quote == "'" || quote == "\"" || template) && len(t) >= 2 && ends_with(t, quote)
|
|
358
|
+
value when {
|
|
359
|
+
!closed then null
|
|
360
|
+
template && re_test(body, /(?:^|[^\\])(?:\\\\)*\$\{/) then null
|
|
361
|
+
!contains(body, "\\") then
|
|
362
|
+
if contains(body, quote) || !template && re_test(body, /[\r\n]/) then null else {
|
|
363
|
+
value units: text_units(body, 1)
|
|
364
|
+
value { text: body, spans: units.spans, units: units.units }
|
|
365
|
+
}
|
|
366
|
+
else then {
|
|
367
|
+
value tokens: re_matches(body, /\\(?:x([0-9a-fA-F]{2})|u\{([0-9a-fA-F]+)\}|u([0-9a-fA-F]{4})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(\r\n|[\r\n\u{2028}\u{2029}])|([xu])|([\s\S]))|[\s\S]/)
|
|
368
|
+
value parts: map(tokens, token => js_escape_part(token, quote, template))
|
|
369
|
+
value if any(parts, part => part == null) then null else text_from_units({ value parts; filter (part) part.skip != true })
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
# A quoted string literal's value (escapes and line continuations decoded);
|
|
375
|
+
# null for broken syntax and templates.
|
|
376
|
+
fn js_string(text) = {
|
|
377
|
+
value t: "{text}"
|
|
378
|
+
value quote: slice(t, 0, 1)
|
|
379
|
+
value if quote != "'" && quote != "\"" then null else js_string_parts(t).text
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
schema js_regex string check js_regex_valid "an ECMAScript regex, as `js_re_test` reads it in Unicode mode"
|
|
385
|
+
|
|
386
|
+
define {
|
|
387
|
+
|
|
388
|
+
# ECMAScript regexes as the engine's regex (`re_test`): JavaScript's `\d`,
|
|
389
|
+
# `\w`, `\s`, `.` and line anchors over its four line terminators, ASCII word
|
|
390
|
+
# boundaries, flags `i`, `m`, `s` and `u`. Backreferences, control escapes,
|
|
391
|
+
# surrogate escapes, Unicode case-insensitive word boundaries and (without
|
|
392
|
+
# `u`) case-insensitive text JavaScript folds differently have no
|
|
393
|
+
# translation: null.
|
|
394
|
+
fn js_regex_class_set(name) = when {
|
|
395
|
+
name == "d" then "0-9"
|
|
396
|
+
name == "w" then "A-Za-z0-9_"
|
|
397
|
+
else then "\\t\\n\\r\\x0B\\x0C \\u\{a0\}\\u\{1680\}\\u\{2000\}-\\u\{200a\}\\u\{2028\}\\u\{2029\}\\u\{202f\}\\u\{205f\}\\u\{3000\}\\u\{feff\}"
|
|
398
|
+
}
|
|
399
|
+
fn js_regex_hex(hex, u) = {
|
|
400
|
+
value point: parse_number(hex, 16)
|
|
401
|
+
value if point == null || point > 1114111 || point >= 55296 && point <= 57343 then null else "\\u\{{hex}\}"
|
|
402
|
+
}
|
|
403
|
+
fn js_regex_escape(c, rest, class, flags) = {
|
|
404
|
+
value u: contains(flags, "u")
|
|
405
|
+
value lower: when {
|
|
406
|
+
c == "d" || c == "D" then "d"
|
|
407
|
+
c == "w" || c == "W" then "w"
|
|
408
|
+
c == "s" || c == "S" then "s"
|
|
409
|
+
else then null
|
|
410
|
+
}
|
|
411
|
+
value when {
|
|
412
|
+
lower != null && class && c == lower then js_regex_class_set(c)
|
|
413
|
+
lower != null && c == lower then "[{js_regex_class_set(c)}]"
|
|
414
|
+
lower != null then "[^{js_regex_class_set(lower)}]"
|
|
415
|
+
c != "" && contains("123456789kc", c) then null
|
|
416
|
+
(c == "b" || c == "B") && !class then if contains(flags, "i") && u then null else "(?-u:\\{c})"
|
|
417
|
+
c == "b" then "\\x08"
|
|
418
|
+
c == "u" && starts_with(rest, "\{") then if u then js_regex_hex(trim_end(trim_start(rest, "\{"), "\}"), u) else null
|
|
419
|
+
c == "u" then if re_test(rest, /^[0-9a-fA-F]{4}$/) then js_regex_hex(rest, u) else null
|
|
420
|
+
c == "/" then "/"
|
|
421
|
+
re_test(c, /^[A-Za-z]$/) && !contains("nrtfvx", c) then if u then null else c
|
|
422
|
+
else then "\\{c}{rest}"
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
# One token outside a class (`class` false) or inside one, as the engine's
|
|
426
|
+
# regex; null when it has no translation.
|
|
427
|
+
fn js_regex_piece(t, class, flags) = when {
|
|
428
|
+
starts_with(t, "\\") && len(t) == 1 then null
|
|
429
|
+
starts_with(t, "\\") then js_regex_escape(slice(t, 1, 2), slice(t, 2), class, flags)
|
|
430
|
+
class && (t == "[" || t == "&" || t == "~") then "\\{t}"
|
|
431
|
+
class then t
|
|
432
|
+
t == "." then if contains(flags, "s") then "(?s:.)" else "[^\\n\\r\\u\{2028\}\\u\{2029\}]"
|
|
433
|
+
t == "^" then if contains(flags, "m") then "(?<=^|[\\n\\r\\u\{2028\}\\u\{2029\}])" else "\\A"
|
|
434
|
+
t == "$" then if contains(flags, "m") then "(?=$|[\\n\\r\\u\{2028\}\\u\{2029\}])" else "\\z"
|
|
435
|
+
t == "\{" || t == "\}" then "\\{t}"
|
|
436
|
+
starts_with(t, "(?") && !re_test(t, /^\(\?(?:[:=!]|<[=!]|<[A-Za-z_$][A-Za-z0-9_$]*>)$/) then null
|
|
437
|
+
else then t
|
|
438
|
+
}
|
|
439
|
+
fn js_regex_joined(pieces) = if count({ value pieces; filter (p) type_of(p) == "null" }) > 0 then null else join(pieces, "")
|
|
440
|
+
# A whole class (`[...]`): its contents piece by piece.
|
|
441
|
+
fn js_regex_class(token, flags) = {
|
|
442
|
+
value body: slice(token, 1, -1)
|
|
443
|
+
value negated: starts_with(body, "^")
|
|
444
|
+
value items: re_matches(if negated then slice(body, 1) else body, /\\u\{[0-9a-fA-F]*\}|\\u[0-9a-fA-F]{0,4}|\\[\s\S]?|[\s\S]/)
|
|
445
|
+
value inner: js_regex_joined({ value items; map (item) js_regex_piece(item.text, true, flags) })
|
|
446
|
+
value mark: if negated then "^" else ""
|
|
447
|
+
value if inner == null then null else "[{mark}{inner}]"
|
|
448
|
+
}
|
|
449
|
+
fn js_regex_source(pattern, flags) = {
|
|
450
|
+
value p: "{pattern}"
|
|
451
|
+
value f: "{flags}"
|
|
452
|
+
value valid_flags: re_test(f, /^[imsu]*$/) && count(unique(split(f, ""))) == len(f)
|
|
453
|
+
value tokens: re_matches(p, /\\u\{[0-9a-fA-F]*\}|\\u[0-9a-fA-F]{0,4}|\\[\s\S]?|\[(?:\\[\s\S]|[^\]\\])*\]|\(\?<[=!]|\(\?<[^>]*>|\(\?[\s\S]?|\{[0-9]+(?:,[0-9]*)?\}|[\s\S]/)
|
|
454
|
+
value built: js_regex_joined({ value tokens; map (token) if len(token.text) > 1 && starts_with(token.text, "[") then js_regex_class(token.text, f) else js_regex_piece(token.text, false, f) })
|
|
455
|
+
value prefix: if contains(f, "i") then "(?i)" else ""
|
|
456
|
+
value if !valid_flags || len(p) > 65536 || built == null || js_regex_legacy_gap(p, f) then null else "{prefix}{built}"
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
# Whether `pattern` is an ECMAScript regex `js_re_test` can run in Unicode
|
|
460
|
+
# (`u`) mode: what the `js_regex` schema checks.
|
|
461
|
+
fn js_regex_valid(pattern) = {
|
|
462
|
+
value source: js_regex_source(pattern, "u")
|
|
463
|
+
value source != null && re_valid(source)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
# `i` without `u` folds case by JavaScript's uppercase rules, which part from
|
|
467
|
+
# Unicode folding for characters whose uppercase is several characters or
|
|
468
|
+
# ASCII, the folds of those, and astral ones: no translation there.
|
|
469
|
+
fn js_regex_legacy_gap(text, flags) = contains("{flags}", "i") && !contains("{flags}", "u")
|
|
470
|
+
&& re_test("{text}", /[\u{df}\u{149}\u{17f}\u{1f0}\u{345}\u{390}\u{3b0}\u{587}\u{1e96}-\u{1e9a}\u{1e9e}\u{1f50}-\u{1f56}\u{1f80}-\u{1ffc}\u{2126}\u{212a}\u{212b}\u{fb00}-\u{fb17}\u{10000}-\u{10ffff}]/)
|
|
471
|
+
|
|
472
|
+
# Whether the ECMAScript regex `pattern` with `flags` matches anywhere in
|
|
473
|
+
# `text`; null when it has no translation (see `js_regex_source`).
|
|
474
|
+
fn js_re_test(text, pattern, flags) = {
|
|
475
|
+
value source: js_regex_source(pattern, flags)
|
|
476
|
+
value if source == null || js_regex_legacy_gap(text, flags) then null else re_test("{text}", source)
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
# What `RegExp`'s constructor says about `flags` (`u` with `v`, duplicates,
|
|
480
|
+
# unknown ones; flags in `allowed` pass), as eslint's no-invalid-regexp
|
|
481
|
+
# words it; null when fine.
|
|
482
|
+
fn regex_flags_error(flags, allowed) = {
|
|
483
|
+
value given: split("{flags}", "")
|
|
484
|
+
value extra: { value split(join(allowed, ""), ""); filter (c) !contains("dgimsuvy", c); join "" }
|
|
485
|
+
value known: "dgimsuvy{extra}"
|
|
486
|
+
value rest: { value given; filter (c, i) !contains(known, c) || index_of(given, c) < i; join "" }
|
|
487
|
+
value repeated: { value split(rest, ""); filter (c) contains(known, c); join "" }
|
|
488
|
+
value when {
|
|
489
|
+
contains(given, "u") && contains(given, "v") then "Regex 'u' and 'v' flags cannot be used together"
|
|
490
|
+
repeated != "" then "Duplicate flags ('{repeated}') supplied to RegExp constructor"
|
|
491
|
+
rest != "" then "Invalid flags supplied to RegExp constructor '{rest}'"
|
|
492
|
+
else then null
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import .ts/javascript
|
|
1
2
|
# .ts/layout/literals: literals layout rules.
|
|
2
3
|
|
|
3
4
|
import .ts/layout/shared
|
|
@@ -69,15 +70,19 @@ fn layout_property_quote_site(owner, group) = {
|
|
|
69
70
|
redundant_number: redundant_number, keyword: keyword, name: name, offset: owner.offset
|
|
70
71
|
}
|
|
71
72
|
}
|
|
73
|
+
fn layout_property_quote_owners(n) = when {
|
|
74
|
+
n is ts_object then n.property.nodes
|
|
75
|
+
n is (ts_interface, ts_type_literal, ts_enum) then n.member.nodes
|
|
76
|
+
n is (ts_import, ts_export) then overlapping(ts_import_attribute, n.offset, n.end_offset)
|
|
77
|
+
else then [n]
|
|
78
|
+
}
|
|
79
|
+
fn layout_property_quote_written(owner) = {
|
|
80
|
+
value key: if owner is ts_enum_member then owner.name.node else owner.key.node
|
|
81
|
+
value key is (ts_string, ts_import_attribute_key, ts_import_attribute_string)
|
|
82
|
+
}
|
|
72
83
|
fn layout_property_quote_sites(n) = {
|
|
73
84
|
value group: n is (ts_object, ts_interface, ts_type_literal, ts_enum)
|
|
74
|
-
value
|
|
75
|
-
n is ts_object then n.property.nodes
|
|
76
|
-
n is (ts_interface, ts_type_literal, ts_enum) then n.member.nodes
|
|
77
|
-
n is (ts_import, ts_export) then overlapping(ts_import_attribute, n.offset, n.end_offset)
|
|
78
|
-
else then [n]
|
|
79
|
-
}
|
|
80
|
-
value sites: flat_map(owners, owner =>
|
|
85
|
+
value sites: flat_map(layout_property_quote_owners(n), owner =>
|
|
81
86
|
if owner is ts_import_attribute then [layout_property_quote_site(owner, false), layout_property_quote_site(owner, true)]
|
|
82
87
|
else if owner is (ts_property, ts_pattern_property, ts_property_signature, ts_method_signature, ts_enum_member)
|
|
83
88
|
then [layout_property_quote_site(owner, group)] else [])
|
|
@@ -88,6 +93,8 @@ in ts_property, ts_pattern_property, ts_property_signature, ts_enum_member, ts_o
|
|
|
88
93
|
option keywords boolean = false "require quotes on reserved words"
|
|
89
94
|
option unnecessary boolean = true "report redundant quotes"
|
|
90
95
|
option numbers boolean = false "require quoted numeric keys"
|
|
96
|
+
# With no key quoted and no keyword policy this style has nothing to say.
|
|
97
|
+
when style != "consistent-as-needed" || keywords || any(layout_property_quote_owners(node), owner => layout_property_quote_written(owner))
|
|
91
98
|
let property_sites layout_property_quote_sites(node)
|
|
92
99
|
let property_any_quoted any(property_sites, s => s.quoted)
|
|
93
100
|
let property_necessary any(property_sites, s => s.quoted && !s.redundant || keywords && s.keyword)
|
|
@@ -70,7 +70,12 @@ in ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arr
|
|
|
70
70
|
option tuples trailing_comma_mode | null = null "tuple policy"
|
|
71
71
|
let dangle_policies {arrays:arrays, objects:objects, functions:functions, imports:imports, exports:exports, importAttributes:importAttributes, dynamicImports:dynamicImports, enums:enums, generics:generics, tuples:tuples, "type-arguments":"never"}
|
|
72
72
|
let dangle_tsx ends_with(file, ".tsx")
|
|
73
|
-
|
|
73
|
+
# One-line lists without a trailing comma are right under every mode but `always`.
|
|
74
|
+
let dangle_may_be_wrong contains([style, arrays, objects, functions, imports, exports, importAttributes, dynamicImports, enums, generics, tuples], "always")
|
|
75
|
+
|| position(offset).js_line != position(end_offset).js_line
|
|
76
|
+
|| re_test(byte_slice(source, offset, end_offset), /,\s*[)\]}>]|\/\*/)
|
|
77
|
+
when dangle_may_be_wrong
|
|
78
|
+
each dangle_site in if is ts_import_attributes then layout_attribute_sites(layout_attribute_comma_sites, style, dangle_policies) else layout_dangle_lists(node, style, dangle_policies, ecma_version, dangle_tsx, source) {
|
|
74
79
|
when layout_dangle_wrong(dangle_site, dangle_site.mode)
|
|
75
80
|
report at { start: dangle_site.offset, end: dangle_site.offset + (if byte_slice(source, dangle_site.offset, dangle_site.offset + 2) == "\r\n" then 2 else 1) }
|
|
76
81
|
fix at {start:dangle_site.offset, end:dangle_site.offset + (if dangle_site.trailing then 1 else 0)} as if dangle_site.trailing then "" else ","
|
package/std/ts/layout/shared.dx
CHANGED
|
@@ -91,64 +91,63 @@ fn layout_assignment_pattern(n) = {
|
|
|
91
91
|
fn layout_dangle_mode(kind, style, policies) = { value policies; get kind; nonempty } ?? style
|
|
92
92
|
fn layout_dangle_enabled(kind, edition) = (kind != "functions" || edition >= 2017) && (kind != "dynamicImports" || edition >= 2025)
|
|
93
93
|
fn layout_dangle_wrong(site, mode) = mode != "ignore" && (if mode == "always" || mode == "always-multiline" && site.multiline then site.trailing != site.allowed else site.trailing && !(mode == "only-multiline" && site.multiline) && !site.tsx_single)
|
|
94
|
-
fn
|
|
94
|
+
fn layout_trailing_sites(n, items, kind, style, policies, edition, tsx, source) = if count(items) == 0 || !layout_dangle_enabled(kind, edition) then [] else {
|
|
95
95
|
value mode: layout_dangle_mode(kind, style, policies)
|
|
96
|
-
value
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
value item: last(list.items)
|
|
101
|
-
value if item is ts_hole then [] else {
|
|
96
|
+
value item: last(items)
|
|
97
|
+
# Closed right after the last item: no comma, one line, nothing but `always` to ask.
|
|
98
|
+
value if mode == "ignore" || item is ts_hole
|
|
99
|
+
|| mode != "always" && contains([")", "]", "}}", ">"], byte_slice(source, item.end_offset, item.end_offset + 1)) then [] else {
|
|
102
100
|
value after: token(item, "after")
|
|
103
101
|
value trailing: after.text == ","
|
|
104
102
|
value comma: if trailing then after.node ?? first(overlapping(ts_comma, after.start, after.end)) else null
|
|
105
103
|
value closing: if trailing then token(comma, "after") else after
|
|
106
104
|
value if closing.text != "}}" && closing.text != "]" && closing.text != ")" && closing.text != ">" then [] else {
|
|
107
105
|
value terminal_end: if trailing then after.end else token(item, "last").end
|
|
108
|
-
value multiline: position(terminal_end).js_line != position(closing.start).js_line
|
|
109
|
-
value if !multiline && !trailing &&
|
|
106
|
+
value multiline: terminal_end != closing.start && position(terminal_end).js_line != position(closing.start).js_line
|
|
107
|
+
value if !multiline && !trailing && mode != "always" then [] else {
|
|
110
108
|
value allowed: item not ts_rest && !(item is ts_spread && layout_assignment_pattern(n))
|
|
111
109
|
&& item.name not ts_rest
|
|
112
110
|
value [{
|
|
113
|
-
kind:
|
|
114
|
-
mode:
|
|
111
|
+
kind: kind,
|
|
112
|
+
mode: mode,
|
|
115
113
|
offset: if trailing then after.start else terminal_end,
|
|
116
114
|
trailing: trailing,
|
|
117
115
|
multiline: multiline,
|
|
118
116
|
allowed: allowed,
|
|
119
|
-
tsx_single:
|
|
117
|
+
tsx_single: kind == "generics" && count(items) == 1 && tsx
|
|
120
118
|
}]
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
}
|
|
124
122
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
123
|
+
in ts_call, ts_new, ts_import_call let layout_dangle_items argument.nodes
|
|
124
|
+
in ts_function, ts_method, ts_arrow, ts_function_type let layout_dangle_items parameter.nodes
|
|
125
|
+
in ts_object let layout_dangle_items property.nodes
|
|
126
|
+
in ts_array, ts_array_pattern, ts_tuple_type let layout_dangle_items element.nodes
|
|
127
|
+
in ts_object_pattern let layout_dangle_items { value property.nodes rest.nodes; sort_by (item) item.offset }
|
|
128
|
+
in ts_import let layout_dangle_items { value specifier.nodes default_import.nodes namespace.nodes; sort_by (item) item.offset }
|
|
129
|
+
in ts_export let layout_dangle_items specifier.nodes
|
|
130
|
+
in ts_enum let layout_dangle_items member.nodes
|
|
131
|
+
in ts_call, ts_new, ts_function, ts_method, ts_arrow, ts_function_type let layout_dangle_kind "functions"
|
|
132
|
+
in ts_object, ts_object_pattern let layout_dangle_kind "objects"
|
|
133
|
+
in ts_array, ts_array_pattern let layout_dangle_kind "arrays"
|
|
134
|
+
in ts_tuple_type let layout_dangle_kind "tuples"
|
|
135
|
+
in ts_import_call let layout_dangle_kind "dynamicImports"
|
|
136
|
+
in ts_import let layout_dangle_kind "imports"
|
|
137
|
+
in ts_export let layout_dangle_kind "exports"
|
|
138
|
+
in ts_enum let layout_dangle_kind "enums"
|
|
139
|
+
fn layout_dangle_lists(n, style, policies, edition, tsx, source) = {
|
|
140
|
+
value ordinary: if n.layout_dangle_kind != null then layout_trailing_sites(n, n.layout_dangle_items, n.layout_dangle_kind, style, policies, edition, tsx, source) else []
|
|
141
|
+
value if n.type_parameter == null && n.type_argument == null then ordinary else {
|
|
142
|
+
value generics: if n.type_parameter != null && n is (ts_function, ts_arrow, ts_class, ts_method, ts_interface, ts_type_alias, ts_function_type,
|
|
143
|
+
ts_method_signature, ts_call_signature, ts_infer) then layout_trailing_sites(n, n.type_parameter.nodes, "generics", style, policies, edition, tsx, source) else []
|
|
144
|
+
value arguments: if n.type_argument != null && n is (ts_class, ts_tagged_template, ts_call, ts_new, ts_instantiation, ts_jsx_opening, ts_type_reference,
|
|
145
|
+
ts_type_query, ts_import_type) then layout_trailing_sites(n, n.type_argument.nodes, "type-arguments", style, policies, edition, tsx, source) else []
|
|
146
|
+
value concat(concat(ordinary, generics), arguments)
|
|
147
|
+
}
|
|
149
148
|
}
|
|
150
149
|
# Every list, independent of options: fixtures pin this view.
|
|
151
|
-
fn layout_trailing_commas(n) = layout_dangle_lists(n, "always", {}, 2025, ends_with(n.file, ".tsx"))
|
|
150
|
+
fn layout_trailing_commas(n) = layout_dangle_lists(n, "always", {}, 2025, ends_with(n.file, ".tsx"), null)
|
|
152
151
|
fn layout_terminal_arrow_block(n, end) = {
|
|
153
152
|
value chain: walk([n], current => if current is ts_block then null
|
|
154
153
|
else find(current.children.nodes, child => child.end_offset == end))
|