@inthedark/dxlint 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -20
- package/bin/dxlint +15 -15
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +15 -4
- package/rules/bugs.dx +49 -0
- package/rules/es-no-semi.dx +1 -1
- package/rules/{format.dx → formatting.dx} +7 -9
- package/rules/recommended.dx +5 -50
- package/rules/{conventions.dx → style.dx} +17 -3
- package/rules/suspicious.dx +25 -4
- package/rules/type-safety.dx +18 -0
- package/std/ts/expressions.dx +270 -270
- package/std/ts/indentation.dx +442 -442
- package/std/ts/layout/separators.dx +161 -161
- package/std/ts/restrictions.dx +586 -586
- package/std/ts/style/comments.dx +13 -5
- package/std/ts/typed/floating-promises.dx +147 -147
- package/std/ts/typed/optional-chains.dx +317 -317
- package/std/ts/typed/promises.dx +205 -205
- package/std/ts/typed.dx +585 -585
- package/std/ts/values/enums.dx +104 -104
- package/std/ts/values.dx +1217 -1217
- package/rules/correctness.dx +0 -33
- package/rules/types.dx +0 -21
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
# .ts/layout/separators: separators layout rules.
|
|
2
|
-
|
|
3
|
-
import .ts/layout/shared
|
|
4
|
-
import .ts/import-attributes
|
|
5
|
-
|
|
6
|
-
define {
|
|
7
|
-
|
|
8
|
-
in ts_function, ts_method, ts_arrow, ts_function_type, ts_method_signature, ts_call_signature, ts_class, ts_interface, ts_type_alias let layout_last_type_parameter_end last(type_parameter.end_offset)
|
|
9
|
-
|
|
10
|
-
group comma_style_lists ts_variable_declaration, ts_array, ts_array_pattern, ts_object, ts_object_pattern,
|
|
11
|
-
ts_function, ts_method, ts_arrow, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export,
|
|
12
|
-
ts_sequence, ts_enum, ts_tuple_type, ts_type_literal, ts_method_signature, ts_call_signature,
|
|
13
|
-
ts_index_signature, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_tagged_template, ts_import_attributes
|
|
1
|
+
# .ts/layout/separators: separators layout rules.
|
|
2
|
+
|
|
3
|
+
import .ts/layout/shared
|
|
4
|
+
import .ts/import-attributes
|
|
5
|
+
|
|
6
|
+
define {
|
|
7
|
+
|
|
8
|
+
in ts_function, ts_method, ts_arrow, ts_function_type, ts_method_signature, ts_call_signature, ts_class, ts_interface, ts_type_alias let layout_last_type_parameter_end last(type_parameter.end_offset)
|
|
9
|
+
|
|
10
|
+
group comma_style_lists ts_variable_declaration, ts_array, ts_array_pattern, ts_object, ts_object_pattern,
|
|
11
|
+
ts_function, ts_method, ts_arrow, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export,
|
|
12
|
+
ts_sequence, ts_enum, ts_tuple_type, ts_type_literal, ts_method_signature, ts_call_signature,
|
|
13
|
+
ts_index_signature, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_tagged_template, ts_import_attributes
|
|
14
14
|
in ts_comma {
|
|
15
15
|
let comma_style_context layout_comma_context(node)
|
|
16
16
|
let comma_style_owner comma_style_context.owner
|
|
17
17
|
let comma_style_field comma_style_context.field
|
|
18
18
|
let comma_style_previous comma_style_context.previous
|
|
19
|
-
}
|
|
20
|
-
in ts_import_attribute_comma {
|
|
21
|
-
let comma_style_owner parent
|
|
22
|
-
let comma_style_field "attribute"
|
|
23
|
-
}
|
|
24
|
-
in ts_comma, ts_import_attribute_comma let comma_style_checked comma_style_owner is comma_style_lists
|
|
19
|
+
}
|
|
20
|
+
in ts_import_attribute_comma {
|
|
21
|
+
let comma_style_owner parent
|
|
22
|
+
let comma_style_field "attribute"
|
|
23
|
+
}
|
|
24
|
+
in ts_comma, ts_import_attribute_comma let comma_style_checked comma_style_owner is comma_style_lists
|
|
25
25
|
in ts_comma, ts_import_attribute_comma rule es-comma-style {
|
|
26
26
|
option style "last" | "first" = "last" "comma line position"
|
|
27
27
|
when if style == "last" then space_before.line_break else space_after.line_break
|
|
28
28
|
when comma_style_checked
|
|
29
29
|
when comma_style_owner not (ts_array, ts_array_pattern) || comma_style_previous != null
|
|
30
|
-
when !(token(node, "after").text == "," && space_after.line_break)
|
|
31
|
-
let comma_comments overlapping(ts_comment, token(node, "before").end, token(node, "after").start)
|
|
32
|
-
let comma_after_comment { value comma_comments; filter (comment) comment.offset >= end_offset; first }
|
|
33
|
-
let comma_placement if space_before.line_break && space_after.line_break && !(comma_after_comment is ts_block_comment && position(comma_after_comment.offset).js_line == position(offset).js_line) then "between" else style
|
|
30
|
+
when !(token(node, "after").text == "," && space_after.line_break)
|
|
31
|
+
let comma_comments overlapping(ts_comment, token(node, "before").end, token(node, "after").start)
|
|
32
|
+
let comma_after_comment { value comma_comments; filter (comment) comment.offset >= end_offset; first }
|
|
33
|
+
let comma_placement if space_before.line_break && space_after.line_break && !(comma_after_comment is ts_block_comment && position(comma_after_comment.offset).js_line == position(offset).js_line) then "between" else style
|
|
34
34
|
let comma_gap "{space_before.text}{space_after.text}"
|
|
35
35
|
let comma_fixed_gap if comma_placement == "between" then re_replace(comma_gap, /^([^\r\n\u{2028}\u{2029}]*)(\r\n|[\r\n\u{2028}\u{2029}])/, "$1") else comma_gap
|
|
36
36
|
let comma_between_safe all(comma_comments, c => position(c.offset).js_line == position(c.end_offset).js_line) && (all(comma_comments, c => c is ts_block_comment) || re_test(comma_fixed_gap, /[\r\n\u{2028}\u{2029}][ \t]*$/))
|
|
37
37
|
fix at {start: space_before.start, end: space_after.end} if comma_placement != "between" || comma_between_safe as "{if comma_placement == 'first' then '' else ','}{comma_fixed_gap}{if comma_placement == 'first' then ',' else ''}"
|
|
38
|
-
warn if space_before.line_break && space_after.line_break then "Bad line breaking before and after ','." else if style == "last" then "',' should be placed last." else "',' should be placed first."
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
schema trailing_comma_mode "always-multiline" | "always" | "never" | "only-multiline" | "ignore"
|
|
42
|
-
in ts_import_attributes let layout_attribute_last { value descendants(ts_import_attribute); last }
|
|
43
|
-
in ts_import_attributes let layout_attribute_comma { value descendants(ts_import_attribute_comma); filter (c) c.offset >= layout_attribute_last.end_offset; first }
|
|
44
|
-
in ts_import_attributes let layout_attribute_comma_sites if layout_attribute_last == null then [] else [{
|
|
45
|
-
kind: "importAttributes",
|
|
46
|
-
offset: layout_attribute_comma.offset.nonempty ?? layout_attribute_last.end_offset,
|
|
47
|
-
trailing: layout_attribute_comma != null,
|
|
48
|
-
multiline: { value layout_attribute_comma.end_offset.nonempty ?? layout_attribute_last.end_offset; position }.js_line != position(close.end_offset).js_line,
|
|
49
|
-
allowed: true,
|
|
50
|
-
tsx_single: false
|
|
51
|
-
}]
|
|
52
|
-
fn layout_attribute_sites(sites, style, policies) = {
|
|
53
|
-
value mode: { value policies; get "importAttributes"; nonempty } ?? style
|
|
54
|
-
value if mode != "ignore" then map(sites, site => {
|
|
55
|
-
mode: mode, offset: site.offset, trailing: site.trailing,
|
|
56
|
-
multiline: site.multiline, allowed: site.allowed, tsx_single: site.tsx_single
|
|
57
|
-
}) else []
|
|
58
|
-
}
|
|
59
|
-
in ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arrow, ts_method, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export, ts_enum, ts_tuple_type, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_method_signature, ts_call_signature, ts_import_attributes rule es-comma-dangle {
|
|
60
|
-
option style trailing_comma_mode = "never" "trailing comma policy"
|
|
61
|
-
option arrays trailing_comma_mode | null = null "array policy"
|
|
62
|
-
option objects trailing_comma_mode | null = null "object policy"
|
|
63
|
-
option functions trailing_comma_mode | null = null "parameter and argument policy"
|
|
64
|
-
option imports trailing_comma_mode | null = null "named import policy"
|
|
65
|
-
option exports trailing_comma_mode | null = null "named export policy"
|
|
66
|
-
option importAttributes trailing_comma_mode | null = null "import attribute policy"
|
|
67
|
-
option dynamicImports trailing_comma_mode | null = null "dynamic import policy"
|
|
68
|
-
option enums trailing_comma_mode | null = null "enum policy"
|
|
69
|
-
option generics trailing_comma_mode | null = null "type parameter policy"
|
|
70
|
-
option tuples trailing_comma_mode | null = null "tuple policy"
|
|
71
|
-
let dangle_policies {arrays:arrays, objects:objects, functions:functions, imports:imports, exports:exports, importAttributes:importAttributes, dynamicImports:dynamicImports, enums:enums, generics:generics, tuples:tuples, "type-arguments":"never"}
|
|
38
|
+
warn if space_before.line_break && space_after.line_break then "Bad line breaking before and after ','." else if style == "last" then "',' should be placed last." else "',' should be placed first."
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
schema trailing_comma_mode "always-multiline" | "always" | "never" | "only-multiline" | "ignore"
|
|
42
|
+
in ts_import_attributes let layout_attribute_last { value descendants(ts_import_attribute); last }
|
|
43
|
+
in ts_import_attributes let layout_attribute_comma { value descendants(ts_import_attribute_comma); filter (c) c.offset >= layout_attribute_last.end_offset; first }
|
|
44
|
+
in ts_import_attributes let layout_attribute_comma_sites if layout_attribute_last == null then [] else [{
|
|
45
|
+
kind: "importAttributes",
|
|
46
|
+
offset: layout_attribute_comma.offset.nonempty ?? layout_attribute_last.end_offset,
|
|
47
|
+
trailing: layout_attribute_comma != null,
|
|
48
|
+
multiline: { value layout_attribute_comma.end_offset.nonempty ?? layout_attribute_last.end_offset; position }.js_line != position(close.end_offset).js_line,
|
|
49
|
+
allowed: true,
|
|
50
|
+
tsx_single: false
|
|
51
|
+
}]
|
|
52
|
+
fn layout_attribute_sites(sites, style, policies) = {
|
|
53
|
+
value mode: { value policies; get "importAttributes"; nonempty } ?? style
|
|
54
|
+
value if mode != "ignore" then map(sites, site => {
|
|
55
|
+
mode: mode, offset: site.offset, trailing: site.trailing,
|
|
56
|
+
multiline: site.multiline, allowed: site.allowed, tsx_single: site.tsx_single
|
|
57
|
+
}) else []
|
|
58
|
+
}
|
|
59
|
+
in ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arrow, ts_method, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export, ts_enum, ts_tuple_type, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_method_signature, ts_call_signature, ts_import_attributes rule es-comma-dangle {
|
|
60
|
+
option style trailing_comma_mode = "never" "trailing comma policy"
|
|
61
|
+
option arrays trailing_comma_mode | null = null "array policy"
|
|
62
|
+
option objects trailing_comma_mode | null = null "object policy"
|
|
63
|
+
option functions trailing_comma_mode | null = null "parameter and argument policy"
|
|
64
|
+
option imports trailing_comma_mode | null = null "named import policy"
|
|
65
|
+
option exports trailing_comma_mode | null = null "named export policy"
|
|
66
|
+
option importAttributes trailing_comma_mode | null = null "import attribute policy"
|
|
67
|
+
option dynamicImports trailing_comma_mode | null = null "dynamic import policy"
|
|
68
|
+
option enums trailing_comma_mode | null = null "enum policy"
|
|
69
|
+
option generics trailing_comma_mode | null = null "type parameter policy"
|
|
70
|
+
option tuples trailing_comma_mode | null = null "tuple policy"
|
|
71
|
+
let dangle_policies {arrays:arrays, objects:objects, functions:functions, imports:imports, exports:exports, importAttributes:importAttributes, dynamicImports:dynamicImports, enums:enums, generics:generics, tuples:tuples, "type-arguments":"never"}
|
|
72
72
|
let dangle_tsx ends_with(file, ".tsx")
|
|
73
73
|
each dangle_site in if is ts_import_attributes then layout_attribute_sites(layout_attribute_comma_sites, style, dangle_policies) else layout_dangle_lists(node, style, dangle_policies, ecma_version, dangle_tsx) {
|
|
74
74
|
when layout_dangle_wrong(dangle_site, dangle_site.mode)
|
|
@@ -76,109 +76,109 @@ in ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arr
|
|
|
76
76
|
fix at {start:dangle_site.offset, end:dangle_site.offset + (if dangle_site.trailing then 1 else 0)} as if dangle_site.trailing then "" else ","
|
|
77
77
|
warn if dangle_site.trailing then "Unexpected trailing comma." else "Missing trailing comma."
|
|
78
78
|
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
in ts_empty, ts_semicolon rule es-no-extra-semi {
|
|
82
|
-
when is ts_empty && parent not (ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_if, ts_labeled, ts_with)
|
|
83
|
-
|| is ts_semicolon && parent is ts_class
|
|
84
|
-
let semi_directive next is ts_expression_statement && next.expression is ts_string
|
|
85
|
-
&& (parent is ts_program || parent is ts_block && parent.parent is (ts_function, ts_arrow, ts_method))
|
|
86
|
-
let semi_fix_start token_neighbours.before.start.nonempty ?? offset
|
|
87
|
-
let semi_fix_end token_neighbours.after.end.nonempty ?? end_offset
|
|
88
|
-
fix at { start: semi_fix_start, end: semi_fix_end } if !semi_directive as byte_slice(source, semi_fix_start, offset) byte_slice(source, end_offset, semi_fix_end)
|
|
89
|
-
warn "Unnecessary semicolon."
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
group layout_semi_owner ts_break, ts_continue, ts_debugger, ts_do_while, ts_export, ts_export_assignment, ts_expression_statement, ts_import, ts_return, ts_throw, ts_variable_declaration, ts_class_property
|
|
93
|
-
in layout_semi_owner let layout_semi_last parent is ts_do_while || parent is ts_if && parent.alternate != null && end_offset == parent.consequent.end_offset || parent is (ts_program, ts_block, ts_static_block, ts_case, ts_class) && next == null
|
|
94
|
-
in ts_variable_declaration let layout_semi_initializer parent is ts_for && offset == parent.init.offset
|
|
95
|
-
in ts_semicolon let layout_semi_header if parent is ts_for then parent.init != null && (parent.test == null && previous_token not ts_semicolon || offset < parent.test.offset) || parent.test != null && offset >= parent.test.end_offset else parent.layout_semi_initializer.nonempty ?? false
|
|
96
|
-
in ts_semicolon let layout_semi_statement parent is layout_semi_owner && end_offset == parent.end_offset
|
|
97
|
-
in ts_semicolon rule es-semi-style {
|
|
98
|
-
option style "last" | "first" = "last" "semicolon line position"
|
|
99
|
-
when layout_semi_header || layout_semi_statement
|
|
100
|
-
let semi_at_end style == "last" || layout_semi_header
|
|
101
|
-
when if semi_at_end then position(offset).js_line != position(token(node, "before").end).js_line
|
|
102
|
-
else !(parent.layout_semi_last.nonempty ?? false) && position(end_offset).js_line != position(token(node, "after").start).js_line
|
|
103
|
-
let semi_fix_start token_neighbours.before.end.nonempty ?? offset
|
|
104
|
-
let semi_fix_end token_neighbours.after.start.nonempty ?? end_offset
|
|
105
|
-
fix at { start: semi_fix_start, end: semi_fix_end } if { value overlapping(ts_comment, semi_fix_start, semi_fix_end); count } == 0 as if semi_at_end then ";\n" else "\n;"
|
|
106
|
-
warn if semi_at_end then "Expected this semicolon to be at the end of the previous line." else "Expected this semicolon to be at the beginning of the next line."
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
@macro layout_semi_spacing_options {
|
|
110
|
-
option before boolean = false "space before semicolon"
|
|
111
|
-
option after boolean = true "space after semicolon"
|
|
112
|
-
when layout_semi_header || layout_semi_statement || parent is (ts_type_alias, ts_function, ts_method) && end_offset == parent.end_offset || parent is (ts_interface, ts_type_literal)
|
|
113
|
-
}
|
|
114
|
-
in ts_semicolon rule es-semi-spacing:before {
|
|
115
|
-
@layout_semi_spacing_options
|
|
116
|
-
let semi_space space_before != null && !space_before.line_break && space_before.spaced
|
|
117
|
-
when semi_space != before
|
|
118
|
-
report at { start: if semi_space then space_before.start else offset, end: if semi_space then offset else end_offset }
|
|
119
|
-
let semi_fix_start if semi_space then space_before.start else offset
|
|
120
|
-
fix at { start: semi_fix_start, end: offset } if !space_before.line_break && { value overlapping(ts_comment, semi_fix_start, offset); count } == 0 as if before then " " else ""
|
|
121
|
-
warn if semi_space then "Unexpected whitespace before semicolon." else "Missing whitespace before semicolon."
|
|
122
|
-
}
|
|
123
|
-
in ts_semicolon rule es-semi-spacing:after {
|
|
124
|
-
@layout_semi_spacing_options
|
|
125
|
-
when previous_token != null && next_token != null
|
|
126
|
-
when !space_before.line_break && !space_after.line_break && next_token not (ts_close_paren, ts_close_brace)
|
|
127
|
-
when space_after.spaced != after
|
|
128
|
-
report at { start: if space_after.spaced then end_offset else offset, end: if space_after.spaced then space_after.end else end_offset }
|
|
129
|
-
let semi_fix_end if space_after.spaced then space_after.end else end_offset
|
|
130
|
-
fix at { start: end_offset, end: semi_fix_end } if { value overlapping(ts_comment, end_offset, semi_fix_end); count } == 0 as if after then " " else ""
|
|
131
|
-
warn if space_after.spaced then "Unexpected whitespace after semicolon." else "Missing whitespace after semicolon."
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
in ts_comma rule es-comma-spacing:before {
|
|
135
|
-
option before boolean = false "space before comma"
|
|
136
|
-
option after boolean = true "space after comma"
|
|
137
|
-
# Cheapest and most selective first: most commas are spaced as wanted.
|
|
138
|
-
let comma_space layout_comma_space(space_before.text, true)
|
|
139
|
-
when before != (comma_space != "")
|
|
140
|
-
when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
|
|
141
|
-
when previous_token != null && previous_token not ts_comma
|
|
142
|
-
when !(parent is (ts_array, ts_array_pattern) && previous_token is ts_open_bracket)
|
|
143
|
-
when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
|
|
144
|
-
fix space_before as if before then " " else ""
|
|
145
|
-
warn if before then "A space is required before ','." else "There should be no space before ','."
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
in ts_comma rule es-comma-spacing:after {
|
|
149
|
-
option before boolean = false "space before comma"
|
|
150
|
-
option after boolean = true "space after comma"
|
|
151
|
-
# Cheapest and most selective first: most commas are spaced as wanted.
|
|
152
|
-
let comma_space layout_comma_space(space_after.text, false)
|
|
153
|
-
when after != (comma_space != "")
|
|
154
|
-
when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
|
|
155
|
-
when next_token != null
|
|
156
|
-
when space_after.comments || next_token not (ts_comma, ts_close_paren, ts_close_bracket, ts_close_brace)
|
|
157
|
-
when !(parent is (ts_array, ts_array_pattern) && previous_token is (ts_open_bracket, ts_comma) && next_token is (ts_comma, ts_close_bracket))
|
|
158
|
-
when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
|
|
159
|
-
when after || !{ value space_after.text; trim_start; starts_with "//" }
|
|
160
|
-
fix space_after as if after then " " else ""
|
|
161
|
-
warn if after then "A space is required after ','." else "There should be no space after ','."
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
group semicolon_statement ts_expression_statement, ts_variable_declaration, ts_return, ts_throw, ts_break, ts_continue, ts_do_while, ts_import, ts_import_equals, ts_type_alias, ts_class_property, ts_debugger, ts_export_assignment, ts_export, ts_method, ts_function
|
|
165
|
-
fn layout_next_position_len(source, offset) = {
|
|
166
|
-
value char: byte_char(source, offset)
|
|
167
|
-
value if char == "\r" && byte_char(source, offset + 1) == "\n" then 2 else byte_len(char)
|
|
168
|
-
}
|
|
169
|
-
in semicolon_statement rule es-semi {
|
|
170
|
-
option mode "always" | "never" = "always" "statement semicolon policy"
|
|
171
|
-
option omitLastInOneLineBlock boolean = false "omit final semicolons in single-line blocks"
|
|
172
|
-
option omitLastInOneLineClassBody boolean = false "omit final semicolons in single-line class bodies"
|
|
173
|
-
option beforeStatementContinuationChars "always" | "any" | "never" = "any" "semicolons before continuation characters"
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
in ts_empty, ts_semicolon rule es-no-extra-semi {
|
|
82
|
+
when is ts_empty && parent not (ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_if, ts_labeled, ts_with)
|
|
83
|
+
|| is ts_semicolon && parent is ts_class
|
|
84
|
+
let semi_directive next is ts_expression_statement && next.expression is ts_string
|
|
85
|
+
&& (parent is ts_program || parent is ts_block && parent.parent is (ts_function, ts_arrow, ts_method))
|
|
86
|
+
let semi_fix_start token_neighbours.before.start.nonempty ?? offset
|
|
87
|
+
let semi_fix_end token_neighbours.after.end.nonempty ?? end_offset
|
|
88
|
+
fix at { start: semi_fix_start, end: semi_fix_end } if !semi_directive as byte_slice(source, semi_fix_start, offset) byte_slice(source, end_offset, semi_fix_end)
|
|
89
|
+
warn "Unnecessary semicolon."
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
group layout_semi_owner ts_break, ts_continue, ts_debugger, ts_do_while, ts_export, ts_export_assignment, ts_expression_statement, ts_import, ts_return, ts_throw, ts_variable_declaration, ts_class_property
|
|
93
|
+
in layout_semi_owner let layout_semi_last parent is ts_do_while || parent is ts_if && parent.alternate != null && end_offset == parent.consequent.end_offset || parent is (ts_program, ts_block, ts_static_block, ts_case, ts_class) && next == null
|
|
94
|
+
in ts_variable_declaration let layout_semi_initializer parent is ts_for && offset == parent.init.offset
|
|
95
|
+
in ts_semicolon let layout_semi_header if parent is ts_for then parent.init != null && (parent.test == null && previous_token not ts_semicolon || offset < parent.test.offset) || parent.test != null && offset >= parent.test.end_offset else parent.layout_semi_initializer.nonempty ?? false
|
|
96
|
+
in ts_semicolon let layout_semi_statement parent is layout_semi_owner && end_offset == parent.end_offset
|
|
97
|
+
in ts_semicolon rule es-semi-style {
|
|
98
|
+
option style "last" | "first" = "last" "semicolon line position"
|
|
99
|
+
when layout_semi_header || layout_semi_statement
|
|
100
|
+
let semi_at_end style == "last" || layout_semi_header
|
|
101
|
+
when if semi_at_end then position(offset).js_line != position(token(node, "before").end).js_line
|
|
102
|
+
else !(parent.layout_semi_last.nonempty ?? false) && position(end_offset).js_line != position(token(node, "after").start).js_line
|
|
103
|
+
let semi_fix_start token_neighbours.before.end.nonempty ?? offset
|
|
104
|
+
let semi_fix_end token_neighbours.after.start.nonempty ?? end_offset
|
|
105
|
+
fix at { start: semi_fix_start, end: semi_fix_end } if { value overlapping(ts_comment, semi_fix_start, semi_fix_end); count } == 0 as if semi_at_end then ";\n" else "\n;"
|
|
106
|
+
warn if semi_at_end then "Expected this semicolon to be at the end of the previous line." else "Expected this semicolon to be at the beginning of the next line."
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@macro layout_semi_spacing_options {
|
|
110
|
+
option before boolean = false "space before semicolon"
|
|
111
|
+
option after boolean = true "space after semicolon"
|
|
112
|
+
when layout_semi_header || layout_semi_statement || parent is (ts_type_alias, ts_function, ts_method) && end_offset == parent.end_offset || parent is (ts_interface, ts_type_literal)
|
|
113
|
+
}
|
|
114
|
+
in ts_semicolon rule es-semi-spacing:before {
|
|
115
|
+
@layout_semi_spacing_options
|
|
116
|
+
let semi_space space_before != null && !space_before.line_break && space_before.spaced
|
|
117
|
+
when semi_space != before
|
|
118
|
+
report at { start: if semi_space then space_before.start else offset, end: if semi_space then offset else end_offset }
|
|
119
|
+
let semi_fix_start if semi_space then space_before.start else offset
|
|
120
|
+
fix at { start: semi_fix_start, end: offset } if !space_before.line_break && { value overlapping(ts_comment, semi_fix_start, offset); count } == 0 as if before then " " else ""
|
|
121
|
+
warn if semi_space then "Unexpected whitespace before semicolon." else "Missing whitespace before semicolon."
|
|
122
|
+
}
|
|
123
|
+
in ts_semicolon rule es-semi-spacing:after {
|
|
124
|
+
@layout_semi_spacing_options
|
|
125
|
+
when previous_token != null && next_token != null
|
|
126
|
+
when !space_before.line_break && !space_after.line_break && next_token not (ts_close_paren, ts_close_brace)
|
|
127
|
+
when space_after.spaced != after
|
|
128
|
+
report at { start: if space_after.spaced then end_offset else offset, end: if space_after.spaced then space_after.end else end_offset }
|
|
129
|
+
let semi_fix_end if space_after.spaced then space_after.end else end_offset
|
|
130
|
+
fix at { start: end_offset, end: semi_fix_end } if { value overlapping(ts_comment, end_offset, semi_fix_end); count } == 0 as if after then " " else ""
|
|
131
|
+
warn if space_after.spaced then "Unexpected whitespace after semicolon." else "Missing whitespace after semicolon."
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
in ts_comma rule es-comma-spacing:before {
|
|
135
|
+
option before boolean = false "space before comma"
|
|
136
|
+
option after boolean = true "space after comma"
|
|
137
|
+
# Cheapest and most selective first: most commas are spaced as wanted.
|
|
138
|
+
let comma_space layout_comma_space(space_before.text, true)
|
|
139
|
+
when before != (comma_space != "")
|
|
140
|
+
when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
|
|
141
|
+
when previous_token != null && previous_token not ts_comma
|
|
142
|
+
when !(parent is (ts_array, ts_array_pattern) && previous_token is ts_open_bracket)
|
|
143
|
+
when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
|
|
144
|
+
fix space_before as if before then " " else ""
|
|
145
|
+
warn if before then "A space is required before ','." else "There should be no space before ','."
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
in ts_comma rule es-comma-spacing:after {
|
|
149
|
+
option before boolean = false "space before comma"
|
|
150
|
+
option after boolean = true "space after comma"
|
|
151
|
+
# Cheapest and most selective first: most commas are spaced as wanted.
|
|
152
|
+
let comma_space layout_comma_space(space_after.text, false)
|
|
153
|
+
when after != (comma_space != "")
|
|
154
|
+
when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
|
|
155
|
+
when next_token != null
|
|
156
|
+
when space_after.comments || next_token not (ts_comma, ts_close_paren, ts_close_bracket, ts_close_brace)
|
|
157
|
+
when !(parent is (ts_array, ts_array_pattern) && previous_token is (ts_open_bracket, ts_comma) && next_token is (ts_comma, ts_close_bracket))
|
|
158
|
+
when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
|
|
159
|
+
when after || !{ value space_after.text; trim_start; starts_with "//" }
|
|
160
|
+
fix space_after as if after then " " else ""
|
|
161
|
+
warn if after then "A space is required after ','." else "There should be no space after ','."
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
group semicolon_statement ts_expression_statement, ts_variable_declaration, ts_return, ts_throw, ts_break, ts_continue, ts_do_while, ts_import, ts_import_equals, ts_type_alias, ts_class_property, ts_debugger, ts_export_assignment, ts_export, ts_method, ts_function
|
|
165
|
+
fn layout_next_position_len(source, offset) = {
|
|
166
|
+
value char: byte_char(source, offset)
|
|
167
|
+
value if char == "\r" && byte_char(source, offset + 1) == "\n" then 2 else byte_len(char)
|
|
168
|
+
}
|
|
169
|
+
in semicolon_statement rule es-semi {
|
|
170
|
+
option mode "always" | "never" = "always" "statement semicolon policy"
|
|
171
|
+
option omitLastInOneLineBlock boolean = false "omit final semicolons in single-line blocks"
|
|
172
|
+
option omitLastInOneLineClassBody boolean = false "omit final semicolons in single-line class bodies"
|
|
173
|
+
option beforeStatementContinuationChars "always" | "any" | "never" = "any" "semicolons before continuation characters"
|
|
174
174
|
when mode != "always" || omitLastInOneLineBlock || omitLastInOneLineClassBody || token(node, "last").text != ";"
|
|
175
175
|
let statement_semi layout_statement_semicolon(source, node, mode, omitLastInOneLineBlock, omitLastInOneLineClassBody)
|
|
176
|
-
when statement_semi != null
|
|
177
|
-
let omit_statement_semi omitLastInOneLineBlock && statement_semi.one_line_block || omitLastInOneLineClassBody && statement_semi.one_line_class
|
|
178
|
-
when if mode == "always" then statement_semi.present == omit_statement_semi else if statement_semi.present then statement_semi.redundant || !statement_semi.class_hazard && !statement_semi.same_line && (!statement_semi.hazard_before || !statement_semi.class_field && beforeStatementContinuationChars == "never" && !statement_semi.hazard_after) else !statement_semi.class_field && beforeStatementContinuationChars == "always" && statement_semi.hazard_before
|
|
179
|
-
report at { start: statement_semi.offset, end: if statement_semi.present then statement_semi.end else statement_semi.offset + layout_next_position_len(source, statement_semi.offset) }
|
|
180
|
-
fix at {start:statement_semi.offset, end:statement_semi.end} as if statement_semi.present then "" else ";"
|
|
181
|
-
warn if statement_semi.present then "Extra semicolon." else "Missing semicolon."
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
}
|
|
176
|
+
when statement_semi != null
|
|
177
|
+
let omit_statement_semi omitLastInOneLineBlock && statement_semi.one_line_block || omitLastInOneLineClassBody && statement_semi.one_line_class
|
|
178
|
+
when if mode == "always" then statement_semi.present == omit_statement_semi else if statement_semi.present then statement_semi.redundant || !statement_semi.class_hazard && !statement_semi.same_line && (!statement_semi.hazard_before || !statement_semi.class_field && beforeStatementContinuationChars == "never" && !statement_semi.hazard_after) else !statement_semi.class_field && beforeStatementContinuationChars == "always" && statement_semi.hazard_before
|
|
179
|
+
report at { start: statement_semi.offset, end: if statement_semi.present then statement_semi.end else statement_semi.offset + layout_next_position_len(source, statement_semi.offset) }
|
|
180
|
+
fix at {start:statement_semi.offset, end:statement_semi.end} as if statement_semi.present then "" else ";"
|
|
181
|
+
warn if statement_semi.present then "Extra semicolon." else "Missing semicolon."
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
}
|