@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,142 @@
|
|
|
1
|
+
# .ts/layout/text: text layout rules.
|
|
2
|
+
|
|
3
|
+
import .ts/layout/shared
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
@macro multiple_space_horizontal "[ \\t\\u{{b}}\\u{{c}}\\u{{a0}}\\u{{1680}}\\u{{2000}}-\\u{{200a}}\\u{{202f}}\\u{{205f}}\\u{{3000}}\\u{{feff}}]"
|
|
8
|
+
@macro multiple_space_nonspace "[^ \\t\\u{{b}}\\u{{c}}\\r\\n\\u{{a0}}\\u{{1680}}\\u{{2000}}-\\u{{200a}}\\u{{2028}}\\u{{2029}}\\u{{202f}}\\u{{205f}}\\u{{3000}}\\u{{feff}}]"
|
|
9
|
+
in ts_code match multiple_space_gap /[ \t\u{b}\u{c}\u{a0}\u{1680}\u{2000}-\u{200a}\u{202f}\u{205f}\u{3000}\u{feff}]{2,}/
|
|
10
|
+
fn multiple_space_preview_body(body) = if contains(body, "\n") || len(body) > 12 then {
|
|
11
|
+
value body
|
|
12
|
+
split "\n"
|
|
13
|
+
first
|
|
14
|
+
slice 0 12
|
|
15
|
+
} "..." else body
|
|
16
|
+
fn multiple_space_comment_preview(comment) = (if comment is ts_block_comment then "/*" else "//") {
|
|
17
|
+
value result: if comment is ts_block_comment then -2 else len(comment.text)
|
|
18
|
+
value slice(comment.text, 2, result)
|
|
19
|
+
multiple_space_preview_body
|
|
20
|
+
} (if comment is ts_block_comment then "*/" else "")
|
|
21
|
+
in multiple_space_gap {
|
|
22
|
+
let multiple_space_right node_at(end_offset)
|
|
23
|
+
let multiple_space_eol_comment multiple_space_right is ts_comment && {
|
|
24
|
+
value result: "^" @multiple_space_horizontal "*(?:$|[\\r\\n\\u{{2028}}\\u{{2029}}])"
|
|
25
|
+
value re_test(multiple_space_right.text_after, result)
|
|
26
|
+
}
|
|
27
|
+
let multiple_space_display if multiple_space_right is ts_comment then multiple_space_comment_preview(multiple_space_right) else ts_token_neighbours(source, node).after.text
|
|
28
|
+
let multiple_spaces_checked true
|
|
29
|
+
}
|
|
30
|
+
in multiple_space_gap rule es-no-multi-spaces {
|
|
31
|
+
option includeTabs boolean = true "include tabs in repeated whitespace"
|
|
32
|
+
when multiple_spaces_checked && { value result: if includeTabs then /[ \t]{2}/ else / {2}/; value re_test(text, result) }
|
|
33
|
+
&& {
|
|
34
|
+
value js_line_start: position(offset).js_line_start
|
|
35
|
+
value byte_slice(source, js_line_start, offset)
|
|
36
|
+
re_test @multiple_space_nonspace
|
|
37
|
+
}
|
|
38
|
+
&& { value pattern: "^" @multiple_space_nonspace; value re_test(text_after, pattern) }
|
|
39
|
+
&& { value overlapping(ts_comment); count } == 0
|
|
40
|
+
fix as " "
|
|
41
|
+
warn "Multiple spaces found before '{multiple_space_display}'."
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@macro layout_comment_policy {
|
|
46
|
+
option style "always" | "never" = "always" "spacing inside comment delimiters"
|
|
47
|
+
option exceptions string[] = [] "repeated sequences exempt from spacing"
|
|
48
|
+
option markers string[] = [] "comment prefix markers"
|
|
49
|
+
let comment_block is ts_block_comment
|
|
50
|
+
let comment_body { value result: byte_len(text) - (if comment_block then 2 else 0); value byte_slice(text, 2, result) }
|
|
51
|
+
let comment_marker_pattern "(?:" { value markers; map (m) re_escape(m); join "|" } (if count(markers) > 0 then "|\\*)" else "\\*)")
|
|
52
|
+
let comment_exception_pattern if count(exceptions) == 0 then "\\s" else "(?:\\s|(?:" { value exceptions; map (e) "(?:" re_escape(e) ")+"; join "|" } ")(?:$|[\\n\\r\\u{{2028}}\\u{{2029}}]))"
|
|
53
|
+
let comment_marker { value comment_body; re_find ("^" comment_marker_pattern); nonempty } ?? ""
|
|
54
|
+
when comment_body != "" && comment_body != "*" && !contains(markers, comment_body)
|
|
55
|
+
when comment_block || !re_test(comment_body, /^\/\s*<(?:reference|amd-)/)
|
|
56
|
+
}
|
|
57
|
+
fn layout_comment_open_wrong(body, style, marker_pattern, exception_pattern) = if style == "always" then !re_test(body, "^" marker_pattern "?" exception_pattern) else re_test(body, "^" marker_pattern "?[ \\t]+")
|
|
58
|
+
fn layout_comment_open_message(style, exceptions, marker, identifier) = when {
|
|
59
|
+
style == "always" then if count(exceptions) > 0 then "Expected exception block, space or tab after '{identifier}{marker}' in comment." else "Expected space or tab after '{identifier}{marker}' in comment."
|
|
60
|
+
marker != "" then "Unexpected space or tab after marker ({marker}) in comment."
|
|
61
|
+
else then "Unexpected space or tab after '{identifier}' in comment."
|
|
62
|
+
}
|
|
63
|
+
fn layout_comment_open_fix(n, body, style, marker) = {
|
|
64
|
+
value marker_size: byte_len(marker)
|
|
65
|
+
value start: n.offset + 2 + marker_size
|
|
66
|
+
value spaces: if style == "never" then { value byte_slice(body, marker_size); re_find /^[ \t]+/ } ?? "" else ""
|
|
67
|
+
value { start: start, end: start + byte_len(spaces) }
|
|
68
|
+
}
|
|
69
|
+
in ts_line_comment rule es-spaced-comment:line {
|
|
70
|
+
@layout_comment_policy
|
|
71
|
+
when layout_comment_open_wrong(comment_body, style, comment_marker_pattern, comment_exception_pattern)
|
|
72
|
+
let comment_fix layout_comment_open_fix(node, comment_body, style, comment_marker)
|
|
73
|
+
fix at comment_fix as if style == "always" then " " else ""
|
|
74
|
+
warn layout_comment_open_message(style, exceptions, comment_marker, "//")
|
|
75
|
+
}
|
|
76
|
+
in ts_block_comment rule es-spaced-comment:block {
|
|
77
|
+
@layout_comment_policy
|
|
78
|
+
option balanced boolean = false "require matching closing delimiter spacing"
|
|
79
|
+
let opening_fix layout_comment_open_fix(node, comment_body, style, comment_marker)
|
|
80
|
+
let closing_spaces if style == "never" then re_find(comment_body, /[ \t]+$/) ?? "" else ""
|
|
81
|
+
let closing_fix { start: end_offset - 2 - byte_len(closing_spaces), end: end_offset - 2 }
|
|
82
|
+
each comment_end in [false, true] {
|
|
83
|
+
when !comment_end || balanced
|
|
84
|
+
when if comment_end then (if style == "always" then !re_test(comment_body, comment_exception_pattern "$") else re_test(comment_body, /[ \t]+$/)) else layout_comment_open_wrong(comment_body, style, comment_marker_pattern, comment_exception_pattern)
|
|
85
|
+
fix at (if comment_end then closing_fix else opening_fix) as if style == "always" then " " else ""
|
|
86
|
+
warn when {
|
|
87
|
+
!comment_end then layout_comment_open_message(style, exceptions, comment_marker, "/*")
|
|
88
|
+
style == "always" then "Expected space or tab before '*/' in comment."
|
|
89
|
+
else then "Unexpected space or tab before '*/' in comment."
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Non-BOF runs include the previous line ending: physical boundaries without lookbehind.
|
|
96
|
+
in .js layer layout_blank_lines match layout_blank_run (
|
|
97
|
+
/\A(?:(?:[^\S\r\n]*[\r\n])+(?:[^\S\r\n]+\z)?|[^\S\r\n]+\z)/
|
|
98
|
+
or /(?:\r\n[^\S\r\n]*[\r\n]|\n[^\S\r\n]*[\r\n]|\r\r|\r[^\S\r\n]+[\r\n])(?:[^\S\r\n]*[\r\n])*(?:[^\S\r\n]+\z)?/
|
|
99
|
+
or /(?:\r\n|[\r\n])[^\S\r\n]+\z/
|
|
100
|
+
)
|
|
101
|
+
in layout_blank_run rule es-no-multiple-empty-lines {
|
|
102
|
+
option max int(0..) = 2 "maximum consecutive blank lines"
|
|
103
|
+
option maxEOF int(0..) | null = null "maximum final blank lines"
|
|
104
|
+
option maxBOF int(0..) | null = null "maximum initial blank lines"
|
|
105
|
+
let blank_prefix when {
|
|
106
|
+
offset == 0 then 0
|
|
107
|
+
starts_with(text, "\r\n") then 2
|
|
108
|
+
else then 1
|
|
109
|
+
}
|
|
110
|
+
let blank_text byte_slice(text, blank_prefix)
|
|
111
|
+
let blank_kind when {
|
|
112
|
+
offset == 0 then "bof"
|
|
113
|
+
end_offset == byte_len(source) then "eof"
|
|
114
|
+
else then "middle"
|
|
115
|
+
}
|
|
116
|
+
let blank_count re_count(blank_text, /\r\n|[\n\r]/) + (if re_test(blank_text, /[\r\n]\z/) then 0 else 1)
|
|
117
|
+
let blank_limit when {
|
|
118
|
+
blank_kind == "bof" then maxBOF.nonempty ?? max
|
|
119
|
+
blank_kind == "eof" then maxEOF.nonempty ?? max
|
|
120
|
+
else then max
|
|
121
|
+
}
|
|
122
|
+
when blank_count > blank_limit
|
|
123
|
+
# Searching the syntax tree costs more than counting, so it comes after.
|
|
124
|
+
when containing(ts_template.quasi) == null && containing(ts_template_type.quasi) == null
|
|
125
|
+
# Blank whitespace cannot contain NUL; split after line endings to retain their byte lengths.
|
|
126
|
+
let blank_parts { value blank_text; re_replace /(\r\n|[\r\n])/ "$1\0"; split "\0" }
|
|
127
|
+
let blank_remove { value blank_parts; slice 0 (blank_count - blank_limit); join ""; byte_len }
|
|
128
|
+
fix at { start: offset + blank_prefix, end: offset + blank_prefix + blank_remove } as ""
|
|
129
|
+
report at { start: offset + blank_prefix + {
|
|
130
|
+
value blank_parts
|
|
131
|
+
slice 0 blank_limit
|
|
132
|
+
join ""
|
|
133
|
+
byte_len
|
|
134
|
+
}, end: end_offset }
|
|
135
|
+
warn when {
|
|
136
|
+
blank_kind == "bof" then "Too many blank lines at the beginning of file. Max of {blank_limit} allowed."
|
|
137
|
+
blank_kind == "eof" then "Too many blank lines at the end of file. Max of {blank_limit} allowed."
|
|
138
|
+
else then "More than {blank_limit} blank {if blank_limit == 1 then 'line' else 'lines'} not allowed."
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# .ts/layout/types: types layout rules.
|
|
2
|
+
|
|
3
|
+
import .ts/layout/shared
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
schema member_separator_options { delimiter?: "semi" | "comma" | "none"; requireLast?: boolean }
|
|
8
|
+
schema member_separator_config { multiline?: member_separator_options; singleline?: member_separator_options }
|
|
9
|
+
fn layout_member_line_end(source, end) = { value byte_slice(source, end); re_test /^(?:[\n\r\u{2028}\u{2029}]|$)/ }
|
|
10
|
+
in ts_interface, ts_type_literal let layout_last_member last(member.node)
|
|
11
|
+
in ts_interface, ts_type_literal let layout_members_single position(opener.ts_open_brace.offset).js_line == position(end_offset).js_line
|
|
12
|
+
in ts_interface, ts_type_literal let layout_last_member_inline position(layout_last_member.member_terminal_token.end).js_line == position(end_offset).js_line
|
|
13
|
+
in ts_property_signature, ts_method_signature, ts_call_signature, ts_index_signature rule es-member-delimiter-style {
|
|
14
|
+
option delimiter "semi" | "comma" | "none" | null = null "default separator for legacy configurations"
|
|
15
|
+
option multiline member_separator_options = {} "multiline member separators"
|
|
16
|
+
option singleline member_separator_options = {} "single-line member separators"
|
|
17
|
+
option overrides { interface?: member_separator_config; typeLiteral?: member_separator_config } = {} "separators by declaration kind"
|
|
18
|
+
option multilineDetection "brackets" | "last-member" = "brackets" "multiline classification"
|
|
19
|
+
when parent is (ts_interface, ts_type_literal)
|
|
20
|
+
let member_single parent.layout_members_single || multilineDetection == "last-member" && parent.layout_last_member_inline
|
|
21
|
+
let member_override if parent is ts_interface then overrides.interface else overrides.typeLiteral
|
|
22
|
+
let member_base if member_single then singleline else multiline
|
|
23
|
+
let member_policy if member_single then member_override.singleline else member_override.multiline
|
|
24
|
+
let member_delimiter member_policy.delimiter.nonempty ?? member_base.delimiter.nonempty ?? delimiter.nonempty ?? "semi"
|
|
25
|
+
let member_require_last member_policy.requireLast.nonempty ?? member_base.requireLast.nonempty ?? !member_single
|
|
26
|
+
let member_last offset == parent.layout_last_member.offset
|
|
27
|
+
let member_token member_terminal_token
|
|
28
|
+
let member_actual if member_token.text == ";" then "semi" else if member_token.text == "," then "comma" else "none"
|
|
29
|
+
when member_actual != (if member_last && !member_require_last then "none" else member_delimiter)
|
|
30
|
+
let member_remove member_last && !member_require_last || member_delimiter == "none"
|
|
31
|
+
let member_comment_gap "^(?:\\s*" @layout_comment_pattern ")*\\s*$"
|
|
32
|
+
let member_comment { value overlapping(ts_comment, member_token.end, parent.end_offset); filter (c) { value byte_slice(source, member_token.end, c.offset); re_test member_comment_gap }; last }
|
|
33
|
+
let member_remove_safe member_single || layout_member_line_end(source, member_token.end) || member_comment != null && (position(member_comment.end_offset).js_line > position(member_token.end).js_line || layout_member_line_end(source, member_comment.end_offset))
|
|
34
|
+
fix at { start: if member_actual == "none" then member_token.end else member_token.start, end: member_token.end } if !member_remove || member_remove_safe as (if member_remove then "" else if member_delimiter == "semi" then ";" else ",")
|
|
35
|
+
report at member_token.end
|
|
36
|
+
warn if member_last && !member_require_last || member_delimiter == "none" then (if member_actual == "semi" then "Unexpected separator (;)." else "Unexpected separator (,).") else if member_delimiter == "semi" then "Expected a semicolon." else "Expected a comma."
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
schema annotation_gap_options { before?: boolean; after?: boolean }
|
|
40
|
+
group layout_annotation_owner ts_declarator, ts_parameter, ts_class_property, ts_property_signature,
|
|
41
|
+
ts_index_signature, ts_tuple_member, ts_mapped_type, ts_function, ts_arrow, ts_method,
|
|
42
|
+
ts_method_signature, ts_call_signature, ts_function_type
|
|
43
|
+
in ts_declarator, ts_parameter, ts_class_property, ts_property_signature, ts_index_signature, ts_tuple_member, ts_mapped_type let layout_annotation_start type.offset
|
|
44
|
+
in ts_function, ts_arrow, ts_method, ts_method_signature, ts_call_signature, ts_function_type let layout_annotation_start return_type.offset
|
|
45
|
+
in ts_declarator let layout_annotation_category "variable"
|
|
46
|
+
in ts_parameter let layout_annotation_category "parameter"
|
|
47
|
+
in ts_class_property, ts_property_signature, ts_index_signature, ts_method_signature, ts_call_signature let layout_annotation_category "property"
|
|
48
|
+
in ts_function, ts_arrow, ts_method let layout_annotation_category "returnType"
|
|
49
|
+
in ts_function_type, ts_tuple_member, ts_mapped_type let layout_annotation_category "colon"
|
|
50
|
+
in ts_colon let layout_annotation_optional token(node, "before").text == "?"
|
|
51
|
+
@macro layout_annotation_options {
|
|
52
|
+
option before boolean = false "space before annotation punctuation"
|
|
53
|
+
option after boolean = true "space after annotation punctuation"
|
|
54
|
+
option overrides { colon?: annotation_gap_options; variable?: annotation_gap_options | "ignore"; property?: annotation_gap_options | "ignore"; parameter?: annotation_gap_options | "ignore"; returnType?: annotation_gap_options | "ignore"; questionMark?: annotation_gap_options | "ignore" } = {} "annotation context policies"
|
|
55
|
+
}
|
|
56
|
+
@macro layout_annotation_policy {
|
|
57
|
+
let annotation_category parent.layout_annotation_category
|
|
58
|
+
let annotation_policy if annotation_category == "variable" then overrides.variable else if annotation_category == "property" then overrides.property else if annotation_category == "parameter" then overrides.parameter else if annotation_category == "returnType" then overrides.returnType else overrides.colon
|
|
59
|
+
when annotation_policy != "ignore"
|
|
60
|
+
let annotation_colon_before overrides.colon.before.nonempty ?? before
|
|
61
|
+
let annotation_colon_after overrides.colon.after.nonempty ?? after
|
|
62
|
+
let annotation_optional layout_annotation_optional && overrides.questionMark != "ignore"
|
|
63
|
+
let annotation_label if !annotation_optional then ":" else if starts_with(parent.optional, "+") then "+?:" else if starts_with(parent.optional, "-") then "-?:" else "?:"
|
|
64
|
+
let annotation_before if annotation_optional then overrides.questionMark.before.nonempty ?? annotation_colon_before else annotation_policy.before.nonempty ?? annotation_colon_before
|
|
65
|
+
let annotation_after annotation_policy.after.nonempty ?? annotation_colon_after
|
|
66
|
+
}
|
|
67
|
+
@macro layout_annotation_context {
|
|
68
|
+
when parent is layout_annotation_owner
|
|
69
|
+
when parent.layout_annotation_start != null && offset < parent.layout_annotation_start
|
|
70
|
+
}
|
|
71
|
+
in ts_colon rule es-type-annotation-spacing:before {
|
|
72
|
+
@layout_annotation_options
|
|
73
|
+
@layout_annotation_context
|
|
74
|
+
when len(overrides) > 0 || layout_annotation_optional || before || token(node, "before").end != offset
|
|
75
|
+
@layout_annotation_policy
|
|
76
|
+
let annotation_gap if annotation_optional then ts_token_space(source, parent.optional.node, true) else space_before
|
|
77
|
+
when annotation_gap != null && annotation_gap.spaced != annotation_before
|
|
78
|
+
report at if annotation_optional then parent.optional.offset else offset
|
|
79
|
+
warn if annotation_before then "Expected a space before the '{annotation_label}'." else "Unexpected space before the '{annotation_label}'."
|
|
80
|
+
fix at { start: annotation_gap.start, end: if annotation_before then annotation_gap.start else annotation_gap.end } if annotation_before || !annotation_gap.comments as if annotation_before then " " else ""
|
|
81
|
+
}
|
|
82
|
+
in ts_colon rule es-type-annotation-spacing:after {
|
|
83
|
+
@layout_annotation_options
|
|
84
|
+
@layout_annotation_context
|
|
85
|
+
let annotation_edge token(node, "after")
|
|
86
|
+
let annotation_gap_text if annotation_edge == null then null else byte_slice(source, end_offset, annotation_edge.start)
|
|
87
|
+
when len(overrides) > 0 || (if annotation_gap_text == "" then false else if annotation_gap_text == " " then true else space_after.spaced) != after
|
|
88
|
+
@layout_annotation_policy
|
|
89
|
+
when space_after.spaced != annotation_after
|
|
90
|
+
warn if annotation_after then "Expected a space after the '{annotation_label}'." else "Unexpected space after the '{annotation_label}'."
|
|
91
|
+
fix at { start: end_offset, end: if annotation_after then end_offset else space_after.end } if annotation_after || !space_after.comments as if annotation_after then " " else ""
|
|
92
|
+
}
|
|
93
|
+
in ts_colon rule es-type-annotation-spacing:between {
|
|
94
|
+
@layout_annotation_context
|
|
95
|
+
when layout_annotation_optional
|
|
96
|
+
@layout_annotation_options
|
|
97
|
+
when (if token(node, "before").end == offset then false else space_before.spaced) != (overrides.questionMark.after.nonempty ?? false)
|
|
98
|
+
@layout_annotation_policy
|
|
99
|
+
when annotation_optional
|
|
100
|
+
let annotation_inner overrides.questionMark.after.nonempty ?? false
|
|
101
|
+
when space_before.spaced != annotation_inner
|
|
102
|
+
warn if annotation_inner then "Expected a space between the '?' and the ':'." else "Unexpected space between the '?' and the ':'."
|
|
103
|
+
fix at { start: space_before.start, end: if annotation_inner then space_before.start else offset } if annotation_inner || !space_before.comments as if annotation_inner then " " else ""
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
group layout_generic_owner ts_function, ts_arrow, ts_class, ts_interface, ts_type_alias, ts_method,
|
|
107
|
+
ts_call, ts_new, ts_tagged_template, ts_type_reference, ts_function_type, ts_method_signature, ts_call_signature
|
|
108
|
+
in layout_generic_owner let layout_generic_parameter_tokens type_parameter.token_neighbours
|
|
109
|
+
in layout_generic_owner let layout_generic_argument_tokens type_argument.token_neighbours
|
|
110
|
+
in layout_generic_owner let layout_generic_parameters { open: first(layout_generic_parameter_tokens).before.start, close: last(layout_generic_parameter_tokens).after.start, closed: last(layout_generic_parameter_tokens).after.text == ">" }
|
|
111
|
+
in layout_generic_owner let layout_generic_arguments { open: first(layout_generic_argument_tokens).before.start, close: last(layout_generic_argument_tokens).after.start, closed: last(layout_generic_argument_tokens).after.text == ">" }
|
|
112
|
+
in ts_open_angle, ts_close_angle let layout_generic_edge parent is layout_generic_owner && (parent.layout_generic_parameters.closed && offset == (if is ts_open_angle then parent.layout_generic_parameters.open else parent.layout_generic_parameters.close) || parent.layout_generic_arguments.closed && offset == (if is ts_open_angle then parent.layout_generic_arguments.open else parent.layout_generic_arguments.close))
|
|
113
|
+
@macro layout_generic_policy {
|
|
114
|
+
option before boolean = false "space before generic brackets"
|
|
115
|
+
option after boolean = false "space after generic brackets"
|
|
116
|
+
when layout_generic_edge
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
in ts_open_angle rule es-type-generic-spacing {
|
|
120
|
+
@layout_generic_policy
|
|
121
|
+
when parent not (ts_arrow, ts_function_type, ts_call_signature)
|
|
122
|
+
when parent not (ts_function, ts_class) || parent.name != null
|
|
123
|
+
when previous_token != null
|
|
124
|
+
let generic_space layout_comma_space(space_before.text, true)
|
|
125
|
+
when before != (generic_space != "")
|
|
126
|
+
report at { start: offset - byte_len(generic_space), end: offset }
|
|
127
|
+
fix at { start: offset - byte_len(generic_space), end: offset } as if before then " " else ""
|
|
128
|
+
warn "Generic spaces mismatch"
|
|
129
|
+
}
|
|
130
|
+
in ts_close_angle rule es-type-generic-spacing:after {
|
|
131
|
+
@layout_generic_policy
|
|
132
|
+
when parent is (ts_call, ts_new, ts_tagged_template, ts_call_signature, ts_method_signature, ts_function_type)
|
|
133
|
+
when next_token != null
|
|
134
|
+
let generic_space layout_comma_space(space_after.text, false)
|
|
135
|
+
when after != (generic_space != "")
|
|
136
|
+
report at { start: end_offset, end: end_offset + byte_len(generic_space) }
|
|
137
|
+
fix at { start: end_offset, end: end_offset + byte_len(generic_space) } as if after then " " else ""
|
|
138
|
+
warn "Generic spaces mismatch"
|
|
139
|
+
}
|
|
140
|
+
in ts_open_angle rule es-type-generic-spacing:inner-open {
|
|
141
|
+
@layout_generic_policy
|
|
142
|
+
when re_test(space_after.text, /\s/) && !re_test(space_after.text, /^[\n\r]/)
|
|
143
|
+
report at { start: end_offset, end: space_after.end }
|
|
144
|
+
fix at { start: end_offset, end: space_after.end } if !space_after.comments as ""
|
|
145
|
+
warn "Generic spaces mismatch"
|
|
146
|
+
}
|
|
147
|
+
in ts_close_angle rule es-type-generic-spacing:inner-close {
|
|
148
|
+
@layout_generic_policy
|
|
149
|
+
when re_test(space_before.text, /\s/) && !re_test(space_before.text, /^[\n\r]/)
|
|
150
|
+
report at { start: space_before.start, end: offset }
|
|
151
|
+
fix at { start: space_before.start, end: offset } if !space_before.comments as ""
|
|
152
|
+
warn "Generic spaces mismatch"
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
# Named tuple members use `name?: Type`.
|
|
156
|
+
in ts_tuple_member let layout_tuple_prefix !name.space_after.comments && (optional == null || !ts_token_space(source, optional.node, false).comments)
|
|
157
|
+
in ts_colon rule es-type-named-tuple-spacing:optional {
|
|
158
|
+
when parent is ts_tuple_member && parent.optional != null
|
|
159
|
+
when parent.layout_tuple_prefix
|
|
160
|
+
when space_before.spaced
|
|
161
|
+
report at parent.offset
|
|
162
|
+
fix space_before as ""
|
|
163
|
+
warn "Unexpected space between '?' and the ':'."
|
|
164
|
+
}
|
|
165
|
+
in ts_tuple_member.name rule es-type-named-tuple-spacing:before {
|
|
166
|
+
@ts_token_context
|
|
167
|
+
when parent.layout_tuple_prefix
|
|
168
|
+
when space_after.spaced
|
|
169
|
+
report at parent.offset
|
|
170
|
+
fix space_after as ""
|
|
171
|
+
warn "Unexpected space before the ':'."
|
|
172
|
+
}
|
|
173
|
+
in ts_colon rule es-type-named-tuple-spacing:after {
|
|
174
|
+
when parent is ts_tuple_member
|
|
175
|
+
when parent.layout_tuple_prefix
|
|
176
|
+
when { value space_after.text; re_find /^\s*/; len } != 1
|
|
177
|
+
report at parent.offset
|
|
178
|
+
fix space_after as " "
|
|
179
|
+
warn "Expected a space after the ':'."
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
}
|
package/std/ts/layout.dx
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# .ts/layout: spacing, punctuation and line-break rule definitions, all off.
|
|
2
|
+
|
|
3
|
+
import .ts/layout/blocks
|
|
4
|
+
import .ts/layout/calls
|
|
5
|
+
import .ts/layout/expressions
|
|
6
|
+
import .ts/layout/separators
|
|
7
|
+
import .ts/layout/types
|
|
8
|
+
import .ts/layout/literals
|
|
9
|
+
import .ts/layout/text
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# .ts/license: license headers, modelled on eslint-plugin-license-header's
|
|
2
|
+
# `header`. The first comment before any code must be the header, alone on
|
|
3
|
+
# line 1, followed by exactly one blank line. Turn each `header:*` part on
|
|
4
|
+
# with the header text, with `\n` line breaks:
|
|
5
|
+
#
|
|
6
|
+
# rule es-header:* { set license "/*!\n * Copyright ...\n */" }
|
|
7
|
+
|
|
8
|
+
define {
|
|
9
|
+
|
|
10
|
+
@macro license_text ({ value to: @newline; value replace(license, "\n", to) })
|
|
11
|
+
@macro license_wanted license != "" && (skip_files == "" || !re_test(file, skip_files))
|
|
12
|
+
fn license_like(comment) = re_test(comment, /(?i)(?:Copyright|@license|SPDX-License-Identifier)\b/)
|
|
13
|
+
|
|
14
|
+
in ts_program let license_prefix re_find(text, /\A\s*/)
|
|
15
|
+
in ts_program let license_first_comment { value overlapping(ts_comment); first }
|
|
16
|
+
in ts_program let license_header if license_first_comment != null && license_first_comment.offset == byte_len(license_prefix) then license_first_comment else null
|
|
17
|
+
in ts_program let license_suffix_space if license_header == null then "" else { value text; byte_slice license_header.end_offset; re_find /\A\s*/ }
|
|
18
|
+
|
|
19
|
+
in ts_program rule es-header:missing warn "Missing license header" {
|
|
20
|
+
option license string = "" "the license header"
|
|
21
|
+
option skip_files regex = "" "file paths without a header"
|
|
22
|
+
report change
|
|
23
|
+
when @license_wanted && byte_len(license_prefix) < byte_len(text)
|
|
24
|
+
when license_header == null || !license_like(license_header.text)
|
|
25
|
+
fix as license_prefix @license_text @newline @newline { value license_prefix; byte_len: byte_len; value byte_slice(text, byte_len) }
|
|
26
|
+
}
|
|
27
|
+
in ts_program rule es-header:invalid warn "Invalid license header" {
|
|
28
|
+
option license string = "" "the license header"
|
|
29
|
+
option skip_files regex = "" "file paths without a header"
|
|
30
|
+
when @license_wanted && license_header != null
|
|
31
|
+
when license_like(license_header.text) && license_header.text != @license_text
|
|
32
|
+
report at { start: license_header.offset, end: license_header.end_offset }
|
|
33
|
+
fix as byte_slice(text, 0, license_header.offset) @license_text byte_slice(text, license_header.end_offset)
|
|
34
|
+
}
|
|
35
|
+
in ts_program rule es-header:before warn "Superfluous new lines before license header" {
|
|
36
|
+
option license string = "" "the license header"
|
|
37
|
+
option skip_files regex = "" "file paths without a header"
|
|
38
|
+
when @license_wanted && license_header != null && contains(license_prefix, "\n")
|
|
39
|
+
when license_header.text == @license_text
|
|
40
|
+
report at license_header.offset
|
|
41
|
+
fix as byte_slice(text, license_header.offset)
|
|
42
|
+
}
|
|
43
|
+
in ts_program rule es-header:after warn "{if re_count(license_suffix_space, /\n/) > 2 then 'Superfluous new lines' else 'Missing new line'} after license header" {
|
|
44
|
+
option license string = "" "the license header"
|
|
45
|
+
option skip_files regex = "" "file paths without a header"
|
|
46
|
+
when @license_wanted && license_header != null
|
|
47
|
+
when license_header.text == @license_text
|
|
48
|
+
let suffix_end license_header.end_offset + byte_len(license_suffix_space)
|
|
49
|
+
when re_count(license_suffix_space, /\n/) != 2 && suffix_end < end_offset
|
|
50
|
+
report at { start: license_header.end_offset, end: suffix_end }
|
|
51
|
+
fix as byte_slice(text, 0, license_header.end_offset) @newline @newline byte_slice(text, suffix_end)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import .ts/names
|
|
2
|
+
import .ts/import-attributes
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn ts_literal_quoted(text) = {
|
|
7
|
+
value decoded: js_string_parts(text).text
|
|
8
|
+
value if type_of(decoded) == "str" then { type: "string", value: decoded } else null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fn ts_literal_unary(n) = {
|
|
12
|
+
value operand: n.argument.literal_value
|
|
13
|
+
value operator: n.operator.text
|
|
14
|
+
value when {
|
|
15
|
+
!contains(["+", "-"], operator) || operand == null then null
|
|
16
|
+
operand.type == "bigint" then
|
|
17
|
+
if operator != "-" then null else {
|
|
18
|
+
type: "bigint",
|
|
19
|
+
value: when {
|
|
20
|
+
operand.value == "0" then "0"
|
|
21
|
+
starts_with(operand.value, "-") then slice(operand.value, 1)
|
|
22
|
+
else then "-" operand.value
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
!contains(["number", "string"], operand.type) then null
|
|
26
|
+
else then {
|
|
27
|
+
value number: js_number_unary(operator, js_number(operand))
|
|
28
|
+
value { type: "number", value: if number == 0 && js_number_binary("/", 1, number) < 0 then "-0" else js_to_string(number) }
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
in ts_node let literal_value null
|
|
34
|
+
in ts_string, ts_import_attribute_key, ts_import_attribute_string let literal_value ts_literal_quoted(text)
|
|
35
|
+
in ts_template let literal_value if count(expression) == 0 then ts_literal_quoted(text) else null
|
|
36
|
+
in ts_number let literal_value {
|
|
37
|
+
value number: js_number({ type: "number", value: text })
|
|
38
|
+
value if number == null then null else { type: "number", value: js_to_string(number) }
|
|
39
|
+
}
|
|
40
|
+
in ts_bigint let literal_value {
|
|
41
|
+
value number: js_bigint(text)
|
|
42
|
+
value if number == null then null else { type: "bigint", value: number }
|
|
43
|
+
}
|
|
44
|
+
in ts_boolean let literal_value { type: "boolean", value: text }
|
|
45
|
+
in ts_null let literal_value { type: "null", value: "null" }
|
|
46
|
+
in ts_regex let literal_value { type: "regex", value: text }
|
|
47
|
+
in ts_parenthesized let literal_value expression.literal_value
|
|
48
|
+
in ts_unary let literal_value ts_literal_unary(node)
|
|
49
|
+
|
|
50
|
+
in ts_node let ts_literal_text null
|
|
51
|
+
in ts_string, ts_template, ts_number, ts_bigint, ts_boolean, ts_null, ts_regex let ts_literal_text literal_value.value
|
|
52
|
+
in ts_parenthesized let ts_literal_text expression.ts_literal_text
|
|
53
|
+
|
|
54
|
+
fn ts_literal_key(n) = when {
|
|
55
|
+
n is ts_identifier then n.identifier_name
|
|
56
|
+
n is ts_computed_key then n.expression.ts_literal_text
|
|
57
|
+
else then n.ts_literal_text
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
in ts_node let static_name ts_literal_text
|
|
61
|
+
in ts_property, ts_pattern_property, ts_method, ts_class_property, ts_property_signature, ts_method_signature let static_name ts_literal_key(key.node)
|
|
62
|
+
in ts_enum_member let static_name ts_literal_key(name.node)
|
|
63
|
+
in ts_member let static_name ts_literal_key(property.node)
|
|
64
|
+
in ts_index let static_name index.ts_literal_text
|
|
65
|
+
in ts_parenthesized let static_name expression.static_name
|
|
66
|
+
|
|
67
|
+
fn ts_reference_atom(tag, text) = "{tag}{len(text)}:{text}"
|
|
68
|
+
fn ts_reference_read(n, strict) = if strict then n.reference_key_strict else n.reference_key
|
|
69
|
+
fn ts_reference_compute(n, strict) = when {
|
|
70
|
+
n is ts_parenthesized then ts_reference_read(n.expression.node, strict)
|
|
71
|
+
n is ts_identifier then ts_reference_atom("id", n.identifier_name)
|
|
72
|
+
n is ts_private_name then ts_reference_atom("private", n.text)
|
|
73
|
+
n is ts_this then "this"
|
|
74
|
+
n is ts_super then "super"
|
|
75
|
+
n is (ts_string, ts_number, ts_boolean, ts_null, ts_bigint, ts_regex)
|
|
76
|
+
|| n is ts_template && count(n.expression) == 0 then {
|
|
77
|
+
value literal: n.literal_value
|
|
78
|
+
value if literal == null then null else ts_reference_atom("literal:" literal.type, literal.value)
|
|
79
|
+
}
|
|
80
|
+
n is (ts_member, ts_index) then {
|
|
81
|
+
value object: ts_reference_read(n.object.node, strict)
|
|
82
|
+
value name: if strict then null else n.static_name
|
|
83
|
+
value property: if name != null then ts_reference_atom("static", name) else {
|
|
84
|
+
value key: ts_reference_read(n.property.node ?? n.index.node, strict)
|
|
85
|
+
value if key == null then null else ts_reference_atom(if n is ts_member then "member" else "index", key)
|
|
86
|
+
}
|
|
87
|
+
value if object == null || property == null then null
|
|
88
|
+
else ts_reference_atom("object", object) ts_reference_atom("property", property)
|
|
89
|
+
}
|
|
90
|
+
else then null
|
|
91
|
+
}
|
|
92
|
+
in ts_node let reference_key ts_reference_compute(node, false)
|
|
93
|
+
in ts_node let reference_key_strict ts_reference_compute(node, true)
|
|
94
|
+
|
|
95
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# String and collection method policies.
|
|
2
|
+
import .ts/constants
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn collection_value_node(n) = {
|
|
7
|
+
value n
|
|
8
|
+
walk (current) if current is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion) then current.expression.node
|
|
9
|
+
else if current is ts_identifier then ts_runtime_binding_init(current)
|
|
10
|
+
else null
|
|
11
|
+
last
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
fn collection_value(n) =
|
|
15
|
+
if n is ts_identifier && n.resolved == "global" && contains(["readonly", "writable"], n.global) && contains(["NaN", "undefined", "Infinity"], n.text) then {type: if n.text == "undefined" then "undefined" else "number", value: n.text}
|
|
16
|
+
else ts_runtime_constant(n)
|
|
17
|
+
|
|
18
|
+
fn collection_static_value(n) = { value n; collection_value_node; collection_value }
|
|
19
|
+
fn collection_method(n, name) = n is ts_member && n.property == name || n is ts_index && collection_static_value(n.index.node).type == "string" && collection_static_value(n.index.node).value == name
|
|
20
|
+
|
|
21
|
+
fn collection_arrayish(n) = {
|
|
22
|
+
value arms: value_parts(n)
|
|
23
|
+
value parts: map(arms ?? [], arm => filter(arm.parts, part => !contains(part.traits, "parameter")
|
|
24
|
+
&& !(contains(part.traits, "intersection") && count(part.traits) == 1
|
|
25
|
+
&& part.carrier_known && part.carrier == null)))
|
|
26
|
+
value nonnull: filter(parts, arm => !(count(arm) > 0 && all(arm, part =>
|
|
27
|
+
contains(part.traits, "null") || contains(part.traits, "undefined"))))
|
|
28
|
+
value arms != null && count(nonnull) > 0 && all(nonnull, arm => count(arm) > 0
|
|
29
|
+
&& all(arm, part => {
|
|
30
|
+
value shape: ts_value_array_part(part, n)
|
|
31
|
+
value shape.known && (shape.array || shape.tuple)
|
|
32
|
+
}))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn collection_filter(n) = n != null && {
|
|
36
|
+
value n
|
|
37
|
+
walk (item) if item is ts_parenthesized then item.expression.node
|
|
38
|
+
else if item is ts_sequence then last(item.expression.nodes)
|
|
39
|
+
else if item is ts_conditional then [item.consequent.node, item.alternate.node]
|
|
40
|
+
else null
|
|
41
|
+
all (item) if item is ts_parenthesized then item.expression.node != null
|
|
42
|
+
else if item is ts_sequence then count(item.expression.nodes) > 0
|
|
43
|
+
else if item is ts_conditional then item.consequent.node != null && item.alternate.node != null
|
|
44
|
+
else item is ts_call && item.optional == null
|
|
45
|
+
&& { value item.callee.node; unparenthesised_node; collection_method "filter" }
|
|
46
|
+
&& collection_arrayish(unparenthesised_node(item.callee.node).object.node)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fn collection_index_zero(v) = v != null && (contains(["string", "bigint"], v.type) && v.value == "0" || v.type == "number" && js_compare(v, 0) == 0)
|
|
50
|
+
fn collection_at_number_zero(n) = n != null && (js_compare(n, n) == null || n > -1 && n < 1)
|
|
51
|
+
fn collection_at_zero(v) = v != null && { value v; js_number; collection_at_number_zero }
|
|
52
|
+
|
|
53
|
+
in ts_index rule es-prefer-find {
|
|
54
|
+
when optional == null && { value index.node; collection_static_value; collection_index_zero } && collection_filter(object.node)
|
|
55
|
+
warn "Prefer .find(...) instead of .filter(...)[0]."
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
in ts_call rule es-prefer-find:at {
|
|
59
|
+
let method unparenthesised_node(callee.node)
|
|
60
|
+
when count(argument) == 1 && method.optional == null && collection_method(method, "at") && {
|
|
61
|
+
value argument.nodes
|
|
62
|
+
first
|
|
63
|
+
collection_static_value
|
|
64
|
+
collection_at_zero
|
|
65
|
+
} && collection_filter(method.object.node)
|
|
66
|
+
warn "Prefer .find(...) instead of .filter(...)[0]."
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|