@inthedark/dxlint 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/bin/dxlint +15 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +40 -4
- package/rules/conventions.dx +34 -0
- package/rules/correctness.dx +33 -0
- package/rules/es-no-semi.dx +11 -0
- package/rules/format.dx +49 -0
- package/rules/recommended.dx +51 -0
- package/rules/suspicious.dx +31 -0
- package/rules/types.dx +21 -0
- package/std/c.dx +23 -0
- package/std/core/lines.dx +51 -0
- package/std/core/lists.dx +76 -0
- package/std/core.dx +30 -0
- package/std/css.dx +17 -0
- package/std/dx.dx +31 -0
- package/std/json.dx +104 -0
- package/std/markdown.dx +11 -0
- package/std/python.dx +21 -0
- package/std/rust/format.dx +316 -0
- package/std/rust.dx +34 -0
- package/std/toml.dx +19 -0
- package/std/ts/arithmetic.dx +87 -0
- package/std/ts/assertions.dx +477 -0
- package/std/ts/calls.dx +246 -0
- package/std/ts/comments.dx +10 -0
- package/std/ts/conditions.dx +531 -0
- package/std/ts/constants.dx +335 -0
- package/std/ts/deprecation.dx +185 -0
- package/std/ts/directives.dx +60 -0
- package/std/ts/effects.dx +219 -0
- package/std/ts/enums.dx +233 -0
- package/std/ts/expression-fixes.dx +67 -0
- package/std/ts/expressions.dx +270 -0
- package/std/ts/flow.dx +2 -0
- package/std/ts/format.dx +755 -0
- package/std/ts/globals.dx +106 -0
- package/std/ts/grammar.dx +488 -0
- package/std/ts/helpers.dx +102 -0
- package/std/ts/import-attributes.dx +88 -0
- package/std/ts/import-no-deprecated.dx +73 -0
- package/std/ts/indentation.dx +442 -0
- package/std/ts/jsx.dx +646 -0
- package/std/ts/layout/blocks.dx +281 -0
- package/std/ts/layout/calls.dx +79 -0
- package/std/ts/layout/expressions.dx +213 -0
- package/std/ts/layout/literals.dx +114 -0
- package/std/ts/layout/separators.dx +184 -0
- package/std/ts/layout/shared.dx +203 -0
- package/std/ts/layout/text.dx +142 -0
- package/std/ts/layout/types.dx +182 -0
- package/std/ts/layout.dx +9 -0
- package/std/ts/license.dx +54 -0
- package/std/ts/literal-text.dx +3 -0
- package/std/ts/literals.dx +95 -0
- package/std/ts/methods.dx +69 -0
- package/std/ts/modules.dx +268 -0
- package/std/ts/names.dx +348 -0
- package/std/ts/naming.dx +169 -0
- package/std/ts/narrowing.dx +96 -0
- package/std/ts/parentheses.dx +262 -0
- package/std/ts/precedence.dx +25 -0
- package/std/ts/problems/calls.dx +34 -0
- package/std/ts/problems/classes.dx +39 -0
- package/std/ts/problems/control-flow.dx +257 -0
- package/std/ts/problems/declarations.dx +67 -0
- package/std/ts/problems/duplicates.dx +70 -0
- package/std/ts/problems/expressions.dx +154 -0
- package/std/ts/problems/helpers.dx +22 -0
- package/std/ts/problems/imports.dx +43 -0
- package/std/ts/problems/non-null.dx +78 -0
- package/std/ts/problems/numeric-precision.dx +66 -0
- package/std/ts/problems/regexp.dx +66 -0
- package/std/ts/problems/statements.dx +36 -0
- package/std/ts/problems/typescript.dx +55 -0
- package/std/ts/problems/whitespace.dx +40 -0
- package/std/ts/problems.dx +20 -0
- package/std/ts/regex/checks.dx +249 -0
- package/std/ts/regex/findings.dx +209 -0
- package/std/ts/regex/grammar.dx +80 -0
- package/std/ts/regex/suggestions.dx +94 -0
- package/std/ts/regex/values.dx +378 -0
- package/std/ts/regex.dx +5 -0
- package/std/ts/restrictions/comments.dx +76 -0
- package/std/ts/restrictions.dx +811 -0
- package/std/ts/scopes.dx +68 -0
- package/std/ts/scoping/atomic.dx +40 -0
- package/std/ts/scoping/bindings.dx +126 -0
- package/std/ts/scoping/constness.dx +72 -0
- package/std/ts/scoping/globals.dx +52 -0
- package/std/ts/scoping/loops.dx +86 -0
- package/std/ts/scoping/member-uses.dx +111 -0
- package/std/ts/scoping/receivers.dx +107 -0
- package/std/ts/scoping/shadowing.dx +202 -0
- package/std/ts/scoping/usage.dx +393 -0
- package/std/ts/scoping.dx +842 -0
- package/std/ts/selectors.dx +10 -0
- package/std/ts/spreads.dx +97 -0
- package/std/ts/structure/declarations.dx +52 -0
- package/std/ts/structure/lines.dx +79 -0
- package/std/ts/structure/lists.dx +144 -0
- package/std/ts/structure/padding.dx +258 -0
- package/std/ts/structure.dx +963 -0
- package/std/ts/style/classes.dx +157 -0
- package/std/ts/style/comments.dx +75 -0
- package/std/ts/style/declarations.dx +82 -0
- package/std/ts/style/expressions.dx +125 -0
- package/std/ts/style/imports.dx +44 -0
- package/std/ts/style/ordering.dx +192 -0
- package/std/ts/style/returns.dx +152 -0
- package/std/ts/style/signatures.dx +308 -0
- package/std/ts/style/type-exports.dx +62 -0
- package/std/ts/style/type-imports.dx +166 -0
- package/std/ts/style/types.dx +474 -0
- package/std/ts/style.dx +29 -0
- package/std/ts/suggestions/accessors.dx +138 -0
- package/std/ts/suggestions/builtins.dx +110 -0
- package/std/ts/suggestions/comparisons.dx +41 -0
- package/std/ts/suggestions/computed-keys.dx +20 -0
- package/std/ts/suggestions/constructors.dx +54 -0
- package/std/ts/suggestions/control-flow.dx +204 -0
- package/std/ts/suggestions/curly.dx +124 -0
- package/std/ts/suggestions/directives.dx +79 -0
- package/std/ts/suggestions/errors.dx +106 -0
- package/std/ts/suggestions/expressions.dx +230 -0
- package/std/ts/suggestions/functions.dx +260 -0
- package/std/ts/suggestions/globals.dx +187 -0
- package/std/ts/suggestions/helpers.dx +17 -0
- package/std/ts/suggestions/object-shorthand.dx +148 -0
- package/std/ts/suggestions/object-spread.dx +96 -0
- package/std/ts/suggestions/regexp.dx +59 -0
- package/std/ts/suggestions/renames.dx +50 -0
- package/std/ts/suggestions/strings.dx +47 -0
- package/std/ts/suggestions/templates.dx +104 -0
- package/std/ts/suggestions.dx +24 -0
- package/std/ts/template-fixes.dx +22 -0
- package/std/ts/tokens.dx +124 -0
- package/std/ts/type-specifiers.dx +93 -0
- package/std/ts/typed/assignments.dx +154 -0
- package/std/ts/typed/awaitability.dx +157 -0
- package/std/ts/typed/boolean-comparisons.dx +82 -0
- package/std/ts/typed/defaults.dx +97 -0
- package/std/ts/typed/enum-consistency.dx +68 -0
- package/std/ts/typed/enums.dx +52 -0
- package/std/ts/typed/errors.dx +32 -0
- package/std/ts/typed/floating-promises.dx +147 -0
- package/std/ts/typed/for-in-arrays.dx +21 -0
- package/std/ts/typed/includes.dx +102 -0
- package/std/ts/typed/nullish-coalescing.dx +149 -0
- package/std/ts/typed/optional-chains.dx +317 -0
- package/std/ts/typed/promises.dx +205 -0
- package/std/ts/typed/qualifiers.dx +108 -0
- package/std/ts/typed/readonly-members.dx +159 -0
- package/std/ts/typed/readonly-parameters.dx +121 -0
- package/std/ts/typed/redundant-constituents.dx +171 -0
- package/std/ts/typed/regexp-exec.dx +79 -0
- package/std/ts/typed/rejection-handlers.dx +91 -0
- package/std/ts/typed/return-await.dx +116 -0
- package/std/ts/typed/return-this.dx +67 -0
- package/std/ts/typed/strict-void-return.dx +121 -0
- package/std/ts/typed/string-boundaries.dx +153 -0
- package/std/ts/typed/switches.dx +147 -0
- package/std/ts/typed/template-expressions.dx +127 -0
- package/std/ts/typed/template-values.dx +88 -0
- package/std/ts/typed/transfers.dx +106 -0
- package/std/ts/typed/type-arguments.dx +337 -0
- package/std/ts/typed/type-constituents.dx +143 -0
- package/std/ts/typed/type-conversions.dx +82 -0
- package/std/ts/typed/type-parameters.dx +280 -0
- package/std/ts/typed/unbound-method.dx +167 -0
- package/std/ts/typed/unsafe-calls.dx +73 -0
- package/std/ts/typed/unsafe-members.dx +36 -0
- package/std/ts/typed/void-expressions.dx +101 -0
- package/std/ts/typed.dx +585 -0
- package/std/ts/values/arguments.dx +13 -0
- package/std/ts/values/enums.dx +104 -0
- package/std/ts/values/inspection.dx +274 -0
- package/std/ts/values.dx +1217 -0
- package/std/ts.dx +77 -0
- package/std/whitespace.dx +37 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Non-null assertions and optional chains used where a short circuit throws.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_parenthesized let under_non_null parent is ts_non_null || parent is ts_parenthesized && parent.under_non_null == true
|
|
6
|
+
# The object or callee of `?.`, through parentheses.
|
|
7
|
+
group optional_chain_head ts_non_null, ts_parenthesized
|
|
8
|
+
in optional_chain_head let heads_optional_chain is (ts_member.object, ts_index.object) && parent.optional != null
|
|
9
|
+
|| is ts_call.callee && parent.optional != null
|
|
10
|
+
|| parent is ts_parenthesized && parent.heads_optional_chain == true
|
|
11
|
+
in ts_non_null rule es-no-extra-non-null-assertion {
|
|
12
|
+
when parent is ts_non_null || parent is ts_parenthesized && parent.under_non_null == true || heads_optional_chain
|
|
13
|
+
fix as { value result: len(text) - 1; value slice(text, 0, result) }
|
|
14
|
+
warn "Forbidden extra non-null assertion."
|
|
15
|
+
}
|
|
16
|
+
# `x?.y!` (at the end of the chain), `(x?.y)!`.
|
|
17
|
+
@macro non_null_chain_message "Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong."
|
|
18
|
+
in ts_non_null rule es-no-non-null-asserted-optional-chain {
|
|
19
|
+
when expression.in_optional_chain == true
|
|
20
|
+
when not (ts_member.object, ts_index.object, ts_call.callee)
|
|
21
|
+
warn @non_null_chain_message
|
|
22
|
+
}
|
|
23
|
+
in ts_non_null.expression rule es-no-non-null-asserted-optional-chain:paren {
|
|
24
|
+
when is ts_parenthesized && wraps_optional_chain == true
|
|
25
|
+
report at unparenthesised_node(node)
|
|
26
|
+
warn @non_null_chain_message
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# Each optional chain whose `undefined` the node would trip on, at the chain.
|
|
30
|
+
fn problem_optional_pattern(n) = unparenthesised_node(n) is (ts_array, ts_array_pattern, ts_object, ts_object_pattern)
|
|
31
|
+
fn problem_optional_operand(n) =
|
|
32
|
+
if n is ts_assignment && n.operator == "=" && problem_optional_pattern(n.left.node)
|
|
33
|
+
|| n is ts_assignment_pattern && problem_optional_pattern(n.left.node) then n.right.node
|
|
34
|
+
else if n is (ts_declarator, ts_parameter) && problem_optional_pattern(n.name.node) then n.init.node
|
|
35
|
+
else if n is ts_class then n.extends.node
|
|
36
|
+
else if n is ts_call && n.optional == null || n is ts_new then n.callee.node
|
|
37
|
+
else if n is (ts_member, ts_index) && n.optional == null then n.object.node
|
|
38
|
+
else if n is ts_tagged_template then n.tag.node
|
|
39
|
+
else if n is ts_for_of || n is ts_binary && contains(["in", "instanceof"], n.operator) then n.right.node
|
|
40
|
+
else if n is ts_spread && n.parent not ts_object || n is ts_yield && n.operator == "*" then n.argument.node
|
|
41
|
+
else if n is ts_with then n.object.node
|
|
42
|
+
else null
|
|
43
|
+
fn problem_optional_root(n) = {
|
|
44
|
+
value n
|
|
45
|
+
walk (current) if current is (ts_member.object, ts_index.object, ts_call.callee, ts_non_null.expression)
|
|
46
|
+
&& current.parent.in_optional_chain == true then current.parent.node else null
|
|
47
|
+
last
|
|
48
|
+
}
|
|
49
|
+
fn problem_optional_use(n) = {
|
|
50
|
+
value n
|
|
51
|
+
walk (current) {
|
|
52
|
+
value parent: current.parent.node
|
|
53
|
+
value if parent is ts_parenthesized || parent is ts_await
|
|
54
|
+
|| parent is ts_logical && (parent.operator == "&&" || current is ts_logical.right)
|
|
55
|
+
|| parent is ts_sequence && current == last(parent.expression.nodes)
|
|
56
|
+
|| current is (ts_conditional.consequent, ts_conditional.alternate)
|
|
57
|
+
then parent else null
|
|
58
|
+
}
|
|
59
|
+
last
|
|
60
|
+
}
|
|
61
|
+
in ts_member.optional, ts_index.optional, ts_call.optional rule es-no-unsafe-optional-chaining {
|
|
62
|
+
option disallowArithmeticOperators boolean = false
|
|
63
|
+
when (if parent is ts_call then parent.callee.in_optional_chain else parent.object.in_optional_chain) != true
|
|
64
|
+
let chain problem_optional_root(parent.node)
|
|
65
|
+
when chain is (ts_member, ts_index, ts_call)
|
|
66
|
+
let operand problem_optional_use(chain)
|
|
67
|
+
let user operand.parent.node
|
|
68
|
+
let arithmetic disallowArithmeticOperators
|
|
69
|
+
&& (user is ts_binary && contains(["+", "-", "/", "*", "%", "**"], user.operator)
|
|
70
|
+
|| user is ts_unary && contains(["+", "-"], user.operator)
|
|
71
|
+
|| user is ts_assignment && contains(["+=", "-=", "/=", "*=", "%=", "**="], user.operator) && operand == user.right.node)
|
|
72
|
+
when arithmetic || problem_optional_operand(user) == operand
|
|
73
|
+
report at chain
|
|
74
|
+
warn if !arithmetic then "Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError."
|
|
75
|
+
else "Unsafe arithmetic operation on optional chaining. It can result in NaN."
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn problem_number_significand(text, as_float) = {
|
|
4
|
+
value source: if !as_float && ends_with(text, ".") then slice(text, 0, -1) else text
|
|
5
|
+
value trimmed: trim_start(source, "0")
|
|
6
|
+
value digits: if trimmed == "" then source else trimmed
|
|
7
|
+
value parts: split(digits, ".")
|
|
8
|
+
value if as_float || count(parts) > 1 then
|
|
9
|
+
if first(parts) == "" then {
|
|
10
|
+
value fraction: last(parts)
|
|
11
|
+
value significant: trim_start(fraction, "0")
|
|
12
|
+
value coefficient: if significant == "" then fraction else significant
|
|
13
|
+
value { coefficient: coefficient, magnitude: len(coefficient) - len(digits) }
|
|
14
|
+
} else {
|
|
15
|
+
value { coefficient: replace(digits, ".", ""), magnitude: len(first(parts)) - 1 }
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
value significant: trim_end(digits, "0")
|
|
19
|
+
value { coefficient: if significant == "" then digits else significant, magnitude: len(digits) - 1 }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn problem_number_scientific(text, as_float) = {
|
|
24
|
+
value parts: split(text, "e")
|
|
25
|
+
value number: problem_number_significand(first(parts), as_float)
|
|
26
|
+
value adjustment: if count(parts) == 1 then 0 else js_number(last(parts))
|
|
27
|
+
value { coefficient: number.coefficient, magnitude: number.magnitude + adjustment }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn problem_number_precision(text) = {
|
|
31
|
+
value raw: lower(replace(text, "_", ""))
|
|
32
|
+
value legacy_octal: re_test(raw, /^0[0-7]+$/)
|
|
33
|
+
value number: js_number(if legacy_octal then "0o" slice(raw, 1) else raw)
|
|
34
|
+
value when {
|
|
35
|
+
number == null then false
|
|
36
|
+
re_test(raw, /^0[xbo]/) || legacy_octal then {
|
|
37
|
+
value radix: when {
|
|
38
|
+
starts_with(raw, "0x") then 16
|
|
39
|
+
starts_with(raw, "0b") then 2
|
|
40
|
+
else then 8
|
|
41
|
+
}
|
|
42
|
+
value stored: integer_radix(number, radix)
|
|
43
|
+
value stored == null || !ends_with(raw, stored)
|
|
44
|
+
}
|
|
45
|
+
else then {
|
|
46
|
+
value intended: problem_number_scientific(raw, false)
|
|
47
|
+
value precision: len(intended.coefficient)
|
|
48
|
+
value when {
|
|
49
|
+
number == 0 then re_test(intended.coefficient, /[1-9]/)
|
|
50
|
+
precision > 100 then true
|
|
51
|
+
else then {
|
|
52
|
+
value stored: problem_number_scientific(js_number_to_precision(number, precision), true)
|
|
53
|
+
value intended.coefficient != stored.coefficient || intended.magnitude != stored.magnitude
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
in ts_number rule es-no-loss-of-precision {
|
|
61
|
+
when !re_test(text, /^[0-9]{1,15}$/)
|
|
62
|
+
when problem_number_precision(text)
|
|
63
|
+
warn "This number literal will lose precision at runtime."
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Regular expression validation rules.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn backreference_reason(kind) = (if kind == "nested" then "from within that group" else if kind == "forward" then "which appears later in the pattern" else if kind == "backward" then "which appears before in the same lookbehind" else if kind == "disjunctive" then "which is in another alternative" else "which is in a negative lookaround")
|
|
6
|
+
fn misleading_message(kind) = (if kind == "surrogatePairWithoutUFlag" then "Unexpected surrogate pair in character class. Use 'u' flag." else if kind == "surrogatePair" then "Unexpected surrogate pair in character class." else if kind == "combiningClass" then "Unexpected combined character in character class." else if kind == "emojiModifier" then "Unexpected modified Emoji in character class." else if kind == "regionalIndicatorSymbol" then "Unexpected national flag in character class." else "Unexpected joined character sequence in character class.")
|
|
7
|
+
|
|
8
|
+
in ts_regex rule es-no-empty-character-class {
|
|
9
|
+
when contains(text, "[")
|
|
10
|
+
each f in regexp_empty_class_findings {
|
|
11
|
+
warn "Empty class."
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
# On literals, and on plain string patterns of `RegExp(...)` (at the string).
|
|
16
|
+
in regexp_site rule es-no-control-regex {
|
|
17
|
+
when is ts_regex || regexp_candidate
|
|
18
|
+
when is ts_regex || regexp_input.written == "string"
|
|
19
|
+
each f in regexp_control_findings {
|
|
20
|
+
report at regexp_input.pattern_node
|
|
21
|
+
warn "Unexpected control character(s) in regular expression: {first(f.data)}."
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
in regexp_site rule es-no-useless-backreference {
|
|
26
|
+
when is ts_regex || regexp_candidate
|
|
27
|
+
each f in regexp_backreference_findings {
|
|
28
|
+
warn "Backreference '{first(f.data)}' will be ignored. It references group '{ { value f.data; slice 1; first } }'{last(f.data)} {backreference_reason(f.kind)}."
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# Each sequence where it is written (or at the pattern when it is
|
|
33
|
+
# computed). With computed flags, nothing is known.
|
|
34
|
+
in regexp_site let misleading_character_class_findings regexp_misleading_findings
|
|
35
|
+
in regexp_site rule es-no-misleading-character-class {
|
|
36
|
+
when is ts_regex || regexp_candidate
|
|
37
|
+
when type_of(regexp_input.flags) == "str" || is ts_regex
|
|
38
|
+
each f in misleading_character_class_findings {
|
|
39
|
+
report at { start: f.offset, end: f.end }
|
|
40
|
+
warn misleading_message(f.kind)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# Literals, and `RegExp` calls with a string literal pattern and flags.
|
|
45
|
+
in regexp_site rule es-no-regex-spaces {
|
|
46
|
+
when is ts_regex || regexp_candidate
|
|
47
|
+
let spaces_argument if is (ts_call, ts_new) then { value argument.nodes; first; unparenthesised_node } else null
|
|
48
|
+
let spaces_fixable is ts_regex || regexp_input.source == slice(spaces_argument.text, 1, -1)
|
|
49
|
+
when is ts_regex || regexp_input.written == "string" && type_of(regexp_flags) == "str"
|
|
50
|
+
each f in regexp_space_findings {
|
|
51
|
+
fix at { start: f.offset, end: f.end } if spaces_fixable as " {{{f.kind}}}"
|
|
52
|
+
warn "Spaces are hard to count. Use {{{f.kind}}}."
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
# The flags first (`RegExp` checks them first), then the pattern.
|
|
57
|
+
in ts_call, ts_new rule es-no-invalid-regexp {
|
|
58
|
+
option allowConstructorFlags string[] = [] "flags beyond the standard ones that are allowed"
|
|
59
|
+
when regexp_candidate
|
|
60
|
+
when callee.global_value == "RegExp"
|
|
61
|
+
let flags_problem if regexp_flags == null then null else regex_flags_error(regexp_flags, allowConstructorFlags)
|
|
62
|
+
when flags_problem != null || regexp_pattern_error != null
|
|
63
|
+
warn "{ flags_problem.nonempty ?? regexp_pattern_error }."
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Statement-level mistakes: debugger, empty patterns, octal and array holes.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_debugger rule es-no-debugger warn "Unexpected 'debugger' statement."
|
|
6
|
+
|
|
7
|
+
in ts_object_pattern let empty_object_pattern_checked true
|
|
8
|
+
in ts_object_pattern rule es-no-empty-pattern:object {
|
|
9
|
+
when count(property) == 0 && rest == null
|
|
10
|
+
when empty_object_pattern_checked
|
|
11
|
+
warn "Unexpected empty object pattern."
|
|
12
|
+
}
|
|
13
|
+
in ts_array_pattern rule es-no-empty-pattern:array {
|
|
14
|
+
when count(element) == 0
|
|
15
|
+
warn "Unexpected empty array pattern."
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
in ts_static_block.body rule es-no-empty-static-block {
|
|
19
|
+
when re_test(text, /^\{\s*\}$/)
|
|
20
|
+
warn "Unexpected empty static block."
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
in ts_number rule es-no-octal {
|
|
24
|
+
when re_test(text, /^0[0-9]/)
|
|
25
|
+
warn "Octal literals should not be used."
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
# Every hole, at its comma.
|
|
29
|
+
in ts_array let is_assignment_target parent is ts_assignment && parent.left == text || parent is (ts_for_of, ts_for_in)
|
|
30
|
+
in ts_hole rule es-no-sparse-arrays {
|
|
31
|
+
when parent is ts_array && !parent.is_assignment_target
|
|
32
|
+
report at { start: offset, end: offset + 1 }
|
|
33
|
+
warn "Unexpected comma in middle of array."
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# TypeScript-specific declarations: directives, Function types and interfaces.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_class.name, ts_interface.name rule es-no-unsafe-declaration-merging {
|
|
6
|
+
when contains(declaration_kinds, "class") && contains(declaration_kinds, "interface")
|
|
7
|
+
warn "Unsafe declaration merging between classes and interfaces."
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
# The directive and its description, as TypeScript reads them: `// @ts-x`
|
|
11
|
+
# (`///` too for check/nocheck) or the last line of a block comment.
|
|
12
|
+
in ts_comment let ts_comment_body if is ts_line_comment then slice(text, 2) else { value result: len(text) - 2; value slice(text, 2, result); re_replace /^[\s\S]*(?:\r\n|[\r\n\u{2028}\u{2029}])/ "" }
|
|
13
|
+
in ts_comment let ts_pragma is ts_line_comment && re_test(text, /^\/\/\/?\s*@ts-(?:check|nocheck)/)
|
|
14
|
+
in ts_comment let ts_suppress if is ts_line_comment then re_test(ts_comment_body, /^\/*\s*@ts-(?:expect-error|ignore)/) else re_test(ts_comment_body, /^\s*(?:\/|\*)*\s*@ts-(?:expect-error|ignore)/)
|
|
15
|
+
in ts_comment let ts_directive if ts_pragma then re_replace(text, /^\/\/\/?\s*@ts-(check|nocheck)[\s\S]*$/, "$1") else if ts_suppress then re_replace(ts_comment_body, /^[\/*\s]*@ts-(expect-error|ignore)[\s\S]*$/, "$1") else null
|
|
16
|
+
in ts_comment let ts_directive_description if ts_pragma then re_replace(text, /^\/\/\/?\s*@ts-(?:check|nocheck)([^\r\n\u{2028}\u{2029}]*)[\s\S]*$/, "$1") else re_replace(ts_comment_body, /^[\/*\s]*@ts-(?:expect-error|ignore)([^\r\n\u{2028}\u{2029}]*)[\s\S]*$/, "$1")
|
|
17
|
+
# `@ts-nocheck` only counts before the first statement.
|
|
18
|
+
in ts_comment let ts_directive_effective ts_directive != "nocheck" || { value overlapping(ts_program); first }.first_token.start == null || offset < { value overlapping(ts_program); first }.first_token.start
|
|
19
|
+
fn ts_directive_message(directive, description, mode, minimum, pattern) = (
|
|
20
|
+
if mode == "allow" then null
|
|
21
|
+
else if mode == "deny" && directive == "ignore" then "Use \"@ts-expect-error\" instead of \"@ts-ignore\", as \"@ts-ignore\" will do nothing if the following line is error-free."
|
|
22
|
+
else if mode == "deny" then "Do not use \"@ts-{directive}\" because it alters compilation errors."
|
|
23
|
+
else if { value description; trim; graphemes } < minimum then "Include a description after the \"@ts-{directive}\" directive to explain why the @ts-{directive} is necessary. The description must be {minimum} characters or longer."
|
|
24
|
+
else if pattern != null && !re_test(description, pattern) then "The description for the \"@ts-{directive}\" directive must match the {pattern} format."
|
|
25
|
+
else null
|
|
26
|
+
)
|
|
27
|
+
@macro ban_ts_directive(name, mode_default) {
|
|
28
|
+
default option mode enum(allow, deny, require-description) = mode_default
|
|
29
|
+
option descriptionPattern regex | null = null
|
|
30
|
+
option minimumDescriptionLength int(0..) = 3
|
|
31
|
+
when ts_directive == name && ts_directive_effective
|
|
32
|
+
let message ts_directive_message(ts_directive, ts_directive_description, mode, minimumDescriptionLength, descriptionPattern)
|
|
33
|
+
when message != null
|
|
34
|
+
warn message
|
|
35
|
+
}
|
|
36
|
+
in ts_comment rule es-ban-ts-comment:expect-error { @ban_ts_directive("expect-error", require-description) }
|
|
37
|
+
in ts_comment rule es-ban-ts-comment:ignore { @ban_ts_directive("ignore", deny) }
|
|
38
|
+
in ts_comment rule es-ban-ts-comment:nocheck { @ban_ts_directive("nocheck", deny) }
|
|
39
|
+
in ts_comment rule es-ban-ts-comment:check { @ban_ts_directive("check", allow) }
|
|
40
|
+
|
|
41
|
+
# `Function` as a type (not a local type of that name), also `implements
|
|
42
|
+
# Function`.
|
|
43
|
+
in ts_type_reference rule es-no-unsafe-function-type {
|
|
44
|
+
when name == "Function" && name.resolved == null && name.reference_kind == null
|
|
45
|
+
warn "The `Function` type accepts any function-like value.\nPrefer explicitly defining any function parameters and return type."
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
in ts_interface rule es-no-empty-interface warn "{if count(extends) > 0 then 'An interface declaring no members is equivalent to its supertype.' else 'An empty interface is equivalent to `{{}}`.'}" {
|
|
49
|
+
when count(member) == 0
|
|
50
|
+
# Several supertypes make a useful intersection.
|
|
51
|
+
when count(extends) <= 1
|
|
52
|
+
report at { start: opener.ts_open_brace.offset, end: closer.ts_close_brace.end_offset }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Irregular whitespace and unexpected newlines.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
# Runs of irregular spaces, and each irregular line terminator. A BOM
|
|
6
|
+
# starting the file is not one.
|
|
7
|
+
group problem_template ts_template, ts_template_type
|
|
8
|
+
in ts_program layer text match problem_irregular_ws /[\u{000B}\u{000C}\u{0085}\u{00A0}\u{1680}\u{180E}\u{2000}-\u{200B}\u{202F}\u{205F}\u{3000}\u{FEFF}]+|[\u{2028}\u{2029}]/
|
|
9
|
+
in problem_irregular_ws let irregular_whitespace_checked { value overlapping(ts_string); count } == 0
|
|
10
|
+
in problem_irregular_ws rule es-no-irregular-whitespace {
|
|
11
|
+
when !(offset == 0 && text == "\u{FEFF}")
|
|
12
|
+
when irregular_whitespace_checked
|
|
13
|
+
warn "Irregular whitespace not allowed."
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
in ts_call let problem_multiline_opener if optional == null && count(argument) > 0 then
|
|
17
|
+
(if count(type_argument) == 0 then opener.ts_open_paren.node else opener.ts_open_angle.node)
|
|
18
|
+
else null
|
|
19
|
+
in ts_index let problem_multiline_opener if optional == null then opener.ts_open_bracket.node else null
|
|
20
|
+
in ts_call.problem_multiline_opener, ts_index.problem_multiline_opener, ts_tagged_template.quasi, ts_binary.operator rule es-no-unexpected-multiline {
|
|
21
|
+
when is (ts_open_paren, ts_open_angle, ts_open_bracket, ts_template) || text == "/"
|
|
22
|
+
when re_test(byte_slice(source, token(node, "before").end ?? offset, offset), /[\r\n\u{2028}\u{2029}]/)
|
|
23
|
+
let multiline_kind if parent is ts_call && parent.optional == null && count(parent.argument) > 0
|
|
24
|
+
&& (is ts_open_paren && count(parent.type_argument) == 0 || is ts_open_angle && count(parent.type_argument) > 0) then "function"
|
|
25
|
+
else if is ts_open_bracket && parent is ts_index && parent.optional == null then "property"
|
|
26
|
+
else if is ts_tagged_template.quasi then "taggedTemplate"
|
|
27
|
+
else if is ts_binary.operator && text == "/" && parent is ts_binary.left && parent.operator == "/" && parent.parent.operator == "/" then {
|
|
28
|
+
value flags: parent.parent.right.first_token
|
|
29
|
+
value slash: parent.parent.right.token_neighbours.before
|
|
30
|
+
value if flags is ts_identifier && flags.text ~ /^[dgimsuvy]+$/ && slash.text == "/" && slash.end == flags.start then "division" else null
|
|
31
|
+
} else null
|
|
32
|
+
when multiline_kind != null
|
|
33
|
+
report at { start: offset, end: offset + 1 }
|
|
34
|
+
warn if multiline_kind == "function" then "Unexpected newline between function and ( of function call."
|
|
35
|
+
else if multiline_kind == "property" then "Unexpected newline between object and [ of property access."
|
|
36
|
+
else if multiline_kind == "taggedTemplate" then "Unexpected newline between template tag and template literal."
|
|
37
|
+
else "Unexpected newline between numerator and division operator."
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# .ts/problems: ESLint core "possible problems" and typescript-eslint rules
|
|
2
|
+
# on the native TypeScript layers. Messages and positions follow ESLint's.
|
|
3
|
+
|
|
4
|
+
import .ts/helpers
|
|
5
|
+
import .ts/constants
|
|
6
|
+
import .ts/problems/numeric-precision
|
|
7
|
+
import .ts/scoping/member-uses
|
|
8
|
+
import .ts/problems/helpers
|
|
9
|
+
import .ts/problems/control-flow
|
|
10
|
+
import .ts/problems/statements
|
|
11
|
+
import .ts/problems/calls
|
|
12
|
+
import .ts/problems/expressions
|
|
13
|
+
import .ts/problems/duplicates
|
|
14
|
+
import .ts/problems/imports
|
|
15
|
+
import .ts/problems/typescript
|
|
16
|
+
import .ts/problems/non-null
|
|
17
|
+
import .ts/problems/classes
|
|
18
|
+
import .ts/problems/declarations
|
|
19
|
+
import .ts/problems/regexp
|
|
20
|
+
import .ts/problems/whitespace
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import .ts/constants
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn regexp_categories(value) = re_test(value, /^(?:C|Other|Cc|Control|cntrl|Cf|Format|Cn|Unassigned|Co|Private_Use|Cs|Surrogate|L|Letter|LC|Cased_Letter|Ll|Lowercase_Letter|Lm|Modifier_Letter|Lo|Other_Letter|Lt|Titlecase_Letter|Lu|Uppercase_Letter|M|Mark|Combining_Mark|Mc|Spacing_Mark|Me|Enclosing_Mark|Mn|Nonspacing_Mark|N|Number|Nd|Decimal_Number|digit|Nl|Letter_Number|No|Other_Number|P|Punctuation|punct|Pc|Connector_Punctuation|Pd|Dash_Punctuation|Pe|Close_Punctuation|Pf|Final_Punctuation|Pi|Initial_Punctuation|Po|Other_Punctuation|Ps|Open_Punctuation|S|Symbol|Sc|Currency_Symbol|Sk|Modifier_Symbol|Sm|Math_Symbol|So|Other_Symbol|Z|Separator|Zl|Line_Separator|Zp|Paragraph_Separator|Zs|Space_Separator)$/)
|
|
6
|
+
fn regexp_binary_property(value) = re_test(value, /^(?:ASCII|ASCII_Hex_Digit|AHex|Alphabetic|Alpha|Any|Assigned|Bidi_Control|Bidi_C|Bidi_Mirrored|Bidi_M|Case_Ignorable|CI|Cased|Changes_When_Casefolded|CWCF|Changes_When_Casemapped|CWCM|Changes_When_Lowercased|CWL|Changes_When_NFKC_Casefolded|CWKCF|Changes_When_Titlecased|CWT|Changes_When_Uppercased|CWU|Dash|Default_Ignorable_Code_Point|DI|Deprecated|Dep|Diacritic|Dia|Emoji|Emoji_Component|EComp|Emoji_Modifier|EMod|Emoji_Modifier_Base|EBase|Emoji_Presentation|EPres|Extended_Pictographic|ExtPict|Extender|Ext|Grapheme_Base|Gr_Base|Grapheme_Extend|Gr_Ext|Hex_Digit|Hex|IDS_Binary_Operator|IDSB|IDS_Trinary_Operator|IDST|ID_Continue|IDC|ID_Start|IDS|Ideographic|Ideo|Join_Control|Join_C|Logical_Order_Exception|LOE|Lowercase|Lower|Math|Noncharacter_Code_Point|NChar|Pattern_Syntax|Pat_Syn|Pattern_White_Space|Pat_WS|Quotation_Mark|QMark|Radical|Regional_Indicator|RI|Sentence_Terminal|STerm|Soft_Dotted|SD|Terminal_Punctuation|Term|Unified_Ideograph|UIdeo|Uppercase|Upper|Variation_Selector|VS|White_Space|space|XID_Continue|XIDC|XID_Start|XIDS)$/)
|
|
7
|
+
fn regexp_string_property(value) = contains(["Basic_Emoji", "Emoji_Keycap_Sequence", "RGI_Emoji_Modifier_Sequence", "RGI_Emoji_Flag_Sequence", "RGI_Emoji_Tag_Sequence", "RGI_Emoji_ZWJ_Sequence", "RGI_Emoji"], value)
|
|
8
|
+
fn regexp_script(value) = re_test(value, /^[A-Z][A-Za-z0-9_]*$/) && {
|
|
9
|
+
value pattern: "\\p{{Script=" value "}}"
|
|
10
|
+
value re_valid(pattern)
|
|
11
|
+
}
|
|
12
|
+
fn regexp_property_valid(raw, sets) = {
|
|
13
|
+
value parts: split(raw, "=")
|
|
14
|
+
value key: first(parts)
|
|
15
|
+
value value: last(parts)
|
|
16
|
+
value if len(parts) > 2 then false
|
|
17
|
+
else if len(parts) == 2 then if contains(["General_Category", "gc"], key) then regexp_categories(value)
|
|
18
|
+
else contains(["Script", "sc", "Script_Extensions", "scx"], key) && regexp_script(value)
|
|
19
|
+
else regexp_categories(value) || regexp_binary_property(value) || sets && regexp_string_property(value)
|
|
20
|
+
}
|
|
21
|
+
fn regexp_name(raw) = {
|
|
22
|
+
value literal: '"' raw '"'
|
|
23
|
+
value decoded: js_string_parts(literal)
|
|
24
|
+
value if decoded == null || re_test(raw, /\\[^u]/) then null
|
|
25
|
+
else if re_test(decoded.text, /^[$_\p{Alphabetic}][$_\p{Alphabetic}\p{Number}\p{Mark}\u{200c}\u{200d}]*$/) then decoded.text else null
|
|
26
|
+
}
|
|
27
|
+
fn regexp_disjunctive(nodes, a, b) = {
|
|
28
|
+
value a_path: regexp_ancestors(nodes, a)
|
|
29
|
+
value b_path: regexp_ancestors(nodes, b)
|
|
30
|
+
value any(a_path, first => first.kind == "regexp_alternative"
|
|
31
|
+
&& any(b_path, second => second.kind == "regexp_alternative" && first.parent == second.parent && first.id != second.id))
|
|
32
|
+
}
|
|
33
|
+
fn regexp_group_error(nodes, entry) = {
|
|
34
|
+
value open: regexp_field_text(nodes, entry, "open")
|
|
35
|
+
value name: regexp_group_name(nodes, entry)
|
|
36
|
+
value flags: slice(open, 2, -1)
|
|
37
|
+
value duplicate: { value ["i", "m", "s"]; filter (flag) re_count(flags, flag) > 1; first }
|
|
38
|
+
value if starts_with(entry.text, "(?<") && !starts_with(entry.text, "(?<=") && !starts_with(entry.text, "(?<!")
|
|
39
|
+
&& (open == "(" || regexp_name(name) == null) then { message: "Invalid capture group name", offset: entry.start }
|
|
40
|
+
else if open == "(" && starts_with(entry.text, "(?") then { message: "Invalid group", offset: entry.start }
|
|
41
|
+
else if duplicate != null then { message: "Duplicated flag '" duplicate "'", offset: entry.start }
|
|
42
|
+
else if starts_with(open, "(?") && ends_with(open, ":") && open != "(?:" && re_count(flags, /-/) > 1 then { message: "Invalid group", offset: entry.start }
|
|
43
|
+
else if open == "(?-:" then { message: "Invalid empty flags", offset: entry.start }
|
|
44
|
+
else if len(entry.fields.close) == 0 then { message: "Unterminated group", offset: entry.end }
|
|
45
|
+
else null
|
|
46
|
+
}
|
|
47
|
+
fn regexp_escape_error(nodes, input, entry) = {
|
|
48
|
+
value raw: entry.text
|
|
49
|
+
value unicode: regexp_unicode(input)
|
|
50
|
+
value in_class: unicode && regexp_in_class(nodes, entry)
|
|
51
|
+
value sets: contains(input.flags, "v")
|
|
52
|
+
value if raw == "\\" && (unicode || byte_char(input.text, entry.end) != "c") then { message: "\\ at end of pattern", offset: entry.end }
|
|
53
|
+
else if !unicode then null
|
|
54
|
+
else if starts_with(raw, "\\u") && (!re_test(raw, /^\\u(?:[0-9a-fA-F]{4}|[dD][89aAbB][0-9a-fA-F]{2}\\u[dD][c-fC-F][0-9a-fA-F]{2}|\{[0-9a-fA-F]+\})$/)
|
|
55
|
+
|| js_string_parts('"' raw '"') == null) then { message: "Invalid unicode escape", offset: entry.end }
|
|
56
|
+
else if starts_with(raw, "\\x") && !re_test(raw, /^\\x[0-9a-fA-F]{2}$/) then { message: "Invalid escape", offset: entry.end }
|
|
57
|
+
else if starts_with(raw, "\\c") && !re_test(raw, /^\\c[A-Za-z]$/) then { message: if in_class then "Invalid class escape" else "Invalid unicode escape", offset: entry.end }
|
|
58
|
+
else if re_test(raw, /^\\[pP]/) then {
|
|
59
|
+
value property: slice(raw, 3, -1)
|
|
60
|
+
value valid: starts_with(raw, "\\p{{") || starts_with(raw, "\\P{{")
|
|
61
|
+
value if !valid || !ends_with(raw, "}}") || !regexp_property_valid(property, sets)
|
|
62
|
+
|| starts_with(raw, "\\P") && regexp_string_property(property) then {
|
|
63
|
+
message: if in_class then "Invalid property name in character class" else "Invalid property name", offset: entry.end
|
|
64
|
+
} else null
|
|
65
|
+
}
|
|
66
|
+
else if re_test(raw, /^\\[0-9]/) && (in_class || starts_with(raw, "\\0") && byte_char(input.text, entry.end) ~ /[0-9]/)
|
|
67
|
+
&& raw != "\\0" then { message: if in_class then "Invalid class escape" else "Invalid escape", offset: entry.end }
|
|
68
|
+
else if raw == "\\0" && byte_char(input.text, entry.end) ~ /[0-9]/ then { message: if in_class then "Invalid class escape" else "Invalid escape", offset: entry.end }
|
|
69
|
+
else if re_test(raw, /^\\[1-9]/) && !in_class || starts_with(raw, "\\k") && !in_class then null
|
|
70
|
+
else if re_test(raw, /^\\[bBdDsSwWntrfv0]$/) then null
|
|
71
|
+
else if starts_with(raw, "\\u") || starts_with(raw, "\\x") || starts_with(raw, "\\c") then null
|
|
72
|
+
else if !re_test(raw, /^\\[\^$\\.*+?()\[\]{}|\/]$/) && !(in_class && raw == "\\-")
|
|
73
|
+
&& !(sets && in_class && re_test(raw, /^\\[&!#%,:;<=>@`~]$/)) then {
|
|
74
|
+
message: if in_class then "Invalid class escape" else "Invalid escape", offset: entry.end
|
|
75
|
+
} else null
|
|
76
|
+
}
|
|
77
|
+
fn regexp_atom_point(nodes, input, entry) = if regexp_kind(nodes, input, entry) != "character" then null else {
|
|
78
|
+
value units: regexp_character_units(input, entry)
|
|
79
|
+
value regexp_point(units)
|
|
80
|
+
}
|
|
81
|
+
fn regexp_quantifier_error(nodes, input, entry) = {
|
|
82
|
+
value element: { value regexp_field(nodes, entry, "element"); first }
|
|
83
|
+
value kind: regexp_kind(nodes, input, element)
|
|
84
|
+
value behind: starts_with(element.text, "(?<=") || starts_with(element.text, "(?<!")
|
|
85
|
+
value operator: regexp_field_text(nodes, entry, "operator")
|
|
86
|
+
value if kind == "assertion" || kind == "invalid" || kind == "quantifier" || kind == "lookaround" && (behind || regexp_unicode(input)) then {
|
|
87
|
+
message: "Nothing to repeat", offset: element.end
|
|
88
|
+
} else if starts_with(operator, "{{") then {
|
|
89
|
+
value plain: trim_end(operator, "?")
|
|
90
|
+
value counts: { value plain; slice 1 -1; split "," }
|
|
91
|
+
value from: { value counts; first; num }
|
|
92
|
+
value to: if last(counts) == "" then null else { value counts; last; num }
|
|
93
|
+
value if to != null && to < from then { message: "numbers out of order in {{}} quantifier", offset: element.end } else null
|
|
94
|
+
} else null
|
|
95
|
+
}
|
|
96
|
+
fn regexp_class_strings(nodes, input, entry) = {
|
|
97
|
+
value items: regexp_field(nodes, entry, "item")
|
|
98
|
+
value operators: filter(items, item => item.kind == "regexp_set_operator")
|
|
99
|
+
value operands: filter(items, item => item.kind != "regexp_set_operator")
|
|
100
|
+
value strings: map(operands, operand => if operand.kind == "regexp_class" then regexp_class_strings(nodes, input, operand)
|
|
101
|
+
else if operand.kind == "regexp_string_disjunction" then {
|
|
102
|
+
value alternatives: regexp_field(nodes, operand, "alternative")
|
|
103
|
+
value any(alternatives, alternative => {
|
|
104
|
+
value parts: regexp_field(nodes, alternative, "item")
|
|
105
|
+
value len(parts) != 1
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
else if operand.kind == "regexp_escape" && starts_with(operand.text, "\\p{{") then {
|
|
109
|
+
value property: slice(operand.text, 3, -1)
|
|
110
|
+
value regexp_string_property(property)
|
|
111
|
+
} else false)
|
|
112
|
+
value if first(operators).text == "&&" then all(strings, value => value == true)
|
|
113
|
+
else if first(operators).text == "--" then first(strings) == true else any(strings, value => value == true)
|
|
114
|
+
}
|
|
115
|
+
fn regexp_class_error(nodes, input, entry) = {
|
|
116
|
+
value sets: contains(input.flags, "v")
|
|
117
|
+
value items: regexp_field(nodes, entry, "item")
|
|
118
|
+
value operators: filter(items, item => item.kind == "regexp_set_operator")
|
|
119
|
+
value operands: filter(items, item => item.kind != "regexp_set_operator")
|
|
120
|
+
value if len(entry.fields.close) == 0 then { message: "Unterminated character class", offset: entry.end }
|
|
121
|
+
else if sets && len(operators) > 0 && (first(operands).kind == "regexp_range" || any(operators, operator => operator.text != first(operators).text)) then {
|
|
122
|
+
message: "Invalid set operation in character class", offset: entry.end
|
|
123
|
+
}
|
|
124
|
+
else if sets && len(operators) > 0 && (len(operands) != len(operators) + 1 || first(items).kind == "regexp_set_operator"
|
|
125
|
+
|| last(items).kind == "regexp_set_operator" || any(operands, operand => operand.kind == "regexp_range")) then {
|
|
126
|
+
message: "Invalid character in character class", offset: entry.end
|
|
127
|
+
}
|
|
128
|
+
else if sets && regexp_field_text(nodes, entry, "negate") == "^" && regexp_class_strings(nodes, input, entry) then {
|
|
129
|
+
message: "Negated character class may contain strings", offset: entry.end
|
|
130
|
+
} else null
|
|
131
|
+
}
|
|
132
|
+
fn regexp_range_error(nodes, input, entry) = {
|
|
133
|
+
value from: { value regexp_field(nodes, entry, "from"); first }
|
|
134
|
+
value to: { value regexp_field(nodes, entry, "to"); first }
|
|
135
|
+
value from_value: regexp_atom_point(nodes, input, from)
|
|
136
|
+
value to_value: regexp_atom_point(nodes, input, to)
|
|
137
|
+
value if from_value != null && to_value != null && from_value > to_value then {
|
|
138
|
+
message: "Range out of order in character class", offset: entry.end
|
|
139
|
+
} else if regexp_unicode(input) && (from_value == null || to_value == null) then {
|
|
140
|
+
message: "Invalid character class", offset: entry.end
|
|
141
|
+
} else null
|
|
142
|
+
}
|
|
143
|
+
fn regexp_character_error(nodes, input, entry) = if !regexp_unicode(input)
|
|
144
|
+
|| !contains("{{}}]&!#$%*+,.:;<=>?@^`~()[/-|", entry.text) then null else {
|
|
145
|
+
value raw: entry.text
|
|
146
|
+
value in_class: regexp_in_class(nodes, entry)
|
|
147
|
+
value if !in_class && regexp_unicode(input) && contains(["{{", "}}", "]"], raw) then {
|
|
148
|
+
message: "Lone quantifier brackets", offset: entry.start
|
|
149
|
+
} else if in_class && contains(input.flags, "v") && contains(["(", ")", "[", "{{", "}}", "/", "-", "|"], raw) then {
|
|
150
|
+
message: "Invalid character in character class", offset: entry.start
|
|
151
|
+
} else if in_class && contains(input.flags, "v") && contains("&!#$%*+,.:;<=>?@^`~", raw)
|
|
152
|
+
&& (byte_char(input.text, entry.end) == raw || any(nodes, operator => operator.kind == "regexp_set_operator"
|
|
153
|
+
&& operator.end == entry.start && ends_with(operator.text, raw))) then {
|
|
154
|
+
message: "Invalid character in character class", offset: entry.start
|
|
155
|
+
} else null
|
|
156
|
+
}
|
|
157
|
+
fn regexp_backreference_targets(nodes, input, entry) = {
|
|
158
|
+
value groups: regexp_groups(nodes)
|
|
159
|
+
value raw: entry.text
|
|
160
|
+
value if starts_with(raw, "\\k<") then {
|
|
161
|
+
value name: { value raw; slice 3 -1; regexp_name }
|
|
162
|
+
value filter(groups, group => {
|
|
163
|
+
value raw_name: regexp_group_name(nodes, group)
|
|
164
|
+
value regexp_name(raw_name) == name
|
|
165
|
+
})
|
|
166
|
+
} else if re_test(raw, /^\\[1-9][0-9]*$/) then {
|
|
167
|
+
value number: { value raw; slice 1; num }
|
|
168
|
+
value group: regexp_at(groups, number - 1)
|
|
169
|
+
value if group == null then [] else [group]
|
|
170
|
+
} else []
|
|
171
|
+
}
|
|
172
|
+
fn regexp_backreference_error(nodes, input, entry) = {
|
|
173
|
+
value targets: regexp_backreference_targets(nodes, input, entry)
|
|
174
|
+
value raw: entry.text
|
|
175
|
+
value if starts_with(raw, "\\k") && (!starts_with(raw, "\\k<") || !ends_with(raw, ">")
|
|
176
|
+
|| { value raw; slice 3 -1; regexp_name } == null) then { message: "Invalid named reference", offset: entry.end }
|
|
177
|
+
else if len(targets) == 0 then {
|
|
178
|
+
message: if starts_with(raw, "\\k") then "Invalid named capture referenced" else "Invalid escape", offset: byte_len(input.text)
|
|
179
|
+
} else null
|
|
180
|
+
}
|
|
181
|
+
fn regexp_entry_error(nodes, input, entry) = if entry.kind == "regexp_group" then regexp_group_error(nodes, entry)
|
|
182
|
+
else if entry.kind == "regexp_escape" then {
|
|
183
|
+
value error: regexp_escape_error(nodes, input, entry)
|
|
184
|
+
value if error != null then error else if regexp_escape_kind(nodes, input, entry) == "backreference" then regexp_backreference_error(nodes, input, entry) else null
|
|
185
|
+
}
|
|
186
|
+
else if entry.kind == "regexp_quantifier" then regexp_quantifier_error(nodes, input, entry)
|
|
187
|
+
else if entry.kind == "regexp_class" then regexp_class_error(nodes, input, entry)
|
|
188
|
+
else if entry.kind == "regexp_range" then regexp_range_error(nodes, input, entry)
|
|
189
|
+
else if entry.kind == "regexp_character" then regexp_character_error(nodes, input, entry)
|
|
190
|
+
else if entry.kind == "regexp_invalid" then {
|
|
191
|
+
message: if entry.text == ")" then "Unmatched ')'" else "Nothing to repeat", offset: entry.start
|
|
192
|
+
} else null
|
|
193
|
+
fn regexp_error(nodes, input) = {
|
|
194
|
+
value root: find(nodes, entry => entry.parent == null)
|
|
195
|
+
value entries: filter(regexp_ordered(nodes, root.id), entry => entry.kind != "TEXT")
|
|
196
|
+
value errors: map(entries, entry => regexp_entry_error(nodes, input, entry))
|
|
197
|
+
value structural: { value errors; filter (error) error != null; sort_by (error) error.offset; first }
|
|
198
|
+
value if structural != null then structural else {
|
|
199
|
+
value groups: regexp_groups(nodes)
|
|
200
|
+
value duplicate: {
|
|
201
|
+
value groups
|
|
202
|
+
filter (group) {
|
|
203
|
+
value raw_name: regexp_group_name(nodes, group)
|
|
204
|
+
value name: regexp_name(raw_name)
|
|
205
|
+
value name != null && any(groups, previous => previous.start < group.start && regexp_group_name(nodes, previous) != null && {
|
|
206
|
+
value previous_name: regexp_group_name(nodes, previous)
|
|
207
|
+
value regexp_name(previous_name) == name && !regexp_disjunctive(nodes, group, previous)
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
first
|
|
211
|
+
}
|
|
212
|
+
value if duplicate != null then { message: "Duplicate capture group name", offset: byte_len(input.text) } else null
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
in regexp_site let regexp_error if regexp_tree.ok then regexp_error(regexp_nodes, regexp_input) else null
|
|
216
|
+
|
|
217
|
+
fn regexp_input_flags(input, flags) = {
|
|
218
|
+
text: input.text, source: input.source, flags: flags, spans: input.spans, units: input.units,
|
|
219
|
+
offset: input.offset, end_offset: input.end_offset, written: input.written, pattern_node: input.pattern_node
|
|
220
|
+
}
|
|
221
|
+
fn regexp_mode_error(n, input, flags) = {
|
|
222
|
+
value mapped: regexp_input_flags(input, flags)
|
|
223
|
+
value unicode: contains(flags, "u")
|
|
224
|
+
value sets: contains(flags, "v")
|
|
225
|
+
value parsed: regexp_parse(n, mapped, unicode, sets)
|
|
226
|
+
value nodes: if parsed.ok then parsed.nodes else []
|
|
227
|
+
value error: if parsed.ok then regexp_error(nodes, mapped) else null
|
|
228
|
+
value if unicode && sets then "Regex 'u' and 'v' flags cannot be used together"
|
|
229
|
+
else if error == null then null else "Invalid regular expression: /" input.text "/" (if unicode then "u" else "") (if sets then "v" else "") ": " error.message
|
|
230
|
+
}
|
|
231
|
+
fn regexp_flags(n) = {
|
|
232
|
+
value arguments: n.argument.nodes
|
|
233
|
+
value second: regexp_at(arguments, 1)
|
|
234
|
+
value if second == null then "" else if second is ts_string then ts_runtime_string(second) else null
|
|
235
|
+
}
|
|
236
|
+
fn regexp_pattern_error(n, input) = {
|
|
237
|
+
value argument: first(n.argument.nodes)
|
|
238
|
+
value flags: regexp_flags(n)
|
|
239
|
+
value if input == null || argument not ts_string || n not (ts_call, ts_new) then null
|
|
240
|
+
else if type_of(flags) == "str" then regexp_mode_error(n, input, flags) else {
|
|
241
|
+
value unicode: regexp_mode_error(n, input, "u")
|
|
242
|
+
value sets: if unicode != null then regexp_mode_error(n, input, "v") else null
|
|
243
|
+
value if sets == null then null else regexp_mode_error(n, input, "")
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
in regexp_site let regexp_flags if node is ts_regex then regexp_input.flags else regexp_flags(node)
|
|
247
|
+
in regexp_site let regexp_pattern_error regexp_pattern_error(node, regexp_input)
|
|
248
|
+
|
|
249
|
+
}
|