@inthedark/dxlint 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +29 -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,209 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn regexp_location(input, start, end) = if input.spans == null || end <= start then {
|
|
4
|
+
offset: input.pattern_node.offset, end: input.pattern_node.end_offset
|
|
5
|
+
} else {
|
|
6
|
+
value prefix: byte_slice(input.text, 0, start)
|
|
7
|
+
value through: byte_slice(input.text, 0, end)
|
|
8
|
+
value first_index: len(prefix)
|
|
9
|
+
value last_index: len(through) - 1
|
|
10
|
+
value a: regexp_at(input.spans, first_index)
|
|
11
|
+
value b: regexp_at(input.spans, last_index)
|
|
12
|
+
value { offset: a.offset, end: b.end_offset }
|
|
13
|
+
}
|
|
14
|
+
fn regexp_written(input, start, end) = if input.spans == null then byte_slice(input.text, start, end) else {
|
|
15
|
+
value span: regexp_location(input, start, end)
|
|
16
|
+
value byte_slice(input.pattern_node.text, span.offset - input.pattern_node.offset, span.end - input.pattern_node.offset)
|
|
17
|
+
}
|
|
18
|
+
fn regexp_finding(input, check, kind, data, start, end) = {
|
|
19
|
+
value span: regexp_location(input, start, end)
|
|
20
|
+
value { check: check, kind: kind, data: data, offset: span.offset, end: span.end }
|
|
21
|
+
}
|
|
22
|
+
fn regexp_controls(nodes, input) = {
|
|
23
|
+
value atoms: filter(nodes, entry => entry.kind == "regexp_character" && re_test(entry.text, /^[\u{0}-\u{1f}]$/)
|
|
24
|
+
|| entry.kind == "regexp_escape" && regexp_kind(nodes, input, entry) == "character")
|
|
25
|
+
value parts: flat(map(atoms, entry => regexp_character_parts(nodes, input, entry)))
|
|
26
|
+
value characters: filter(parts, entry => type_of(entry.value) == "num" && entry.value <= 31 && {
|
|
27
|
+
value raw: byte_slice(input.text, entry.start, entry.end)
|
|
28
|
+
value character: regexp_character(entry.value)
|
|
29
|
+
value written: regexp_written(input, entry.start, entry.end)
|
|
30
|
+
value starts_with(raw, character) || starts_with(raw, "\\x") || starts_with(raw, "\\u")
|
|
31
|
+
|| starts_with(written, "\\x") || starts_with(written, "\\u")
|
|
32
|
+
})
|
|
33
|
+
value if len(characters) == 0 then [] else {
|
|
34
|
+
value data: map(characters, character => {
|
|
35
|
+
value hex: regexp_hex(character.value)
|
|
36
|
+
value "\\x" (if len(hex) == 1 then "0" else "") hex
|
|
37
|
+
})
|
|
38
|
+
value first: first(characters)
|
|
39
|
+
value last: last(characters)
|
|
40
|
+
value joined: join(data, ", ")
|
|
41
|
+
value [regexp_finding(input, "control", "", [joined], first.start, last.end)]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
fn regexp_space_step(state, character, nodes, input) = {
|
|
45
|
+
value offset: state.offset + byte_len(character)
|
|
46
|
+
value run: if character == " " then state.run + 1 else 0
|
|
47
|
+
value previous: state.run - (if contains(["+", "*", "{{", "?"], character) then 1 else 0)
|
|
48
|
+
value start: state.offset - state.run
|
|
49
|
+
value in_class: any(nodes, entry => entry.kind == "class" && entry.start <= start && start < entry.end)
|
|
50
|
+
value kind: str(previous)
|
|
51
|
+
value report: if state.report == null && character != " " && previous >= 2 && !in_class
|
|
52
|
+
then regexp_finding(input, "spaces", kind, [], start, start + previous) else state.report
|
|
53
|
+
value { offset: offset, run: run, report: report }
|
|
54
|
+
}
|
|
55
|
+
fn regexp_spaces(nodes, input) = {
|
|
56
|
+
value end: byte_len(input.text)
|
|
57
|
+
value written: regexp_written(input, 0, end)
|
|
58
|
+
value if !contains(written, " ") then [] else {
|
|
59
|
+
value characters: { value split(input.text, ""); concat [""] }
|
|
60
|
+
value items: concat([{ offset: 0, run: 0, report: null }], characters)
|
|
61
|
+
value result: reduce(items, (state, character) => regexp_space_step(state, character, nodes, input))
|
|
62
|
+
value if result.report == null then [] else [result.report]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
fn regexp_backreference_problem(nodes, reference, group) = {
|
|
66
|
+
value reference_path: regexp_ancestors(nodes, reference)
|
|
67
|
+
value group_path: regexp_ancestors(nodes, group)
|
|
68
|
+
value common: filter(group_path, parent => any(reference_path, other => parent.id == other.id))
|
|
69
|
+
value cut: filter(group_path, parent => !any(common, other => parent.id == other.id))
|
|
70
|
+
value look: {
|
|
71
|
+
value common
|
|
72
|
+
filter (parent) parent.kind == "regexp_group"
|
|
73
|
+
&& any(["(?=", "(?!", "(?<=", "(?<!"], open => starts_with(parent.text, open))
|
|
74
|
+
first
|
|
75
|
+
}
|
|
76
|
+
value backward: look != null && (starts_with(look.text, "(?<=") || starts_with(look.text, "(?<!"))
|
|
77
|
+
value if any(reference_path, parent => parent.id == group.id) then "nested"
|
|
78
|
+
else if last(cut).kind == "regexp_alternative" then "disjunctive"
|
|
79
|
+
else if !backward && reference.end <= group.start then "forward"
|
|
80
|
+
else if backward && group.end <= reference.start then "backward"
|
|
81
|
+
else if any(cut, parent => parent.kind == "regexp_group"
|
|
82
|
+
&& (starts_with(parent.text, "(?!") || starts_with(parent.text, "(?<!"))) then "intoNegativeLookaround"
|
|
83
|
+
else null
|
|
84
|
+
}
|
|
85
|
+
fn regexp_backreferences(nodes, input) = {
|
|
86
|
+
value references: filter(nodes, entry => entry.kind == "regexp_escape"
|
|
87
|
+
&& re_test(entry.text, /^\\(?:[1-9]|k)/) && regexp_kind(nodes, input, entry) == "backreference")
|
|
88
|
+
value reports: map(references, reference => {
|
|
89
|
+
value groups: regexp_backreference_targets(nodes, input, reference)
|
|
90
|
+
value entry: reference
|
|
91
|
+
value problems: map(groups, group => { kind: regexp_backreference_problem(nodes, entry, group), group: group })
|
|
92
|
+
value same: filter(problems, problem => problem.kind != "disjunctive")
|
|
93
|
+
value selected: if len(same) == 0 then problems else same
|
|
94
|
+
value other: len(selected) - 1
|
|
95
|
+
value suffix: if other == 0 then "" else if other == 1 then " and another group" else " and other " str(other) " groups"
|
|
96
|
+
value first: first(selected)
|
|
97
|
+
value if len(problems) == 0 || any(problems, problem => problem.kind == null) then [] else
|
|
98
|
+
[regexp_finding(input, "backref", first.kind, [entry.text, first.group.text, suffix], entry.start, entry.end)]
|
|
99
|
+
})
|
|
100
|
+
value flat(reports)
|
|
101
|
+
}
|
|
102
|
+
fn regexp_sequence_step(state, entry, nodes) = if entry.kind == "character" then {
|
|
103
|
+
current: concat(state.current, [entry]), complete: state.complete
|
|
104
|
+
} else if entry.kind == "class_range" then {
|
|
105
|
+
value ends: filter(nodes, child => child.parent == entry.id)
|
|
106
|
+
value first: first(ends)
|
|
107
|
+
value complete: concat(state.current, [first])
|
|
108
|
+
value { current: [last(ends)], complete: concat(state.complete, [complete]) }
|
|
109
|
+
} else { current: [], complete: concat(state.complete, [state.current]) }
|
|
110
|
+
fn regexp_sequences(nodes, cls) = {
|
|
111
|
+
value children: filter(nodes, entry => entry.parent == cls.id)
|
|
112
|
+
value items: concat([{ current: [], complete: [] }], children)
|
|
113
|
+
value result: reduce(items, (state, entry) => regexp_sequence_step(state, entry, nodes))
|
|
114
|
+
value sequences: concat(result.complete, [result.current])
|
|
115
|
+
value filter(sequences, sequence => len(sequence) > 0)
|
|
116
|
+
}
|
|
117
|
+
fn regexp_acceptable_escape(input, entry) = {
|
|
118
|
+
value written: regexp_written(input, entry.start, entry.end)
|
|
119
|
+
value after: re_replace(written, /^\\+/, "")
|
|
120
|
+
value units: regexp_units(after)
|
|
121
|
+
value point: regexp_point(units)
|
|
122
|
+
value itself: len(after) == 1 && (point == entry.value || point > 65535 && any(units, unit => unit.value == entry.value))
|
|
123
|
+
value starts_with(written, "\\") && !itself
|
|
124
|
+
}
|
|
125
|
+
fn regexp_mark(entry) = entry.character != null && re_test(entry.character, /^\p{M}$/)
|
|
126
|
+
fn regexp_pair_findings(sequence, input, check, allow_escape) = {
|
|
127
|
+
value parts: map(sequence, current => {
|
|
128
|
+
value index: index_of(sequence, current)
|
|
129
|
+
value previous: regexp_at(sequence, index - 1)
|
|
130
|
+
value accepted: !allow_escape || !regexp_acceptable_escape(input, current)
|
|
131
|
+
value previous_accepted: !allow_escape || !regexp_acceptable_escape(input, previous)
|
|
132
|
+
value combining: if regexp_mark(current) && !regexp_mark(previous) then ["combiningClass"] else []
|
|
133
|
+
value surrogate: if previous.value >= 55296 && previous.value <= 56319 && current.value >= 56320 && current.value <= 57343
|
|
134
|
+
then [if starts_with(previous.raw, "\\u{{") || starts_with(current.raw, "\\u{{") then "surrogatePair" else "surrogatePairWithoutUFlag"] else []
|
|
135
|
+
value modifier: if current.value >= 127995 && current.value <= 127999 && !(previous.value >= 127995 && previous.value <= 127999) then ["emojiModifier"] else []
|
|
136
|
+
value regional: if current.value >= 127462 && current.value <= 127487 && previous.value >= 127462 && previous.value <= 127487 then ["regionalIndicatorSymbol"] else []
|
|
137
|
+
value pair: if !previous_accepted then [] else flat([surrogate, modifier, regional])
|
|
138
|
+
value kinds: if index == 0 || !accepted then [] else concat(combining, pair)
|
|
139
|
+
value map(kinds, kind => regexp_finding(input, check, kind, [], previous.start, current.end))
|
|
140
|
+
})
|
|
141
|
+
value flat(parts)
|
|
142
|
+
}
|
|
143
|
+
fn regexp_zwj_step(state, current, sequence, input, check, allow_escape) = {
|
|
144
|
+
value index: index_of(sequence, current)
|
|
145
|
+
value previous: regexp_at(sequence, index - 1)
|
|
146
|
+
value next: regexp_at(sequence, index + 1)
|
|
147
|
+
value valid: index > 0 && index + 1 < len(sequence) && current.value == 8205 && previous.value != 8205 && next.value != 8205
|
|
148
|
+
&& (!allow_escape || !regexp_acceptable_escape(input, previous) && !regexp_acceptable_escape(input, current) && !regexp_acceptable_escape(input, next))
|
|
149
|
+
value if !valid then state else {
|
|
150
|
+
value adjacent: state.last != null && state.last.id == previous.id
|
|
151
|
+
value report: if state.first != null && !adjacent then
|
|
152
|
+
[regexp_finding(input, check, "zwj", [], state.first.start, state.last.end)] else []
|
|
153
|
+
value { first: if adjacent then state.first else previous, last: next, reports: concat(state.reports, report) }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
fn regexp_zwj_findings(sequence, input, check, allow_escape) = {
|
|
157
|
+
value items: concat([{ first: null, last: null, reports: [] }], sequence)
|
|
158
|
+
value result: reduce(items, (state, current) => regexp_zwj_step(state, current, sequence, input, check, allow_escape))
|
|
159
|
+
value final: if result.first == null then [] else
|
|
160
|
+
[regexp_finding(input, check, "zwj", [], result.first.start, result.last.end)]
|
|
161
|
+
value concat(result.reports, final)
|
|
162
|
+
}
|
|
163
|
+
fn regexp_misleading(nodes, input, allow_escape) = {
|
|
164
|
+
value check: if allow_escape then "misleading_escaped" else "misleading"
|
|
165
|
+
value classes: filter(nodes, entry => entry.kind == "class" && !entry.expression)
|
|
166
|
+
value parts: map(classes, cls => {
|
|
167
|
+
value sequences: regexp_sequences(nodes, cls)
|
|
168
|
+
value parts: map(sequences, sequence => {
|
|
169
|
+
value pairs: regexp_pair_findings(sequence, input, check, allow_escape)
|
|
170
|
+
value joined: regexp_zwj_findings(sequence, input, check, allow_escape)
|
|
171
|
+
value concat(pairs, joined)
|
|
172
|
+
})
|
|
173
|
+
value flat(parts)
|
|
174
|
+
})
|
|
175
|
+
value findings: flat(parts)
|
|
176
|
+
value order: ["surrogatePairWithoutUFlag", "surrogatePair", "combiningClass", "emojiModifier", "regionalIndicatorSymbol", "zwj"]
|
|
177
|
+
value { value findings; sort_by (finding) index_of(order, finding.kind) }
|
|
178
|
+
}
|
|
179
|
+
fn regexp_empty_classes(nodes, input) = {
|
|
180
|
+
value classes: filter(nodes, entry => entry.kind == "class" && !entry.negate && !entry.expression && !any(nodes, child => child.parent == entry.id))
|
|
181
|
+
value map(classes, cls => regexp_finding(input, "empty_class", "", [], cls.start, cls.end))
|
|
182
|
+
}
|
|
183
|
+
fn regexp_checked_by_call(n) = {
|
|
184
|
+
value owner: n.parent.node
|
|
185
|
+
value arguments: owner.argument.nodes
|
|
186
|
+
value first: first(arguments)
|
|
187
|
+
value n is ts_regex && owner is (ts_call, ts_new) && first == n && len(arguments) > 1 && regexp_input(owner) != null
|
|
188
|
+
}
|
|
189
|
+
in regexp_site let regexp_control_findings if regexp_tree.ok
|
|
190
|
+
then regexp_controls(regexp_nodes, regexp_input) else []
|
|
191
|
+
in regexp_site let regexp_empty_class_findings if node is ts_regex && regexp_tree.ok && regexp_error == null
|
|
192
|
+
then regexp_empty_classes(regexp_view_nodes, regexp_input) else []
|
|
193
|
+
in regexp_site let regexp_space_findings if regexp_tree.ok && regexp_error == null
|
|
194
|
+
then regexp_spaces(regexp_view_nodes, regexp_input) else []
|
|
195
|
+
in regexp_site let regexp_backreference_findings if re_test(regexp_input.text, /\\(?:[1-9]|k)/)
|
|
196
|
+
&& regexp_tree.ok && regexp_error == null then regexp_backreferences(regexp_nodes, regexp_input) else []
|
|
197
|
+
in regexp_site let regexp_misleading_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
|
|
198
|
+
then regexp_misleading(regexp_view_nodes, regexp_input, false) else []
|
|
199
|
+
in regexp_site let regexp_misleading_escaped_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
|
|
200
|
+
then regexp_misleading(regexp_view_nodes, regexp_input, true) else []
|
|
201
|
+
in regexp_site let regexp_findings if regexp_tree.ok != true then null else {
|
|
202
|
+
value flat([
|
|
203
|
+
regexp_control_findings, regexp_empty_class_findings, regexp_space_findings,
|
|
204
|
+
regexp_backreference_findings, regexp_misleading_findings, regexp_misleading_escaped_findings
|
|
205
|
+
])
|
|
206
|
+
sort_by (finding) finding.check
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
@macro regexp_alternatives alternative:emit(regexp_alternative, term:regexp_term*) ("|" alternative:emit(regexp_alternative, term:regexp_term*))*
|
|
2
|
+
@macro regexp_pattern emit(regexp_pattern, @regexp_alternatives end)
|
|
3
|
+
parser regexp_syntax in js {
|
|
4
|
+
flag unicode
|
|
5
|
+
flag sets
|
|
6
|
+
flag named
|
|
7
|
+
flag grouped
|
|
8
|
+
line_break /[\r\n]/
|
|
9
|
+
|
|
10
|
+
token unicode_escape /\\(?:u[dD][89aAbB][0-9a-fA-F]{2}\\u[dD][c-fC-F][0-9a-fA-F]{2}|u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|c[A-Za-z]|[pP]\{[^}]*\}|k<[^>]*>|[1-9][0-9]*|[\s\S]?)/
|
|
11
|
+
token named_escape /\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|c[A-Za-z]|k<[^>]*>|[1-9][0-9]*|[0-3][0-7]{0,2}|[4-7][0-7]?|[^c]?)/
|
|
12
|
+
token legacy_escape /\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|c[A-Za-z]|[1-9][0-9]*|[0-3][0-7]{0,2}|[4-7][0-7]?|[^c]?)/
|
|
13
|
+
token unicode_class_escape /\\(?:u[dD][89aAbB][0-9a-fA-F]{2}\\u[dD][c-fC-F][0-9a-fA-F]{2}|u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|c[A-Za-z]|[pP]\{[^}]*\}|[\s\S]?)/
|
|
14
|
+
token legacy_class_escape /\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|c[A-Za-z0-9_]|[0-3][0-7]{0,2}|[4-7][0-7]?|[^c]?)/
|
|
15
|
+
token string_open "\\q{"
|
|
16
|
+
token group_open /\((?:\?(?:[:=!]|<[=!]|<[^>)]*>|[ims-]+:))?/
|
|
17
|
+
token count /\{[0-9]+(?:,[0-9]*)?\}/
|
|
18
|
+
token repeat /[*+?]/
|
|
19
|
+
token set_operator /&&|--/
|
|
20
|
+
token unit /[\s\S]/
|
|
21
|
+
|
|
22
|
+
goal unicode raw { unicode_escape group_open count repeat unit }
|
|
23
|
+
goal named raw { named_escape group_open count repeat unit }
|
|
24
|
+
goal legacy raw { legacy_escape group_open count repeat unit }
|
|
25
|
+
goal unicode_class raw { unicode_class_escape unit }
|
|
26
|
+
goal legacy_class raw { legacy_class_escape unit }
|
|
27
|
+
goal sets raw { string_open unicode_class_escape set_operator unit }
|
|
28
|
+
goal strings raw { unicode_class_escape unit }
|
|
29
|
+
|
|
30
|
+
node regexp_pattern { alternative: regexp_alternative* }
|
|
31
|
+
node regexp_alternative { term?: regexp_term* }
|
|
32
|
+
node regexp_group { open: text, alternative: regexp_alternative*, close?: text }
|
|
33
|
+
node regexp_class { open: text, negate?: text, item?: regexp_class_item*, close?: text }
|
|
34
|
+
node regexp_range { from: regexp_class_item, dash: text, to: regexp_class_item }
|
|
35
|
+
node regexp_escape { value: text }
|
|
36
|
+
node regexp_character { value: text }
|
|
37
|
+
node regexp_assertion { value: text }
|
|
38
|
+
node regexp_set { value: text }
|
|
39
|
+
node regexp_invalid { value: text }
|
|
40
|
+
node regexp_quantifier { element: regexp_term, operator: text }
|
|
41
|
+
node regexp_string_disjunction { open: text, alternative: regexp_string_alternative*, close?: text }
|
|
42
|
+
node regexp_string_alternative { item?: regexp_class_item* }
|
|
43
|
+
node regexp_set_operator { value: text }
|
|
44
|
+
|
|
45
|
+
root pattern = if(unicode, goal(unicode, (@regexp_pattern)), if(named, goal(named, (@regexp_pattern)), goal(legacy, (@regexp_pattern))))
|
|
46
|
+
production regexp_term precedence regexp_atom {
|
|
47
|
+
suffix 1 text(count "?"? | repeat "?"?) => regexp_quantifier(element, operator)
|
|
48
|
+
}
|
|
49
|
+
production regexp_atom = (
|
|
50
|
+
emit(regexp_group, open:group_open set(grouped, (@regexp_alternatives)) close:")"?)
|
|
51
|
+
| if(sets, goal(sets, regexp_class), if(unicode, goal(unicode_class, regexp_class), goal(legacy_class, regexp_class)))
|
|
52
|
+
| emit(regexp_assertion, value:/\^|\$/)
|
|
53
|
+
| emit(regexp_set, value:".")
|
|
54
|
+
| emit(regexp_escape, value:if(unicode, unicode_escape, if(named, named_escape, legacy_escape)))
|
|
55
|
+
| emit(regexp_invalid, value:(repeat | count | if(grouped, fail, ")")))
|
|
56
|
+
| emit(regexp_character, value:(not("|" | ")") unit))
|
|
57
|
+
)
|
|
58
|
+
production regexp_class = emit(regexp_class, open:"[" negate:"^"? item:regexp_class_item* close:"]"?)
|
|
59
|
+
production regexp_class_item = if(sets, (
|
|
60
|
+
regexp_class | regexp_strings | emit(regexp_set_operator, value:set_operator)
|
|
61
|
+
| try(emit(regexp_range, from:regexp_class_atom dash:"-" not("]") to:regexp_class_atom))
|
|
62
|
+
| regexp_class_atom
|
|
63
|
+
), (
|
|
64
|
+
try(emit(regexp_range, from:regexp_class_atom dash:"-" not("]") to:regexp_class_atom))
|
|
65
|
+
| regexp_class_atom
|
|
66
|
+
))
|
|
67
|
+
production regexp_class_atom = (
|
|
68
|
+
emit(regexp_escape, value:if(sets, unicode_class_escape, if(unicode, unicode_class_escape, legacy_class_escape)))
|
|
69
|
+
| emit(regexp_character, value:(not("]") unit))
|
|
70
|
+
)
|
|
71
|
+
production regexp_strings = emit(regexp_string_disjunction, open:string_open goal(strings, (
|
|
72
|
+
alternative:emit(regexp_string_alternative, item:regexp_string_item*)
|
|
73
|
+
("|" alternative:emit(regexp_string_alternative, item:regexp_string_item*))*
|
|
74
|
+
close:"}"?
|
|
75
|
+
)))
|
|
76
|
+
production regexp_string_item = (
|
|
77
|
+
emit(regexp_escape, value:unicode_class_escape)
|
|
78
|
+
| emit(regexp_character, value:(not("}" | "|") unit))
|
|
79
|
+
)
|
|
80
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import .ts/layout/literals
|
|
2
|
+
import .ts/constants
|
|
3
|
+
import .ts/literal-text
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
in quoted_literals let regexp_literal_escape_parts {
|
|
8
|
+
value overlapping(ts_literal_piece, offset, end_offset)
|
|
9
|
+
filter (piece) piece.offset >= offset && piece.end_offset <= end_offset && starts_with(piece.text, "\\")
|
|
10
|
+
unique_by (piece) { start: piece.offset, end: piece.end_offset }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
fn regexp_literal_useless_escapes(n) = if n.parent is (ts_jsx_attribute, ts_jsx_element, ts_jsx_fragment)
|
|
14
|
+
|| n.parent is ts_tagged_template && n.parent.quasi.node == n then [] else {
|
|
15
|
+
value template: n is ts_template
|
|
16
|
+
value quote: if template then "`" else slice(n.text, 0, 1)
|
|
17
|
+
value escapes: n.regexp_literal_escape_parts
|
|
18
|
+
value unnecessary: filter(escapes, escape => {
|
|
19
|
+
value own: !template || any(n.quasi.nodes, part => part.offset <= escape.offset && escape.end_offset <= part.end_offset)
|
|
20
|
+
value character: byte_char(escape.text, 1)
|
|
21
|
+
value necessary: re_test(character, /^[0-9]$/) || contains("\\nrvtbfux\r\n\u{2028}\u{2029}", character) || character == quote
|
|
22
|
+
value after: escape.offset - n.offset + 1 + byte_len(character)
|
|
23
|
+
value special: if template && character == "$" then byte_char(n.text, after) != "{{"
|
|
24
|
+
else if template && character == "{{" then byte_char(n.text, escape.offset - n.offset - 1) != "$"
|
|
25
|
+
else !necessary
|
|
26
|
+
value own && special
|
|
27
|
+
})
|
|
28
|
+
value map(unnecessary, escape => { character: byte_char(escape.text, 1), offset: escape.offset, regex: false })
|
|
29
|
+
}
|
|
30
|
+
fn regexp_regex_escape_necessary(nodes, input, entry) = {
|
|
31
|
+
value ancestors: regexp_ancestors(nodes, entry)
|
|
32
|
+
value cls: { value ancestors; filter (parent) parent.kind == "regexp_class"; first }
|
|
33
|
+
value sets: contains(input.flags, "v")
|
|
34
|
+
value units: regexp_character_units(input, entry)
|
|
35
|
+
value point: regexp_point(units)
|
|
36
|
+
value character: regexp_character(point)
|
|
37
|
+
value allowed: if cls == null then "\\bcdDfnpPrsStvwWxu0123456789]^/.$*+?[{{}}|()Bk"
|
|
38
|
+
else if sets then "\\bcdDfnpPrsStvwWxu0123456789]q/[{{}}|()-" else "\\bcdDfnpPrsStvwWxu0123456789]"
|
|
39
|
+
value if character == null || !regexp_unicode(input) && point > 65535 || entry.text != "\\" character || contains(allowed, character) then true
|
|
40
|
+
else if cls == null then false
|
|
41
|
+
else if character == "^" && cls.start + 1 == entry.start then true
|
|
42
|
+
else if !sets && character == "-" && cls.start + 1 != entry.start && entry.end != cls.end - 1 then true
|
|
43
|
+
else if sets && contains("!#$%&*+,.:;<=>?@^`~", character) then {
|
|
44
|
+
value previous: byte_char(input.text, entry.start - 1)
|
|
45
|
+
value next: byte_char(input.text, entry.end)
|
|
46
|
+
value negate: regexp_field_text(nodes, cls, "negate") == "^"
|
|
47
|
+
value next == character || previous == character && (character != "^" || !negate || cls.start + 1 < entry.start - 1)
|
|
48
|
+
} else false
|
|
49
|
+
}
|
|
50
|
+
fn regexp_regex_useless_escapes(n) = if n.regexp_error != null || n.regexp_tree.ok != true then [] else {
|
|
51
|
+
value input: n.regexp_input
|
|
52
|
+
value nodes: n.regexp_nodes
|
|
53
|
+
value characters: filter(nodes, entry => regexp_kind(nodes, input, entry) == "character" && !regexp_regex_escape_necessary(nodes, input, entry))
|
|
54
|
+
value map(characters, entry => {
|
|
55
|
+
value units: regexp_character_units(input, entry)
|
|
56
|
+
value point: regexp_point(units)
|
|
57
|
+
value { character: regexp_character(point), offset: entry.offset, regex: true }
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
in ts_string, ts_template, ts_regex let regexp_useless_escapes if node is ts_regex then regexp_regex_useless_escapes(node) else regexp_literal_useless_escapes(node)
|
|
61
|
+
|
|
62
|
+
fn regexp_unnamed_captures(n) = {
|
|
63
|
+
value input: if n is ts_regex then n.regexp_input else {
|
|
64
|
+
value arguments: n.argument.nodes
|
|
65
|
+
value first: first(arguments)
|
|
66
|
+
value flags_argument: regexp_at(arguments, 1)
|
|
67
|
+
value flags: if type_of(ts_runtime_string(flags_argument)) == "str" then ts_runtime_string(flags_argument) else ""
|
|
68
|
+
value if n.callee.global_value == "RegExp" then regexp_computed_input(first, flags) else null
|
|
69
|
+
}
|
|
70
|
+
value if input == null then [] else {
|
|
71
|
+
value unicode: contains(input.flags, "u")
|
|
72
|
+
value sets: contains(input.flags, "v")
|
|
73
|
+
value parsed: regexp_parse(n, input, unicode, sets)
|
|
74
|
+
value nodes: if parsed.ok then parsed.nodes else []
|
|
75
|
+
value valid: parsed.ok == true && regexp_error(nodes, input) == null
|
|
76
|
+
value groups: if !valid then [] else regexp_groups(nodes)
|
|
77
|
+
value unnamed: filter(groups, group => regexp_group_name(nodes, group) == null)
|
|
78
|
+
value map(unnamed, group => group.text)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
in regexp_site let regexp_unnamed_captures regexp_unnamed_captures(node)
|
|
82
|
+
|
|
83
|
+
fn regexp_unicode_info(n) = if n is ts_regex then { flags: n.regexp_input.flags }
|
|
84
|
+
else if n.callee.global_value != "RegExp" then null else {
|
|
85
|
+
value arguments: n.argument.nodes
|
|
86
|
+
value first: first(arguments)
|
|
87
|
+
value pattern: unparenthesised_node(first)
|
|
88
|
+
value flag_argument: regexp_at(arguments, 1)
|
|
89
|
+
value flags: if flag_argument == null then "" else ts_runtime_string(flag_argument)
|
|
90
|
+
value if pattern is ts_spread || type_of(flags) != "str" then null else { flags: flags }
|
|
91
|
+
}
|
|
92
|
+
in regexp_site let regexp_unicode_info regexp_unicode_info(node)
|
|
93
|
+
|
|
94
|
+
}
|