@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,378 @@
|
|
|
1
|
+
import .ts/expression-fixes
|
|
2
|
+
import .ts/constants
|
|
3
|
+
import .ts/suggestions/globals
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
group regexp_site ts_regex, ts_call, ts_new
|
|
8
|
+
|
|
9
|
+
fn regexp_record(nodes, id) = get(nodes, id)
|
|
10
|
+
fn regexp_field(nodes, entry, name) = {
|
|
11
|
+
value ids: get(entry.fields, name) ?? []
|
|
12
|
+
value map(ids, id => regexp_record(nodes, id))
|
|
13
|
+
}
|
|
14
|
+
fn regexp_ancestors(nodes, entry) = {
|
|
15
|
+
value entry
|
|
16
|
+
walk (current) if current.parent == null then null else regexp_record(nodes, current.parent)
|
|
17
|
+
drop 1
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
fn regexp_units(text) = {
|
|
21
|
+
value decoded: js_string_parts(json_string(text))
|
|
22
|
+
value decoded.units
|
|
23
|
+
}
|
|
24
|
+
fn regexp_positions(characters, offset) = {
|
|
25
|
+
value { offset: offset, index: 0, unit: 0 }
|
|
26
|
+
walk (state) if state.index >= len(characters) then null else {
|
|
27
|
+
value width: byte_len(get(characters, state.index))
|
|
28
|
+
value { offset: state.offset + width, index: state.index + 1, unit: state.unit + (if width == 4 then 2 else 1) }
|
|
29
|
+
}
|
|
30
|
+
take len(characters)
|
|
31
|
+
}
|
|
32
|
+
fn regexp_literal_input(n, override_flags) = {
|
|
33
|
+
value pieces: split(n.text, "/")
|
|
34
|
+
value written_flags: last(pieces)
|
|
35
|
+
value flags: if override_flags == null then written_flags else override_flags.value
|
|
36
|
+
value end: byte_len(n.text) - byte_len(written_flags) - 1
|
|
37
|
+
value pattern: byte_slice(n.text, 1, end)
|
|
38
|
+
value characters: split(pattern, "")
|
|
39
|
+
value decoded: js_string_parts(json_string(pattern))
|
|
40
|
+
value positions: regexp_positions(characters, n.offset + 1)
|
|
41
|
+
value spans: map(positions, position => {
|
|
42
|
+
offset: position.offset, end_offset: position.offset + byte_len(get(characters, position.index))
|
|
43
|
+
})
|
|
44
|
+
value units: flat(map(positions, position => {
|
|
45
|
+
value span: get(spans, position.index)
|
|
46
|
+
value count: if span.end_offset - span.offset == 4 then 2 else 1
|
|
47
|
+
value map(slice(decoded.units, position.unit, position.unit + count), unit => {
|
|
48
|
+
value: unit.value, offset: span.offset, end_offset: span.end_offset
|
|
49
|
+
})
|
|
50
|
+
}))
|
|
51
|
+
value {
|
|
52
|
+
text: pattern, source: pattern, flags: flags, spans: spans, units: units,
|
|
53
|
+
offset: n.offset + 1, end_offset: n.offset + end, written: "regex", pattern_node: n
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
fn regexp_string_input(n, flags) = {
|
|
57
|
+
value decoded: js_string_parts(n.text)
|
|
58
|
+
value mapped: if decoded == null then null else {
|
|
59
|
+
value characters: split(decoded.text, "")
|
|
60
|
+
value positions: regexp_positions(characters, 0)
|
|
61
|
+
value segments: map(positions, position => {
|
|
62
|
+
value character: get(characters, position.index)
|
|
63
|
+
value span: get(decoded.spans, position.index)
|
|
64
|
+
value unit: get(decoded.units, position.unit)
|
|
65
|
+
value text: if byte_len(character) != 4 && unit.value >= 55296 && unit.value <= 57343
|
|
66
|
+
then "\\u" pad_start(regexp_hex(unit.value), 4, "0") else character
|
|
67
|
+
value { text: text, spans: map(split(text, ""), character => span) }
|
|
68
|
+
})
|
|
69
|
+
value { text: join(map(segments, segment => segment.text), ""), spans: flat(map(segments, segment => segment.spans)) }
|
|
70
|
+
}
|
|
71
|
+
value if decoded == null then null else {
|
|
72
|
+
text: mapped.text, source: mapped.text, flags: flags,
|
|
73
|
+
spans: map(mapped.spans, span => { offset: n.offset + span.offset, end_offset: n.offset + span.end_offset }),
|
|
74
|
+
units: map(decoded.units, unit => { value: unit.value, offset: n.offset + unit.offset, end_offset: n.offset + unit.end_offset }),
|
|
75
|
+
offset: n.offset + 1, end_offset: n.end_offset - 1, written: if n is ts_string then "string" else "template", pattern_node: n
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
fn regexp_computed_input(n, flags) = {
|
|
79
|
+
value text: ts_runtime_string(n)
|
|
80
|
+
value if type_of(text) != "str" then null else {
|
|
81
|
+
text: text, source: text, flags: flags, spans: null, units: null,
|
|
82
|
+
offset: n.offset, end_offset: n.end_offset, written: "computed", pattern_node: n
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
fn regexp_initialised_binding(n) = {
|
|
86
|
+
value declaration: n.declaration.node
|
|
87
|
+
value declarator: declaration.parent.node
|
|
88
|
+
value declaration != null && declarator is ts_declarator && declarator.init != null
|
|
89
|
+
}
|
|
90
|
+
# Only a file that spells the name can alias the global; binding lookups stay off every other call.
|
|
91
|
+
in ts_program let regexp_mentioned contains(source, "RegExp")
|
|
92
|
+
in ts_call, ts_new let regexp_candidate {
|
|
93
|
+
value callee: unparenthesised_node(node.callee.node)
|
|
94
|
+
value callee is ts_identifier && (callee.text == "RegExp" || ts_module_program(node).regexp_mentioned && regexp_initialised_binding(callee))
|
|
95
|
+
|| callee is (ts_member, ts_index) && ts_global_method_name(callee) == "RegExp"
|
|
96
|
+
|| callee is (ts_non_null, ts_as, ts_satisfies, ts_type_assertion, ts_conditional, ts_logical)
|
|
97
|
+
}
|
|
98
|
+
fn regexp_input(n) = if n is ts_regex then regexp_literal_input(n, null)
|
|
99
|
+
else if n not (ts_call, ts_new) || !n.regexp_candidate || n.callee.global_value != "RegExp" then null else {
|
|
100
|
+
value args: n.argument.nodes
|
|
101
|
+
value argument: first(args)
|
|
102
|
+
value pattern: unparenthesised_node(argument)
|
|
103
|
+
value flag_args: drop(args, 1)
|
|
104
|
+
value flag_argument: first(flag_args)
|
|
105
|
+
value flags: if flag_argument == null then "" else ts_runtime_string(flag_argument)
|
|
106
|
+
value if pattern == null then null
|
|
107
|
+
else if pattern is ts_regex then if flag_argument == null then null else regexp_literal_input(pattern, { value: flags })
|
|
108
|
+
else if pattern is (ts_string, ts_template) then {
|
|
109
|
+
value literal: regexp_string_input(pattern, flags)
|
|
110
|
+
value if literal != null then literal else regexp_computed_input(pattern, flags)
|
|
111
|
+
} else regexp_computed_input(pattern, flags)
|
|
112
|
+
}
|
|
113
|
+
fn regexp_parse(n, input, unicode, sets) = if input == null then null else {
|
|
114
|
+
value initial: syntax_tree(n, "regexp_syntax", {
|
|
115
|
+
text: input.text, spans: input.spans, offset: input.offset, end_offset: input.end_offset,
|
|
116
|
+
flags: { unicode: unicode || sets, sets: sets, named: false }
|
|
117
|
+
})
|
|
118
|
+
value if unicode || sets then initial else {
|
|
119
|
+
value groups: regexp_groups(initial.nodes)
|
|
120
|
+
value named: any(groups, group => regexp_group_name(initial.nodes, group) != null)
|
|
121
|
+
value if !named then initial else syntax_tree(n, "regexp_syntax", {
|
|
122
|
+
text: input.text, spans: input.spans, offset: input.offset, end_offset: input.end_offset,
|
|
123
|
+
flags: { unicode: false, sets: false, named: true }
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
in regexp_site let regexp_input regexp_input(node)
|
|
128
|
+
in regexp_site let regexp_tree {
|
|
129
|
+
value unicode: contains(regexp_input.flags, "u")
|
|
130
|
+
value sets: contains(regexp_input.flags, "v")
|
|
131
|
+
value regexp_parse(node, regexp_input, unicode, sets)
|
|
132
|
+
}
|
|
133
|
+
fn regexp_traversal(nodes, id, after_children) = {
|
|
134
|
+
value width: len(str(len(nodes)))
|
|
135
|
+
value {entry: regexp_record(nodes, id), path: ""}
|
|
136
|
+
walk (item) map(item.entry.children, (child, index) => {
|
|
137
|
+
entry: regexp_record(nodes, child), path: item.path pad_start(str(index), width, "0"),
|
|
138
|
+
})
|
|
139
|
+
sort_by (item) item.path (if contains(after_children, item.entry.kind) then "~" else "")
|
|
140
|
+
map (item) item.entry
|
|
141
|
+
}
|
|
142
|
+
fn regexp_ordered(nodes, id) = regexp_traversal(nodes, id, [])
|
|
143
|
+
in regexp_site let regexp_nodes if regexp_tree.ok then get(regexp_tree, "nodes") else null
|
|
144
|
+
in regexp_site let regexp_view_nodes if regexp_tree.ok then regexp_view(regexp_nodes, regexp_input) else null
|
|
145
|
+
|
|
146
|
+
fn regexp_at(values, index) = if index < 0 then null else get(values, index)
|
|
147
|
+
fn regexp_field_text(nodes, entry, name) = {
|
|
148
|
+
value fields: regexp_field(nodes, entry, name)
|
|
149
|
+
value first(fields).text
|
|
150
|
+
}
|
|
151
|
+
fn regexp_group_kind(nodes, entry) = {
|
|
152
|
+
value open: regexp_field_text(nodes, entry, "open")
|
|
153
|
+
value if open == "(" || starts_with(open, "(?<") && !starts_with(open, "(?<=") && !starts_with(open, "(?<!") then "capturing_group"
|
|
154
|
+
else if contains(["(?=", "(?!", "(?<=", "(?<!"], open) then "lookaround" else "group"
|
|
155
|
+
}
|
|
156
|
+
fn regexp_groups(nodes) = {
|
|
157
|
+
value nodes
|
|
158
|
+
filter (entry) entry.kind == "regexp_group" && regexp_group_kind(nodes, entry) == "capturing_group"
|
|
159
|
+
sort_by (entry) entry.start
|
|
160
|
+
}
|
|
161
|
+
fn regexp_group_name(nodes, entry) = {
|
|
162
|
+
value open: regexp_field_text(nodes, entry, "open")
|
|
163
|
+
value if open == "(" then null else slice(open, 3, -1)
|
|
164
|
+
}
|
|
165
|
+
fn regexp_in_class(nodes, entry) = {
|
|
166
|
+
value ancestors: regexp_ancestors(nodes, entry)
|
|
167
|
+
value any(ancestors, ancestor => contains(["regexp_class", "regexp_string_disjunction"], ancestor.kind))
|
|
168
|
+
}
|
|
169
|
+
fn regexp_unicode(input) = contains(input.flags, "u") || contains(input.flags, "v")
|
|
170
|
+
fn regexp_escape_kind(nodes, input, entry) = {
|
|
171
|
+
value raw: entry.text
|
|
172
|
+
value in_class: regexp_in_class(nodes, entry)
|
|
173
|
+
value unicode: regexp_unicode(input)
|
|
174
|
+
value if !in_class && contains(["\\b", "\\B"], raw) then "assertion"
|
|
175
|
+
else if re_test(raw, /^\\[dDsSwW]$/) || unicode && re_test(raw, /^\\[pP]/) then "character_set"
|
|
176
|
+
else if !in_class && re_test(raw, /^\\[1-9][0-9]*$/) then {
|
|
177
|
+
value number: { value raw; slice 1; num }
|
|
178
|
+
value if unicode || number <= len(regexp_groups(nodes)) then "backreference" else "character"
|
|
179
|
+
}
|
|
180
|
+
else if !in_class && starts_with(raw, "\\k") && (unicode || {
|
|
181
|
+
value groups: regexp_groups(nodes)
|
|
182
|
+
value any(groups, group => regexp_group_name(nodes, group) != null)
|
|
183
|
+
}) then "backreference"
|
|
184
|
+
else "character"
|
|
185
|
+
}
|
|
186
|
+
fn regexp_character_units(input, entry) = {
|
|
187
|
+
value raw: entry.text
|
|
188
|
+
value if raw == "\\" then [{ value: 92 }]
|
|
189
|
+
else if re_test(raw, /^\\[\r\n\u{2028}\u{2029}]$/) then {
|
|
190
|
+
value identity: slice(raw, 1)
|
|
191
|
+
value regexp_units(identity)
|
|
192
|
+
}
|
|
193
|
+
else if entry.kind == "regexp_character" then
|
|
194
|
+
if input.units == null then regexp_units(raw) else filter(input.units, unit => unit.offset >= entry.offset && unit.end_offset <= entry.end_offset)
|
|
195
|
+
else if re_test(raw, /^\\c[A-Za-z0-9_]$/) then {
|
|
196
|
+
value character: slice(raw, -1)
|
|
197
|
+
value units: regexp_units(character)
|
|
198
|
+
value [{ value: first(units).value % 32 }]
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
value quoted: '"' raw '"'
|
|
202
|
+
value decoded: js_string_parts(quoted)
|
|
203
|
+
value if decoded != null then decoded.units else {
|
|
204
|
+
value identity: slice(raw, 1)
|
|
205
|
+
value regexp_units(identity)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
fn regexp_point(units) = if len(units) == 2 && first(units).value >= 55296 && first(units).value <= 56319
|
|
210
|
+
&& last(units).value >= 56320 && last(units).value <= 57343 then 65536 + (first(units).value - 55296) * 1024 + last(units).value - 56320
|
|
211
|
+
else first(units).value
|
|
212
|
+
fn regexp_hex(value) = {
|
|
213
|
+
value digits: split("0123456789abcdef", "")
|
|
214
|
+
value digit: regexp_at(digits, value % 16)
|
|
215
|
+
value if value < 16 then digit else {
|
|
216
|
+
value parts: walk(value, current => if current < 16 then null else divide((current - current % 16), 16))
|
|
217
|
+
value fold(parts, "", (text, current) => regexp_at(digits, current % 16) text)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
fn regexp_character(value) = if value == null || value >= 55296 && value <= 57343 then null else {
|
|
221
|
+
value hex: regexp_hex(value)
|
|
222
|
+
value literal: '"\\u{{' hex '}}"'
|
|
223
|
+
value js_string(literal)
|
|
224
|
+
}
|
|
225
|
+
fn regexp_kind(nodes, input, entry) = if entry.kind == "regexp_group" then regexp_group_kind(nodes, entry)
|
|
226
|
+
else if entry.kind == "regexp_escape" then regexp_escape_kind(nodes, input, entry)
|
|
227
|
+
else if entry.kind == "regexp_class" then "class"
|
|
228
|
+
else if entry.kind == "regexp_range" then "class_range"
|
|
229
|
+
else if entry.kind == "regexp_string_disjunction" then "string_disjunction"
|
|
230
|
+
else if entry.kind == "regexp_set" then "character_set"
|
|
231
|
+
else slice(entry.kind, 7)
|
|
232
|
+
fn regexp_flat_range(nodes, input, entry) = entry.kind == "regexp_range" && !regexp_unicode(input) && {
|
|
233
|
+
value from: { value regexp_field(nodes, entry, "from"); first }
|
|
234
|
+
value to: { value regexp_field(nodes, entry, "to"); first }
|
|
235
|
+
value regexp_kind(nodes, input, from) != "character" || regexp_kind(nodes, input, to) != "character"
|
|
236
|
+
}
|
|
237
|
+
fn regexp_view_parent(nodes, input, entry) = {
|
|
238
|
+
value terminal: {
|
|
239
|
+
value entry
|
|
240
|
+
walk (current) {
|
|
241
|
+
value parent: regexp_record(nodes, current.parent)
|
|
242
|
+
value if parent.kind == "regexp_string_alternative" || regexp_flat_range(nodes, input, parent) then parent else null
|
|
243
|
+
}
|
|
244
|
+
last
|
|
245
|
+
}
|
|
246
|
+
value terminal.parent
|
|
247
|
+
}
|
|
248
|
+
fn regexp_initial(values) = {
|
|
249
|
+
value count: len(values) - 1
|
|
250
|
+
value take(values, count)
|
|
251
|
+
}
|
|
252
|
+
fn regexp_character_parts(nodes, input, entry) = {
|
|
253
|
+
value units: regexp_character_units(input, entry)
|
|
254
|
+
value point: regexp_point(units)
|
|
255
|
+
value separate: entry.kind == "regexp_escape" && point <= 65535 && len(units) > 1
|
|
256
|
+
value if regexp_unicode(input) || len(units) <= 1 then [{ id: entry.id, value: point, start: entry.start, end: entry.end }] else
|
|
257
|
+
map(units, unit => {
|
|
258
|
+
value index: index_of(units, unit)
|
|
259
|
+
value {
|
|
260
|
+
id: str(entry.id) ":" str(index), value: unit.value,
|
|
261
|
+
start: if separate then entry.start + unit.offset - 1 else entry.start,
|
|
262
|
+
end: if separate then entry.start + unit.end_offset - 1 else entry.end
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
fn regexp_decimal_tail(nodes, input, entry) = if entry.kind != "regexp_escape" || regexp_unicode(input)
|
|
267
|
+
|| regexp_escape_kind(nodes, input, entry) != "character" then false else {
|
|
268
|
+
value units: regexp_character_units(input, entry)
|
|
269
|
+
value point: regexp_point(units)
|
|
270
|
+
value len(units) > 1 && point <= 65535
|
|
271
|
+
}
|
|
272
|
+
fn regexp_view_ids(nodes, input, entry) = if entry.kind == "TEXT" || entry.kind == "regexp_set_operator" then []
|
|
273
|
+
else if entry.kind == "regexp_string_alternative" || regexp_flat_range(nodes, input, entry) then {
|
|
274
|
+
value parts: map(entry.children, id => {
|
|
275
|
+
value child: regexp_record(nodes, id)
|
|
276
|
+
value regexp_view_ids(nodes, input, child)
|
|
277
|
+
})
|
|
278
|
+
value ids: flat(parts)
|
|
279
|
+
value dash: str(entry.id) ":dash"
|
|
280
|
+
value if entry.kind == "regexp_range" then concat(ids, [dash]) else ids
|
|
281
|
+
} else if entry.kind == "regexp_quantifier" then {
|
|
282
|
+
value element: { value regexp_field(nodes, entry, "element"); first }
|
|
283
|
+
value parts: regexp_view_ids(nodes, input, element)
|
|
284
|
+
value prefix: if regexp_decimal_tail(nodes, input, element) then regexp_initial(parts) else []
|
|
285
|
+
value concat(prefix, [entry.id])
|
|
286
|
+
} else if entry.kind == "regexp_range" then {
|
|
287
|
+
value from: { value regexp_field(nodes, entry, "from"); first }
|
|
288
|
+
value to: { value regexp_field(nodes, entry, "to"); first }
|
|
289
|
+
value a: regexp_view_ids(nodes, input, from)
|
|
290
|
+
value b: regexp_view_ids(nodes, input, to)
|
|
291
|
+
value a_prefix: regexp_initial(a)
|
|
292
|
+
value b_prefix: regexp_initial(b)
|
|
293
|
+
value prefix: concat(a_prefix, b_prefix)
|
|
294
|
+
value concat(prefix, [entry.id])
|
|
295
|
+
} else if regexp_kind(nodes, input, entry) != "character" then [entry.id] else {
|
|
296
|
+
value parts: regexp_character_parts(nodes, input, entry)
|
|
297
|
+
value map(parts, part => part.id)
|
|
298
|
+
}
|
|
299
|
+
fn regexp_view_children(nodes, input, entry) = if entry.kind == "regexp_quantifier" then {
|
|
300
|
+
value element: { value regexp_field(nodes, entry, "element"); first }
|
|
301
|
+
value parts: regexp_view_ids(nodes, input, element)
|
|
302
|
+
value if regexp_decimal_tail(nodes, input, element) then [last(parts)] else parts
|
|
303
|
+
} else if entry.kind == "regexp_range" && !regexp_flat_range(nodes, input, entry) then {
|
|
304
|
+
value from: { value regexp_field(nodes, entry, "from"); first }
|
|
305
|
+
value to: { value regexp_field(nodes, entry, "to"); first }
|
|
306
|
+
value a: regexp_view_ids(nodes, input, from)
|
|
307
|
+
value b: regexp_view_ids(nodes, input, to)
|
|
308
|
+
value [last(a), last(b)]
|
|
309
|
+
} else {
|
|
310
|
+
value child_parts: map(entry.children, id => {
|
|
311
|
+
value child: regexp_record(nodes, id)
|
|
312
|
+
value regexp_view_ids(nodes, input, child)
|
|
313
|
+
})
|
|
314
|
+
value flat(child_parts)
|
|
315
|
+
}
|
|
316
|
+
fn regexp_part_parent(nodes, input, entry, id) = {
|
|
317
|
+
value parent: regexp_record(nodes, entry.parent)
|
|
318
|
+
value lifted: parent.kind == "regexp_quantifier" && regexp_decimal_tail(nodes, input, entry)
|
|
319
|
+
|| parent.kind == "regexp_range" && !regexp_flat_range(nodes, input, parent)
|
|
320
|
+
value if !lifted then regexp_view_parent(nodes, input, entry) else {
|
|
321
|
+
value parts: regexp_character_parts(nodes, input, entry)
|
|
322
|
+
value if id != last(parts).id then regexp_view_parent(nodes, input, parent) else regexp_view_parent(nodes, input, entry)
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
fn regexp_view_record(nodes, input, entry, kind, value, id, part) = {
|
|
326
|
+
value start: if part == null then entry.start else part.start
|
|
327
|
+
value end: if part == null then entry.end else part.end
|
|
328
|
+
value span: if start == entry.start && end == entry.end then { offset: entry.offset, end: entry.end_offset } else regexp_location(input, start, end)
|
|
329
|
+
value parent: if part == null then regexp_view_parent(nodes, input, entry) else regexp_part_parent(nodes, input, entry, id)
|
|
330
|
+
value children: regexp_view_children(nodes, input, entry)
|
|
331
|
+
value raw_name: if kind == "capturing_group" then regexp_group_name(nodes, entry) else null
|
|
332
|
+
value {
|
|
333
|
+
id: id, parent: parent, kind: kind, raw: byte_slice(input.text, start, end), written: regexp_written(input, start, end), children: children,
|
|
334
|
+
start: start, end: end, offset: span.offset, end_offset: span.end,
|
|
335
|
+
value: value, character: regexp_character(value), name: if raw_name == null then null else regexp_name(raw_name),
|
|
336
|
+
behind: kind == "lookaround" && starts_with(entry.text, "(?<"),
|
|
337
|
+
negative: kind == "lookaround" && (starts_with(entry.text, "(?!") || starts_with(entry.text, "(?<!")),
|
|
338
|
+
negate: if kind == "class" then regexp_field_text(nodes, entry, "negate") == "^" else false,
|
|
339
|
+
expression: kind == "class" && any(nodes, item => item.parent == entry.id && item.kind == "regexp_set_operator")
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
fn regexp_view_base(nodes, input, entry) = {
|
|
343
|
+
value kind: regexp_kind(nodes, input, entry)
|
|
344
|
+
value if kind == "quantifier" then {
|
|
345
|
+
value element: { value regexp_field(nodes, entry, "element"); first }
|
|
346
|
+
value extent: if regexp_decimal_tail(nodes, input, element) then {
|
|
347
|
+
value parts: regexp_character_parts(nodes, input, element)
|
|
348
|
+
value { start: last(parts).start, end: entry.end }
|
|
349
|
+
} else null
|
|
350
|
+
value [regexp_view_record(nodes, input, entry, kind, null, entry.id, extent)]
|
|
351
|
+
} else if kind != "character" then [regexp_view_record(nodes, input, entry, kind, null, entry.id, null)] else {
|
|
352
|
+
value parts: regexp_character_parts(nodes, input, entry)
|
|
353
|
+
value map(parts, part => regexp_view_record(nodes, input, entry, kind, part.value, part.id, part))
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
fn regexp_view_entry(nodes, input, entry) = if contains(["TEXT", "regexp_set_operator", "regexp_string_alternative"], entry.kind)
|
|
357
|
+
|| regexp_flat_range(nodes, input, entry) then [] else {
|
|
358
|
+
value parts: regexp_view_base(nodes, input, entry)
|
|
359
|
+
value parent: regexp_record(nodes, entry.parent)
|
|
360
|
+
value to: { value regexp_field(nodes, parent, "to"); first }
|
|
361
|
+
value if !regexp_flat_range(nodes, input, parent) || to.id != entry.id then parts else {
|
|
362
|
+
value dash: { value regexp_field(nodes, parent, "dash"); first }
|
|
363
|
+
value id: str(parent.id) ":dash"
|
|
364
|
+
value record: regexp_view_record(nodes, input, dash, "character", 45, id, null)
|
|
365
|
+
value concat(parts, [record])
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
fn regexp_view(nodes, input) = {
|
|
369
|
+
value root: find(nodes, entry => entry.parent == null)
|
|
370
|
+
value ordered: regexp_traversal(nodes, root.id, ["regexp_range", "regexp_quantifier"])
|
|
371
|
+
value entries: filter(ordered, entry => entry.kind != "TEXT")
|
|
372
|
+
value parts: map(entries, entry => regexp_view_entry(nodes, input, entry))
|
|
373
|
+
value flat(parts)
|
|
374
|
+
}
|
|
375
|
+
in regexp_site let regexp_syntax if regexp_tree.ok && type_of(regexp_input.flags) == "str" && regex_flags_error(regexp_input.flags, "") == null
|
|
376
|
+
&& regexp_error == null then regexp_view_nodes else null
|
|
377
|
+
|
|
378
|
+
}
|
package/std/ts/regex.dx
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Comment metadata and inline, warning-term and initial-case policies.
|
|
2
|
+
define {
|
|
3
|
+
|
|
4
|
+
@macro restriction_directive(x) re_test(x, /^\s*(?:eslint(?:[- ]|$)|globals?\s|exported\s)/)
|
|
5
|
+
in ts_program {
|
|
6
|
+
let restriction_comment_catalog descendants(ts_comment)
|
|
7
|
+
let restriction_comment_line_catalog overlapping(ts_line)
|
|
8
|
+
}
|
|
9
|
+
in ts_comment {
|
|
10
|
+
let restriction_comment_body { value result: if is ts_block_comment then len(text) - 2 else len(text); value slice(text, 2, result) }
|
|
11
|
+
let restriction_comment_clean replace(restriction_comment_body, "*", "")
|
|
12
|
+
let restriction_comment_lowercase re_test(restriction_comment_clean, /^\s*\p{Lowercase}/)
|
|
13
|
+
let restriction_comment_uppercase re_test(restriction_comment_clean, /^\s*\p{Uppercase}/)
|
|
14
|
+
let restriction_comment_prefix { value js_line_start: position(offset).js_line_start; value byte_slice(source, js_line_start, offset) }
|
|
15
|
+
let restriction_comment_line_end {
|
|
16
|
+
value result: position(end_offset).js_line - 1
|
|
17
|
+
value js_line: position(end_offset).js_line
|
|
18
|
+
value slice(ts_program.restriction_comment_line_catalog, result, js_line)
|
|
19
|
+
first
|
|
20
|
+
}.end_offset
|
|
21
|
+
let restriction_comment_suffix byte_slice(source, end_offset, restriction_comment_line_end)
|
|
22
|
+
let restriction_comment_preamble trim(restriction_comment_prefix) != ""
|
|
23
|
+
let restriction_comment_postamble trim(restriction_comment_suffix) != ""
|
|
24
|
+
let restriction_comment_empty_jsx parent is ts_jsx_expression && parent.expression == null && (!restriction_comment_preamble || trim(restriction_comment_prefix) == "{{") && (!restriction_comment_postamble || trim(restriction_comment_suffix) == "}}")
|
|
25
|
+
let restriction_comment_index index_of(ts_program.restriction_comment_catalog, node)
|
|
26
|
+
let restriction_comment_previous { value ts_program.restriction_comment_catalog; slice (restriction_comment_index - 1) restriction_comment_index; last }
|
|
27
|
+
let restriction_comment_consecutive restriction_comment_previous != null && { value source; byte_slice restriction_comment_previous.end_offset offset; trim } == ""
|
|
28
|
+
}
|
|
29
|
+
in ts_comment rule es-no-inline-comments {
|
|
30
|
+
option ignorePattern regex = "" "comments to ignore"
|
|
31
|
+
when (restriction_comment_preamble || restriction_comment_postamble) && !restriction_comment_empty_jsx && !@restriction_directive(restriction_comment_body) && !(ignorePattern != "" && re_test(restriction_comment_body, ignorePattern))
|
|
32
|
+
warn "Unexpected comment inline with code."
|
|
33
|
+
}
|
|
34
|
+
in ts_comment rule es-no-warning-comments {
|
|
35
|
+
option terms string[] = ["todo", "fixme", "xxx"] "warning terms"
|
|
36
|
+
option location "start" | "anywhere" = "start" "where warning terms may occur"
|
|
37
|
+
option decoration string(1..1)[] = [] "characters ignored before a starting warning"
|
|
38
|
+
let body { value text; re_replace /^\/[\/*]/ ""; re_replace /\*\/$/ "" }
|
|
39
|
+
let compact { value body; trim; re_replace /\s+/ " " }
|
|
40
|
+
let short { value { value compact; re_find /^.{0,40}(?:\s|$)/; nonempty } ?? ""; trim }
|
|
41
|
+
let display short (if len(compact) > len(short) then "..." else "")
|
|
42
|
+
when !(@restriction_directive(body) && re_test(body, /\bno-warning-comments\b/))
|
|
43
|
+
@each term in terms
|
|
44
|
+
when {
|
|
45
|
+
value result: "(?i)" (if location == "start" then "^[\\s" { value decoration; join ""; re_escape } "]*" else if re_test(term, /^\w/) then "\\b" else "") re_escape(term) (if re_test(term, /\w$/) then "\\b" else "")
|
|
46
|
+
value re_test(body, result)
|
|
47
|
+
}
|
|
48
|
+
warn "Unexpected '{term}' comment: '{display}'."
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn comment_case_fix(n, body, mode) = {
|
|
52
|
+
value prefix: re_find(body, /^[^\p{L}]*/)
|
|
53
|
+
value char: re_find(body, /\p{L}/)
|
|
54
|
+
value start: n.offset + 2 + byte_len(prefix)
|
|
55
|
+
value {
|
|
56
|
+
target: { start: start, end: start + byte_len(char) },
|
|
57
|
+
text: if mode == "always" then upper(char) else lower(char)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
in ts_comment let capitalized_comments_checked true
|
|
61
|
+
@macro restriction_capitalized_comment_body {
|
|
62
|
+
option mode "always" | "never" = "always" "required initial letter case"
|
|
63
|
+
option ignorePattern regex = "" "starting comment text to ignore"
|
|
64
|
+
when capitalized_comments_checked
|
|
65
|
+
when (if mode == "always" then restriction_comment_lowercase else restriction_comment_uppercase)
|
|
66
|
+
&& !re_test(restriction_comment_body, /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/)
|
|
67
|
+
&& !re_test(restriction_comment_clean, /^\s*[^:/?#\s]+:\/\/[^?#]/)
|
|
68
|
+
&& !(ignorePattern != "" && re_test(restriction_comment_clean, "^\\s*(?:" ignorePattern ")"))
|
|
69
|
+
let edit comment_case_fix(node, restriction_comment_body, mode)
|
|
70
|
+
fix at edit.target as edit.text
|
|
71
|
+
warn if mode == "always" then "Comments should not begin with a lowercase character." else "Comments should not begin with an uppercase character."
|
|
72
|
+
}
|
|
73
|
+
in ts_line_comment rule es-capitalized-comments:line { @restriction_capitalized_comment_body }
|
|
74
|
+
in ts_block_comment rule es-capitalized-comments:block { @restriction_capitalized_comment_body }
|
|
75
|
+
|
|
76
|
+
}
|