@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
package/std/ts/indentation.dx
CHANGED
|
@@ -1,442 +1,442 @@
|
|
|
1
|
-
import .ts/import-attributes
|
|
2
|
-
|
|
3
|
-
define {
|
|
4
|
-
|
|
5
|
-
schema indent_list_offset int(0..) | "first"
|
|
6
|
-
group indent_generic_arguments ts_call, ts_new, ts_type_reference, ts_type_query
|
|
7
|
-
group indent_generic_parameters ts_function, ts_arrow, ts_method, ts_class, ts_interface, ts_type_alias, ts_function_type, ts_method_signature, ts_call_signature
|
|
8
|
-
group indent_calls ts_call, ts_new, ts_import_call
|
|
9
|
-
group indent_offset_ternary_expressions ts_call, ts_await, ts_new
|
|
10
|
-
group indent_lists ts_call, ts_new, ts_function, ts_arrow, ts_method, ts_array, ts_array_pattern, ts_tuple_type, ts_object, ts_object_pattern, ts_import, ts_import_attributes
|
|
11
|
-
group indent_attribute_parts ts_import_attribute, ts_import_attribute_comma, ts_import_attribute_comment
|
|
12
|
-
# Closing dotted names are text, not component references; native token children require an overlay.
|
|
13
|
-
in ts_jsx_name layer indent_jsx_names match indent_jsx_name_member dot:/\./ /\s*/ property:/[^\s.:]+/
|
|
14
|
-
in indent_jsx_name_member let indent_jsx_closing containing(ts_jsx_closing.name)
|
|
15
|
-
in indent_jsx_name_member let indent_jsx_closing_anchor if position(indent_jsx_closing.offset).js_column == len(indent_jsx_closing.indent) + 1 then indent_jsx_closing else node_at(indent_jsx_closing.token_neighbours.before.start)
|
|
16
|
-
|
|
17
|
-
fn indent_numeric(mode) = if type_of(mode) == "num" then mode else 1
|
|
18
|
-
fn indent_variable_mode(owner, mode, vars, lets, consts, using) =
|
|
19
|
-
if owner not ts_variable_declaration then mode
|
|
20
|
-
else owner.indent_declaration_offset.nonempty ?? {
|
|
21
|
-
value (if owner.kind.text == "var" then vars
|
|
22
|
-
else if owner.kind.text == "let" then lets
|
|
23
|
-
else if owner.kind.text == "const" then consts
|
|
24
|
-
else if owner.kind.text == "using" || owner.kind.text == "await using" then using
|
|
25
|
-
else null)
|
|
26
|
-
nonempty
|
|
27
|
-
} ?? mode
|
|
28
|
-
fn indent_steps(unit, levels) = {
|
|
29
|
-
value unit
|
|
30
|
-
byte_slice: byte_slice 0 1
|
|
31
|
-
value len(unit) * levels
|
|
32
|
-
round: round
|
|
33
|
-
value repeat(byte_slice, round)
|
|
34
|
-
}
|
|
35
|
-
fn indent_declaration_level(owner, style, mode) =
|
|
36
|
-
if !owner.indent_multiline_declaration then 0
|
|
37
|
-
else if style != "first" then indent_numeric(style)
|
|
38
|
-
else if mode == 0 then 0
|
|
39
|
-
else {
|
|
40
|
-
value result: position(owner.indent_first_item.first_token.start).js_column - 1 - len(owner.indent)
|
|
41
|
-
value result_2: if mode == "tab" then 1 else mode
|
|
42
|
-
value divide(result, result_2)
|
|
43
|
-
}
|
|
44
|
-
fn indent_function_offset(owner, policy, declaration, expression) =
|
|
45
|
-
policy.nonempty ?? if owner is ts_function && owner.parent is (ts_program, ts_block, ts_export) then declaration else expression
|
|
46
|
-
fn indent_jsx_token_level(item) = if item is ts_jsx_closing.token && item.parent.parent is ts_jsx_fragment && item.text == "/" && item.space_after.line_break then 1 else 0
|
|
47
|
-
fn indent_native_expected(item, unit) = if item == null then null else if item is (ts_jsx_opening.token, ts_jsx_closing.token) then item.parent.parent.indent { value item; levels: indent_jsx_token_level; value indent_steps(unit, levels) } else item.indent_anchor indent_steps(unit, item.indent_level)
|
|
48
|
-
fn indent_attribute_expected(item, unit) =
|
|
49
|
-
if item.offset < item.indent_attribute_container.open.end_offset then { value item.indent_attribute_container.indent_module_owner.indent_module_tail; node_at; indent_native_expected unit }
|
|
50
|
-
else indent_native_expected(item.indent_attribute_container.indent_attribute_anchor, unit) {
|
|
51
|
-
value levels: if item.indent_attribute_item != null && item.indent_attribute_item.indent_attribute_anchor.offset != item.indent_attribute_item.offset && position(item.indent_attribute_item.indent_attribute_anchor.first_token.start).js_line == position(item.indent_attribute_container.open.offset).js_line then 0 else 1
|
|
52
|
-
value indent_steps(unit, levels)
|
|
53
|
-
}
|
|
54
|
-
fn indent_expected(item, unit) =
|
|
55
|
-
if item == null then null
|
|
56
|
-
else if item is indent_attribute_parts then indent_attribute_expected(item, unit)
|
|
57
|
-
else if item is ts_import_attributes.close then indent_native_expected(item.parent.indent_attribute_anchor, unit)
|
|
58
|
-
else indent_native_expected(item, unit)
|
|
59
|
-
fn indent_attribute_node(at) =
|
|
60
|
-
{ value overlapping(ts_import_attributes.close, at, at + 1); first; nonempty } ?? {
|
|
61
|
-
value overlapping(indent_attribute_parts, at, at + 1)
|
|
62
|
-
filter (item) at >= (item.indent_attribute_container.indent_module_owner.indent_module_tail.nonempty ?? item.indent_attribute_container.open.end_offset) && at < item.indent_attribute_container.close.offset
|
|
63
|
-
first
|
|
64
|
-
}
|
|
65
|
-
fn indent_node_at(at) = { value at; indent_attribute_node; nonempty } ?? node_at(at)
|
|
66
|
-
fn indent_list_close(item) = if item.token_neighbours.after.text == "," then node_at(item.token_neighbours.after.start).token_neighbours.after.start else item.token_neighbours.after.start
|
|
67
|
-
fn indent_list_anchor(item) =
|
|
68
|
-
if item.indent_list_previous == null || position(item.indent_list_previous.last_token.end).js_line <= position(item.parent.indent_list_open.offset).js_line then item.offset
|
|
69
|
-
else item.indent_list_previous.first_token.start
|
|
70
|
-
fn indent_first_list_item(nodes, at) =
|
|
71
|
-
{
|
|
72
|
-
value nodes
|
|
73
|
-
fold null (best, item) if item.indent_list_first != null && item.first_token.start == at && (best == null || item.end_offset < best.end_offset) then item else best
|
|
74
|
-
}
|
|
75
|
-
fn indent_callee_tail(callee) =
|
|
76
|
-
{
|
|
77
|
-
value callee
|
|
78
|
-
walk (item) if item is ts_member && position(item.object.last_token.end).js_line == position(item.property.first_token.start).js_line then item.object.node else null
|
|
79
|
-
last
|
|
80
|
-
}
|
|
81
|
-
fn indent_optional_call_anchor(owner) = if position(owner.callee.last_token.end).js_line == position(owner.indent_list_open.offset).js_line then owner.callee.last_token.start else owner.callee.first_token.start
|
|
82
|
-
fn indent_function_anchor(owner) =
|
|
83
|
-
if owner not (ts_call.argument, ts_new.argument) || position(owner.first_token.start).js_column == len(owner.indent) + 1 then owner.offset
|
|
84
|
-
else if indent_list_anchor(owner) != owner.offset then indent_list_anchor(owner)
|
|
85
|
-
else owner.parent.indent_call_anchor
|
|
86
|
-
fn indent_template_parts(owner, at) = {
|
|
87
|
-
previous: { value owner.quasi.nodes; slice at (at + 1); first },
|
|
88
|
-
next: { value owner.quasi.nodes; slice (at + 1) (at + 2); first },
|
|
89
|
-
expression: { value owner.expression.nodes; slice at (at + 1); first }
|
|
90
|
-
}
|
|
91
|
-
fn indent_template_relative(parts) = position(parts.previous.offset).js_line == position(parts.previous.end_offset).js_line
|
|
92
|
-
fn indent_template_end_inline(parts) = position(parts.expression.end_offset).js_line == position(parts.next.offset).js_line
|
|
93
|
-
fn indent_template_exception(parts) = indent_template_end_inline(parts) && position(parts.previous.end_offset).js_line < position(parts.expression.offset).js_line
|
|
94
|
-
fn indent_template_prefix(parts) = {
|
|
95
|
-
value result: position(parts.previous.end_offset - 2).js_line_start - parts.previous.offset
|
|
96
|
-
value max: max(0, result)
|
|
97
|
-
value byte_slice(parts.previous.text, max)
|
|
98
|
-
re_find /^[ \t]*/
|
|
99
|
-
}
|
|
100
|
-
fn indent_template_base(parts, mode, tab_width) =
|
|
101
|
-
if mode == 0 || indent_template_exception(parts) then 0
|
|
102
|
-
else {
|
|
103
|
-
value prefix: indent_template_prefix(parts)
|
|
104
|
-
value tabs: { value prefix; replace " " ""; len }
|
|
105
|
-
value space_width: if mode == "tab" then tab_width else mode
|
|
106
|
-
value prefix
|
|
107
|
-
replace "\t" ""
|
|
108
|
-
len
|
|
109
|
-
divide space_width
|
|
110
|
-
ceil
|
|
111
|
-
then (space_levels) tabs + space_levels
|
|
112
|
-
}
|
|
113
|
-
fn indent_template_literal(parts, mode, tab_width, closing) =
|
|
114
|
-
{
|
|
115
|
-
value result: if mode == "tab" then "\t" else " "
|
|
116
|
-
value result_2: (if mode == "tab" then 1 else mode) * (indent_template_base(parts, mode, tab_width) + (if closing || indent_template_end_inline(parts) && !indent_template_exception(parts) then 0 else 1))
|
|
117
|
-
value repeat(result, result_2)
|
|
118
|
-
}
|
|
119
|
-
# An inline closer inherits its anchor, not the expression line's physical indent.
|
|
120
|
-
fn indent_template_anchor(owner, at, mode, tab_width) =
|
|
121
|
-
{
|
|
122
|
-
value owner.quasi.nodes
|
|
123
|
-
slice 0 (at + 1)
|
|
124
|
-
fold null (anchor, quasi) if quasi.index == 0 || position(quasi.offset).js_column == len(quasi.indent) + 1 then quasi.offset
|
|
125
|
-
else if { value owner; indent_template_parts (quasi.index - 1); indent_template_relative } then anchor
|
|
126
|
-
else { value owner; indent_template_parts (quasi.index - 1); indent_template_literal mode tab_width true }
|
|
127
|
-
}
|
|
128
|
-
fn indent_template_code_anchor(owner, at, mode, tab_width, closing) =
|
|
129
|
-
if { value owner; indent_template_parts at; indent_template_relative } then indent_template_anchor(owner, at, mode, tab_width)
|
|
130
|
-
else { value owner; indent_template_parts at; indent_template_literal mode tab_width closing }
|
|
131
|
-
fn indent_list_mode(item, call, declaration, expression, array, object, imports, variable) =
|
|
132
|
-
if item is (ts_call.argument, ts_new.argument, ts_import_call.argument) then item.parent.indent_arguments_offset.nonempty ?? call
|
|
133
|
-
else if item is (ts_function.parameter, ts_arrow.parameter, ts_method.parameter) then indent_function_offset(item.parent, item.parent.indent_parameters_offset, declaration, expression)
|
|
134
|
-
else if item is (ts_array.element, ts_array_pattern.element, ts_tuple_type.element) then array
|
|
135
|
-
else if item is (ts_object.property, ts_object_pattern.property) then object
|
|
136
|
-
else if item is ts_import.specifier then imports
|
|
137
|
-
else if item is ts_variable_declaration.declaration then variable
|
|
138
|
-
else 1
|
|
139
|
-
fn indent_flat_ternary(owner, statement) = position(owner.test.offset).js_line == position(owner.consequent.offset).js_line && position(owner.offset).js_line != position(statement.offset).js_line
|
|
140
|
-
fn indent_ternary_level(branch, flat, offsets, statement) =
|
|
141
|
-
if flat && indent_flat_ternary(branch.parent, statement) then 0
|
|
142
|
-
else if offsets && (branch.first_token is (opener, closer, ts_comma, ts_semicolon, ts_colon, ts_dot, ts_question, ts_ellipsis, ts_equals, ts_punctuator) || branch is indent_offset_ternary_expressions) then 2
|
|
143
|
-
else 1
|
|
144
|
-
fn indent_ternary_anchor_level(branch, flat, offsets, statement) =
|
|
145
|
-
if branch is ts_conditional.alternate && position(branch.parent.consequent.last_token.end).js_line == position(branch.first_token.start).js_line then 0
|
|
146
|
-
else { value result: indent_ternary_level(branch, flat, offsets, statement) - 1; value max(0, result) }
|
|
147
|
-
fn indent_comment_after(policy, unit, variable) =
|
|
148
|
-
if policy == null then null
|
|
149
|
-
else if policy.range then indent_expected(policy.next, unit)
|
|
150
|
-
else if !policy.after.indent_tail_unchecked then indent_expected(policy.after, unit)
|
|
151
|
-
else if position(policy.after.offset).js_line != position(policy.comment.offset).js_line then policy.after.indent
|
|
152
|
-
else indent_expected(policy.after.parent.node, unit) {
|
|
153
|
-
value levels: if policy.after.parent is ts_variable_declaration then indent_numeric(variable) else 1
|
|
154
|
-
value indent_steps(unit, levels)
|
|
155
|
-
}
|
|
156
|
-
fn indent_comment_wanted(policy, after, unit, switch_mode) =
|
|
157
|
-
if policy == null then null
|
|
158
|
-
else if policy.empty_switch then indent_expected(policy.switch.indent_switch_open, unit) indent_steps(unit, switch_mode)
|
|
159
|
-
else if policy.range || policy.comment != null && policy.after is ts_semicolon then after
|
|
160
|
-
else null
|
|
161
|
-
|
|
162
|
-
in indent_calls let indent_items argument.nodes
|
|
163
|
-
in ts_function, ts_arrow, ts_method let indent_items parameter.nodes
|
|
164
|
-
in ts_function, ts_arrow, ts_method let indent_body_root {
|
|
165
|
-
value node
|
|
166
|
-
walk (item) if item is ts_arrow.body && position(item.first_token.start).js_column > len(item.indent) + 1 then item.parent.node else null
|
|
167
|
-
last
|
|
168
|
-
}
|
|
169
|
-
in ts_array, ts_array_pattern, ts_tuple_type let indent_items element.nodes
|
|
170
|
-
in ts_object, ts_object_pattern let indent_items property.nodes
|
|
171
|
-
in ts_import let indent_items specifier.nodes
|
|
172
|
-
in ts_import_attributes let indent_items descendants(ts_import_attribute)
|
|
173
|
-
in indent_lists, ts_import_call let indent_first_item find(indent_items, item => token(item, "first") != null)
|
|
174
|
-
in indent_lists let indent_list_open if is ts_import_attributes then open.node else { value token.nodes; filter (item) item is (ts_open_paren, ts_open_bracket, ts_open_brace); first }
|
|
175
|
-
in ts_variable_declaration let indent_first_item first(declaration.nodes)
|
|
176
|
-
in ts_variable_declaration let indent_multiline_declaration { value last(declaration.nodes).offset; position }.js_line > position(offset).js_line
|
|
177
|
-
in ts_node let indent_list_first if is (ts_call.argument, ts_new.argument, ts_import_call.argument, ts_function.parameter, ts_arrow.parameter, ts_method.parameter, ts_array.element, ts_array_pattern.element, ts_tuple_type.element, ts_object.property, ts_object_pattern.property, ts_import.specifier, ts_variable_declaration.declaration) then parent.indent_first_item else null
|
|
178
|
-
in ts_node let indent_list_ordinal if indent_list_first != null then node_ordinal(node) else null
|
|
179
|
-
in ts_node let indent_list_previous if indent_list_ordinal > 0 && parent is indent_lists then { value parent.indent_items; slice (indent_list_ordinal - 1) indent_list_ordinal; first } else null
|
|
180
|
-
in ts_import_attribute let indent_list_first parent.indent_first_item
|
|
181
|
-
in ts_import_attribute let indent_list_previous { value parent.indent_items; filter (item) item.offset < offset; last }
|
|
182
|
-
in ts_import_attribute let indent_attribute_anchor { value node; walk (item) if indent_list_anchor(item) != item.offset then item.indent_list_previous else null; last }
|
|
183
|
-
in indent_attribute_parts let indent_attribute_container containing(ts_import_attributes)
|
|
184
|
-
in indent_attribute_parts let indent_attribute_item if is ts_import_attribute then node else { value indent_attribute_container.indent_items; filter (item) item.offset >= end_offset; first }
|
|
185
|
-
in ts_import, ts_export let indent_module_attributes { value overlapping(ts_import_attributes); first }
|
|
186
|
-
in ts_import, ts_export let indent_module_from if from.token_neighbours.before.text == "from" then from.token_neighbours.before.start else null
|
|
187
|
-
in ts_import, ts_export let indent_module_tail if indent_module_from != null then indent_module_from else token(indent_module_attributes.open.node, "before").start
|
|
188
|
-
in ts_import_attributes let indent_module_owner { value containing(ts_import); nonempty } ?? containing(ts_export)
|
|
189
|
-
in ts_import_attributes let indent_attribute_line_node { value position(open.offset).js_line_start + byte_len(open.indent); node_at }
|
|
190
|
-
in ts_import_attributes let indent_attribute_anchor if position(indent_attribute_line_node.offset).js_line < position(open.offset).js_line then {
|
|
191
|
-
value position(indent_attribute_line_node.offset).js_line_start + byte_len(indent_attribute_line_node.indent)
|
|
192
|
-
node_at
|
|
193
|
-
} else indent_attribute_line_node
|
|
194
|
-
in indent_calls let indent_call_tail indent_callee_tail(node.callee.node)
|
|
195
|
-
in indent_calls let indent_call_anchor
|
|
196
|
-
if node.callee == null then offset
|
|
197
|
-
else if node.callee is ts_tagged_template then node.callee.quasi.token_neighbours.before.start
|
|
198
|
-
else if node.optional != null then node.optional.offset
|
|
199
|
-
else if node_at(indent_call_tail.last_token.start) is (ts_close_paren, ts_close_bracket, ts_close_brace) then { value node_at(indent_call_tail.last_token.start).paired_token.start; nonempty } ?? indent_call_tail.offset
|
|
200
|
-
else indent_call_tail.last_token.start
|
|
201
|
-
|
|
202
|
-
in ts_expression_statement, ts_variable_declaration let indent_outer_context parent is ts_program
|
|
203
|
-
in ts_call, ts_parenthesized, ts_assignment, ts_logical, ts_sequence, ts_declarator let indent_outer_context parent.indent_outer_context == true
|
|
204
|
-
in ts_unary let indent_outer_context contains(["!", "~", "+", "-"], operator) && parent.indent_outer_context == true
|
|
205
|
-
in ts_function, ts_arrow let indent_called_function node
|
|
206
|
-
in ts_parenthesized let indent_called_function expression.indent_called_function
|
|
207
|
-
in ts_function, ts_arrow let indent_outer_iife containing(ts_call).indent_outer_context == true && containing(ts_call).callee.indent_called_function.offset == offset
|
|
208
|
-
in ts_assignment, ts_assignment_pattern let indent_assignment_value right.node
|
|
209
|
-
in ts_declarator, ts_parameter, ts_class_property let indent_assignment_value init.node
|
|
210
|
-
in ts_jsx_attribute let indent_assignment_value value.node
|
|
211
|
-
in ts_import_equals let indent_assignment_value value.node
|
|
212
|
-
in ts_type_parameter let indent_assignment_value default.node
|
|
213
|
-
in ts_function let indent_return_colon return_type.token_neighbours.before.start
|
|
214
|
-
in ts_function let indent_return_anchor node_at(indent_return_colon).token_neighbours.before.start
|
|
215
|
-
in ts_class let indent_class_head { value { value token.nodes; filter (item) item.text == "class"; first }.offset; nonempty } ?? offset
|
|
216
|
-
in indent_generic_arguments let indent_angle_open first(type_argument.nodes).token_neighbours.before.start
|
|
217
|
-
in indent_generic_parameters let indent_angle_open first(type_parameter.nodes).token_neighbours.before.start
|
|
218
|
-
in indent_generic_arguments let indent_angle_close { value type_argument.nodes; last; indent_list_close }
|
|
219
|
-
in indent_generic_parameters let indent_angle_close { value type_parameter.nodes; last; indent_list_close }
|
|
220
|
-
in ts_index let indent_index_open index.token_neighbours.before.start
|
|
221
|
-
in ts_if let indent_control_tails [consequent.last_token.end, alternate.last_token.end]
|
|
222
|
-
in ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_with let indent_control_tails [body.last_token.end]
|
|
223
|
-
in ts_semicolon, ts_empty let indent_semicolon_control if space_before.line_break && space_after.line_break == false && next_token != null then {
|
|
224
|
-
value node
|
|
225
|
-
walk (item) item.parent.node
|
|
226
|
-
filter (item) { value item.indent_control_tails.nonempty ?? []; contains end_offset }
|
|
227
|
-
last
|
|
228
|
-
} else null
|
|
229
|
-
in ts_semicolon let indent_tail_unchecked parent is (ts_variable_declaration, ts_declarator, ts_type_alias, ts_import_equals, ts_export_assignment)
|
|
230
|
-
in ts_comment let indent_comment_switch containing(ts_switch)
|
|
231
|
-
in ts_comment let indent_comment_list containing(indent_lists)
|
|
232
|
-
in ts_switch let indent_switch_open { value token.nodes; filter (item) item is ts_open_brace; first }
|
|
233
|
-
|
|
234
|
-
in ts_node let indent_reset false
|
|
235
|
-
in ts_token let indent_reset if parent is ts_jsx_opening then parent.indent_reset else if parent is (ts_jsx_attribute, ts_jsx_spread_attribute) then parent.parent.indent_reset else false
|
|
236
|
-
in ts_line let indent_checked true
|
|
237
|
-
in indent_calls let indent_arguments_offset null
|
|
238
|
-
in ts_function, ts_arrow, ts_method let indent_parameters_offset null
|
|
239
|
-
in ts_function, ts_arrow, ts_method, ts_static_block let indent_body_offset null
|
|
240
|
-
in ts_function let indent_return_type_offset null
|
|
241
|
-
in ts_variable_declaration let indent_declaration_offset null
|
|
242
|
-
|
|
243
|
-
in ts_line rule es-indent {
|
|
244
|
-
indent reset indent_reset
|
|
245
|
-
option mode int(0..) | "tab" = 4 "spaces per level, or tab"
|
|
246
|
-
option tab_length number(1..) = 4 "spaces per raw-template tab in tab mode"
|
|
247
|
-
option switch_case int(0..) = 1
|
|
248
|
-
option variable_declarator indent_list_offset = 1
|
|
249
|
-
option variable_declarator_var indent_list_offset | null = null
|
|
250
|
-
option variable_declarator_let indent_list_offset | null = null
|
|
251
|
-
option variable_declarator_const indent_list_offset | null = null
|
|
252
|
-
option variable_declarator_using indent_list_offset | null = null
|
|
253
|
-
option member_expression int(0..) = 1
|
|
254
|
-
option function_declaration_parameters indent_list_offset = 1
|
|
255
|
-
option function_declaration_body int(0..) = 1
|
|
256
|
-
option function_declaration_return_type int(0..) = 1
|
|
257
|
-
option function_expression_parameters indent_list_offset = 1
|
|
258
|
-
option function_expression_body int(0..) = 1
|
|
259
|
-
option function_expression_return_type int(0..) = 1
|
|
260
|
-
option static_block int(0..) = 1
|
|
261
|
-
option call_expression indent_list_offset = 1
|
|
262
|
-
option array_expression indent_list_offset = 1
|
|
263
|
-
option object_expression indent_list_offset = 1
|
|
264
|
-
option import_declaration indent_list_offset = 1
|
|
265
|
-
option assignment_operator int(0..) = 1
|
|
266
|
-
option outerIIFEBody int(0..) = 1
|
|
267
|
-
option flat_ternary_expressions boolean = false
|
|
268
|
-
option offset_ternary_expressions boolean = false "extra depth for grouped ternary branches"
|
|
269
|
-
|
|
270
|
-
indent in ts_block, ts_interface, ts_namespace.body
|
|
271
|
-
indent in ts_class from indent_class_head
|
|
272
|
-
indent ts_class.token from if is (ts_open_brace, ts_close_brace) then parent.indent_class_head else if contains(["class", "abstract", "declare"], text) then parent.offset else offset by 0
|
|
273
|
-
indent ts_enum.token from if is ts_open_brace then parent.offset else offset by 0
|
|
274
|
-
indent indent_generic_arguments.token, indent_generic_parameters.token from if offset == parent.indent_angle_close then parent.indent_angle_open else offset by 0
|
|
275
|
-
indent in ts_array, ts_array_pattern, ts_tuple_type by indent_numeric(array_expression)
|
|
276
|
-
indent in ts_object, ts_object_pattern, ts_enum, ts_type_literal by indent_numeric(object_expression)
|
|
277
|
-
indent in ts_parenthesized, ts_parenthesized_type, ts_computed_key, ts_mapped_type
|
|
278
|
-
indent ts_template.expression from indent_template_code_anchor(parent.node, index, mode, tab_length, false) by if { value parent.node; indent_template_parts index; indent_template_relative } then 1 else 0
|
|
279
|
-
indent ts_template.quasi from if index == 0 then offset else indent_template_code_anchor(parent.node, index - 1, mode, tab_length, true) by 0
|
|
280
|
-
indent in ts_index from indent_index_open
|
|
281
|
-
indent ts_index.token from if text == "]" then parent.indent_index_open else if text == "?." then parent.offset else parent.optional.offset.nonempty ?? parent.offset by if text == "]" then 0 else indent_numeric(member_expression)
|
|
282
|
-
indent in indent_calls from indent_call_anchor by { value indent_arguments_offset.nonempty ?? call_expression; indent_numeric }
|
|
283
|
-
indent indent_calls.token from if text == "?." then indent_optional_call_anchor(parent) else if is (ts_open_paren, ts_close_paren) then parent.indent_call_anchor else offset by if text == "?." then 1 else 0
|
|
284
|
-
indent in ts_function.body, ts_arrow.body, ts_method.body from indent_function_anchor(parent.indent_body_root) by if parent.indent_outer_iife then parent.indent_body_offset.nonempty ?? indent_numeric(outerIIFEBody) else indent_function_offset(parent, parent.indent_body_offset, function_declaration_body, function_expression_body)
|
|
285
|
-
indent in ts_static_block.body by parent.indent_body_offset.nonempty ?? static_block
|
|
286
|
-
indent ts_function.parameter, ts_arrow.parameter, ts_method.parameter by {
|
|
287
|
-
value parent
|
|
288
|
-
indent_function_offset parent.indent_parameters_offset function_declaration_parameters function_expression_parameters
|
|
289
|
-
indent_numeric
|
|
290
|
-
}
|
|
291
|
-
indent ts_function.return_type from parent.indent_return_anchor by indent_function_offset(parent, parent.indent_return_type_offset, function_declaration_return_type, function_expression_return_type)
|
|
292
|
-
indent ts_function.token from if is ts_close_paren then indent_function_anchor(parent.indent_body_root) else if offset == parent.indent_return_colon then parent.indent_return_anchor else offset by if is ts_close_paren then 0 else indent_function_offset(parent, parent.indent_return_type_offset, function_declaration_return_type, function_expression_return_type)
|
|
293
|
-
indent ts_arrow.token, ts_method.token from if is ts_close_paren then indent_function_anchor(parent.indent_body_root) else offset by 0
|
|
294
|
-
indent ts_arrow.return_type, ts_parameter.type
|
|
295
|
-
indent ts_call.argument, ts_new.argument, ts_import_call.argument from parent.indent_call_anchor by { value parent.indent_arguments_offset.nonempty ?? call_expression; indent_numeric }
|
|
296
|
-
indent ts_switch.case by switch_case
|
|
297
|
-
indent ts_case.body by if is ts_block && count(parent.body.nodes) == 1 then 0 else 1
|
|
298
|
-
indent ts_variable_declaration.declaration class declarators by {
|
|
299
|
-
value parent
|
|
300
|
-
style: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
301
|
-
value indent_declaration_level(parent, style, mode)
|
|
302
|
-
}
|
|
303
|
-
indent in ts_variable_declaration class declarators by {
|
|
304
|
-
value node
|
|
305
|
-
style: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
306
|
-
value indent_declaration_level(node, style, mode)
|
|
307
|
-
}
|
|
308
|
-
indent ts_declarator.token from token_neighbours.before.start by if text == "=" then indent_numeric(assignment_operator) else 0
|
|
309
|
-
indent ts_member.property, ts_member.token, ts_member.optional, ts_index.optional, ts_meta_property.token by indent_numeric(member_expression)
|
|
310
|
-
indent ts_call.optional from indent_optional_call_anchor(parent)
|
|
311
|
-
indent ts_import.specifier by indent_numeric(import_declaration)
|
|
312
|
-
indent ts_import.token, ts_export.token from if parent.indent_module_tail != null && offset >= parent.indent_module_tail && node not ts_semicolon then parent.offset else offset
|
|
313
|
-
indent ts_import.from, ts_export.from from if parent.indent_module_from != null then parent.offset else offset
|
|
314
|
-
indent ts_export.specifier
|
|
315
|
-
indent ts_assignment.right, ts_assignment_pattern.right by indent_numeric(assignment_operator)
|
|
316
|
-
indent ts_class_property.key from if offset != parent.offset && (is ts_computed_key || first(parent.decorator.nodes) == null) then parent.offset else offset by if is ts_computed_key then 0 else 1
|
|
317
|
-
indent ts_class_property.token from if text == "=" then token_neighbours.before.start else if text == ";" then parent.init.token_neighbours.before.start.nonempty ?? parent.key.last_token.start else offset by if text == ";" then 1 else indent_numeric(assignment_operator)
|
|
318
|
-
indent ts_class_property.init from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
319
|
-
indent ts_declarator.init from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
320
|
-
indent ts_import_equals.value, ts_type_parameter.default from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
321
|
-
indent ts_import_equals.token, ts_type_parameter.token from if text == "=" then token_neighbours.before.start else offset by indent_numeric(assignment_operator)
|
|
322
|
-
indent ts_arrow.body from indent_function_anchor(parent.indent_body_root) by if is ts_block then 0 else 1
|
|
323
|
-
indent ts_property.value, ts_return.argument, ts_throw.argument
|
|
324
|
-
indent ts_conditional.consequent, ts_conditional.alternate by {
|
|
325
|
-
value statement: containing(ts_statement)
|
|
326
|
-
value indent_ternary_level(node, flat_ternary_expressions, offset_ternary_expressions, statement)
|
|
327
|
-
}
|
|
328
|
-
# An anchor at the branch itself skips this candidate; own-line branches keep ordinary collapsing.
|
|
329
|
-
indent ts_conditional.consequent, ts_conditional.alternate from if position(first_token.start).js_line == position(token_neighbours.before.start).js_line && position(parent.offset).js_line < position(first_token.start).js_line then token_neighbours.before.start else offset class branches by {
|
|
330
|
-
value statement: containing(ts_statement)
|
|
331
|
-
value indent_ternary_anchor_level(node, flat_ternary_expressions, offset_ternary_expressions, statement)
|
|
332
|
-
}
|
|
333
|
-
indent ts_conditional.token by if flat_ternary_expressions && { value statement: containing(ts_statement); value indent_flat_ternary(parent, statement) } then 0 else 1
|
|
334
|
-
indent ts_conditional_type.consequent, ts_conditional_type.alternate, ts_conditional_type.token by if parent is ts_conditional_type.alternate && position(parent.extends.last_token.end).js_line == position(parent.consequent.first_token.start).js_line then 0 else 1
|
|
335
|
-
indent in ts_jsx_element, ts_jsx_fragment
|
|
336
|
-
indent in ts_jsx_opening
|
|
337
|
-
indent in ts_jsx_expression, ts_jsx_spread_attribute
|
|
338
|
-
indent ts_jsx_element.body, ts_jsx_fragment.body
|
|
339
|
-
indent ts_jsx_element.closing, ts_jsx_fragment.closing from parent.offset by 0
|
|
340
|
-
indent in ts_jsx_fragment.closing from parent.offset by 1
|
|
341
|
-
indent ts_jsx_closing.name from token_neighbours.before.start
|
|
342
|
-
indent ts_jsx_opening.token, ts_jsx_closing.token from parent.parent.offset by indent_jsx_token_level(node)
|
|
343
|
-
indent ts_jsx_attribute.token from parent.offset by indent_numeric(assignment_operator)
|
|
344
|
-
indent ts_jsx_attribute.value from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
345
|
-
indent ts_function_type.parameter, ts_method_signature.parameter, ts_call_signature.parameter
|
|
346
|
-
indent ts_function_type.return_type, ts_method_signature.return_type, ts_call_signature.return_type
|
|
347
|
-
indent indent_generic_arguments.type_argument, indent_generic_parameters.type_parameter from parent.indent_angle_open
|
|
348
|
-
indent ts_type_parameter.constraint
|
|
349
|
-
indent ts_for.init, ts_for.test, ts_for.update
|
|
350
|
-
indent ts_if.test, ts_while.test, ts_do_while.test, ts_with.object from token_neighbours.before.start
|
|
351
|
-
# A forward anchor leaves non-block interiors under their existing owners.
|
|
352
|
-
indent in ts_if.consequent, ts_if.alternate, ts_for.body, ts_for_in.body, ts_for_of.body, ts_while.body, ts_do_while.body, ts_with.body from if is ts_block then parent.offset else end_offset
|
|
353
|
-
indent ts_if.consequent, ts_if.alternate, ts_for.body, ts_for_in.body, ts_for_of.body, ts_while.body, ts_do_while.body, ts_with.body from if is ts_empty then node.indent_semicolon_control else null by if is ts_block || is ts_empty && node.indent_semicolon_control != null then 0 else 1
|
|
354
|
-
indent ts_semicolon from indent_semicolon_control.nonempty ?? parent.node by if indent_semicolon_control == null && parent is (ts_block, ts_class, ts_interface, ts_type_literal) then 1 else 0
|
|
355
|
-
indent ts_call.argument, ts_new.argument, ts_function.parameter, ts_arrow.parameter, ts_method.parameter, ts_array.element, ts_array_pattern.element, ts_tuple_type.element, ts_object.property, ts_object_pattern.property, ts_import.specifier from indent_list_anchor(node) by 0
|
|
356
|
-
|
|
357
|
-
when trim(text) != ""
|
|
358
|
-
when indent_checked
|
|
359
|
-
let first_offset offset + byte_len(indent)
|
|
360
|
-
let first_nodes overlapping(ts_node, first_offset, first_offset + 1)
|
|
361
|
-
let semicolon { value overlapping(ts_semicolon, first_offset, first_offset + 1); first }
|
|
362
|
-
when semicolon == null || !semicolon.indent_tail_unchecked
|
|
363
|
-
let jsx_member { value overlapping(indent_jsx_name_member, first_offset, first_offset + 1); first }
|
|
364
|
-
let list_item indent_first_list_item(first_nodes, first_offset)
|
|
365
|
-
let list_mode if list_item == null then 1 else {
|
|
366
|
-
value list_item.parent
|
|
367
|
-
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
368
|
-
value indent_list_mode(list_item, call_expression, function_declaration_parameters, function_expression_parameters, array_expression, object_expression, import_declaration, variable)
|
|
369
|
-
}
|
|
370
|
-
let binary_right { value overlapping(ts_binary.right, first_offset, first_offset + 1); first }
|
|
371
|
-
let logical_right { value overlapping(ts_logical.right, first_offset, first_offset + 1); first }
|
|
372
|
-
let binary_operator { value overlapping(ts_binary.operator, first_offset, first_offset + 1); first }
|
|
373
|
-
let logical_operator { value overlapping(ts_logical.operator, first_offset, first_offset + 1); first }
|
|
374
|
-
when binary_right == null || binary_right.first_token.start != first_offset
|
|
375
|
-
when logical_right == null || logical_right.first_token.start != first_offset
|
|
376
|
-
when binary_operator == null && logical_operator == null
|
|
377
|
-
let type_operator { value overlapping(ts_union.token, first_offset, first_offset + 1); first }
|
|
378
|
-
let intersection_operator { value overlapping(ts_intersection.token, first_offset, first_offset + 1); first }
|
|
379
|
-
when type_operator.text != "|" && intersection_operator.text != "&"
|
|
380
|
-
when !any(first_nodes, item => item is (ts_union.type, ts_intersection.type) && item.first_token.start == first_offset)
|
|
381
|
-
let quasi { value overlapping(ts_template.quasi, first_offset, first_offset + 1); first }
|
|
382
|
-
let comment { value overlapping(ts_comment, first_offset, first_offset + 1); first }
|
|
383
|
-
let block_comment if comment is ts_block_comment then comment else null
|
|
384
|
-
let string_literal { value overlapping(ts_string, first_offset, first_offset + 1); first }
|
|
385
|
-
let attribute_literal { value overlapping(ts_import_attribute_string, first_offset, first_offset + 1); first }
|
|
386
|
-
let jsx_text { value overlapping(ts_jsx_text, first_offset, first_offset + 1); first }
|
|
387
|
-
when quasi == null || position(quasi.offset).js_line == position(first_offset).js_line
|
|
388
|
-
when block_comment == null || position(block_comment.offset).js_line == position(first_offset).js_line
|
|
389
|
-
when string_literal == null || position(string_literal.offset).js_line == position(first_offset).js_line
|
|
390
|
-
when attribute_literal == null || position(attribute_literal.offset).js_line == position(first_offset).js_line
|
|
391
|
-
let unit if mode == "tab" then "\t" else repeat(" ", mode)
|
|
392
|
-
let comment_before if comment == null then null else indent_node_at(comment.token_neighbours.before.start)
|
|
393
|
-
let comment_after if comment == null then null else indent_node_at(comment.token_neighbours.after.start)
|
|
394
|
-
let comment_switch comment.indent_comment_switch
|
|
395
|
-
let comment_last_case last(comment_switch.case.nodes)
|
|
396
|
-
when comment_last_case == null || comment.offset < comment_last_case.end_offset
|
|
397
|
-
let comment_empty_switch comment_switch != null && comment_last_case == null && comment.offset > comment_switch.indent_switch_open.offset
|
|
398
|
-
let comment_list comment.indent_comment_list
|
|
399
|
-
let comment_next { value comment_list.indent_items; filter (item) item.first_token.start >= comment.end_offset; first }
|
|
400
|
-
let comment_list_mode {
|
|
401
|
-
value comment_next.parent
|
|
402
|
-
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
403
|
-
value indent_list_mode(comment_next, call_expression, function_declaration_parameters, function_expression_parameters, array_expression, object_expression, import_declaration, variable)
|
|
404
|
-
}
|
|
405
|
-
let comment_list_range comment_next.indent_list_previous != null && comment_next.indent_list_previous.last_token.end <= comment.offset && position(comment_next.indent_list_previous.last_token.end).js_line > position(comment_list.indent_list_open.offset).js_line && !(comment_list_mode == "first" && position(comment_next.first_token.start).js_column == len(comment_next.indent) + 1)
|
|
406
|
-
let before_wanted indent_expected(comment_before, unit)
|
|
407
|
-
let comment_policy if comment == null then null else { comment: comment, after: comment_after, next: comment_next, range: comment_list_range, switch: comment_switch, empty_switch: comment_empty_switch }
|
|
408
|
-
# Only line-leading declaration semicolons are unchecked; inline ones retain their declaration offset.
|
|
409
|
-
let after_wanted {
|
|
410
|
-
value comment_after.parent
|
|
411
|
-
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
412
|
-
value indent_comment_after(comment_policy, unit, variable)
|
|
413
|
-
}
|
|
414
|
-
when comment == null || comment_list_range || !(comment_before != null && comment.space_before.blank_gaps == 0 && indent == before_wanted || comment_after != null && comment.space_after.blank_gaps == 0 && indent == after_wanted)
|
|
415
|
-
let aligned_first if list_mode == "first" && list_item.offset != list_item.indent_list_first.offset then list_item.indent_list_first else null
|
|
416
|
-
let aligned_line_start position(aligned_first.first_token.start).js_line_start
|
|
417
|
-
let aligned_quasi if aligned_first == null then null else {
|
|
418
|
-
value overlapping(ts_template.quasi, aligned_line_start, aligned_line_start + 1)
|
|
419
|
-
filter (item) position(item.offset).js_line < position(aligned_first.first_token.start).js_line
|
|
420
|
-
first
|
|
421
|
-
}
|
|
422
|
-
let aligned_anchor if aligned_first == null then "" else if aligned_quasi != null then indent_expected(aligned_quasi, unit) else if position(aligned_first.first_token.start).js_line == position(aligned_first.parent.offset).js_line then list_item.parent_indent else indent_expected(aligned_first, unit)
|
|
423
|
-
let aligned_column if aligned_quasi == null then len(aligned_first.indent) else position(aligned_quasi.offset).js_column - 1
|
|
424
|
-
let comment_wanted if comment_policy == null then null else indent_comment_wanted(comment_policy, after_wanted, unit, switch_case)
|
|
425
|
-
let attribute_node indent_attribute_node(first_offset)
|
|
426
|
-
# Raw JSX text uses the tag line's actual indentation, not virtual code-token offsets.
|
|
427
|
-
let wanted if jsx_text != null then jsx_text.parent.indent unit else if jsx_member != null then indent_expected(jsx_member.indent_jsx_closing_anchor, unit) { value member_expression; levels: indent_numeric; value indent_steps(unit, levels) } else if type_of(comment_wanted) != "null" then comment_wanted else if attribute_node != null then indent_expected(attribute_node, unit) else if aligned_first != null then aligned_anchor {
|
|
428
|
-
value result: if mode == "tab" then "\t" else " "
|
|
429
|
-
value result_2: position(aligned_first.first_token.start).js_column - 1 - aligned_column
|
|
430
|
-
value repeat(result, result_2)
|
|
431
|
-
} else indent_anchor indent_steps(unit, indent_level)
|
|
432
|
-
when indent != wanted
|
|
433
|
-
let spaces { value indent; replace "\t" ""; len }
|
|
434
|
-
let tabs { value indent; replace " " ""; len }
|
|
435
|
-
let unit_name if mode == "tab" then "tab" else "space"
|
|
436
|
-
let actual if spaces > 0 then "{spaces}{if mode == 'tab' then if spaces == 1 then ' space' else ' spaces' else ''}" else if tabs > 0 then "{tabs}{if mode != 'tab' then if tabs == 1 then ' tab' else ' tabs' else ''}" else "0"
|
|
437
|
-
report at if jsx_text != null then jsx_text.node else { start: offset, end: offset + byte_len(indent) }
|
|
438
|
-
fix at { start: offset, end: offset + byte_len(indent) } as wanted
|
|
439
|
-
warn "Expected indentation of {len(wanted)} {unit_name}{if len(wanted) == 1 then '' else 's'} but found {actual}."
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
}
|
|
1
|
+
import .ts/import-attributes
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
schema indent_list_offset int(0..) | "first"
|
|
6
|
+
group indent_generic_arguments ts_call, ts_new, ts_type_reference, ts_type_query
|
|
7
|
+
group indent_generic_parameters ts_function, ts_arrow, ts_method, ts_class, ts_interface, ts_type_alias, ts_function_type, ts_method_signature, ts_call_signature
|
|
8
|
+
group indent_calls ts_call, ts_new, ts_import_call
|
|
9
|
+
group indent_offset_ternary_expressions ts_call, ts_await, ts_new
|
|
10
|
+
group indent_lists ts_call, ts_new, ts_function, ts_arrow, ts_method, ts_array, ts_array_pattern, ts_tuple_type, ts_object, ts_object_pattern, ts_import, ts_import_attributes
|
|
11
|
+
group indent_attribute_parts ts_import_attribute, ts_import_attribute_comma, ts_import_attribute_comment
|
|
12
|
+
# Closing dotted names are text, not component references; native token children require an overlay.
|
|
13
|
+
in ts_jsx_name layer indent_jsx_names match indent_jsx_name_member dot:/\./ /\s*/ property:/[^\s.:]+/
|
|
14
|
+
in indent_jsx_name_member let indent_jsx_closing containing(ts_jsx_closing.name)
|
|
15
|
+
in indent_jsx_name_member let indent_jsx_closing_anchor if position(indent_jsx_closing.offset).js_column == len(indent_jsx_closing.indent) + 1 then indent_jsx_closing else node_at(indent_jsx_closing.token_neighbours.before.start)
|
|
16
|
+
|
|
17
|
+
fn indent_numeric(mode) = if type_of(mode) == "num" then mode else 1
|
|
18
|
+
fn indent_variable_mode(owner, mode, vars, lets, consts, using) =
|
|
19
|
+
if owner not ts_variable_declaration then mode
|
|
20
|
+
else owner.indent_declaration_offset.nonempty ?? {
|
|
21
|
+
value (if owner.kind.text == "var" then vars
|
|
22
|
+
else if owner.kind.text == "let" then lets
|
|
23
|
+
else if owner.kind.text == "const" then consts
|
|
24
|
+
else if owner.kind.text == "using" || owner.kind.text == "await using" then using
|
|
25
|
+
else null)
|
|
26
|
+
nonempty
|
|
27
|
+
} ?? mode
|
|
28
|
+
fn indent_steps(unit, levels) = {
|
|
29
|
+
value unit
|
|
30
|
+
byte_slice: byte_slice 0 1
|
|
31
|
+
value len(unit) * levels
|
|
32
|
+
round: round
|
|
33
|
+
value repeat(byte_slice, round)
|
|
34
|
+
}
|
|
35
|
+
fn indent_declaration_level(owner, style, mode) =
|
|
36
|
+
if !owner.indent_multiline_declaration then 0
|
|
37
|
+
else if style != "first" then indent_numeric(style)
|
|
38
|
+
else if mode == 0 then 0
|
|
39
|
+
else {
|
|
40
|
+
value result: position(owner.indent_first_item.first_token.start).js_column - 1 - len(owner.indent)
|
|
41
|
+
value result_2: if mode == "tab" then 1 else mode
|
|
42
|
+
value divide(result, result_2)
|
|
43
|
+
}
|
|
44
|
+
fn indent_function_offset(owner, policy, declaration, expression) =
|
|
45
|
+
policy.nonempty ?? if owner is ts_function && owner.parent is (ts_program, ts_block, ts_export) then declaration else expression
|
|
46
|
+
fn indent_jsx_token_level(item) = if item is ts_jsx_closing.token && item.parent.parent is ts_jsx_fragment && item.text == "/" && item.space_after.line_break then 1 else 0
|
|
47
|
+
fn indent_native_expected(item, unit) = if item == null then null else if item is (ts_jsx_opening.token, ts_jsx_closing.token) then item.parent.parent.indent { value item; levels: indent_jsx_token_level; value indent_steps(unit, levels) } else item.indent_anchor indent_steps(unit, item.indent_level)
|
|
48
|
+
fn indent_attribute_expected(item, unit) =
|
|
49
|
+
if item.offset < item.indent_attribute_container.open.end_offset then { value item.indent_attribute_container.indent_module_owner.indent_module_tail; node_at; indent_native_expected unit }
|
|
50
|
+
else indent_native_expected(item.indent_attribute_container.indent_attribute_anchor, unit) {
|
|
51
|
+
value levels: if item.indent_attribute_item != null && item.indent_attribute_item.indent_attribute_anchor.offset != item.indent_attribute_item.offset && position(item.indent_attribute_item.indent_attribute_anchor.first_token.start).js_line == position(item.indent_attribute_container.open.offset).js_line then 0 else 1
|
|
52
|
+
value indent_steps(unit, levels)
|
|
53
|
+
}
|
|
54
|
+
fn indent_expected(item, unit) =
|
|
55
|
+
if item == null then null
|
|
56
|
+
else if item is indent_attribute_parts then indent_attribute_expected(item, unit)
|
|
57
|
+
else if item is ts_import_attributes.close then indent_native_expected(item.parent.indent_attribute_anchor, unit)
|
|
58
|
+
else indent_native_expected(item, unit)
|
|
59
|
+
fn indent_attribute_node(at) =
|
|
60
|
+
{ value overlapping(ts_import_attributes.close, at, at + 1); first; nonempty } ?? {
|
|
61
|
+
value overlapping(indent_attribute_parts, at, at + 1)
|
|
62
|
+
filter (item) at >= (item.indent_attribute_container.indent_module_owner.indent_module_tail.nonempty ?? item.indent_attribute_container.open.end_offset) && at < item.indent_attribute_container.close.offset
|
|
63
|
+
first
|
|
64
|
+
}
|
|
65
|
+
fn indent_node_at(at) = { value at; indent_attribute_node; nonempty } ?? node_at(at)
|
|
66
|
+
fn indent_list_close(item) = if item.token_neighbours.after.text == "," then node_at(item.token_neighbours.after.start).token_neighbours.after.start else item.token_neighbours.after.start
|
|
67
|
+
fn indent_list_anchor(item) =
|
|
68
|
+
if item.indent_list_previous == null || position(item.indent_list_previous.last_token.end).js_line <= position(item.parent.indent_list_open.offset).js_line then item.offset
|
|
69
|
+
else item.indent_list_previous.first_token.start
|
|
70
|
+
fn indent_first_list_item(nodes, at) =
|
|
71
|
+
{
|
|
72
|
+
value nodes
|
|
73
|
+
fold null (best, item) if item.indent_list_first != null && item.first_token.start == at && (best == null || item.end_offset < best.end_offset) then item else best
|
|
74
|
+
}
|
|
75
|
+
fn indent_callee_tail(callee) =
|
|
76
|
+
{
|
|
77
|
+
value callee
|
|
78
|
+
walk (item) if item is ts_member && position(item.object.last_token.end).js_line == position(item.property.first_token.start).js_line then item.object.node else null
|
|
79
|
+
last
|
|
80
|
+
}
|
|
81
|
+
fn indent_optional_call_anchor(owner) = if position(owner.callee.last_token.end).js_line == position(owner.indent_list_open.offset).js_line then owner.callee.last_token.start else owner.callee.first_token.start
|
|
82
|
+
fn indent_function_anchor(owner) =
|
|
83
|
+
if owner not (ts_call.argument, ts_new.argument) || position(owner.first_token.start).js_column == len(owner.indent) + 1 then owner.offset
|
|
84
|
+
else if indent_list_anchor(owner) != owner.offset then indent_list_anchor(owner)
|
|
85
|
+
else owner.parent.indent_call_anchor
|
|
86
|
+
fn indent_template_parts(owner, at) = {
|
|
87
|
+
previous: { value owner.quasi.nodes; slice at (at + 1); first },
|
|
88
|
+
next: { value owner.quasi.nodes; slice (at + 1) (at + 2); first },
|
|
89
|
+
expression: { value owner.expression.nodes; slice at (at + 1); first }
|
|
90
|
+
}
|
|
91
|
+
fn indent_template_relative(parts) = position(parts.previous.offset).js_line == position(parts.previous.end_offset).js_line
|
|
92
|
+
fn indent_template_end_inline(parts) = position(parts.expression.end_offset).js_line == position(parts.next.offset).js_line
|
|
93
|
+
fn indent_template_exception(parts) = indent_template_end_inline(parts) && position(parts.previous.end_offset).js_line < position(parts.expression.offset).js_line
|
|
94
|
+
fn indent_template_prefix(parts) = {
|
|
95
|
+
value result: position(parts.previous.end_offset - 2).js_line_start - parts.previous.offset
|
|
96
|
+
value max: max(0, result)
|
|
97
|
+
value byte_slice(parts.previous.text, max)
|
|
98
|
+
re_find /^[ \t]*/
|
|
99
|
+
}
|
|
100
|
+
fn indent_template_base(parts, mode, tab_width) =
|
|
101
|
+
if mode == 0 || indent_template_exception(parts) then 0
|
|
102
|
+
else {
|
|
103
|
+
value prefix: indent_template_prefix(parts)
|
|
104
|
+
value tabs: { value prefix; replace " " ""; len }
|
|
105
|
+
value space_width: if mode == "tab" then tab_width else mode
|
|
106
|
+
value prefix
|
|
107
|
+
replace "\t" ""
|
|
108
|
+
len
|
|
109
|
+
divide space_width
|
|
110
|
+
ceil
|
|
111
|
+
then (space_levels) tabs + space_levels
|
|
112
|
+
}
|
|
113
|
+
fn indent_template_literal(parts, mode, tab_width, closing) =
|
|
114
|
+
{
|
|
115
|
+
value result: if mode == "tab" then "\t" else " "
|
|
116
|
+
value result_2: (if mode == "tab" then 1 else mode) * (indent_template_base(parts, mode, tab_width) + (if closing || indent_template_end_inline(parts) && !indent_template_exception(parts) then 0 else 1))
|
|
117
|
+
value repeat(result, result_2)
|
|
118
|
+
}
|
|
119
|
+
# An inline closer inherits its anchor, not the expression line's physical indent.
|
|
120
|
+
fn indent_template_anchor(owner, at, mode, tab_width) =
|
|
121
|
+
{
|
|
122
|
+
value owner.quasi.nodes
|
|
123
|
+
slice 0 (at + 1)
|
|
124
|
+
fold null (anchor, quasi) if quasi.index == 0 || position(quasi.offset).js_column == len(quasi.indent) + 1 then quasi.offset
|
|
125
|
+
else if { value owner; indent_template_parts (quasi.index - 1); indent_template_relative } then anchor
|
|
126
|
+
else { value owner; indent_template_parts (quasi.index - 1); indent_template_literal mode tab_width true }
|
|
127
|
+
}
|
|
128
|
+
fn indent_template_code_anchor(owner, at, mode, tab_width, closing) =
|
|
129
|
+
if { value owner; indent_template_parts at; indent_template_relative } then indent_template_anchor(owner, at, mode, tab_width)
|
|
130
|
+
else { value owner; indent_template_parts at; indent_template_literal mode tab_width closing }
|
|
131
|
+
fn indent_list_mode(item, call, declaration, expression, array, object, imports, variable) =
|
|
132
|
+
if item is (ts_call.argument, ts_new.argument, ts_import_call.argument) then item.parent.indent_arguments_offset.nonempty ?? call
|
|
133
|
+
else if item is (ts_function.parameter, ts_arrow.parameter, ts_method.parameter) then indent_function_offset(item.parent, item.parent.indent_parameters_offset, declaration, expression)
|
|
134
|
+
else if item is (ts_array.element, ts_array_pattern.element, ts_tuple_type.element) then array
|
|
135
|
+
else if item is (ts_object.property, ts_object_pattern.property) then object
|
|
136
|
+
else if item is ts_import.specifier then imports
|
|
137
|
+
else if item is ts_variable_declaration.declaration then variable
|
|
138
|
+
else 1
|
|
139
|
+
fn indent_flat_ternary(owner, statement) = position(owner.test.offset).js_line == position(owner.consequent.offset).js_line && position(owner.offset).js_line != position(statement.offset).js_line
|
|
140
|
+
fn indent_ternary_level(branch, flat, offsets, statement) =
|
|
141
|
+
if flat && indent_flat_ternary(branch.parent, statement) then 0
|
|
142
|
+
else if offsets && (branch.first_token is (opener, closer, ts_comma, ts_semicolon, ts_colon, ts_dot, ts_question, ts_ellipsis, ts_equals, ts_punctuator) || branch is indent_offset_ternary_expressions) then 2
|
|
143
|
+
else 1
|
|
144
|
+
fn indent_ternary_anchor_level(branch, flat, offsets, statement) =
|
|
145
|
+
if branch is ts_conditional.alternate && position(branch.parent.consequent.last_token.end).js_line == position(branch.first_token.start).js_line then 0
|
|
146
|
+
else { value result: indent_ternary_level(branch, flat, offsets, statement) - 1; value max(0, result) }
|
|
147
|
+
fn indent_comment_after(policy, unit, variable) =
|
|
148
|
+
if policy == null then null
|
|
149
|
+
else if policy.range then indent_expected(policy.next, unit)
|
|
150
|
+
else if !policy.after.indent_tail_unchecked then indent_expected(policy.after, unit)
|
|
151
|
+
else if position(policy.after.offset).js_line != position(policy.comment.offset).js_line then policy.after.indent
|
|
152
|
+
else indent_expected(policy.after.parent.node, unit) {
|
|
153
|
+
value levels: if policy.after.parent is ts_variable_declaration then indent_numeric(variable) else 1
|
|
154
|
+
value indent_steps(unit, levels)
|
|
155
|
+
}
|
|
156
|
+
fn indent_comment_wanted(policy, after, unit, switch_mode) =
|
|
157
|
+
if policy == null then null
|
|
158
|
+
else if policy.empty_switch then indent_expected(policy.switch.indent_switch_open, unit) indent_steps(unit, switch_mode)
|
|
159
|
+
else if policy.range || policy.comment != null && policy.after is ts_semicolon then after
|
|
160
|
+
else null
|
|
161
|
+
|
|
162
|
+
in indent_calls let indent_items argument.nodes
|
|
163
|
+
in ts_function, ts_arrow, ts_method let indent_items parameter.nodes
|
|
164
|
+
in ts_function, ts_arrow, ts_method let indent_body_root {
|
|
165
|
+
value node
|
|
166
|
+
walk (item) if item is ts_arrow.body && position(item.first_token.start).js_column > len(item.indent) + 1 then item.parent.node else null
|
|
167
|
+
last
|
|
168
|
+
}
|
|
169
|
+
in ts_array, ts_array_pattern, ts_tuple_type let indent_items element.nodes
|
|
170
|
+
in ts_object, ts_object_pattern let indent_items property.nodes
|
|
171
|
+
in ts_import let indent_items specifier.nodes
|
|
172
|
+
in ts_import_attributes let indent_items descendants(ts_import_attribute)
|
|
173
|
+
in indent_lists, ts_import_call let indent_first_item find(indent_items, item => token(item, "first") != null)
|
|
174
|
+
in indent_lists let indent_list_open if is ts_import_attributes then open.node else { value token.nodes; filter (item) item is (ts_open_paren, ts_open_bracket, ts_open_brace); first }
|
|
175
|
+
in ts_variable_declaration let indent_first_item first(declaration.nodes)
|
|
176
|
+
in ts_variable_declaration let indent_multiline_declaration { value last(declaration.nodes).offset; position }.js_line > position(offset).js_line
|
|
177
|
+
in ts_node let indent_list_first if is (ts_call.argument, ts_new.argument, ts_import_call.argument, ts_function.parameter, ts_arrow.parameter, ts_method.parameter, ts_array.element, ts_array_pattern.element, ts_tuple_type.element, ts_object.property, ts_object_pattern.property, ts_import.specifier, ts_variable_declaration.declaration) then parent.indent_first_item else null
|
|
178
|
+
in ts_node let indent_list_ordinal if indent_list_first != null then node_ordinal(node) else null
|
|
179
|
+
in ts_node let indent_list_previous if indent_list_ordinal > 0 && parent is indent_lists then { value parent.indent_items; slice (indent_list_ordinal - 1) indent_list_ordinal; first } else null
|
|
180
|
+
in ts_import_attribute let indent_list_first parent.indent_first_item
|
|
181
|
+
in ts_import_attribute let indent_list_previous { value parent.indent_items; filter (item) item.offset < offset; last }
|
|
182
|
+
in ts_import_attribute let indent_attribute_anchor { value node; walk (item) if indent_list_anchor(item) != item.offset then item.indent_list_previous else null; last }
|
|
183
|
+
in indent_attribute_parts let indent_attribute_container containing(ts_import_attributes)
|
|
184
|
+
in indent_attribute_parts let indent_attribute_item if is ts_import_attribute then node else { value indent_attribute_container.indent_items; filter (item) item.offset >= end_offset; first }
|
|
185
|
+
in ts_import, ts_export let indent_module_attributes { value overlapping(ts_import_attributes); first }
|
|
186
|
+
in ts_import, ts_export let indent_module_from if from.token_neighbours.before.text == "from" then from.token_neighbours.before.start else null
|
|
187
|
+
in ts_import, ts_export let indent_module_tail if indent_module_from != null then indent_module_from else token(indent_module_attributes.open.node, "before").start
|
|
188
|
+
in ts_import_attributes let indent_module_owner { value containing(ts_import); nonempty } ?? containing(ts_export)
|
|
189
|
+
in ts_import_attributes let indent_attribute_line_node { value position(open.offset).js_line_start + byte_len(open.indent); node_at }
|
|
190
|
+
in ts_import_attributes let indent_attribute_anchor if position(indent_attribute_line_node.offset).js_line < position(open.offset).js_line then {
|
|
191
|
+
value position(indent_attribute_line_node.offset).js_line_start + byte_len(indent_attribute_line_node.indent)
|
|
192
|
+
node_at
|
|
193
|
+
} else indent_attribute_line_node
|
|
194
|
+
in indent_calls let indent_call_tail indent_callee_tail(node.callee.node)
|
|
195
|
+
in indent_calls let indent_call_anchor
|
|
196
|
+
if node.callee == null then offset
|
|
197
|
+
else if node.callee is ts_tagged_template then node.callee.quasi.token_neighbours.before.start
|
|
198
|
+
else if node.optional != null then node.optional.offset
|
|
199
|
+
else if node_at(indent_call_tail.last_token.start) is (ts_close_paren, ts_close_bracket, ts_close_brace) then { value node_at(indent_call_tail.last_token.start).paired_token.start; nonempty } ?? indent_call_tail.offset
|
|
200
|
+
else indent_call_tail.last_token.start
|
|
201
|
+
|
|
202
|
+
in ts_expression_statement, ts_variable_declaration let indent_outer_context parent is ts_program
|
|
203
|
+
in ts_call, ts_parenthesized, ts_assignment, ts_logical, ts_sequence, ts_declarator let indent_outer_context parent.indent_outer_context == true
|
|
204
|
+
in ts_unary let indent_outer_context contains(["!", "~", "+", "-"], operator) && parent.indent_outer_context == true
|
|
205
|
+
in ts_function, ts_arrow let indent_called_function node
|
|
206
|
+
in ts_parenthesized let indent_called_function expression.indent_called_function
|
|
207
|
+
in ts_function, ts_arrow let indent_outer_iife containing(ts_call).indent_outer_context == true && containing(ts_call).callee.indent_called_function.offset == offset
|
|
208
|
+
in ts_assignment, ts_assignment_pattern let indent_assignment_value right.node
|
|
209
|
+
in ts_declarator, ts_parameter, ts_class_property let indent_assignment_value init.node
|
|
210
|
+
in ts_jsx_attribute let indent_assignment_value value.node
|
|
211
|
+
in ts_import_equals let indent_assignment_value value.node
|
|
212
|
+
in ts_type_parameter let indent_assignment_value default.node
|
|
213
|
+
in ts_function let indent_return_colon return_type.token_neighbours.before.start
|
|
214
|
+
in ts_function let indent_return_anchor node_at(indent_return_colon).token_neighbours.before.start
|
|
215
|
+
in ts_class let indent_class_head { value { value token.nodes; filter (item) item.text == "class"; first }.offset; nonempty } ?? offset
|
|
216
|
+
in indent_generic_arguments let indent_angle_open first(type_argument.nodes).token_neighbours.before.start
|
|
217
|
+
in indent_generic_parameters let indent_angle_open first(type_parameter.nodes).token_neighbours.before.start
|
|
218
|
+
in indent_generic_arguments let indent_angle_close { value type_argument.nodes; last; indent_list_close }
|
|
219
|
+
in indent_generic_parameters let indent_angle_close { value type_parameter.nodes; last; indent_list_close }
|
|
220
|
+
in ts_index let indent_index_open index.token_neighbours.before.start
|
|
221
|
+
in ts_if let indent_control_tails [consequent.last_token.end, alternate.last_token.end]
|
|
222
|
+
in ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_with let indent_control_tails [body.last_token.end]
|
|
223
|
+
in ts_semicolon, ts_empty let indent_semicolon_control if space_before.line_break && space_after.line_break == false && next_token != null then {
|
|
224
|
+
value node
|
|
225
|
+
walk (item) item.parent.node
|
|
226
|
+
filter (item) { value item.indent_control_tails.nonempty ?? []; contains end_offset }
|
|
227
|
+
last
|
|
228
|
+
} else null
|
|
229
|
+
in ts_semicolon let indent_tail_unchecked parent is (ts_variable_declaration, ts_declarator, ts_type_alias, ts_import_equals, ts_export_assignment)
|
|
230
|
+
in ts_comment let indent_comment_switch containing(ts_switch)
|
|
231
|
+
in ts_comment let indent_comment_list containing(indent_lists)
|
|
232
|
+
in ts_switch let indent_switch_open { value token.nodes; filter (item) item is ts_open_brace; first }
|
|
233
|
+
|
|
234
|
+
in ts_node let indent_reset false
|
|
235
|
+
in ts_token let indent_reset if parent is ts_jsx_opening then parent.indent_reset else if parent is (ts_jsx_attribute, ts_jsx_spread_attribute) then parent.parent.indent_reset else false
|
|
236
|
+
in ts_line let indent_checked true
|
|
237
|
+
in indent_calls let indent_arguments_offset null
|
|
238
|
+
in ts_function, ts_arrow, ts_method let indent_parameters_offset null
|
|
239
|
+
in ts_function, ts_arrow, ts_method, ts_static_block let indent_body_offset null
|
|
240
|
+
in ts_function let indent_return_type_offset null
|
|
241
|
+
in ts_variable_declaration let indent_declaration_offset null
|
|
242
|
+
|
|
243
|
+
in ts_line rule es-indent {
|
|
244
|
+
indent reset indent_reset
|
|
245
|
+
option mode int(0..) | "tab" = 4 "spaces per level, or tab"
|
|
246
|
+
option tab_length number(1..) = 4 "spaces per raw-template tab in tab mode"
|
|
247
|
+
option switch_case int(0..) = 1
|
|
248
|
+
option variable_declarator indent_list_offset = 1
|
|
249
|
+
option variable_declarator_var indent_list_offset | null = null
|
|
250
|
+
option variable_declarator_let indent_list_offset | null = null
|
|
251
|
+
option variable_declarator_const indent_list_offset | null = null
|
|
252
|
+
option variable_declarator_using indent_list_offset | null = null
|
|
253
|
+
option member_expression int(0..) = 1
|
|
254
|
+
option function_declaration_parameters indent_list_offset = 1
|
|
255
|
+
option function_declaration_body int(0..) = 1
|
|
256
|
+
option function_declaration_return_type int(0..) = 1
|
|
257
|
+
option function_expression_parameters indent_list_offset = 1
|
|
258
|
+
option function_expression_body int(0..) = 1
|
|
259
|
+
option function_expression_return_type int(0..) = 1
|
|
260
|
+
option static_block int(0..) = 1
|
|
261
|
+
option call_expression indent_list_offset = 1
|
|
262
|
+
option array_expression indent_list_offset = 1
|
|
263
|
+
option object_expression indent_list_offset = 1
|
|
264
|
+
option import_declaration indent_list_offset = 1
|
|
265
|
+
option assignment_operator int(0..) = 1
|
|
266
|
+
option outerIIFEBody int(0..) = 1
|
|
267
|
+
option flat_ternary_expressions boolean = false
|
|
268
|
+
option offset_ternary_expressions boolean = false "extra depth for grouped ternary branches"
|
|
269
|
+
|
|
270
|
+
indent in ts_block, ts_interface, ts_namespace.body
|
|
271
|
+
indent in ts_class from indent_class_head
|
|
272
|
+
indent ts_class.token from if is (ts_open_brace, ts_close_brace) then parent.indent_class_head else if contains(["class", "abstract", "declare"], text) then parent.offset else offset by 0
|
|
273
|
+
indent ts_enum.token from if is ts_open_brace then parent.offset else offset by 0
|
|
274
|
+
indent indent_generic_arguments.token, indent_generic_parameters.token from if offset == parent.indent_angle_close then parent.indent_angle_open else offset by 0
|
|
275
|
+
indent in ts_array, ts_array_pattern, ts_tuple_type by indent_numeric(array_expression)
|
|
276
|
+
indent in ts_object, ts_object_pattern, ts_enum, ts_type_literal by indent_numeric(object_expression)
|
|
277
|
+
indent in ts_parenthesized, ts_parenthesized_type, ts_computed_key, ts_mapped_type
|
|
278
|
+
indent ts_template.expression from indent_template_code_anchor(parent.node, index, mode, tab_length, false) by if { value parent.node; indent_template_parts index; indent_template_relative } then 1 else 0
|
|
279
|
+
indent ts_template.quasi from if index == 0 then offset else indent_template_code_anchor(parent.node, index - 1, mode, tab_length, true) by 0
|
|
280
|
+
indent in ts_index from indent_index_open
|
|
281
|
+
indent ts_index.token from if text == "]" then parent.indent_index_open else if text == "?." then parent.offset else parent.optional.offset.nonempty ?? parent.offset by if text == "]" then 0 else indent_numeric(member_expression)
|
|
282
|
+
indent in indent_calls from indent_call_anchor by { value indent_arguments_offset.nonempty ?? call_expression; indent_numeric }
|
|
283
|
+
indent indent_calls.token from if text == "?." then indent_optional_call_anchor(parent) else if is (ts_open_paren, ts_close_paren) then parent.indent_call_anchor else offset by if text == "?." then 1 else 0
|
|
284
|
+
indent in ts_function.body, ts_arrow.body, ts_method.body from indent_function_anchor(parent.indent_body_root) by if parent.indent_outer_iife then parent.indent_body_offset.nonempty ?? indent_numeric(outerIIFEBody) else indent_function_offset(parent, parent.indent_body_offset, function_declaration_body, function_expression_body)
|
|
285
|
+
indent in ts_static_block.body by parent.indent_body_offset.nonempty ?? static_block
|
|
286
|
+
indent ts_function.parameter, ts_arrow.parameter, ts_method.parameter by {
|
|
287
|
+
value parent
|
|
288
|
+
indent_function_offset parent.indent_parameters_offset function_declaration_parameters function_expression_parameters
|
|
289
|
+
indent_numeric
|
|
290
|
+
}
|
|
291
|
+
indent ts_function.return_type from parent.indent_return_anchor by indent_function_offset(parent, parent.indent_return_type_offset, function_declaration_return_type, function_expression_return_type)
|
|
292
|
+
indent ts_function.token from if is ts_close_paren then indent_function_anchor(parent.indent_body_root) else if offset == parent.indent_return_colon then parent.indent_return_anchor else offset by if is ts_close_paren then 0 else indent_function_offset(parent, parent.indent_return_type_offset, function_declaration_return_type, function_expression_return_type)
|
|
293
|
+
indent ts_arrow.token, ts_method.token from if is ts_close_paren then indent_function_anchor(parent.indent_body_root) else offset by 0
|
|
294
|
+
indent ts_arrow.return_type, ts_parameter.type
|
|
295
|
+
indent ts_call.argument, ts_new.argument, ts_import_call.argument from parent.indent_call_anchor by { value parent.indent_arguments_offset.nonempty ?? call_expression; indent_numeric }
|
|
296
|
+
indent ts_switch.case by switch_case
|
|
297
|
+
indent ts_case.body by if is ts_block && count(parent.body.nodes) == 1 then 0 else 1
|
|
298
|
+
indent ts_variable_declaration.declaration class declarators by {
|
|
299
|
+
value parent
|
|
300
|
+
style: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
301
|
+
value indent_declaration_level(parent, style, mode)
|
|
302
|
+
}
|
|
303
|
+
indent in ts_variable_declaration class declarators by {
|
|
304
|
+
value node
|
|
305
|
+
style: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
306
|
+
value indent_declaration_level(node, style, mode)
|
|
307
|
+
}
|
|
308
|
+
indent ts_declarator.token from token_neighbours.before.start by if text == "=" then indent_numeric(assignment_operator) else 0
|
|
309
|
+
indent ts_member.property, ts_member.token, ts_member.optional, ts_index.optional, ts_meta_property.token by indent_numeric(member_expression)
|
|
310
|
+
indent ts_call.optional from indent_optional_call_anchor(parent)
|
|
311
|
+
indent ts_import.specifier by indent_numeric(import_declaration)
|
|
312
|
+
indent ts_import.token, ts_export.token from if parent.indent_module_tail != null && offset >= parent.indent_module_tail && node not ts_semicolon then parent.offset else offset
|
|
313
|
+
indent ts_import.from, ts_export.from from if parent.indent_module_from != null then parent.offset else offset
|
|
314
|
+
indent ts_export.specifier
|
|
315
|
+
indent ts_assignment.right, ts_assignment_pattern.right by indent_numeric(assignment_operator)
|
|
316
|
+
indent ts_class_property.key from if offset != parent.offset && (is ts_computed_key || first(parent.decorator.nodes) == null) then parent.offset else offset by if is ts_computed_key then 0 else 1
|
|
317
|
+
indent ts_class_property.token from if text == "=" then token_neighbours.before.start else if text == ";" then parent.init.token_neighbours.before.start.nonempty ?? parent.key.last_token.start else offset by if text == ";" then 1 else indent_numeric(assignment_operator)
|
|
318
|
+
indent ts_class_property.init from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
319
|
+
indent ts_declarator.init from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
320
|
+
indent ts_import_equals.value, ts_type_parameter.default from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
321
|
+
indent ts_import_equals.token, ts_type_parameter.token from if text == "=" then token_neighbours.before.start else offset by indent_numeric(assignment_operator)
|
|
322
|
+
indent ts_arrow.body from indent_function_anchor(parent.indent_body_root) by if is ts_block then 0 else 1
|
|
323
|
+
indent ts_property.value, ts_return.argument, ts_throw.argument
|
|
324
|
+
indent ts_conditional.consequent, ts_conditional.alternate by {
|
|
325
|
+
value statement: containing(ts_statement)
|
|
326
|
+
value indent_ternary_level(node, flat_ternary_expressions, offset_ternary_expressions, statement)
|
|
327
|
+
}
|
|
328
|
+
# An anchor at the branch itself skips this candidate; own-line branches keep ordinary collapsing.
|
|
329
|
+
indent ts_conditional.consequent, ts_conditional.alternate from if position(first_token.start).js_line == position(token_neighbours.before.start).js_line && position(parent.offset).js_line < position(first_token.start).js_line then token_neighbours.before.start else offset class branches by {
|
|
330
|
+
value statement: containing(ts_statement)
|
|
331
|
+
value indent_ternary_anchor_level(node, flat_ternary_expressions, offset_ternary_expressions, statement)
|
|
332
|
+
}
|
|
333
|
+
indent ts_conditional.token by if flat_ternary_expressions && { value statement: containing(ts_statement); value indent_flat_ternary(parent, statement) } then 0 else 1
|
|
334
|
+
indent ts_conditional_type.consequent, ts_conditional_type.alternate, ts_conditional_type.token by if parent is ts_conditional_type.alternate && position(parent.extends.last_token.end).js_line == position(parent.consequent.first_token.start).js_line then 0 else 1
|
|
335
|
+
indent in ts_jsx_element, ts_jsx_fragment
|
|
336
|
+
indent in ts_jsx_opening
|
|
337
|
+
indent in ts_jsx_expression, ts_jsx_spread_attribute
|
|
338
|
+
indent ts_jsx_element.body, ts_jsx_fragment.body
|
|
339
|
+
indent ts_jsx_element.closing, ts_jsx_fragment.closing from parent.offset by 0
|
|
340
|
+
indent in ts_jsx_fragment.closing from parent.offset by 1
|
|
341
|
+
indent ts_jsx_closing.name from token_neighbours.before.start
|
|
342
|
+
indent ts_jsx_opening.token, ts_jsx_closing.token from parent.parent.offset by indent_jsx_token_level(node)
|
|
343
|
+
indent ts_jsx_attribute.token from parent.offset by indent_numeric(assignment_operator)
|
|
344
|
+
indent ts_jsx_attribute.value from token_neighbours.before.start by indent_numeric(assignment_operator)
|
|
345
|
+
indent ts_function_type.parameter, ts_method_signature.parameter, ts_call_signature.parameter
|
|
346
|
+
indent ts_function_type.return_type, ts_method_signature.return_type, ts_call_signature.return_type
|
|
347
|
+
indent indent_generic_arguments.type_argument, indent_generic_parameters.type_parameter from parent.indent_angle_open
|
|
348
|
+
indent ts_type_parameter.constraint
|
|
349
|
+
indent ts_for.init, ts_for.test, ts_for.update
|
|
350
|
+
indent ts_if.test, ts_while.test, ts_do_while.test, ts_with.object from token_neighbours.before.start
|
|
351
|
+
# A forward anchor leaves non-block interiors under their existing owners.
|
|
352
|
+
indent in ts_if.consequent, ts_if.alternate, ts_for.body, ts_for_in.body, ts_for_of.body, ts_while.body, ts_do_while.body, ts_with.body from if is ts_block then parent.offset else end_offset
|
|
353
|
+
indent ts_if.consequent, ts_if.alternate, ts_for.body, ts_for_in.body, ts_for_of.body, ts_while.body, ts_do_while.body, ts_with.body from if is ts_empty then node.indent_semicolon_control else null by if is ts_block || is ts_empty && node.indent_semicolon_control != null then 0 else 1
|
|
354
|
+
indent ts_semicolon from indent_semicolon_control.nonempty ?? parent.node by if indent_semicolon_control == null && parent is (ts_block, ts_class, ts_interface, ts_type_literal) then 1 else 0
|
|
355
|
+
indent ts_call.argument, ts_new.argument, ts_function.parameter, ts_arrow.parameter, ts_method.parameter, ts_array.element, ts_array_pattern.element, ts_tuple_type.element, ts_object.property, ts_object_pattern.property, ts_import.specifier from indent_list_anchor(node) by 0
|
|
356
|
+
|
|
357
|
+
when trim(text) != ""
|
|
358
|
+
when indent_checked
|
|
359
|
+
let first_offset offset + byte_len(indent)
|
|
360
|
+
let first_nodes overlapping(ts_node, first_offset, first_offset + 1)
|
|
361
|
+
let semicolon { value overlapping(ts_semicolon, first_offset, first_offset + 1); first }
|
|
362
|
+
when semicolon == null || !semicolon.indent_tail_unchecked
|
|
363
|
+
let jsx_member { value overlapping(indent_jsx_name_member, first_offset, first_offset + 1); first }
|
|
364
|
+
let list_item indent_first_list_item(first_nodes, first_offset)
|
|
365
|
+
let list_mode if list_item == null then 1 else {
|
|
366
|
+
value list_item.parent
|
|
367
|
+
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
368
|
+
value indent_list_mode(list_item, call_expression, function_declaration_parameters, function_expression_parameters, array_expression, object_expression, import_declaration, variable)
|
|
369
|
+
}
|
|
370
|
+
let binary_right { value overlapping(ts_binary.right, first_offset, first_offset + 1); first }
|
|
371
|
+
let logical_right { value overlapping(ts_logical.right, first_offset, first_offset + 1); first }
|
|
372
|
+
let binary_operator { value overlapping(ts_binary.operator, first_offset, first_offset + 1); first }
|
|
373
|
+
let logical_operator { value overlapping(ts_logical.operator, first_offset, first_offset + 1); first }
|
|
374
|
+
when binary_right == null || binary_right.first_token.start != first_offset
|
|
375
|
+
when logical_right == null || logical_right.first_token.start != first_offset
|
|
376
|
+
when binary_operator == null && logical_operator == null
|
|
377
|
+
let type_operator { value overlapping(ts_union.token, first_offset, first_offset + 1); first }
|
|
378
|
+
let intersection_operator { value overlapping(ts_intersection.token, first_offset, first_offset + 1); first }
|
|
379
|
+
when type_operator.text != "|" && intersection_operator.text != "&"
|
|
380
|
+
when !any(first_nodes, item => item is (ts_union.type, ts_intersection.type) && item.first_token.start == first_offset)
|
|
381
|
+
let quasi { value overlapping(ts_template.quasi, first_offset, first_offset + 1); first }
|
|
382
|
+
let comment { value overlapping(ts_comment, first_offset, first_offset + 1); first }
|
|
383
|
+
let block_comment if comment is ts_block_comment then comment else null
|
|
384
|
+
let string_literal { value overlapping(ts_string, first_offset, first_offset + 1); first }
|
|
385
|
+
let attribute_literal { value overlapping(ts_import_attribute_string, first_offset, first_offset + 1); first }
|
|
386
|
+
let jsx_text { value overlapping(ts_jsx_text, first_offset, first_offset + 1); first }
|
|
387
|
+
when quasi == null || position(quasi.offset).js_line == position(first_offset).js_line
|
|
388
|
+
when block_comment == null || position(block_comment.offset).js_line == position(first_offset).js_line
|
|
389
|
+
when string_literal == null || position(string_literal.offset).js_line == position(first_offset).js_line
|
|
390
|
+
when attribute_literal == null || position(attribute_literal.offset).js_line == position(first_offset).js_line
|
|
391
|
+
let unit if mode == "tab" then "\t" else repeat(" ", mode)
|
|
392
|
+
let comment_before if comment == null then null else indent_node_at(comment.token_neighbours.before.start)
|
|
393
|
+
let comment_after if comment == null then null else indent_node_at(comment.token_neighbours.after.start)
|
|
394
|
+
let comment_switch comment.indent_comment_switch
|
|
395
|
+
let comment_last_case last(comment_switch.case.nodes)
|
|
396
|
+
when comment_last_case == null || comment.offset < comment_last_case.end_offset
|
|
397
|
+
let comment_empty_switch comment_switch != null && comment_last_case == null && comment.offset > comment_switch.indent_switch_open.offset
|
|
398
|
+
let comment_list comment.indent_comment_list
|
|
399
|
+
let comment_next { value comment_list.indent_items; filter (item) item.first_token.start >= comment.end_offset; first }
|
|
400
|
+
let comment_list_mode {
|
|
401
|
+
value comment_next.parent
|
|
402
|
+
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
403
|
+
value indent_list_mode(comment_next, call_expression, function_declaration_parameters, function_expression_parameters, array_expression, object_expression, import_declaration, variable)
|
|
404
|
+
}
|
|
405
|
+
let comment_list_range comment_next.indent_list_previous != null && comment_next.indent_list_previous.last_token.end <= comment.offset && position(comment_next.indent_list_previous.last_token.end).js_line > position(comment_list.indent_list_open.offset).js_line && !(comment_list_mode == "first" && position(comment_next.first_token.start).js_column == len(comment_next.indent) + 1)
|
|
406
|
+
let before_wanted indent_expected(comment_before, unit)
|
|
407
|
+
let comment_policy if comment == null then null else { comment: comment, after: comment_after, next: comment_next, range: comment_list_range, switch: comment_switch, empty_switch: comment_empty_switch }
|
|
408
|
+
# Only line-leading declaration semicolons are unchecked; inline ones retain their declaration offset.
|
|
409
|
+
let after_wanted {
|
|
410
|
+
value comment_after.parent
|
|
411
|
+
variable: indent_variable_mode variable_declarator variable_declarator_var variable_declarator_let variable_declarator_const variable_declarator_using
|
|
412
|
+
value indent_comment_after(comment_policy, unit, variable)
|
|
413
|
+
}
|
|
414
|
+
when comment == null || comment_list_range || !(comment_before != null && comment.space_before.blank_gaps == 0 && indent == before_wanted || comment_after != null && comment.space_after.blank_gaps == 0 && indent == after_wanted)
|
|
415
|
+
let aligned_first if list_mode == "first" && list_item.offset != list_item.indent_list_first.offset then list_item.indent_list_first else null
|
|
416
|
+
let aligned_line_start position(aligned_first.first_token.start).js_line_start
|
|
417
|
+
let aligned_quasi if aligned_first == null then null else {
|
|
418
|
+
value overlapping(ts_template.quasi, aligned_line_start, aligned_line_start + 1)
|
|
419
|
+
filter (item) position(item.offset).js_line < position(aligned_first.first_token.start).js_line
|
|
420
|
+
first
|
|
421
|
+
}
|
|
422
|
+
let aligned_anchor if aligned_first == null then "" else if aligned_quasi != null then indent_expected(aligned_quasi, unit) else if position(aligned_first.first_token.start).js_line == position(aligned_first.parent.offset).js_line then list_item.parent_indent else indent_expected(aligned_first, unit)
|
|
423
|
+
let aligned_column if aligned_quasi == null then len(aligned_first.indent) else position(aligned_quasi.offset).js_column - 1
|
|
424
|
+
let comment_wanted if comment_policy == null then null else indent_comment_wanted(comment_policy, after_wanted, unit, switch_case)
|
|
425
|
+
let attribute_node indent_attribute_node(first_offset)
|
|
426
|
+
# Raw JSX text uses the tag line's actual indentation, not virtual code-token offsets.
|
|
427
|
+
let wanted if jsx_text != null then jsx_text.parent.indent unit else if jsx_member != null then indent_expected(jsx_member.indent_jsx_closing_anchor, unit) { value member_expression; levels: indent_numeric; value indent_steps(unit, levels) } else if type_of(comment_wanted) != "null" then comment_wanted else if attribute_node != null then indent_expected(attribute_node, unit) else if aligned_first != null then aligned_anchor {
|
|
428
|
+
value result: if mode == "tab" then "\t" else " "
|
|
429
|
+
value result_2: position(aligned_first.first_token.start).js_column - 1 - aligned_column
|
|
430
|
+
value repeat(result, result_2)
|
|
431
|
+
} else indent_anchor indent_steps(unit, indent_level)
|
|
432
|
+
when indent != wanted
|
|
433
|
+
let spaces { value indent; replace "\t" ""; len }
|
|
434
|
+
let tabs { value indent; replace " " ""; len }
|
|
435
|
+
let unit_name if mode == "tab" then "tab" else "space"
|
|
436
|
+
let actual if spaces > 0 then "{spaces}{if mode == 'tab' then if spaces == 1 then ' space' else ' spaces' else ''}" else if tabs > 0 then "{tabs}{if mode != 'tab' then if tabs == 1 then ' tab' else ' tabs' else ''}" else "0"
|
|
437
|
+
report at if jsx_text != null then jsx_text.node else { start: offset, end: offset + byte_len(indent) }
|
|
438
|
+
fix at { start: offset, end: offset + byte_len(indent) } as wanted
|
|
439
|
+
warn "Expected indentation of {len(wanted)} {unit_name}{if len(wanted) == 1 then '' else 's'} but found {actual}."
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
}
|