@inthedark/dxlint 0.0.0-stage → 0.1.1

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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
package/std/ts/jsx.dx ADDED
@@ -0,0 +1,646 @@
1
+ # .ts/jsx: @stylistic's JSX rules (spacing, wrapping, props, closing tags),
2
+ # on the native `ts_jsx_*` layers.
3
+
4
+ import .ts/expression-fixes
5
+
6
+ define {
7
+
8
+ in ts_node, ts_open_angle, ts_close_angle, ts_punctuator let jsx_line_prefix { value js_line_start: position(offset).js_line_start; value byte_slice(source, js_line_start, offset) }
9
+ fn jsx_indent(prefix) = { value { value result: "^" jsx_space_regex() "*"; value re_find(prefix, result); nonempty } ?? ""; len }
10
+ fn jsx_indent_operator(text) = re_test(text, /^[ \t]*[?:]/)
11
+ fn jsx_indent_spaces(text) = { value { value text; re_find /^ */; nonempty } ?? ""; len }
12
+ fn jsx_indent_tabs(text) = { value { value text; re_find /^\t*/; nonempty } ?? ""; len }
13
+ in ts_jsx_opening {
14
+ let jsx_prop_indent_operator jsx_indent_operator(jsx_line_prefix)
15
+ let jsx_prop_indent_first_column { value first(property.nodes).offset; position }.js_column - 1
16
+ }
17
+ in ts_jsx_attribute, ts_jsx_spread_attribute {
18
+ let jsx_prop_indent_line {
19
+ value js_line_start: position(offset).js_line_start
20
+ value byte_slice(source, js_line_start, end_offset)
21
+ re_find /^[^\n\r\u{2028}\u{2029}]*/
22
+ nonempty
23
+ } ?? ""
24
+ let jsx_prop_indent_operator jsx_indent_operator(jsx_prop_indent_line)
25
+ let jsx_prop_indent_extra previous.jsx_prop_indent_extra == true || !jsx_prop_indent_operator && !contains(jsx_prop_indent_line, "<") && (if previous == null then parent.jsx_prop_indent_operator else previous.jsx_prop_indent_operator)
26
+ }
27
+ in ts_jsx_attribute, ts_jsx_spread_attribute rule es-jsx-indent-props {
28
+ option mode int | "tab" | "first" = 4 "prop indent size or alignment"
29
+ let size if mode == "tab" then 1 else mode
30
+ let base_indent if mode == "tab" then jsx_indent_tabs(parent.jsx_line_prefix) else jsx_indent_spaces(parent.jsx_line_prefix)
31
+ let actual if mode == "tab" then jsx_indent_tabs(jsx_line_prefix) else jsx_indent_spaces(jsx_line_prefix)
32
+ let expected if mode == "first" then parent.jsx_prop_indent_first_column else base_indent + size * (if jsx_prop_indent_extra then 2 else 1)
33
+ when parent is ts_jsx_opening && jsx_trim_space(jsx_line_prefix) == "" && actual != expected
34
+ report at if is ts_jsx_attribute then name.node else argument.node
35
+ fix at { start: offset - byte_len(jsx_line_prefix), end: offset } if expected >= 0 as { value if mode == "tab" then "\t" else " "; repeat expected }
36
+ warn "Expected indentation of {expected} {if mode == 'tab' then 'tab' else 'space'} {if expected == 1 then 'character' else 'characters'} but found {actual}."
37
+ }
38
+ in ts_node {
39
+ let jsx_child_leading is ts_jsx_text && { value result: "^" jsx_space_regex() "*\n"; value re_test(text, result) }
40
+ let jsx_child_trailing is ts_jsx_text && { value result: "\n" jsx_space_regex() "*$"; value re_test(text, result) }
41
+ let jsx_child_row not ts_jsx_text || jsx_trim_space(text) != ""
42
+ let jsx_child_first_line position(offset).js_line + (if jsx_child_leading then 1 else 0)
43
+ let jsx_child_last_line position(end_offset).js_line - (if jsx_child_trailing then 1 else 0)
44
+ let jsx_child_previous_row if previous == null then null else if previous.jsx_child_row then {first: previous.jsx_child_first_line, last: previous.jsx_child_last_line} else previous.jsx_child_previous_row
45
+ let jsx_child_next_row if next == null then null else if next.jsx_child_row then {first: next.jsx_child_first_line, last: next.jsx_child_last_line} else next.jsx_child_next_row
46
+ let jsx_child_previous_content_expression if previous == null then false else if previous.jsx_child_row then previous is ts_jsx_expression else previous.jsx_child_previous_content_expression
47
+ let jsx_child_next_content_expression if next == null then false else if next.jsx_child_row then next is ts_jsx_expression else next.jsx_child_next_content_expression
48
+ let jsx_child_multiline position(offset).js_line != position(end_offset).js_line
49
+ let jsx_child_next_multiline if jsx_child_newline_eligible == true then jsx_child_multiline else if next != null then next.jsx_child_next_multiline else false
50
+ }
51
+ in ts_jsx_element {
52
+ let jsx_child_inline_name if contains(["a", "abbr", "acronym", "b", "bdo", "big", "button", "cite", "code", "dfn", "em", "i", "img", "input", "kbd", "label", "map", "object", "q", "samp", "script", "select", "small", "span", "strong", "sub", "sup", "textarea", "tt", "var"], opening.name) then opening.name else null
53
+ let jsx_child_descriptor if !re_test(opening.name, /[.:]/) then opening.name else replace(text, "\n", "")
54
+ let jsx_child_newline_eligible true
55
+ }
56
+ in ts_jsx_expression let jsx_child_newline_eligible expression not ts_spread && !{
57
+ value text
58
+ jsx_trim_space: jsx_trim_space
59
+ value result: "{{/*"
60
+ value starts_with(jsx_trim_space, result)
61
+ }
62
+ in ts_jsx_expression, ts_jsx_fragment, ts_jsx_text let jsx_child_descriptor replace(text, "\n", "")
63
+ in ts_jsx_text {
64
+ let jsx_child_preceding_eligible previous.jsx_child_newline_eligible == true
65
+ let jsx_child_preceding_multiline previous.jsx_child_multiline == true
66
+ }
67
+ in ts_jsx_element, ts_jsx_fragment {
68
+ let jsx_one_expression_per_line_enabled true
69
+ let jsx_child_rows {
70
+ value body.nodes
71
+ filter (item) item.jsx_child_row
72
+ map (item) {index: item.index, first: item.jsx_child_first_line, last: item.jsx_child_last_line}
73
+ }
74
+ let jsx_child_one_group count(jsx_child_rows) > 0 && all(jsx_child_rows, row => row.first == first(jsx_child_rows).first && row.last == first(jsx_child_rows).first)
75
+ let jsx_child_first first(body.nodes)
76
+ let jsx_child_last last(body.nodes)
77
+ let jsx_child_single_line_allowed (position(jsx_child_last.end_offset).js_line - position(jsx_child_first.offset).js_line == 0 && !jsx_child_first.jsx_child_leading && !jsx_child_last.jsx_child_trailing) || (position(jsx_child_last.end_offset).js_line - position(jsx_child_first.offset).js_line == 2 && jsx_child_first.jsx_child_leading && jsx_child_last.jsx_child_trailing)
78
+ let jsx_child_single count(body) == 1 && jsx_child_first.jsx_child_multiline == false && position(offset).js_line == position(end_offset).js_line
79
+ let jsx_child_literal count(body) == 1 && jsx_child_first is ts_jsx_text
80
+ let jsx_child_has_jsx any(body.nodes, item => item is (ts_jsx_element, ts_jsx_fragment))
81
+ }
82
+ fn jsx_child_group_contains(row, line) = row.first == line || row.last == line
83
+ in ts_jsx_element.body, ts_jsx_fragment.body rule es-jsx-one-expression-per-line {
84
+ option mode "always" | "single-child" | "single-line" = "always" "same-line child layout"
85
+ let opening_line position(parent.opening.end_offset).js_line
86
+ let closing_line { value parent.closing.offset.nonempty ?? parent.opening.end_offset; position }.js_line
87
+ let first_line jsx_child_first_line
88
+ let last_line jsx_child_last_line
89
+ let first_wrong jsx_child_previous_row != null && jsx_child_group_contains(jsx_child_previous_row, first_line) || first_line == opening_line || first_line == closing_line && !(jsx_child_next_row != null && jsx_child_group_contains(jsx_child_next_row, first_line))
90
+ let last_wrong jsx_child_previous_row != null && jsx_child_group_contains(jsx_child_previous_row, last_line) || last_line == opening_line || last_line == closing_line && !(jsx_child_next_row != null && jsx_child_group_contains(jsx_child_next_row, last_line))
91
+ let single_group_wrong index == first(parent.jsx_child_rows).index && first_line == opening_line || index == last(parent.jsx_child_rows).index && last_line == closing_line
92
+ when jsx_child_row
93
+ when parent.jsx_one_expression_per_line_enabled
94
+ when !(mode == "single-child" && parent.jsx_child_single || mode == "single-line" && parent.jsx_child_single_line_allowed)
95
+ when if mode == "single-line" && parent.jsx_child_one_group then single_group_wrong else first_wrong || last_wrong
96
+ let single_group mode == "single-line" && parent.jsx_child_one_group
97
+ let leading if single_group then index == first(parent.jsx_child_rows).index && first_line == opening_line else jsx_child_previous_row != null && (jsx_child_group_contains(jsx_child_previous_row, first_line) || jsx_child_group_contains(jsx_child_previous_row, last_line)) || first_line == opening_line || last_line == opening_line
98
+ let trailing if single_group then index == last(parent.jsx_child_rows).index && last_line == closing_line else first_line == closing_line && !(jsx_child_next_row != null && jsx_child_group_contains(jsx_child_next_row, first_line)) || last_line == closing_line && !(jsx_child_next_row != null && jsx_child_group_contains(jsx_child_next_row, last_line))
99
+ let leading_space starts_with(text, " ") || previous is ts_jsx_text && ends_with(previous.text, " ")
100
+ let trailing_space ends_with(text, " ") || next is ts_jsx_text && starts_with(next.text, " ")
101
+ let space_expression "{{' '}}"
102
+ let leading_text if leading_space && leading then "\n{space_expression}" else ""
103
+ let trailing_text if trailing_space && trailing then "{space_expression}\n" else ""
104
+ fix at {start: if single_group && !leading then end_offset else offset, end: if single_group && !trailing then offset else end_offset} as if single_group then "{if leading then '\n' else ''}{if leading && trailing then text else ''}{if trailing then '\n' else ''}" else "{leading_text}{if leading then '\n' else ''}{re_replace(text, /(^ +| +$)/, '')}{if trailing then '\n' else ''}{trailing_text}"
105
+ report at node
106
+ warn "`{jsx_child_descriptor}` must be placed on a new line"
107
+ }
108
+ fn jsx_newline_text(text, remove) = {
109
+ value separator: if remove then "\n\n" else "\n"
110
+ value parts: split(text, separator)
111
+ value result: {
112
+ value parts
113
+ fold ({ index: 0, text: "" }) (state, part) {
114
+ index: state.index + 1,
115
+ text: state.text (if state.index == 0 then "" else if remove then {
116
+ value if state.index == len(parts) - 1 || contains(part, "\n") then "\n" else "\n\n"
117
+ } else if state.index == len(parts) - 1 then "\n\n" else "\n") part
118
+ }
119
+ }
120
+ value result.text
121
+ }
122
+ in ts_jsx_element.body, ts_jsx_fragment.body rule es-jsx-newline {
123
+ option prevent boolean = false "prevent blank lines between children"
124
+ option allowMultilines boolean = false "keep blank lines around multiline children"
125
+ let multiline previous.jsx_child_preceding_multiline == true || jsx_child_next_multiline
126
+ let blank { value result: "\n" jsx_space_regex() "*\n"; value re_test(previous.text, result) }
127
+ when previous is ts_jsx_text && previous.jsx_child_preceding_eligible
128
+ when if allowMultilines && multiline then !blank else blank == prevent
129
+ report at previous.node.end_offset
130
+ warn if allowMultilines && multiline then "Multiline JSX elements should start in a new line" else if prevent then "JSX element should not start in a new line" else "JSX element should start in a new line"
131
+ let remove prevent && !(allowMultilines && multiline)
132
+ let fixed jsx_newline_text(previous.text, remove)
133
+ fix at previous.node if fixed != previous.text as fixed
134
+ }
135
+ in ts_jsx_text rule es-jsx-child-element-spacing {
136
+ let after { value result: "^" jsx_space_regex() "*\n"; value re_test(text, result) } && jsx_trim_space(text) != ""
137
+ let before jsx_trim_space(text) != "" && { value result: "\n" jsx_space_regex() "*$"; value re_test(text, result) }
138
+ let preceding after && previous != null
139
+ when (previous != null || next != null) && (previous == null || previous.jsx_child_inline_name != null) && (next == null || next.jsx_child_inline_name != null)
140
+ when preceding || before && next != null
141
+ report at if preceding then node.offset else node.end_offset
142
+ warn "Ambiguous spacing {if preceding then 'after previous' else 'before next'} element {if preceding then previous.jsx_child_inline_name else next.jsx_child_inline_name}"
143
+ }
144
+ schema jsx_wrap_mode boolean | "ignore" | "parens" | "parens-new-line"
145
+ fn jsx_wrap_active(value) = value != false && value != "ignore"
146
+ in ts_jsx_element let jsx_wrap_element true
147
+ in ts_parenthesized let jsx_wrap_element expression.jsx_wrap_element == true
148
+ in ts_jsx_expression let jsx_prop_expression parent is ts_jsx_attribute
149
+ in ts_parenthesized, ts_jsx_element, ts_jsx_fragment, ts_conditional {
150
+ let jsx_wrap_context if parent is ts_parenthesized then parent.jsx_wrap_context else if is ts_declarator.init then "declaration" else if is ts_assignment.right then "assignment" else if is ts_return.argument then "return" else if is ts_arrow.body then "arrow" else if is ts_logical.right then "logical" else if is (ts_conditional.consequent, ts_conditional.alternate) then "condition" else if is ts_jsx_expression.expression && parent.jsx_prop_expression then "prop" else if jsx_wrap_element && is ts_property.value then "propertyValue" else null
151
+ let jsx_wrap_fallback if parent is ts_parenthesized then parent.jsx_wrap_fallback else if parent is ts_conditional then parent.jsx_wrap_context else null
152
+ let jsx_call_argument if parent is ts_parenthesized then parent.jsx_call_argument else is (ts_call.argument, ts_new.argument)
153
+ }
154
+ fn jsx_wrap_edit(source, n, selected, parenthesized, opening, closing) = {
155
+ value newline: selected == "parens-new-line"
156
+ value before: n.token_neighbours.before
157
+ value after: n.token_neighbours.after
158
+ value if parenthesized then {
159
+ start: n.offset, end: n.end_offset,
160
+ text: (if opening then "\n" else "") n.text (if closing then "\n" else "")
161
+ } else if !newline then { start: n.offset, end: n.end_offset, text: "(" n.text ")" }
162
+ else if before != null && position(before.end).js_line < position(n.offset).js_line then {
163
+ value comments: { value source; byte_slice before.end n.offset; trim }
164
+ value tab: starts_with(n.jsx_line_prefix, "\t")
165
+ value unit: if tab then "\t" else " "
166
+ value column: position(n.offset).js_column - 1
167
+ value tail_size: column - (if tab then 1 else 2)
168
+ value tail_column: max(0, tail_size)
169
+ value indent: repeat(unit, column)
170
+ value tail_indent: repeat(unit, tail_column)
171
+ value leading: trim(before.text) (if contains(["{{", "["], before.text) then "" else " ")
172
+ value {
173
+ start: before.start,
174
+ end: if after != null && contains([";", "}}"], after.text) then after.start else n.end_offset,
175
+ text: leading "(\n" indent comments (if comments != "" then "\n" indent else "") n.text "\n" tail_indent ")"
176
+ }
177
+ } else { start: n.offset, end: n.end_offset, text: "(\n" n.text "\n)" }
178
+ }
179
+ in ts_jsx_element, ts_jsx_fragment rule es-jsx-wrap-multilines {
180
+ option declaration jsx_wrap_mode = "parens" "variable initializer wrapping"
181
+ option assignment jsx_wrap_mode = "parens" "assignment wrapping"
182
+ option return jsx_wrap_mode = "parens" "return expression wrapping"
183
+ option arrow jsx_wrap_mode = "parens" "arrow body wrapping"
184
+ option condition jsx_wrap_mode = "ignore" "conditional branch wrapping"
185
+ option logical jsx_wrap_mode = "ignore" "logical right-hand side wrapping"
186
+ option prop jsx_wrap_mode = "ignore" "JSX prop expression wrapping"
187
+ option propertyValue jsx_wrap_mode = "ignore" "object property value wrapping"
188
+ let context if jsx_wrap_context == "condition" && !jsx_wrap_active(condition) && contains(["declaration", "assignment"], jsx_wrap_fallback) then jsx_wrap_fallback else jsx_wrap_context
189
+ let selected {
190
+ value {declaration: declaration, assignment: assignment, return: return, arrow: arrow, condition: condition, logical: logical, prop: prop, propertyValue: propertyValue}
191
+ get context
192
+ nonempty
193
+ } ?? "ignore"
194
+ let parenthesized parent is ts_parenthesized
195
+ when jsx_break(text) && jsx_wrap_active(selected)
196
+ when !parenthesized || selected == "parens-new-line" && (!space_before.line_break || !space_after.line_break)
197
+ warn if !parenthesized then "Missing parentheses around multilines JSX" else "Parentheses around JSX should be on separate lines"
198
+ let edit jsx_wrap_edit(source, node, selected, parenthesized, !space_before.line_break, !space_after.line_break)
199
+ let original { offset: edit.start, end_offset: edit.end, text: byte_slice(source, edit.start, edit.end) }
200
+ fix at edit if expression_fix_retains_comments(original, edit.text) as edit.text
201
+ }
202
+ in ts_jsx_element, ts_jsx_fragment rule es-jsx-function-call-newline {
203
+ option mode "always" | "multiline" = "multiline" "call argument linebreak policy"
204
+ when jsx_call_argument && (mode == "always" || jsx_break(text))
205
+ when !space_before.line_break || next_token not ts_comma && !space_after.line_break
206
+ warn "Missing line break around JSX"
207
+ fix at node as (if !space_before.line_break then "\n" else "") text (if next_token not ts_comma && !space_after.line_break then "\n" else "")
208
+ }
209
+ fn jsx_presence_whitespace(value) = value != "" && jsx_trim_space(value) == ""
210
+ fn jsx_presence_edge(value) = { value result: "^" jsx_space_regex() "|" jsx_space_regex() "$"; value re_test(value, result) }
211
+ fn jsx_presence_entities(value) = re_replace(value, /&[a-zA-Z0-9#]+;/, "")
212
+ fn jsx_presence_quote(raw) = {
213
+ value escaped: {
214
+ value raw
215
+ re_replace /\\/ "\\\\"
216
+ re_replace /"/ "\\\""
217
+ re_replace /\u{8}/ "\\b"
218
+ re_replace /\u{c}/ "\\f"
219
+ re_replace /\n/ "\\n"
220
+ re_replace /\r/ "\\r"
221
+ re_replace /\t/ "\\t"
222
+ }
223
+ value "\"" escaped "\""
224
+ }
225
+ fn jsx_presence_content(raw) = { value end: byte_len(raw) - 1; value byte_slice(raw, 1, end) }
226
+ fn jsx_presence_wrap(raw) = if raw == "" then "" else "{{" jsx_presence_quote(raw) "}}"
227
+ in ts_jsx_text layer jsx_presence_lines match jsx_presence_line /[^\n]+|\n/
228
+ in jsx_presence_line layer jsx_presence_entities match jsx_presence_entity /&[a-zA-Z0-9#]+;/
229
+ fn jsx_presence_line_fix(line) = if jsx_trim_space(line.text) == "" then line.text else {
230
+ value prefix: jsx_edge_space(line.text, true)
231
+ value start: line.offset + byte_len(prefix)
232
+ value overlapping(jsx_presence_entity, start, line.end_offset)
233
+ filter (entity) start <= entity.offset && entity.end_offset <= line.end_offset
234
+ unique_by (entity) entity.offset
235
+ sort_by (entity) entity.offset
236
+ result: fold ({ end: start, text: prefix }) (state, entity) {
237
+ end: entity.end_offset,
238
+ text: state.text { value byte_slice(line.text, state.end - line.offset, entity.offset - line.offset); jsx_presence_wrap } entity.text
239
+ }
240
+ value result.text { value byte_slice(line.text, result.end - line.offset); jsx_presence_wrap }
241
+ }
242
+ fn jsx_presence_text_fix(n) = if !jsx_break(n.text) then jsx_presence_wrap(n.text) else {
243
+ value overlapping(jsx_presence_line, n.offset, n.end_offset)
244
+ filter (line) n.offset <= line.offset && line.end_offset <= n.end_offset
245
+ unique_by (line) line.offset
246
+ sort_by (line) line.offset
247
+ map (line) jsx_presence_line_fix(line)
248
+ join
249
+ }
250
+ fn jsx_presence_remove(inner, attribute) = if inner is (ts_jsx_element, ts_jsx_fragment) then inner.text
251
+ else if attribute then "\"" jsx_presence_content(inner.text) "\""
252
+ else inner.literal_value.value
253
+ in ts_parenthesized let jsx_presence_inner expression.jsx_presence_inner.nonempty ?? expression.node
254
+ in ts_jsx_expression {
255
+ let jsx_presence_expression_inner expression.jsx_presence_inner.nonempty ?? expression.node
256
+ let jsx_presence_comments { value descendants(ts_comment); count }
257
+ }
258
+ in ts_jsx_expression, ts_jsx_text, ts_string, ts_jsx_element, ts_jsx_fragment rule es-jsx-curly-brace-presence {
259
+ option mode "always" | "never" | "ignore" = "never" "default brace policy"
260
+ option props "always" | "never" | "ignore" | null = null "attribute literal brace policy"
261
+ option children "always" | "never" | "ignore" | null = null "child literal brace policy"
262
+ option propElementValues "always" | "never" | "ignore" = "ignore" "JSX element attribute brace policy"
263
+ let attribute is ts_jsx_attribute.value
264
+ let child is (ts_jsx_element.body, ts_jsx_fragment.body)
265
+ let expression_node is ts_jsx_expression
266
+ let inner jsx_presence_expression_inner
267
+ let element_value attribute && (is ts_jsx_element || inner is ts_jsx_element)
268
+ let selected { value (if attribute then props else children); nonempty } ?? mode
269
+ let string inner is ts_string
270
+ let template inner is ts_template && count(inner.expression) == 0
271
+ let value inner.literal_value.value.nonempty ?? ""
272
+ let spelling if template then { value result: len(inner.text) - 1; value slice(inner.text, 1, result) } else inner.text
273
+ let escape contains(spelling, "\\") || jsx_presence_entities(spelling) != spelling || child && re_test(spelling, /[{}<>]/)
274
+ let comment jsx_presence_comments > 0
275
+ let removable if element_value then true else if !comment && string then !escape && !contains(value, "/*") && (!attribute || !jsx_presence_whitespace(value)) && (attribute || !jsx_presence_edge(value)) && (child || !re_test(value, /['"]/)) else if !comment && template then !escape && !contains(spelling, "\n") && !jsx_presence_edge(spelling) && !re_test(value, /['"]/) else !comment && inner is (ts_jsx_element, ts_jsx_fragment)
276
+ let adjacent jsx_child_previous_content_expression || jsx_child_next_content_expression
277
+ let space_expression string && jsx_presence_whitespace(value)
278
+ let unnecessary removable && !(child && (adjacent || space_expression && (count(parent.body) == 1 || previous is ts_jsx_element || next is ts_jsx_element)))
279
+ let raw_line_break jsx_break(text) && jsx_trim_space(text) == ""
280
+ let missing element_value || !raw_line_break && { value text; jsx_presence_entities; jsx_trim_space } != ""
281
+ when attribute || child
282
+ when expression_node || element_value || is (ts_string, ts_jsx_text)
283
+ when if element_value then (if expression_node then propElementValues == "never" else propElementValues != "ignore") else if expression_node then selected == "never" && unnecessary else selected == "always" && missing
284
+ warn if expression_node then "Curly braces are unnecessary here." else "Need to wrap this literal in a JSX expression."
285
+ let replacement if expression_node then jsx_presence_remove(inner, attribute) else if element_value then "{{" text "}}" else if attribute then { value jsx_presence_content(text); jsx_presence_wrap } else jsx_presence_text_fix(node)
286
+ # Single-line entity text is wrapped whole, matching @stylistic's fix; multi-line text keeps entities outside braces via jsx_presence_line_fix.
287
+ let safe_literal !re_test(text, /[\u{0}-\u{7}\u{b}\u{e}-\u{1f}]/) && (expression_node || element_value || !(attribute && jsx_break(text)))
288
+ fix if safe_literal && !(element_value && !expression_node && propElementValues != "always") && expression_fix_retains_comments(node, replacement) as replacement
289
+ }
290
+
291
+ fn jsx_space_regex() = /[\t\n\u{b}\u{c}\r \u{a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]/
292
+ fn jsx_js_space(text) = { value jsx_space_regex: jsx_space_regex(); value re_test(text, jsx_space_regex) }
293
+ fn jsx_strip_space(text) = { value jsx_space_regex: jsx_space_regex(); value re_replace(text, jsx_space_regex, "") }
294
+ fn jsx_gap_safe(text) = jsx_strip_space(text) == ""
295
+ fn jsx_trim_space(text) = {
296
+ value result: "^" jsx_space_regex() "*"
297
+ value re_replace: re_replace(text, result, "")
298
+ value result_2: jsx_space_regex() "*$"
299
+ value re_replace(re_replace, result_2, "")
300
+ }
301
+ fn jsx_utf16_length(text) = len(text) + re_count(text, /[\u{10000}-\u{10ffff}]/)
302
+ fn jsx_break(text) = re_test(text, /[\n\r\u{2028}\u{2029}]/)
303
+ fn jsx_edge_space(text, opening) = {
304
+ value result: if opening then "^" jsx_space_regex() "*" else jsx_space_regex() "*$"
305
+ value re_find(text, result)
306
+ nonempty
307
+ } ?? ""
308
+ fn jsx_curly_trim(gap, opening) = {
309
+ value pattern: if opening then "(^|[\\n\\r\\u{2028}\\u{2029}])" jsx_space_regex() "+" else jsx_space_regex() "+($|[\\n\\r\\u{2028}\\u{2029}])"
310
+ value re_replace(gap, pattern, "$1")
311
+ }
312
+ in ts_jsx_expression {
313
+ let jsx_brace_container expression not ts_spread
314
+ let jsx_brace_attribute parent is ts_jsx_attribute
315
+ let jsx_brace_single !{ value expression.nonempty ?? { value result: len(text) - 1; value slice(text, 1, result) }; jsx_break }
316
+ }
317
+ in ts_jsx_spread_attribute {
318
+ let jsx_brace_container true
319
+ let jsx_brace_attribute true
320
+ let jsx_brace_single !jsx_break(argument)
321
+ }
322
+ in ts_jsx_expression, ts_jsx_spread_attribute {
323
+ let jsx_brace_object {
324
+ value result: 1 + {
325
+ value text
326
+ slice 1
327
+ jsx_edge_space true
328
+ len
329
+ }
330
+ value slice: slice(text, result)
331
+ value result_2: "{{"
332
+ value starts_with(slice, result_2)
333
+ }
334
+ let jsx_brace_left_break opener.ts_open_brace.space_after.line_break
335
+ }
336
+ in ts_open_brace, ts_close_brace {
337
+ let jsx_brace_open is ts_open_brace
338
+ let jsx_brace_gap if jsx_brace_open then space_after.text else space_before.text
339
+ let jsx_brace_edge jsx_edge_space(jsx_brace_gap, jsx_brace_open)
340
+ let jsx_brace_direction if jsx_brace_open then "after" else "before"
341
+ }
342
+ schema jsx_curly_config {when?: "always" | "never"; allowMultiline?: boolean; spacing?: {objectLiterals?: "always" | "never"}}
343
+ @macro jsx_curly_spacing_body {
344
+ option options ("always" | "never" | {when?: "always" | "never"; allowMultiline?: boolean; spacing?: {objectLiterals?: "always" | "never"}; attributes?: boolean | jsx_curly_config; children?: boolean | jsx_curly_config})[] = [] "brace spacing and context overrides"
345
+ let head first(options)
346
+ let cfg if type_of(head) == "str" then {
347
+ value options
348
+ slice 1 2
349
+ first
350
+ nonempty
351
+ } ?? {} else head.nonempty ?? {}
352
+ let context_cfg if parent.jsx_brace_attribute then cfg.attributes.nonempty ?? true else cfg.children.nonempty ?? false
353
+ let base_when if type_of(head) == "str" then head else cfg.when.nonempty ?? "never"
354
+ let scope_cfg if type_of(context_cfg) == "bool" then {} else context_cfg
355
+ let object_spacing scope_cfg.spacing.nonempty ?? cfg.spacing.nonempty ?? {}
356
+ let spacing if parent.jsx_brace_object then object_spacing.objectLiterals.nonempty ?? scope_cfg.when.nonempty ?? base_when else scope_cfg.when.nonempty ?? base_when
357
+ let multiline scope_cfg.allowMultiline.nonempty ?? cfg.allowMultiline.nonempty ?? true
358
+ when parent.jsx_brace_container && context_cfg != false
359
+ when if spacing == "always" then jsx_brace_edge == "" || jsx_break(jsx_brace_edge) && !multiline else (if jsx_break(jsx_brace_edge) then !multiline else jsx_brace_edge != "")
360
+ report at {start: if jsx_brace_open then end_offset else offset - byte_len(jsx_brace_edge), end: if jsx_brace_open then end_offset + byte_len(jsx_brace_edge) else offset}
361
+ warn if jsx_break(jsx_brace_edge) then "There should be no newline {jsx_brace_direction} '{text}'" else if spacing == "always" then "A space is required {jsx_brace_direction} '{text}'" else "There should be no space {jsx_brace_direction} '{text}'"
362
+ let jsx_fix_gap if jsx_break(jsx_brace_edge) then jsx_brace_gap else jsx_brace_edge
363
+ let jsx_fix_text if jsx_break(jsx_brace_edge) then jsx_curly_trim(jsx_fix_gap, jsx_brace_open) else ""
364
+ fix at { start: if jsx_brace_open then end_offset else offset - byte_len(jsx_fix_gap), end: if jsx_brace_open then end_offset + byte_len(jsx_fix_gap) else offset } as (if !jsx_brace_open && spacing == "always" then " " else "") jsx_fix_text (if jsx_brace_open && spacing == "always" then " " else "")
365
+ }
366
+ in ts_open_brace rule es-jsx-curly-spacing:after {
367
+ @jsx_curly_spacing_body
368
+ }
369
+ in ts_close_brace rule es-jsx-curly-spacing:before {
370
+ @jsx_curly_spacing_body
371
+ }
372
+ @macro jsx_curly_newline_body {
373
+ default option mode enum(consistent, require, forbid) = consistent "brace newline policy"
374
+ let needs if mode == "require" then true else if mode == "forbid" then false else parent.jsx_brace_left_break
375
+ when jsx_break(jsx_brace_gap) != needs
376
+ report at if jsx_brace_open then space_after else space_before
377
+ warn "{if needs then 'Expected' else 'Unexpected'} newline {jsx_brace_direction} '{text}'."
378
+ fix at {
379
+ start: if jsx_brace_open then end_offset else if needs then offset else offset - byte_len(jsx_brace_gap),
380
+ end: if jsx_brace_open && !needs then end_offset + byte_len(jsx_brace_gap) else if jsx_brace_open then end_offset else offset
381
+ } if needs || jsx_gap_safe(jsx_brace_gap) as if needs then "\n" else ""
382
+ }
383
+ group jsx_curly_newline_singleline ts_open_brace, ts_close_brace
384
+ group jsx_curly_newline_multiline ts_open_brace, ts_close_brace
385
+ in jsx_curly_newline_singleline rule es-jsx-curly-newline:singleline {
386
+ when parent is ts_jsx_expression && parent.jsx_brace_container && parent.jsx_brace_single
387
+ @jsx_curly_newline_body
388
+ }
389
+ in jsx_curly_newline_multiline rule es-jsx-curly-newline:multiline {
390
+ when parent is ts_jsx_expression && parent.jsx_brace_container && !parent.jsx_brace_single
391
+ @jsx_curly_newline_body
392
+ }
393
+ in ts_jsx_attribute let jsx_tag_last_token_start if value is ts_string then value.offset else end_offset
394
+ in ts_jsx_spread_attribute let jsx_tag_last_token_start end_offset
395
+ in ts_jsx_opening {
396
+ let jsx_tag_last_prop last(property.nodes)
397
+ let jsx_tag_name_line position(name.offset).js_line
398
+ }
399
+ in ts_open_angle, ts_close_angle, ts_punctuator {
400
+ let jsx_tag_opening parent is ts_jsx_opening
401
+ let jsx_tag_self_slash jsx_tag_opening && parent.parent is ts_jsx_element && parent.parent.closing == null && text == "/" && space_after.end == parent.end_offset - 1
402
+ let jsx_tag_kind if parent not (ts_jsx_opening, ts_jsx_closing) || parent.parent not ts_jsx_element then null else if is ts_open_angle && offset == parent.offset then (if jsx_tag_opening then "afterOpening" else "closingSlash") else if !jsx_tag_opening && is ts_punctuator && text == "/" && space_before.start == parent.offset + 1 then "afterOpening" else if jsx_tag_self_slash then "closingSlash" else if is ts_close_angle && end_offset == parent.end_offset && (!jsx_tag_opening || parent.parent.closing != null) then "beforeClosing" else null
403
+ let jsx_tag_gap_after is ts_open_angle || jsx_tag_kind == "afterOpening" || jsx_tag_self_slash
404
+ let jsx_tag_gap if jsx_tag_gap_after then space_after else space_before
405
+ let jsx_tag_span {start: parent.offset, end: parent.end_offset}
406
+ }
407
+ @macro jsx_tag_spacing_body {
408
+ option closingSlash "always" | "never" | "allow" = "never" "spacing beside the closing slash"
409
+ option beforeSelfClosing "always" | "proportional-always" | "never" | "allow" = "always" "spacing before a self-closing slash"
410
+ option afterOpening "always" | "allow-multiline" | "never" | "allow" = "never" "spacing after the opening bracket"
411
+ option beforeClosing "always" | "proportional-always" | "never" | "allow" = "allow" "spacing before the closing bracket"
412
+ let kind if jsx_tag_before_self then "beforeSelfClosing" else jsx_tag_kind
413
+ let gap if jsx_tag_before_self then space_before else jsx_tag_gap
414
+ let policy if kind == "closingSlash" then closingSlash else if kind == "beforeSelfClosing" then beforeSelfClosing else if kind == "afterOpening" then afterOpening else beforeClosing
415
+ let single position(jsx_tag_span.start).js_line == position(jsx_tag_span.end).js_line
416
+ let gap_newline position(gap.start).js_line != position(gap.end).js_line
417
+ let start_newline if kind == "beforeClosing" && jsx_tag_opening && parent.jsx_tag_last_prop != null then position(parent.jsx_tag_last_prop.jsx_tag_last_token_start).js_line != position(offset).js_line else gap_newline
418
+ let needs_newline policy == "proportional-always" && !single && !gap_newline
419
+ let spaced jsx_js_space(gap.text)
420
+ when if jsx_tag_before_self then jsx_tag_self_slash else jsx_tag_kind != null
421
+ when policy != "allow"
422
+ when if kind == "closingSlash" then spaced != (policy == "always") else if kind == "afterOpening" then spaced != (policy == "always") && !(policy == "allow-multiline" && gap_newline) else if needs_newline then true else if kind == "beforeSelfClosing" then !gap_newline && spaced != (policy != "never") else !start_newline && (if policy == "proportional-always" then jsx_tag_opening && spaced == single else spaced != (policy == "always"))
423
+ report at {start: gap.start, end: gap.end}
424
+ warn if kind == "closingSlash" then (if jsx_tag_self_slash then (if policy == "always" then "Whitespace is required between `/` and `>`; write `/ >`" else "Whitespace is forbidden between `/` and `>`; write `/>`") else (if policy == "always" then "Whitespace is required between `<` and `/`; write `< /`" else "Whitespace is forbidden between `<` and `/`; write `</`")) else if needs_newline then "A newline is required before closing bracket" else if kind == "afterOpening" then (if policy == "always" then "A space is required after opening bracket" else "A space is forbidden after opening bracket") else if policy == "never" then "A space is forbidden before closing bracket" else if kind == "beforeSelfClosing" then "A space is required before closing bracket" else "Whitespace is required before closing bracket"
425
+ # Single-line proportional closing gaps must shrink, not grow on every pass.
426
+ let remove_gap policy == "never" || policy == "allow-multiline" || policy == "proportional-always" && kind == "beforeClosing" && single
427
+ let edit { start: if remove_gap then gap.start else gap.end, end: gap.end }
428
+ let replacement if remove_gap then "" else if needs_newline then "\n" else " "
429
+ let original { offset: edit.start, end_offset: edit.end, text: byte_slice(source, edit.start, edit.end) }
430
+ fix at edit if expression_fix_retains_comments(original, replacement) as replacement
431
+ }
432
+ in ts_open_angle, ts_close_angle, ts_punctuator rule es-jsx-tag-spacing {
433
+ let jsx_tag_before_self false
434
+ @jsx_tag_spacing_body
435
+ }
436
+ in ts_punctuator rule es-jsx-tag-spacing:before-self-closing {
437
+ let jsx_tag_before_self true
438
+ @jsx_tag_spacing_body
439
+ }
440
+ group jsx_paired_brackets ts_close_angle
441
+ group jsx_self_closing_brackets ts_punctuator
442
+ in ts_jsx_opening let jsx_closing_bracket_location_enabled true
443
+ @macro jsx_closing_bracket_body {
444
+ default option mode enum(after-props, props-aligned, tag-aligned, line-aligned) = tag-aligned "closing bracket alignment"
445
+ let last_prop parent.jsx_tag_last_prop
446
+ let last_line position(last_prop.end_offset).js_line
447
+ let here position(offset)
448
+ let opening position(parent.offset)
449
+ let selected if last_prop == null then "after-tag" else if last_line == opening.js_line then "after-props" else mode
450
+ let aligned_column if selected == "props-aligned" then position(last_prop.offset).js_column else if selected == "tag-aligned" then opening.js_column else if selected == "line-aligned" then jsx_indent(parent.jsx_line_prefix) + 1 else null
451
+ let aligned if selected == "after-tag" then parent.jsx_tag_name_line == here.js_line else if selected == "after-props" then last_line == here.js_line else aligned_column == here.js_column && (selected != "tag-aligned" || starts_with(parent.jsx_line_prefix, "\t") == starts_with(jsx_line_prefix, "\t"))
452
+ let fallback !aligned && (contains(space_before.text, "/*") || contains(space_before.text, "//")) && contains(["after-tag", "after-props"], selected)
453
+ let placement if fallback then "line-aligned" else selected
454
+ let expected_column if fallback then jsx_indent(parent.jsx_line_prefix) + 1 else aligned_column
455
+ when parent.jsx_closing_bracket_location_enabled
456
+ when !(if fallback then expected_column == here.js_column else aligned)
457
+ let anchor if last_prop != null then last_prop.end_offset else parent.name.end_offset
458
+ let trailing_comment { value overlapping(ts_comment, anchor, offset); last }
459
+ let same_line contains(["after-tag", "after-props"], placement)
460
+ let indent_prefix if placement == "props-aligned" then last_prop.jsx_line_prefix else parent.jsx_line_prefix
461
+ let indentation { value pattern: "^" jsx_space_regex() "*"; value re_find(indent_prefix, pattern) } ?? ""
462
+ let padding if expected_column != null then expected_column - 1 - utf16_len(indentation) else 0
463
+ let closing_text if is ts_punctuator then "/>" else ">"
464
+ fix at {start: if !same_line && trailing_comment != null then trailing_comment.end_offset else anchor, end: parent.end_offset} if !(placement == "after-tag" && parent.type_argument != null) as if same_line then "{if placement == 'after-tag' then ' ' else ''}{closing_text}" else "\n{indentation}{if padding > 1 then repeat(' ', padding) else ''}{closing_text}"
465
+ report at node
466
+ warn "The closing bracket must be {if placement == 'after-tag' then 'placed after the opening tag' else if placement == 'after-props' then 'placed after the last prop' else if placement == 'props-aligned' then 'aligned with the last prop' else if placement == 'tag-aligned' then 'aligned with the opening tag' else 'aligned with the line containing the opening tag'}{if expected_column != null then ' (expected column ' expected_column (if last_prop != null && last_line == here.js_line then ' on the next line)' else ')') else ''}"
467
+ }
468
+ in jsx_paired_brackets rule es-jsx-closing-bracket-location:paired {
469
+ when parent is ts_jsx_opening && parent.parent is ts_jsx_element && parent.parent.closing != null && end_offset == parent.end_offset
470
+ @jsx_closing_bracket_body
471
+ }
472
+ in jsx_self_closing_brackets rule es-jsx-closing-bracket-location:self-closing {
473
+ when parent is ts_jsx_opening && jsx_tag_self_slash
474
+ @jsx_closing_bracket_body
475
+ }
476
+ in ts_open_angle rule es-jsx-closing-tag-location {
477
+ option mode "tag-aligned" | "line-aligned" = "tag-aligned" "closing-tag alignment"
478
+ let here position(offset)
479
+ let expected if mode == "tag-aligned" then position(parent.parent.opening.offset).js_column else jsx_indent(parent.parent.opening.jsx_line_prefix) + 1
480
+ let own_line jsx_trim_space(jsx_line_prefix) == ""
481
+ when parent is ts_jsx_closing && offset == parent.offset && position(parent.parent.opening.offset).js_line != here.js_line && here.js_column != expected
482
+ report at parent.node
483
+ warn if !own_line then "Closing tag of a multiline JSX expression must be on its own line." else if mode == "tag-aligned" then "Expected closing tag to match indentation of opening." else "Expected closing tag to be aligned with the line containing the opening tag"
484
+ let indentation expected - 1
485
+ fix at { start: if own_line then offset - byte_len(jsx_line_prefix) else offset, end: offset } as (if own_line then "" else "\n") repeat(" ", indentation)
486
+ }
487
+
488
+ fn jsx_items(value) = if type_of(value) == "list" then value else if value == null then [] else [value]
489
+ fn jsx_pascal(name) = re_test(name, /^\p{Uppercase}[\p{Uppercase}\p{Lowercase}0-9]*$/) && re_test(name, /[\p{Lowercase}0-9]/)
490
+ fn jsx_caps(name) = re_test(name, /^[\p{Uppercase}0-9](?:[\p{Uppercase}0-9_]*[\p{Uppercase}0-9])?$/)
491
+ in ts_jsx_element {
492
+ let jsx_name_spelling jsx_strip_space(opening.name)
493
+ let jsx_intrinsic_name re_test(jsx_name_spelling, /^[a-z]/)
494
+ let jsx_self_closing_enabled jsx_intrinsic_name || !contains(jsx_name_spelling, ":")
495
+ }
496
+ group jsx_self_closing_elements ts_jsx_element
497
+ in jsx_self_closing_elements rule es-jsx-self-closing-comp {
498
+ let content jsx_items(body)
499
+ let empty len(content) == 0 || len(content) == 1 && { value body.node; jsx_items; first } is ts_jsx_text && { value content; first; contains "\n" } && { value content; first; jsx_strip_space } == "" && !{ value content; first; re_test /\u{a0}/ }
500
+ when closing != null && empty && jsx_self_closing_enabled
501
+ warn "Empty components are self-closing"
502
+ fix at { start: opening.end_offset - 1, end: closing.end_offset } as " />"
503
+ }
504
+ in ts_jsx_element rule es-jsx-pascal-case {
505
+ option allowAllCaps boolean = false "allow screaming snake case"
506
+ option allowLeadingUnderscore boolean = false "allow one leading underscore"
507
+ option allowNamespace boolean = false "check only the first namespace segment"
508
+ option ignore string[] = [] "component-name globs to ignore"
509
+ let parts { value jsx_name_spelling; replace ":" "."; split "." }
510
+ let names if allowNamespace then slice(parts, 0, 1) else parts
511
+ let bad find(names, n => jsx_utf16_length(n) == 1 || !({ value if allowLeadingUnderscore && starts_with(n, "_") then slice(n, 1) else n; jsx_pascal } || allowAllCaps && { value if allowLeadingUnderscore && starts_with(n, "_") then slice(n, 1) else n; jsx_caps } || any(ignore, p => {
512
+ value result: "^" {
513
+ value p
514
+ re_escape
515
+ replace "\\*" ".*"
516
+ replace "\\?" "."
517
+ re_replace "\\\\\\+\\\\\\(([^()]*)\\\\\\)" "(?:$1)+"
518
+ replace "\\|" "|"
519
+ } "$"
520
+ value re_test(n, result)
521
+ })))
522
+ when !jsx_intrinsic_name && bad != null && jsx_utf16_length(bad) != 1
523
+ warn "Imported JSX component {bad} must be in PascalCase{if allowAllCaps then ' or SCREAMING_SNAKE_CASE' else ''}"
524
+ }
525
+ in ts_jsx_element rule es-jsx-shorthand-fragment {
526
+ when contains(["Fragment", "React.Fragment"], jsx_name_spelling) && opening.property == null
527
+ report at opening.node
528
+ warn "Use shorthand fragment syntax `<>...</>` instead of `<{jsx_name_spelling}>...</{jsx_name_spelling}>`."
529
+ let body_start opening.end_offset - offset
530
+ let body_end closing.offset - offset
531
+ let content byte_slice(text, body_start, body_end)
532
+ fix at node if closing != null && opening.type_argument == null && expression_fix_clean(opening.node) && expression_fix_clean(closing.node) as "<>" content "</>"
533
+ }
534
+ in ts_jsx_attribute rule es-jsx-shorthand-boolean {
535
+ when value is ts_jsx_expression && value.jsx_boolean_true == true
536
+ warn "Omit the value for boolean attributes."
537
+ fix at { start: name.end_offset, end: end_offset } if expression_fix_clean(node) && (next == null || next.offset > end_offset) as ""
538
+ }
539
+ in ts_boolean let jsx_boolean_true text == "true"
540
+ in ts_parenthesized let jsx_boolean_true expression.jsx_boolean_true == true
541
+ in ts_jsx_expression let jsx_boolean_true expression.jsx_boolean_true == true
542
+ fn jsx_contains_quote(text, double) = { value result: if double then '"' else "'"; value contains(text, result) } || {
543
+ value result_2: if double then /&(?:quot|#\+?0*34|#[xX]\+?0*22);/ else /&(?:apos|#\+?0*39|#[xX]\+?0*27);/
544
+ value re_test(text, result_2)
545
+ }
546
+ in ts_jsx_attribute.value rule es-jsx-quotes {
547
+ option mode "prefer-double" | "prefer-single" = "prefer-double" "preferred attribute quote"
548
+ let double mode == "prefer-double"
549
+ when is ts_string
550
+ when slice(text, 0, 1) != (if double then '"' else "'") && !{ value result: len(text) - 1; value slice(text, 1, result); jsx_contains_quote double }
551
+ report at node
552
+ warn "Unexpected usage of {if mode == 'prefer-double' then 'singlequote' else 'doublequote'}."
553
+ let quote if double then '"' else "'"
554
+ let body_end byte_len(text) - 1
555
+ let body byte_slice(text, 1, body_end)
556
+ fix at node as quote body quote
557
+ }
558
+ @macro jsx_equals_spacing_body {
559
+ option mode "always" | "never" = "never" "spacing around equals"
560
+ when parent is ts_jsx_attribute
561
+ when { value if jsx_equals_before then space_before.text else space_after.text; jsx_js_space } != (mode == "always")
562
+ report at if jsx_equals_before then space_before else space_after
563
+ warn if mode == "always" then "A space is required {if jsx_equals_before then 'before' else 'after'} '='" else "There should be no space {if jsx_equals_before then 'before' else 'after'} '='"
564
+ let gap if jsx_equals_before then space_before else space_after
565
+ fix at {
566
+ start: if jsx_equals_before && mode == "never" then gap.start else if jsx_equals_before then offset else end_offset,
567
+ end: if !jsx_equals_before && mode == "never" then gap.end else if jsx_equals_before then offset else end_offset
568
+ } if mode == "always" || jsx_gap_safe(gap.text) as if mode == "always" then " " else ""
569
+ }
570
+ in ts_equals rule es-jsx-equals-spacing:before {
571
+ let jsx_equals_before true
572
+ @jsx_equals_spacing_body
573
+ }
574
+ in ts_equals rule es-jsx-equals-spacing:after {
575
+ let jsx_equals_before false
576
+ @jsx_equals_spacing_body
577
+ }
578
+ in ts_jsx_opening {
579
+ let jsx_opening_single !jsx_break(text)
580
+ let jsx_property_count count(property)
581
+ let jsx_first_prop_headline first([] property.jsx_prop_headline) == true
582
+ }
583
+ in ts_jsx_attribute, ts_jsx_spread_attribute {
584
+ let jsx_prop_same !space_before.line_break
585
+ let jsx_prop_headline !{ value parent.text; byte_slice 0 (offset - parent.offset); jsx_break }
586
+ let jsx_prop_first previous == null
587
+ let jsx_prop_run if jsx_prop_same then 1 + (previous.jsx_prop_run.nonempty ?? 0) else 1
588
+ let jsx_prop_group_index if jsx_prop_same && !jsx_prop_first then 1 + previous.jsx_prop_group_index else 0
589
+ let jsx_prop_group_start if jsx_prop_same && !jsx_prop_first then previous.jsx_prop_group_start else offset
590
+ }
591
+ in ts_jsx_attribute let jsx_prop_name jsx_strip_space(name)
592
+ in ts_parenthesized let jsx_unparenthesized_text expression.jsx_unparenthesized_text.nonempty ?? expression
593
+ in ts_jsx_spread_attribute let jsx_prop_name argument.jsx_unparenthesized_text.nonempty ?? argument
594
+ in ts_jsx_attribute, ts_jsx_spread_attribute rule es-jsx-first-prop-new-line {
595
+ option mode "always" | "never" | "multiline" | "multiline-multiprop" | "multiprop" = "multiline-multiprop" "first prop placement"
596
+ let should_wrap mode == "always" || mode == "multiline" && !parent.jsx_opening_single || mode == "multiline-multiprop" && !parent.jsx_opening_single && parent.jsx_property_count > 1 || mode == "multiprop" && parent.jsx_property_count > 1
597
+ let should_inline mode == "never" || mode == "multiprop" && !parent.jsx_opening_single && parent.jsx_property_count <= 1
598
+ when jsx_prop_first && (should_wrap && jsx_prop_headline || should_inline && !jsx_prop_headline)
599
+ report at if is ts_jsx_attribute then name.node else argument.node
600
+ warn if should_wrap then "Property should be placed on a new line" else "Property should be placed on the same line as the component declaration"
601
+ fix at space_before if jsx_gap_safe(space_before.text) && (should_wrap || parent.type_argument == null) as if should_wrap then "\n" else " "
602
+ }
603
+ @macro jsx_max_props_body {
604
+ default option max number(1..) = 1 "maximum props per line"
605
+ when jsx_prop_group_index == max
606
+ let group_start jsx_prop_group_start
607
+ let props { value parent.property.nodes; filter (prop) prop.jsx_prop_group_start == group_start }
608
+ let fixable { value props; filter (prop) prop.jsx_prop_group_index > 0 && jsx_trim_space(prop.space_before.text) != ""; count } == 0
609
+ let fixed { value props; map (prop) (if prop.jsx_prop_group_index == 0 then "" else if prop.jsx_prop_group_index % max == 0 then "\n" else " ") prop.text; join "" }
610
+ report at if is ts_jsx_attribute then name.node else argument.node
611
+ fix at { start: first(props).offset, end: last(props).end_offset } if fixable as fixed
612
+ warn "Prop `{jsx_prop_name}` must be placed on a new line"
613
+ }
614
+ group jsx_max_props_singleline ts_jsx_attribute, ts_jsx_spread_attribute
615
+ group jsx_max_props_multiline ts_jsx_attribute, ts_jsx_spread_attribute
616
+ in jsx_max_props_singleline rule es-jsx-max-props-per-line:singleline {
617
+ when parent.jsx_opening_single && jsx_prop_headline
618
+ @jsx_max_props_body
619
+ }
620
+ in jsx_max_props_multiline rule es-jsx-max-props-per-line:multiline {
621
+ when !(parent.jsx_opening_single && jsx_prop_headline)
622
+ @jsx_max_props_body
623
+ }
624
+ @macro jsx_props_style_body {
625
+ option max_per_line int(1..) = 1 "maximum props per line when wrapping"
626
+ when if need_wrap then jsx_prop_same && (jsx_prop_first || jsx_prop_run > max_per_line && (jsx_prop_run - 1) % max_per_line == 0) else !jsx_prop_same
627
+ report at if is ts_jsx_attribute then name.node else node
628
+ warn if need_wrap then "Prop `{jsx_prop_name}` must be placed on a new line" else "Prop `{jsx_prop_name}` should not be placed on a new line"
629
+ fix at space_before if jsx_gap_safe(space_before.text) as if need_wrap then "\n" else " "
630
+ }
631
+ group jsx_props_style_singleline ts_jsx_attribute, ts_jsx_spread_attribute
632
+ group jsx_props_style_multiline ts_jsx_attribute, ts_jsx_spread_attribute
633
+ in jsx_props_style_singleline rule es-jsx-props-style:singleline {
634
+ default option max int(0..) | null = null "single-line prop limit before wrapping"
635
+ when parent.jsx_opening_single
636
+ let need_wrap max != null && parent.jsx_property_count > max
637
+ @jsx_props_style_body
638
+ }
639
+ in jsx_props_style_multiline rule es-jsx-props-style:multiline {
640
+ default option min int(0..) = 0 "minimum props before multiline wrapping"
641
+ when !parent.jsx_opening_single
642
+ let need_wrap parent.jsx_property_count >= min && !parent.jsx_first_prop_headline
643
+ @jsx_props_style_body
644
+ }
645
+
646
+ }