@inthedark/dxlint 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +29 -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
@@ -0,0 +1,114 @@
1
+ # .ts/layout/literals: literals layout rules.
2
+
3
+ import .ts/layout/shared
4
+ import .ts/import-attributes
5
+ import .ts/literal-text
6
+
7
+ define {
8
+
9
+ in ts_string let layout_quote_fixed_string is (ts_property.key, ts_pattern_property.key, ts_method.key, ts_class_property.key, ts_method_signature.key, ts_property_signature.key, ts_import.from, ts_export.from, ts_import.namespace, ts_export.namespace, ts_import_specifier.imported, ts_namespace.name, ts_enum_member.name, ts_import_type.argument) || parent is ts_export_specifier
10
+ in ts_literal_type let layout_quote_import_argument parent is ts_import_type && offset == parent.argument.offset
11
+ in ts_call let layout_quote_external_module parent is ts_import_equals && offset == parent.value.offset
12
+ in ts_program let layout_quote_prologue true
13
+ in ts_block let layout_quote_prologue (parent is ts_function || parent is ts_arrow || parent is ts_method) && offset == parent.body.offset
14
+ in ts_expression_statement let layout_quote_directive (parent.layout_quote_prologue.nonempty ?? false) && expression is ts_string && (previous == null || (previous.layout_quote_directive.nonempty ?? false))
15
+ in ts_template let layout_quote_template_feature count(expression) > 0 || is ts_tagged_template.quasi || re_test(text, /(^|[^\\])(\\\\)*[\n\r\u{2028}\u{2029}]/)
16
+ group quoted_literals ts_string, ts_template
17
+ fn layout_quote_piece_text(piece, old, target) =
18
+ if starts_with(piece, "\\") && (slice(piece, 1) == old || old == "`" && slice(piece, 1) == "${{") then slice(piece, 1)
19
+ else if piece == target || target == "`" && piece == "${{" then "\\" piece
20
+ else piece
21
+ in quoted_literals rule es-quotes {
22
+ option style "double" | "single" | "backtick" = "double" "string delimiters"
23
+ option avoidEscape boolean = false "allow alternate quotes to avoid escapes"
24
+ option allowTemplateLiterals "never" | "avoidEscape" | "always" = "never" "allow unused template delimiters"
25
+ let quote_template is ts_template
26
+ let quote_fixed (layout_quote_fixed_string.nonempty ?? false) || parent is ts_expression_statement && parent.layout_quote_directive || parent is ts_literal_type && (parent.layout_quote_import_argument.nonempty ?? false) || parent is ts_call && (parent.layout_quote_external_module.nonempty ?? false)
27
+ let quote_delimiter if style == "single" then "'" else if style == "double" then "\"" else "`"
28
+ let quote_alternate if style == "double" then "'" else "\""
29
+ when if quote_template then style != "backtick" && allowTemplateLiterals != "always" && !layout_quote_template_feature && !((allowTemplateLiterals == "avoidEscape") && avoidEscape && contains(text, quote_delimiter)) else parent not ts_jsx_attribute && !(style == "backtick" && quote_fixed) && !starts_with(text, quote_delimiter) && !(avoidEscape && starts_with(text, quote_alternate) && contains(text, quote_delimiter))
30
+ let quote_old slice(text, 0, 1)
31
+ let quote_parts {
32
+ value overlapping(ts_literal_piece, offset, end_offset)
33
+ filter (piece) piece.offset > offset && piece.end_offset < end_offset
34
+ unique_by (piece) {start: piece.offset, end: piece.end_offset}
35
+ map (piece) layout_quote_piece_text(piece.text, quote_old, quote_delimiter)
36
+ join
37
+ }
38
+ fix at {start: offset, end: end_offset} if !(style == "backtick" && re_test(text, /(^|[^\\])(\\\\)*\\(?:[1-9]|0[0-9])/)) && !(quote_template && parent is ts_expression_statement && parent.parent.layout_quote_prologue == true) as "{quote_delimiter}{quote_parts}{quote_delimiter}"
39
+ warn "Strings must use {if style == 'single' then 'singlequote' else if style == 'double' then 'doublequote' else 'backtick'}."
40
+ }
41
+
42
+ group layout_property_quote_owner ts_property, ts_pattern_property, ts_property_signature, ts_method_signature, ts_enum_member, ts_import_attribute
43
+ fn layout_property_quote_key(site, current) = {
44
+ value owner: if current is layout_property_quote_owner && current.offset == site.offset then current else {
45
+ value overlapping(layout_property_quote_owner, site.offset, site.offset + 1)
46
+ filter (item) item.offset == site.offset
47
+ first
48
+ }
49
+ value if owner is ts_enum_member then owner.name.node else owner.key.node
50
+ }
51
+ fn layout_property_quote_text(site, current) = {
52
+ value key: layout_property_quote_key(site, current)
53
+ value name: if site.quoted then site.name else key.parent.static_name ?? site.name
54
+ value if site.quoted then name else "\"{name}\""
55
+ }
56
+ fn layout_property_quote_site(owner, group) = {
57
+ value key: if owner is ts_enum_member then owner.name.node else owner.key.node
58
+ value quoted: key is (ts_string, ts_import_attribute_key, ts_import_attribute_string) && re_test(key.text, /^["']/)
59
+ value numeric: key is (ts_number, ts_bigint)
60
+ value name: if key is ts_identifier || owner is ts_import_attribute && !quoted then key.text else key.literal_value.value
61
+ value identifier: if contains(name, "\\") then js_string("\"" name "\"") ?? "" else name
62
+ value redundant_number: re_test(name, /^(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/)
63
+ && js_to_string(js_number(name)) == name
64
+ value redundant: re_test(identifier, /^[$_\p{ID_Start}][$\u{200c}\u{200d}\p{ID_Continue}]*$/) || redundant_number
65
+ value keyword: re_test(name, /^(?:abstract|boolean|break|byte|case|catch|char|class|const|continue|debugger|default|delete|do|double|else|enum|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with)$/)
66
+ value if key == null || key is ts_computed_key || name == null
67
+ || owner is (ts_property, ts_pattern_property) && owner.value.node == null then null else {
68
+ group: group, quoted: quoted, numeric: numeric, redundant: redundant,
69
+ redundant_number: redundant_number, keyword: keyword, name: name, offset: owner.offset
70
+ }
71
+ }
72
+ fn layout_property_quote_sites(n) = {
73
+ value group: n is (ts_object, ts_interface, ts_type_literal, ts_enum)
74
+ value owners: when {
75
+ n is ts_object then n.property.nodes
76
+ n is (ts_interface, ts_type_literal, ts_enum) then n.member.nodes
77
+ n is (ts_import, ts_export) then overlapping(ts_import_attribute, n.offset, n.end_offset)
78
+ else then [n]
79
+ }
80
+ value sites: flat_map(owners, owner =>
81
+ if owner is ts_import_attribute then [layout_property_quote_site(owner, false), layout_property_quote_site(owner, true)]
82
+ else if owner is (ts_property, ts_pattern_property, ts_property_signature, ts_method_signature, ts_enum_member)
83
+ then [layout_property_quote_site(owner, group)] else [])
84
+ value filter(sites, site => site != null)
85
+ }
86
+ in ts_property, ts_pattern_property, ts_property_signature, ts_enum_member, ts_object, ts_interface, ts_type_literal, ts_enum, ts_import, ts_export rule es-quote-props {
87
+ option style "always" | "as-needed" | "consistent" | "consistent-as-needed" = "always" "property quoting policy"
88
+ option keywords boolean = false "require quotes on reserved words"
89
+ option unnecessary boolean = true "report redundant quotes"
90
+ option numbers boolean = false "require quoted numeric keys"
91
+ let property_sites layout_property_quote_sites(node)
92
+ let property_any_quoted any(property_sites, s => s.quoted)
93
+ let property_necessary any(property_sites, s => s.quoted && !s.redundant || keywords && s.keyword)
94
+ let property_keyword { value property_sites; filter (s) !s.quoted && s.keyword; last }.name
95
+ each property_site in filter(property_sites, s => s.group == (style == "consistent" || style == "consistent-as-needed")) {
96
+ when if style == "always" then !property_site.quoted else if style == "as-needed" then (if property_site.quoted then unnecessary && property_site.redundant && !(numbers && property_site.redundant_number) && !(keywords && property_site.keyword) else keywords && property_site.keyword || numbers && property_site.numeric) else if style == "consistent-as-needed" && property_any_quoted && !property_necessary then property_site.quoted else !property_site.quoted && (property_any_quoted || style == "consistent-as-needed" && keywords && property_keyword != null)
97
+ report at property_site.offset
98
+ fix at layout_property_quote_key(property_site, node) as layout_property_quote_text(property_site, node)
99
+ warn if style == "always" then "Unquoted property '{property_site.name}' found." else if style == "as-needed" then (if property_site.quoted then "Unnecessarily quoted property '{property_site.name}' found." else if property_site.keyword then "Unquoted reserved word '{property_site.name}' used as key." else "Unquoted number literal '{property_site.name}' used as key.") else if style == "consistent-as-needed" && !property_necessary then "Properties shouldn't be quoted as all quotes are redundant." else if style == "consistent-as-needed" && keywords && property_keyword != null then "Properties should be quoted as '{property_keyword}' is a reserved word." else "Inconsistently quoted property '{property_site.name}' found."
100
+ }
101
+ }
102
+
103
+ in ts_number rule es-no-floating-decimal:leading {
104
+ when starts_with(text, ".")
105
+ warn "A leading decimal point can be confused with a dot."
106
+ fix as (if token_neighbours.before.gap == "" && re_test(token_neighbours.before.text, /[\w$]$/) then " 0" else "0") text
107
+ }
108
+ in ts_number rule es-no-floating-decimal:trailing {
109
+ when ends_with(text, ".")
110
+ warn "A trailing decimal point can be confused with a dot."
111
+ fix as text "0"
112
+ }
113
+
114
+ }
@@ -0,0 +1,184 @@
1
+ # .ts/layout/separators: separators layout rules.
2
+
3
+ import .ts/layout/shared
4
+ import .ts/import-attributes
5
+
6
+ define {
7
+
8
+ in ts_function, ts_method, ts_arrow, ts_function_type, ts_method_signature, ts_call_signature, ts_class, ts_interface, ts_type_alias let layout_last_type_parameter_end last(type_parameter.end_offset)
9
+
10
+ group comma_style_lists ts_variable_declaration, ts_array, ts_array_pattern, ts_object, ts_object_pattern,
11
+ ts_function, ts_method, ts_arrow, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export,
12
+ ts_sequence, ts_enum, ts_tuple_type, ts_type_literal, ts_method_signature, ts_call_signature,
13
+ ts_index_signature, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_tagged_template, ts_import_attributes
14
+ in ts_comma {
15
+ let comma_style_context layout_comma_context(node)
16
+ let comma_style_owner comma_style_context.owner
17
+ let comma_style_field comma_style_context.field
18
+ let comma_style_previous comma_style_context.previous
19
+ }
20
+ in ts_import_attribute_comma {
21
+ let comma_style_owner parent
22
+ let comma_style_field "attribute"
23
+ }
24
+ in ts_comma, ts_import_attribute_comma let comma_style_checked comma_style_owner is comma_style_lists
25
+ in ts_comma, ts_import_attribute_comma rule es-comma-style {
26
+ option style "last" | "first" = "last" "comma line position"
27
+ when if style == "last" then space_before.line_break else space_after.line_break
28
+ when comma_style_checked
29
+ when comma_style_owner not (ts_array, ts_array_pattern) || comma_style_previous != null
30
+ when !(token(node, "after").text == "," && space_after.line_break)
31
+ let comma_comments overlapping(ts_comment, token(node, "before").end, token(node, "after").start)
32
+ let comma_after_comment { value comma_comments; filter (comment) comment.offset >= end_offset; first }
33
+ let comma_placement if space_before.line_break && space_after.line_break && !(comma_after_comment is ts_block_comment && position(comma_after_comment.offset).js_line == position(offset).js_line) then "between" else style
34
+ let comma_gap "{space_before.text}{space_after.text}"
35
+ let comma_fixed_gap if comma_placement == "between" then re_replace(comma_gap, /^([^\r\n\u{2028}\u{2029}]*)(\r\n|[\r\n\u{2028}\u{2029}])/, "$1") else comma_gap
36
+ let comma_between_safe all(comma_comments, c => position(c.offset).js_line == position(c.end_offset).js_line) && (all(comma_comments, c => c is ts_block_comment) || re_test(comma_fixed_gap, /[\r\n\u{2028}\u{2029}][ \t]*$/))
37
+ fix at {start: space_before.start, end: space_after.end} if comma_placement != "between" || comma_between_safe as "{if comma_placement == 'first' then '' else ','}{comma_fixed_gap}{if comma_placement == 'first' then ',' else ''}"
38
+ warn if space_before.line_break && space_after.line_break then "Bad line breaking before and after ','." else if style == "last" then "',' should be placed last." else "',' should be placed first."
39
+ }
40
+
41
+ schema trailing_comma_mode "always-multiline" | "always" | "never" | "only-multiline" | "ignore"
42
+ in ts_import_attributes let layout_attribute_last { value descendants(ts_import_attribute); last }
43
+ in ts_import_attributes let layout_attribute_comma { value descendants(ts_import_attribute_comma); filter (c) c.offset >= layout_attribute_last.end_offset; first }
44
+ in ts_import_attributes let layout_attribute_comma_sites if layout_attribute_last == null then [] else [{
45
+ kind: "importAttributes",
46
+ offset: layout_attribute_comma.offset.nonempty ?? layout_attribute_last.end_offset,
47
+ trailing: layout_attribute_comma != null,
48
+ multiline: { value layout_attribute_comma.end_offset.nonempty ?? layout_attribute_last.end_offset; position }.js_line != position(close.end_offset).js_line,
49
+ allowed: true,
50
+ tsx_single: false
51
+ }]
52
+ fn layout_attribute_sites(sites, style, policies) = {
53
+ value mode: { value policies; get "importAttributes"; nonempty } ?? style
54
+ value if mode != "ignore" then map(sites, site => {
55
+ mode: mode, offset: site.offset, trailing: site.trailing,
56
+ multiline: site.multiline, allowed: site.allowed, tsx_single: site.tsx_single
57
+ }) else []
58
+ }
59
+ in ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arrow, ts_method, ts_function_type, ts_call, ts_new, ts_import_call, ts_import, ts_export, ts_enum, ts_tuple_type, ts_class, ts_interface, ts_type_alias, ts_type_reference, ts_method_signature, ts_call_signature, ts_import_attributes rule es-comma-dangle {
60
+ option style trailing_comma_mode = "never" "trailing comma policy"
61
+ option arrays trailing_comma_mode | null = null "array policy"
62
+ option objects trailing_comma_mode | null = null "object policy"
63
+ option functions trailing_comma_mode | null = null "parameter and argument policy"
64
+ option imports trailing_comma_mode | null = null "named import policy"
65
+ option exports trailing_comma_mode | null = null "named export policy"
66
+ option importAttributes trailing_comma_mode | null = null "import attribute policy"
67
+ option dynamicImports trailing_comma_mode | null = null "dynamic import policy"
68
+ option enums trailing_comma_mode | null = null "enum policy"
69
+ option generics trailing_comma_mode | null = null "type parameter policy"
70
+ option tuples trailing_comma_mode | null = null "tuple policy"
71
+ let dangle_policies {arrays:arrays, objects:objects, functions:functions, imports:imports, exports:exports, importAttributes:importAttributes, dynamicImports:dynamicImports, enums:enums, generics:generics, tuples:tuples, "type-arguments":"never"}
72
+ let dangle_tsx ends_with(file, ".tsx")
73
+ each dangle_site in if is ts_import_attributes then layout_attribute_sites(layout_attribute_comma_sites, style, dangle_policies) else layout_dangle_lists(node, style, dangle_policies, ecma_version, dangle_tsx) {
74
+ when layout_dangle_wrong(dangle_site, dangle_site.mode)
75
+ report at { start: dangle_site.offset, end: dangle_site.offset + (if byte_slice(source, dangle_site.offset, dangle_site.offset + 2) == "\r\n" then 2 else 1) }
76
+ fix at {start:dangle_site.offset, end:dangle_site.offset + (if dangle_site.trailing then 1 else 0)} as if dangle_site.trailing then "" else ","
77
+ warn if dangle_site.trailing then "Unexpected trailing comma." else "Missing trailing comma."
78
+ }
79
+ }
80
+
81
+ in ts_empty, ts_semicolon rule es-no-extra-semi {
82
+ when is ts_empty && parent not (ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while, ts_if, ts_labeled, ts_with)
83
+ || is ts_semicolon && parent is ts_class
84
+ let semi_directive next is ts_expression_statement && next.expression is ts_string
85
+ && (parent is ts_program || parent is ts_block && parent.parent is (ts_function, ts_arrow, ts_method))
86
+ let semi_fix_start token_neighbours.before.start.nonempty ?? offset
87
+ let semi_fix_end token_neighbours.after.end.nonempty ?? end_offset
88
+ fix at { start: semi_fix_start, end: semi_fix_end } if !semi_directive as byte_slice(source, semi_fix_start, offset) byte_slice(source, end_offset, semi_fix_end)
89
+ warn "Unnecessary semicolon."
90
+ }
91
+
92
+ group layout_semi_owner ts_break, ts_continue, ts_debugger, ts_do_while, ts_export, ts_export_assignment, ts_expression_statement, ts_import, ts_return, ts_throw, ts_variable_declaration, ts_class_property
93
+ in layout_semi_owner let layout_semi_last parent is ts_do_while || parent is ts_if && parent.alternate != null && end_offset == parent.consequent.end_offset || parent is (ts_program, ts_block, ts_static_block, ts_case, ts_class) && next == null
94
+ in ts_variable_declaration let layout_semi_initializer parent is ts_for && offset == parent.init.offset
95
+ in ts_semicolon let layout_semi_header if parent is ts_for then parent.init != null && (parent.test == null && previous_token not ts_semicolon || offset < parent.test.offset) || parent.test != null && offset >= parent.test.end_offset else parent.layout_semi_initializer.nonempty ?? false
96
+ in ts_semicolon let layout_semi_statement parent is layout_semi_owner && end_offset == parent.end_offset
97
+ in ts_semicolon rule es-semi-style {
98
+ option style "last" | "first" = "last" "semicolon line position"
99
+ when layout_semi_header || layout_semi_statement
100
+ let semi_at_end style == "last" || layout_semi_header
101
+ when if semi_at_end then position(offset).js_line != position(token(node, "before").end).js_line
102
+ else !(parent.layout_semi_last.nonempty ?? false) && position(end_offset).js_line != position(token(node, "after").start).js_line
103
+ let semi_fix_start token_neighbours.before.end.nonempty ?? offset
104
+ let semi_fix_end token_neighbours.after.start.nonempty ?? end_offset
105
+ fix at { start: semi_fix_start, end: semi_fix_end } if { value overlapping(ts_comment, semi_fix_start, semi_fix_end); count } == 0 as if semi_at_end then ";\n" else "\n;"
106
+ warn if semi_at_end then "Expected this semicolon to be at the end of the previous line." else "Expected this semicolon to be at the beginning of the next line."
107
+ }
108
+
109
+ @macro layout_semi_spacing_options {
110
+ option before boolean = false "space before semicolon"
111
+ option after boolean = true "space after semicolon"
112
+ when layout_semi_header || layout_semi_statement || parent is (ts_type_alias, ts_function, ts_method) && end_offset == parent.end_offset || parent is (ts_interface, ts_type_literal)
113
+ }
114
+ in ts_semicolon rule es-semi-spacing:before {
115
+ @layout_semi_spacing_options
116
+ let semi_space space_before != null && !space_before.line_break && space_before.spaced
117
+ when semi_space != before
118
+ report at { start: if semi_space then space_before.start else offset, end: if semi_space then offset else end_offset }
119
+ let semi_fix_start if semi_space then space_before.start else offset
120
+ fix at { start: semi_fix_start, end: offset } if !space_before.line_break && { value overlapping(ts_comment, semi_fix_start, offset); count } == 0 as if before then " " else ""
121
+ warn if semi_space then "Unexpected whitespace before semicolon." else "Missing whitespace before semicolon."
122
+ }
123
+ in ts_semicolon rule es-semi-spacing:after {
124
+ @layout_semi_spacing_options
125
+ when previous_token != null && next_token != null
126
+ when !space_before.line_break && !space_after.line_break && next_token not (ts_close_paren, ts_close_brace)
127
+ when space_after.spaced != after
128
+ report at { start: if space_after.spaced then end_offset else offset, end: if space_after.spaced then space_after.end else end_offset }
129
+ let semi_fix_end if space_after.spaced then space_after.end else end_offset
130
+ fix at { start: end_offset, end: semi_fix_end } if { value overlapping(ts_comment, end_offset, semi_fix_end); count } == 0 as if after then " " else ""
131
+ warn if space_after.spaced then "Unexpected whitespace after semicolon." else "Missing whitespace after semicolon."
132
+ }
133
+
134
+ in ts_comma rule es-comma-spacing:before {
135
+ option before boolean = false "space before comma"
136
+ option after boolean = true "space after comma"
137
+ # Cheapest and most selective first: most commas are spaced as wanted.
138
+ let comma_space layout_comma_space(space_before.text, true)
139
+ when before != (comma_space != "")
140
+ when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
141
+ when previous_token != null && previous_token not ts_comma
142
+ when !(parent is (ts_array, ts_array_pattern) && previous_token is ts_open_bracket)
143
+ when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
144
+ fix space_before as if before then " " else ""
145
+ warn if before then "A space is required before ','." else "There should be no space before ','."
146
+ }
147
+
148
+ in ts_comma rule es-comma-spacing:after {
149
+ option before boolean = false "space before comma"
150
+ option after boolean = true "space after comma"
151
+ # Cheapest and most selective first: most commas are spaced as wanted.
152
+ let comma_space layout_comma_space(space_after.text, false)
153
+ when after != (comma_space != "")
154
+ when !re_test(comma_space, /[\n\r\u{2028}\u{2029}]/)
155
+ when next_token != null
156
+ when space_after.comments || next_token not (ts_comma, ts_close_paren, ts_close_bracket, ts_close_brace)
157
+ when !(parent is (ts_array, ts_array_pattern) && previous_token is (ts_open_bracket, ts_comma) && next_token is (ts_comma, ts_close_bracket))
158
+ when !(parent.layout_last_type_parameter_end != null && space_before.start <= parent.layout_last_type_parameter_end && parent.layout_last_type_parameter_end <= offset)
159
+ when after || !{ value space_after.text; trim_start; starts_with "//" }
160
+ fix space_after as if after then " " else ""
161
+ warn if after then "A space is required after ','." else "There should be no space after ','."
162
+ }
163
+
164
+ group semicolon_statement ts_expression_statement, ts_variable_declaration, ts_return, ts_throw, ts_break, ts_continue, ts_do_while, ts_import, ts_import_equals, ts_type_alias, ts_class_property, ts_debugger, ts_export_assignment, ts_export, ts_method, ts_function
165
+ fn layout_next_position_len(source, offset) = {
166
+ value char: byte_char(source, offset)
167
+ value if char == "\r" && byte_char(source, offset + 1) == "\n" then 2 else byte_len(char)
168
+ }
169
+ in semicolon_statement rule es-semi {
170
+ option mode "always" | "never" = "always" "statement semicolon policy"
171
+ option omitLastInOneLineBlock boolean = false "omit final semicolons in single-line blocks"
172
+ option omitLastInOneLineClassBody boolean = false "omit final semicolons in single-line class bodies"
173
+ option beforeStatementContinuationChars "always" | "any" | "never" = "any" "semicolons before continuation characters"
174
+ when mode != "always" || omitLastInOneLineBlock || omitLastInOneLineClassBody || token(node, "last").text != ";"
175
+ let statement_semi layout_statement_semicolon(source, node, mode, omitLastInOneLineBlock, omitLastInOneLineClassBody)
176
+ when statement_semi != null
177
+ let omit_statement_semi omitLastInOneLineBlock && statement_semi.one_line_block || omitLastInOneLineClassBody && statement_semi.one_line_class
178
+ when if mode == "always" then statement_semi.present == omit_statement_semi else if statement_semi.present then statement_semi.redundant || !statement_semi.class_hazard && !statement_semi.same_line && (!statement_semi.hazard_before || !statement_semi.class_field && beforeStatementContinuationChars == "never" && !statement_semi.hazard_after) else !statement_semi.class_field && beforeStatementContinuationChars == "always" && statement_semi.hazard_before
179
+ report at { start: statement_semi.offset, end: if statement_semi.present then statement_semi.end else statement_semi.offset + layout_next_position_len(source, statement_semi.offset) }
180
+ fix at {start:statement_semi.offset, end:statement_semi.end} as if statement_semi.present then "" else ";"
181
+ warn if statement_semi.present then "Extra semicolon." else "Missing semicolon."
182
+ }
183
+
184
+ }
@@ -0,0 +1,203 @@
1
+ # .ts/layout/shared: structural lists and punctuation.
2
+
3
+ import .ts/helpers
4
+
5
+ define {
6
+
7
+ fn layout_comma_space(gap, before_side) = { value result: if before_side then /\s*$/ else /^\s*/; value re_find(gap, result); nonempty } ?? ""
8
+ fn layout_same_line(gap) = !re_test(gap, /[\n\r\u{2028}\u{2029}]/)
9
+
10
+ fn layout_list_kind(n, field) = when {
11
+ field == "type_parameter" then "generics"
12
+ field == "type_argument" then "type-arguments"
13
+ n is (ts_array, ts_array_pattern) then "arrays"
14
+ n is ts_tuple_type then "tuples"
15
+ n is (ts_object, ts_object_pattern) then "objects"
16
+ n is (ts_function, ts_method, ts_arrow, ts_function_type, ts_call, ts_new) then "functions"
17
+ n is ts_import_call then "dynamicImports"
18
+ n is ts_import then "imports"
19
+ n is ts_export then "exports"
20
+ n is ts_enum then "enums"
21
+ else then null
22
+ }
23
+ in ts_node let layout_list_field {
24
+ value when {
25
+ is ts_variable_declaration.declaration then "declaration"
26
+ is (ts_array.element, ts_array_pattern.element, ts_tuple_type.element) then "element"
27
+ is (ts_object.property, ts_object_pattern.property, ts_object_pattern.rest) then "property"
28
+ is (ts_function.parameter, ts_method.parameter, ts_arrow.parameter, ts_function_type.parameter,
29
+ ts_method_signature.parameter, ts_call_signature.parameter, ts_index_signature.parameter) then "parameter"
30
+ is (ts_call.argument, ts_new.argument, ts_import_call.argument) then "argument"
31
+ is (ts_import.specifier, ts_import.default_import, ts_import.namespace, ts_export.specifier) then "specifier"
32
+ is ts_sequence.expression then "expression"
33
+ is (ts_enum.member, ts_type_literal.member, ts_interface.member) then "member"
34
+ is ts_class.implements then "implements"
35
+ is ts_interface.extends then "extends"
36
+ is (ts_function.type_parameter, ts_arrow.type_parameter, ts_class.type_parameter, ts_method.type_parameter,
37
+ ts_interface.type_parameter, ts_type_alias.type_parameter, ts_function_type.type_parameter,
38
+ ts_method_signature.type_parameter, ts_call_signature.type_parameter, ts_infer.type_parameter) then "type_parameter"
39
+ is (ts_class.type_argument, ts_tagged_template.type_argument, ts_call.type_argument, ts_new.type_argument,
40
+ ts_instantiation.type_argument, ts_jsx_opening.type_argument, ts_type_reference.type_argument,
41
+ ts_type_query.type_argument, ts_import_type.type_argument) then "type_argument"
42
+ else then null
43
+ }
44
+ }
45
+ in ts_node let layout_syntax_lists {
46
+ value owner: node
47
+ value children.nodes
48
+ filter (item) item.layout_list_field != null
49
+ group_by (item) item.layout_list_field
50
+ sort_by (group) when {
51
+ group.key == "type_parameter" then 2
52
+ group.key == "type_argument" then 3
53
+ group.key == "member" then 1
54
+ else then 0
55
+ }
56
+ map (group) {
57
+ field: group.key,
58
+ kind: layout_list_kind(owner, group.key),
59
+ items: { value group.items; sort_by (item) item.offset }
60
+ }
61
+ }
62
+ fn layout_comma_item(n, before) = {
63
+ value edge: if before then n.token_neighbours.before else n.token_neighbours.after
64
+ value tokens: walk(if edge == null then [] else [node_at(edge.start)], current =>
65
+ if current is (ts_comma, ts_open_paren, ts_close_paren) then {
66
+ value next: if before then current.token_neighbours.before else current.token_neighbours.after
67
+ value if next == null then null else node_at(next.start)
68
+ } else null)
69
+ value ancestors: walk([last(tokens)], current => current.parent.node)
70
+ value find(ancestors, item => item.layout_list_field != null && item not ts_hole
71
+ && item.parent.offset <= n.offset && item.parent.end_offset >= n.end_offset
72
+ && (if before then item.end_offset <= n.offset || item.last_token.text == "," && item.last_token.start == n.offset
73
+ else item.offset >= n.end_offset))
74
+ }
75
+ fn layout_comma_context(n) = {
76
+ value before: layout_comma_item(n, true)
77
+ value after: layout_comma_item(n, false)
78
+ value item: before ?? after
79
+ value if item != null then {owner: item.parent.node, field: item.layout_list_field, previous: before}
80
+ else if n.parent is (ts_array, ts_array_pattern) then {owner: n.parent.node, field: "element", previous: null}
81
+ else null
82
+ }
83
+ fn layout_assignment_pattern(n) = {
84
+ value roots: walk([n], current =>
85
+ if current.parent is (ts_array, ts_object, ts_parenthesized) then current.parent.node
86
+ else if current.parent is ts_property && current.parent.value.node == current then current.parent.node
87
+ else null)
88
+ value root: last(roots)
89
+ value root.parent is (ts_assignment, ts_for_in, ts_for_of) && root.parent.left.node == root
90
+ }
91
+ fn layout_dangle_mode(kind, style, policies) = { value policies; get kind; nonempty } ?? style
92
+ fn layout_dangle_enabled(kind, edition) = (kind != "functions" || edition >= 2017) && (kind != "dynamicImports" || edition >= 2025)
93
+ fn layout_dangle_wrong(site, mode) = mode != "ignore" && (if mode == "always" || mode == "always-multiline" && site.multiline then site.trailing != site.allowed else site.trailing && !(mode == "only-multiline" && site.multiline) && !site.tsx_single)
94
+ fn layout_trailing_list(items, kind, style, policies, edition) = {
95
+ value mode: layout_dangle_mode(kind, style, policies)
96
+ value if layout_dangle_enabled(kind, edition) && mode != "ignore" && count(items) > 0
97
+ then {items: items, kind: kind, mode: mode} else null
98
+ }
99
+ fn layout_trailing_site(n, list, tsx) = {
100
+ value item: last(list.items)
101
+ value if item is ts_hole then [] else {
102
+ value after: token(item, "after")
103
+ value trailing: after.text == ","
104
+ value comma: if trailing then after.node ?? first(overlapping(ts_comma, after.start, after.end)) else null
105
+ value closing: if trailing then token(comma, "after") else after
106
+ value if closing.text != "}}" && closing.text != "]" && closing.text != ")" && closing.text != ">" then [] else {
107
+ value terminal_end: if trailing then after.end else token(item, "last").end
108
+ value multiline: position(terminal_end).js_line != position(closing.start).js_line
109
+ value if !multiline && !trailing && list.mode != "always" then [] else {
110
+ value allowed: item not ts_rest && !(item is ts_spread && layout_assignment_pattern(n))
111
+ && item.name not ts_rest
112
+ value [{
113
+ kind: list.kind,
114
+ mode: list.mode,
115
+ offset: if trailing then after.start else terminal_end,
116
+ trailing: trailing,
117
+ multiline: multiline,
118
+ allowed: allowed,
119
+ tsx_single: list.kind == "generics" && count(list.items) == 1 && tsx
120
+ }]
121
+ }
122
+ }
123
+ }
124
+ }
125
+ fn layout_trailing_ordinary(n, style, policies, edition) = when {
126
+ n is (ts_array, ts_array_pattern) then layout_trailing_list(n.element.nodes, "arrays", style, policies, edition)
127
+ n is ts_tuple_type then layout_trailing_list(n.element.nodes, "tuples", style, policies, edition)
128
+ n is ts_object then layout_trailing_list(n.property.nodes, "objects", style, policies, edition)
129
+ n is ts_object_pattern then layout_trailing_list({ value n.property.nodes n.rest.nodes; sort_by (item) item.offset }, "objects", style, policies, edition)
130
+ n is (ts_function, ts_method, ts_arrow, ts_function_type) then layout_trailing_list(n.parameter.nodes, "functions", style, policies, edition)
131
+ n is (ts_call, ts_new) then layout_trailing_list(n.argument.nodes, "functions", style, policies, edition)
132
+ n is ts_import_call then layout_trailing_list(n.argument.nodes, "dynamicImports", style, policies, edition)
133
+ n is ts_import then layout_trailing_list({ value n.specifier.nodes n.default_import.nodes n.namespace.nodes; sort_by (item) item.offset }, "imports", style, policies, edition)
134
+ n is ts_export then layout_trailing_list(n.specifier.nodes, "exports", style, policies, edition)
135
+ n is ts_enum then layout_trailing_list(n.member.nodes, "enums", style, policies, edition)
136
+ else then null
137
+ }
138
+ fn layout_dangle_lists(n, style, policies, edition, tsx) = {
139
+ value lists: [
140
+ layout_trailing_ordinary(n, style, policies, edition),
141
+ if n is (ts_function, ts_arrow, ts_class, ts_method, ts_interface, ts_type_alias, ts_function_type,
142
+ ts_method_signature, ts_call_signature, ts_infer) then layout_trailing_list(n.type_parameter.nodes, "generics", style, policies, edition) else null,
143
+ if n is (ts_class, ts_tagged_template, ts_call, ts_new, ts_instantiation, ts_jsx_opening, ts_type_reference,
144
+ ts_type_query, ts_import_type) then layout_trailing_list(n.type_argument.nodes, "type-arguments", style, policies, edition) else null
145
+ ]
146
+ value lists
147
+ compact
148
+ flat_map (list) layout_trailing_site(n, list, tsx)
149
+ }
150
+ # Every list, independent of options: fixtures pin this view.
151
+ fn layout_trailing_commas(n) = layout_dangle_lists(n, "always", {}, 2025, ends_with(n.file, ".tsx"))
152
+ fn layout_terminal_arrow_block(n, end) = {
153
+ value chain: walk([n], current => if current is ts_block then null
154
+ else find(current.children.nodes, child => child.end_offset == end))
155
+ value terminal: last(chain)
156
+ value terminal is ts_block && terminal.parent is ts_arrow
157
+ }
158
+ fn layout_statement_semicolon(source, n, mode, omit_block, omit_class) = {
159
+ value header: n is ts_variable_declaration
160
+ && (n.parent is ts_for && n.parent.init.node == n
161
+ || n.parent is (ts_for_in, ts_for_of) && n.parent.left.node == n)
162
+ value if header || n is (ts_function, ts_method) && n.body.node != null
163
+ || n is ts_export && n.declaration.node != null then null else {
164
+ value last: token(n, "last")
165
+ value last_node: node_at(last.start)
166
+ value present: last_node is ts_semicolon
167
+ value before: if present then token(last_node, "before") else last
168
+ value next: token(last_node, "after")
169
+ value field: n is ts_class_property
170
+ value closing: next.text == "}}"
171
+ value body_open: if closing && (omit_block && n.parent is ts_static_block
172
+ || omit_class && field && n.parent is ts_class)
173
+ then first(filter(n.parent.token.nodes, token => token.text == "{{")) else null
174
+ value {
175
+ present: present,
176
+ offset: if present then last.start else last.end,
177
+ end: last.end,
178
+ redundant: next == null || contains(["}}", ";"], next.text),
179
+ same_line: mode == "never" && before != null && next != null && layout_same_line(byte_slice(source, before.end, next.start)),
180
+ hazard_before: mode == "never" && next != null && !contains(["++", "--"], next.text)
181
+ && re_test(next.text, /^[-\[(\/+`]/),
182
+ hazard_after: mode == "never" && (when {
183
+ n is (ts_do_while, ts_break, ts_continue, ts_debugger, ts_import) then false
184
+ n is ts_export && n.kind != "default" then false
185
+ n is ts_return then n.argument.node != null
186
+ else then !(before.text == "}}" && layout_terminal_arrow_block(n, before.end))
187
+ }),
188
+ class_field: field,
189
+ class_hazard: mode == "never" && field && (n.init.node == null && n.key is ts_identifier
190
+ && contains(["get", "set", "static"], n.key.text)
191
+ && !(n.key.text == "static" && any(n.modifier.nodes, modifier => modifier.text == "static"))
192
+ || contains(["*", "in", "instanceof"], next.text)),
193
+ one_line_block: omit_block && closing && (n.parent is ts_block && layout_same_line(n.parent.text)
194
+ || n.parent is ts_static_block && layout_same_line(byte_slice(source, n.parent.offset, body_open.offset))),
195
+ one_line_class: omit_class && field && closing && n.parent is ts_class && body_open != null
196
+ && layout_same_line(byte_slice(source, body_open.offset, n.parent.end_offset))
197
+ }
198
+ }
199
+ }
200
+
201
+ @macro layout_comment_pattern "(?:/\\*(?:[^*]|\\*+[^*/])*\\*+/|//[^\\n\\r\\u{{2028}}\\u{{2029}}]*)"
202
+
203
+ }