@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/json.dx ADDED
@@ -0,0 +1,104 @@
1
+ # .json: JSON/JSONC values, nested lists and comments.
2
+
3
+ import .core
4
+
5
+ lang json "*.json" "*.jsonc" {
6
+ match json_value
7
+ match json_line_comment
8
+ match json_block_comment
9
+ }
10
+
11
+ define {
12
+ match json_line_comment like core_line_comment @core_line_comment_pattern
13
+ match json_block_comment like core_block_comment @core_block_comment_pattern
14
+ match json_string /"/ until /"/
15
+ in json_string match json_escape /\\[\s\S]/
16
+ match json_number /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/
17
+ match json_keyword /\b(?:true|false|null)\b/
18
+ match json_trivia (space or json_line_comment or json_block_comment)+
19
+ match json_value (:json_object or :json_array or :json_string or :json_number or :json_keyword)
20
+ match json_property key:json_string json_trivia? /:/ json_trivia? value:json_value
21
+ match json_object /\{/ until /\}/
22
+ match json_array /\[/ until /\]/
23
+ in json_object {
24
+ match json_property
25
+ match json_comma /,/
26
+ match json_line_comment
27
+ match json_block_comment
28
+ }
29
+ in json_array {
30
+ match json_value
31
+ match json_comma
32
+ match json_line_comment
33
+ match json_block_comment
34
+ }
35
+
36
+ group json_list json_object, json_array
37
+ group core_list json_list
38
+ group json_comment json_line_comment, json_block_comment
39
+ fn json_edge(item, tail) = {
40
+ value head: slice(item.text, 0, 1)
41
+ value bracketed: contains(["[", "{{"], head)
42
+ value {
43
+ start: if tail && bracketed then item.end_offset - 1 else item.offset,
44
+ end: if !tail && bracketed then item.offset + 1 else item.end_offset,
45
+ text: if !bracketed then item.text else if !tail then head else {
46
+ value tail_start: len(item.text) - 1
47
+ value item.text
48
+ slice tail_start
49
+ },
50
+ comment: false,
51
+ }
52
+ }
53
+ in json_property let json_child_edges [json_edge(key.node, false), json_edge(value.node, true)]
54
+ in json_value let json_child_edges [json_edge(node, false), json_edge(node, true)]
55
+ in json_comma, json_comment let json_child_edges [{ start: offset, end: end_offset, text: text, comment: is json_comment }]
56
+ in json_list let json_items filter(children.node, child => child is (json_property, json_value))
57
+ in json_list let json_edges { value children.node; fold ([]) (edges, child) edges child.json_child_edges }
58
+ in json_list let json_plain filter(json_edges, edge => !edge.comment)
59
+ in json_list let json_open { start: offset, end: offset + 1, text: slice(text, 0, 1) }
60
+ in json_list let json_close { start: end_offset - 1, end: end_offset, text: { value result: len(text) - 1; value slice(text, result) } }
61
+ in json_list let json_first { value json_edges; first; nonempty } ?? json_close
62
+ in json_list let json_last { value json_edges; last; nonempty } ?? json_open
63
+ in json_list let json_layout_context {
64
+ kind: if is json_object then "JSONObjectExpression" else "JSONArrayExpression",
65
+ brackets: if is json_object then "{{}}" else "[]",
66
+ count: count(json_items), single: !re_test(text, /[\n\r\u{2028}\u{2029}]/),
67
+ first_broken: count(json_items) > 0 && {
68
+ value offset_2: first(json_items).offset
69
+ value byte_slice(source, offset + 1, offset_2)
70
+ re_test /[\n\r\u{2028}\u{2029}]/
71
+ },
72
+ empty_comments: count(json_edges) > 0,
73
+ }
74
+ in json_list let json_spacing_pairs [[json_open, json_first]] (if count(json_edges) == 0 then [] else [[json_last, json_close]])
75
+ in json_list let json_wrapping_pairs ([[json_open, { value json_plain; first; nonempty } ?? json_close]]
76
+ {
77
+ value json_items
78
+ slice 1
79
+ map (item) [{ value json_plain; filter (edge) edge.end <= item.offset; last }, { value if item is json_property then item.key.node else item; json_edge false }]
80
+ }
81
+ [[{ value json_plain; last; nonempty } ?? json_open, json_close]])
82
+ in json_list let json_layout_comments descendants(json_comment)
83
+ in json_list let core_list_layout (
84
+ map(json_spacing_pairs, pair => {
85
+ value pair
86
+ before: first
87
+ value pair
88
+ after: last
89
+ value core_list_site(source, json_layout_context, "spacing", before, after, json_layout_comments)
90
+ })
91
+ (if json_layout_context.count > 0 then map(json_wrapping_pairs, pair => {
92
+ value pair
93
+ before_2: first
94
+ value pair
95
+ after_2: last
96
+ value core_list_site(source, json_layout_context, "wrapping", before_2, after_2, json_layout_comments)
97
+ }) else map(json_spacing_pairs, pair => {
98
+ value pair
99
+ before_3: first
100
+ value pair
101
+ after_3: last
102
+ value core_list_site(source, json_layout_context, "empty_wrapping", before_3, after_3, json_layout_comments)
103
+ })))
104
+ }
@@ -0,0 +1,11 @@
1
+ # std/markdown: code fences, inline code and HTML comments, so prose rules
2
+ # can stay out of code samples.
3
+
4
+ import .core
5
+
6
+ lang markdown "*.md" "*.markdown" {
7
+ match md_fence /^[ \t]*```/ until /^[ \t]*```/
8
+ match md_tilde_fence /^[ \t]*~~~/ until /^[ \t]*~~~/
9
+ match md_code /`/ until /`|$/
10
+ match md_comment like core_html_comment @core_html_comment_pattern
11
+ }
package/std/python.dx ADDED
@@ -0,0 +1,21 @@
1
+ # std/python: Python structure. Triple-quoted strings win over plain ones
2
+ # because the longest match at a position wins.
3
+
4
+ import .core
5
+
6
+ lang python "*.py" "*.pyi" {
7
+ match parentheses
8
+ match brackets
9
+ match braces
10
+ }
11
+
12
+ group py_code .python, parentheses, brackets, braces
13
+
14
+ in py_code {
15
+ match py_comment /#/ until /$/
16
+ match py_triple_string /(?:\b[rRbBuUfF]{1,2})?"""/ until /"""/
17
+ match py_triple_single_string /(?:\b[rRbBuUfF]{1,2})?'''/ until /'''/
18
+ match py_string /(?:\b[rRbBuUfF]{1,2})?"/ until /"|$/
19
+ match py_single_string /(?:\b[rRbBuUfF]{1,2})?'/ until /'|$/
20
+ }
21
+ in py_string, py_single_string, py_triple_string, py_triple_single_string match py_escape /\\(?:.|\n)/
@@ -0,0 +1,316 @@
1
+ # .rs/format: Rust layout rules modelled on topiary's Rust formatting.
2
+ # Topiary drops every space it is not told to keep, so most rules are "one
3
+ # space here" or "no space there". Unlike topiary, macro arguments in
4
+ # `(...)` and `[...]` are checked like other code (`vec![...]`,
5
+ # `assert_eq!(...)`); `name! { ... }` bodies and attribute arguments are
6
+ # leaves: nothing inside them is checked. `pub(crate)` stays tight. Rule
7
+ # names start with `rs-`, apart from the language-neutral ones in
8
+ # `.whitespace`.
9
+
10
+ define {
11
+
12
+ # ---- leaves ------------------------------------------------------------------
13
+
14
+ match rs_attribute like brackets /(?<=#!?[ \t]*)\[/ until /\]/
15
+ match rs_macro_brace like braces /(?<=\w![ \t]*(?:\w+[ \t]*)?)\{/ until /\}/
16
+ in rs_code match rs_attribute
17
+ in rs_code match rs_macro_brace
18
+ @macro in_leaf (rs_attribute != null || rs_macro_brace != null)
19
+
20
+ # Things that end an operand: an identifier, literal, closing bracket or `?`.
21
+ @macro operand_end /[\w)\]}'"?]/
22
+
23
+ # ---- whitespace ----------------------------------------------------------------
24
+
25
+ # Topiary keeps whitespace-only lines as they are; `:blank` cleans them too.
26
+ in rs_code match rs_trailing_space /(?<=\S)[ \t]+(?=\r?\n|\z)/
27
+ in rs_trailing_space rule rs-no-trailing-spaces warn "Trailing whitespace." {
28
+ when !@in_leaf
29
+ fix as ""
30
+ }
31
+ in rs_code match rs_blank_line_space /^[ \t]+(?=\r?\n)/
32
+ in rs_blank_line_space rule rs-no-trailing-spaces:blank warn "Whitespace on a blank line." {
33
+ when !@in_leaf
34
+ fix as ""
35
+ }
36
+
37
+ in rs_code match rs_multi_space /(?<=\S)[ \t]{2,}(?=\S)/
38
+ in rs_multi_space rule rs-no-multi-spaces warn "Multiple spaces." {
39
+ when !@in_leaf
40
+ fix as " "
41
+ }
42
+
43
+ in rs_code match rs_blank_run /^(?:[ \t]*\r?\n){2,}/
44
+ in rs_blank_run rule rs-no-multiple-empty-lines warn "More than one blank line." {
45
+ report change
46
+ fix as re_find(text, /^[ \t]*\r?\n/)
47
+ }
48
+ # No blank lines before a closing brace or at the end of the file.
49
+ in rs_code match rs_blank_before_close /(?<=\S[ \t]*\r?\n)(?:[ \t]*\r?\n)+(?=[ \t]*[}\]])/
50
+ in rs_blank_before_close rule rs-no-blank-before-close warn "Blank line before a closing bracket." {
51
+ when !@in_leaf
52
+ fix as ""
53
+ }
54
+ in .rust layer text match rs_end /[^\n]\z|(?<=\n)(?:[ \t]*\r?\n)+\z/
55
+ in rs_end rule rs-eol-last warn "The file must end in exactly one line break." {
56
+ fix as if ends_with(text, "\n") then "" else text @newline
57
+ }
58
+
59
+ # ---- paths, fields, calls ---------------------------------------------------------
60
+
61
+ # `a :: b`, `a . b`, `x ?`
62
+ in rs_code match rs_path_space /[ \t]+::[ \t]*|(?<=\S)::[ \t]+/
63
+ in rs_path_space rule rs-path-spacing warn "No spaces around `::`." {
64
+ when !@in_leaf
65
+ fix as "::"
66
+ }
67
+ in rs_code match rs_dot_space /(?<=[\w)\]}?])[ \t]+\.(?=[\w])|(?<=[\w)\]}?]\.)[ \t]+(?=\w)/
68
+ in rs_dot_space rule rs-dot-spacing warn "No spaces around `.`." {
69
+ when !@in_leaf
70
+ fix as "."
71
+ }
72
+ in rs_code match rs_try_space /(?<=[\w)\]}])[ \t]+\?/
73
+ in rs_try_space rule rs-try-spacing warn "No space before `?`." {
74
+ when !@in_leaf
75
+ fix as "?"
76
+ }
77
+
78
+ # Keywords that may be followed by `(` or `[` with a space.
79
+ @macro rs_keyword_lead /\b(?:if|match|while|for|in|return|let|as|else|move|mut|ref|where|impl|dyn|fn|pub|unsafe|async|await|break|const|static|type|use|mod|struct|enum|trait|extern|crate|loop|yield)[ \t]+$/
80
+ # `f (x)` -> `f(x)`, `a [i]` -> `a[i]`; `fn name (` keeps its space.
81
+ in rs_code match rs_call_space /(?<=[\w)\]>])[ \t]+(?=[(\[])/
82
+ in rs_call_space rule rs-call-spacing warn "No space before a call's `(` or an index's `[`." {
83
+ when !@in_leaf && !re_test(before text, @rs_keyword_lead) && !re_test(before, /\bfn[ \t]+\w+(?:<[^(){};]*>)?$|\b(?:Fn|FnMut|FnOnce)$|'\w+$|[=-]>$/)
84
+ fix as ""
85
+ }
86
+ # `fn name(` -> `fn name (`, and `Fn(T)` sugar -> `Fn (T)`.
87
+ in rs_code match rs_function_name /(?<=\bfn[ \t]+)\w+(?:<[^(){};]*>)?(?=\()|\b(?:Fn|FnMut|FnOnce)(?=\()/
88
+ in rs_function_name rule rs-fn-parenthesis-spacing warn "One space between a function's name and its parameters." {
89
+ report change
90
+ when !@in_leaf
91
+ fix as text " "
92
+ }
93
+
94
+ # ---- brackets ------------------------------------------------------------------
95
+
96
+ in rs_code match rs_parenthesis_inner_space /(?<=\()[ \t]+(?=[^\s)])|(?<=[^\s(])[ \t]+(?=\))/
97
+ in rs_parenthesis_inner_space rule rs-parenthesis-spacing warn "No spaces just inside parentheses." {
98
+ when !@in_leaf
99
+ fix as ""
100
+ }
101
+
102
+ # Array literals get a space inside; index and array types do not.
103
+ # Also `&[1, 2]` and nested `[[0; 2]; 2]`; `x: &[u8]` is a type.
104
+ match rs_array like brackets /(?<=(?:[=(,\[]|=>|\breturn|\bin)[ \t]*(?:&[ \t]*(?:mut[ \t]+)?)?)\[/ until /\]/
105
+ in rs_code match rs_array
106
+ in rs_array rule rs-array-spacing warn "Single-line arrays have a space inside the brackets." {
107
+ report change
108
+ when !@in_leaf && !contains(text, "\n") && text != "[]"
109
+ fix as "[ " { value text; slice 1 -1; trim } " ]"
110
+ }
111
+ # Index brackets: `a[i]`, never `a[ i ]`.
112
+ match rs_index like brackets /(?<=[\w)\]?])\[/ until /\]/
113
+ in rs_code match rs_index
114
+ in rs_code match rs_bracket_inner_space /(?<=\[)[ \t]+(?=[^\s\]])|(?<=[^\s\[])[ \t]+(?=\])/
115
+ in rs_bracket_inner_space rule rs-bracket-spacing warn "No spaces just inside index brackets." {
116
+ when !@in_leaf && parent is rs_index
117
+ fix as ""
118
+ }
119
+
120
+ # `x{` -> `x {`; single-line `{x}` -> `{ x }`; `{}` -> `{ }`.
121
+ in braces rule rs-brace-spacing:before warn "One space before `\{`." {
122
+ report change
123
+ when !@in_leaf && not rs_macro_brace && re_test(before, /[\w)>\]']$/)
124
+ fix as " " text
125
+ }
126
+ in braces rule rs-brace-spacing:inside warn "Single-line braces have one space inside." {
127
+ report change
128
+ when !@in_leaf && not rs_macro_brace && !contains(text, "\n")
129
+ fix as if { value text; slice 1 -1; trim } == "" then "{{ }}" else "{{ " { value text; slice 1 -1; trim } " }}"
130
+ }
131
+
132
+ # `use a::{ b::c, d }`: a list holding paths, globs or nested lists has one
133
+ # item per line.
134
+ match rs_use_list like braces /(?<=\buse\b[^;]*::[ \t]*)\{/ until /\}/
135
+ in rs_code match rs_use_list
136
+ in rs_use_list match rs_use_list
137
+ # Reported at each comma followed by another item on its line (commas of
138
+ # nested lists belong to those lists). An overlay, so comma-spacing still
139
+ # sees the comma.
140
+ in rs_use_list layer text match rs_use_comma /,(?=[ \t]*[^\s}\/])/
141
+ in rs_use_comma rule rs-use-list-breaks warn "Use lists with paths have one item per line." {
142
+ when {
143
+ value containing(rs_use_list).text
144
+ slice 1 -1
145
+ re_replace /\{[^{}]*\}/ "{{}}"
146
+ re_replace /\{[^{}]*\}/ "{{}}"
147
+ re_test /::|\*|\{/
148
+ }
149
+ }
150
+
151
+ # ---- punctuation ------------------------------------------------------------
152
+
153
+ in rs_code match rs_comma_space_before /[ \t]+(?=,)/
154
+ in rs_comma_space_before rule rs-comma-spacing:before warn "No space before `,`." {
155
+ when !@in_leaf
156
+ fix as ""
157
+ }
158
+ in rs_code match rs_comma_tight /,(?=[^\s)\]}>,])/
159
+ in rs_comma_tight rule rs-comma-spacing:after warn "One space after `,`." {
160
+ when !@in_leaf
161
+ fix as ", "
162
+ }
163
+ in rs_code match rs_semicolon_space_before /(?<=\S)[ \t]+;/
164
+ in rs_semicolon_space_before rule rs-semi-spacing warn "No space before `;`." {
165
+ when !@in_leaf
166
+ fix as ";"
167
+ }
168
+
169
+ # `a : T` -> `a: T`, `a:T` -> `a: T` (not `::`).
170
+ in rs_code match rs_colon_space_before /(?<=[\w)\]>'])[ \t]+:(?!:)/
171
+ in rs_colon_space_before rule rs-colon-spacing:before warn "No space before `:`." {
172
+ when !@in_leaf
173
+ fix as ":"
174
+ }
175
+ # Not `Token![:]`.
176
+ in rs_code match rs_colon_tight /(?<![:\s\[]):(?![:\s\]])/
177
+ in rs_colon_tight rule rs-colon-spacing:after warn "One space after `:`." {
178
+ when !@in_leaf
179
+ fix as ": "
180
+ }
181
+
182
+ # ---- operators ------------------------------------------------------------------
183
+
184
+ # `& x` -> `&x`, `& mut` -> `&mut`, `* const` -> `*const`; unary `!`, `-`, `*`.
185
+ # A line-leading operator continues the previous line: not unary.
186
+ in rs_code match rs_unary_space /(?<![\w)\]}'"?\r\n&*!-][ \t]*)(?:&&?|\*|!|-)[ \t]+(?=[\w&(\[*'"!|-])/
187
+ in rs_unary_space rule rs-unary-spacing warn "No space after a unary operator or `&`." {
188
+ when !@in_leaf
189
+ fix as trim(text)
190
+ }
191
+
192
+ # Binary operators and arrows: one space on each side. `<` and `>` are left
193
+ # alone (generics), and so are `&` and `|` (references and closures).
194
+ @macro rs_infix_operators /(?:<<=|>>=|==|!=|<=|>=|&&|\|\||->|=>|[+\-*\/%^|&]=|=|\+|[*\/%^]|(?<=[\w)\]] ?)-)/
195
+ in rs_code match rs_infix_tight /(?<=[\w)\]}'"?])(?:<<=|>>=|==|!=|<=|>=|&&|\|\||->|=>|[+\-*\/%^|&]=|=(?![=>])|\+(?<![0-9][eE]\+)|[*\/%^](?!=)|-(?<![0-9][eE]-)(?![>=]))[ \t]*(?=[\w(\[{"'&!*|.-])|(?<=[ \t])(?:<<=|>>=|==|!=|<=|>=|&&|\|\||->|=>|[+\-*\/%^|&]=|(?<![=!<>+\-*\/%^|&])=(?![=>])|\+(?!=)|[\/%^](?!=))(?=[\w(\["'&!|.-])/
196
+ in rs_infix_tight rule rs-operator-spacing warn "Operator `{trim(text)}` needs a space on each side." {
197
+ report change
198
+ when !@in_leaf
199
+ when text != trim(text) " " || !re_test(before, /[ \t]$/)
200
+ fix as (if re_test(before, /[ \t]$/) then "" else " ") trim(text) " "
201
+ }
202
+ # `)->T` style tight arrows after a parenthesis are covered above; `T>` before
203
+ # `->` too. Generic brackets stay tight: `Vec< T >` -> `Vec<T>`.
204
+ in rs_code match rs_generic_open_space /(?<=\w<)[ \t]+(?=[\w&'(\[])/
205
+ in rs_generic_open_space rule rs-generic-spacing:open warn "No space after a generic's `<`." {
206
+ when !@in_leaf
207
+ fix as ""
208
+ }
209
+
210
+ # ---- closures ------------------------------------------------------------------
211
+
212
+ in rs_code match rs_closure_params /(?<=(?:[=(,{:]|=>|\bmove|\breturn)[ \t]*)\|[^|\r\n]*\|/
213
+ in rs_closure_params rule rs-closure-spacing warn "Closure parameters sit tight inside their pipes." {
214
+ when !@in_leaf
215
+ fix as "|" { value text; slice 1 -1; trim } "|"
216
+ }
217
+ # `|x|x + 1` -> `|x| x + 1`: the body's first character.
218
+ in rs_code match rs_closure_tight /(?<=(?:[=(,{:]|=>|\bmove|\breturn)[ \t]*\|[^|\r\n]*\|)[^\s,)]/
219
+ in rs_closure_tight rule rs-closure-spacing:after warn "One space after closure parameters." {
220
+ report change
221
+ when !@in_leaf
222
+ fix as " " text
223
+ }
224
+
225
+ # ---- keywords ------------------------------------------------------------------
226
+
227
+ # `if(x)`, `return(a, b)`, `move|x|`; `{` is brace-spacing's, and `impl<T>`
228
+ # stays tight.
229
+ in rs_code match rs_keyword_tight /(?<![\w.:#'$])(?:if|match|while|for|in|return|let|as|else|move|where|impl|dyn|unsafe|async|static|const|use|mod|struct|enum|trait|extern|fn|loop|ref)(?=[(\[|&*!"'-])/
230
+ in rs_keyword_tight rule rs-keyword-spacing:after warn "One space after `{text}`." {
231
+ when !@in_leaf
232
+ fix as text " "
233
+ }
234
+ in rs_code match rs_keyword_before /(?<=[)\]}])(?:else|as|in|where)\b/
235
+ in rs_keyword_before rule rs-keyword-spacing:before warn "One space before `{text}`." {
236
+ when !@in_leaf
237
+ fix as " " text
238
+ }
239
+
240
+ # ---- indentation -------------------------------------------------------------
241
+ # Block indentation: one unit deeper than the line its bracket opened on; a
242
+ # line starting with that bracket's closer sits at the opener's indentation.
243
+ # Continuation lines (`.method()`, `&& b`, a line after `=`) get one more
244
+ # unit. Unlike topiary, nothing else moves: no staircases, no counter-indents.
245
+
246
+ # Each code line: its indentation and first character. Its own layer, so the
247
+ # character still belongs to whatever node starts there.
248
+ in rs_code layer lines match rs_line /^[ \t]*\S/
249
+ group rs_strings_and_comments rs_line_comment, rs_block_comment, rs_string, rs_raw_string, rs_raw_string_hash, rs_raw_string_double_hash, rs_char
250
+
251
+ @macro line_indent slice(text, 0, -1)
252
+ @macro line_code text ({ value text_after; re_find /^[^\r\n]*/; nonempty } ?? "")
253
+ @macro line_code_trimmed ({ value @line_code; trim_start })
254
+ # `auto`: a tab when the code around (the block's opener line, else the line
255
+ # above) is indented with tabs, else 4 spaces. Never the line itself: a tab
256
+ # line in a spaces file is the mistake.
257
+ @macro indent_unit (if unit != "auto" then unit else if starts_with(indent_anchor, "\t") || indent_anchor == "" && { value @previous_indent; starts_with "\t" } then "\t" else " ")
258
+ # Lines in a block are one level deeper than the line the block opened on,
259
+ # however many brackets opened there (`foo(Bar {` is one level, not two).
260
+ # A block opened at the end of a continuation line (`for x in a⏎ .b()? {`)
261
+ # may also be measured from the statement, one level up: both occur.
262
+ @macro opener_continues ({ value indent_anchor_line; trim_start; re_test /^(?:\.[^.]|\?|&&|\|\||[+\-*\/%^][ \t]|=>|->)/ })
263
+ @macro statement_anchor (if @opener_continues && len(indent_anchor) >= { value @indent_unit; len } then { value result: len(indent_anchor) - { value @indent_unit; len }; value slice(indent_anchor, 0, result) } else indent_anchor)
264
+ @macro level_from(anchor) (if !indented then "" else if indent_closes then anchor else anchor @indent_unit)
265
+ @macro block_level @level_from(indent_anchor)
266
+ @macro statement_level ({ value statement_anchor: @statement_anchor; value @level_from(statement_anchor) })
267
+ # The previous line without its trailing comment.
268
+ @macro previous_line_code ({ value previous_line.nonempty ?? ""; re_replace /(?:^|\s)\/\/[^"]*$|\/\*.*\*\/\s*$/ ""; trim })
269
+ @macro previous_indent ({ value previous_line.nonempty ?? ""; re_find /^[ \t]*/; nonempty } ?? "")
270
+ # The previous line leaves its statement open: not after `;`, `,`, brackets,
271
+ # attributes, comments or a blank line.
272
+ @macro previous_open @previous_line_code != "" && !{ value @previous_line_code; re_test /[;,{}()\[\]]$|^#|^\*|\*\/$/ }
273
+ @macro starts_continuing ({ value @line_code_trimmed; re_test /^(?:\.[^.]|\?|&&|\|\||[+\-*\/%][ \t]|=>|->|as\b)/ })
274
+ # `where` bounds keep their extra unit across the commas between them.
275
+ @macro in_where @previous_indent == @block_level @indent_unit && { value @previous_line_code; re_test /^(?:where[ \t]+)?[\w'][\w<>:' +?]*:[ \t][^;{}]*,$/ }
276
+ @macro continues !indent_closes && !{ value @line_code_trimmed; re_test /^(?:where\b|\{)/ } && (@starts_continuing || @previous_open || @in_where)
277
+ @macro continuation (if @continues then @indent_unit else "")
278
+ # Operator lines (`&& b`, `^ salt`) and method chains inside a hanging
279
+ # bracket (`Ok(chunks⏎ .collect())`): the codebase uses both flat and
280
+ # indented forms, so the first line may pick either. The rest of a chain
281
+ # always lines up with the line above it.
282
+ @macro operator_line /^(?:&&|\|\||[+\-*\/%^|&][ \t]|==|!=|<=|>=|[<>][ \t])/
283
+ @macro starts_operator ({ value @line_code_trimmed; re_test @operator_line })
284
+ # The previous line starts with the same operator (`&&` after `&&`): a nested
285
+ # `== b` line in between does not count.
286
+ @macro previous_starts_operator ({ value previous_line.nonempty ?? ""; trim_start; re_find @operator_line } == { value @line_code_trimmed; re_find @operator_line })
287
+ @macro starts_dot ({ value @line_code_trimmed; re_test /^\.[^.]/ })
288
+ @macro previous_starts_dot ({ value previous_line.nonempty ?? ""; trim_start; re_test /^\.[^.]/ })
289
+ @macro is_comment ({ value @line_code_trimmed; starts_with "//" })
290
+ @macro next_indent ({ value next_line.nonempty ?? ""; re_find /^[ \t]*/; nonempty } ?? "")
291
+ # Every indentation this line may have; the first is the preferred one.
292
+ @macro acceptable (
293
+ if @starts_operator && @previous_starts_operator || @starts_dot && @previous_starts_dot then [@previous_indent]
294
+ else if @starts_operator || @starts_dot && indent_hangs then [@block_level @indent_unit, @block_level, @statement_level @indent_unit, @statement_level]
295
+ else [@block_level @continuation, @statement_level @continuation]
296
+ )
297
+ # Comment lines may also follow the code line above or below them.
298
+ @macro rs_allowed_indents (if @is_comment then @acceptable [@next_indent] (if @previous_line_code != "" then [@previous_indent] else []) else @acceptable)
299
+ @macro expected_indent ({ value @acceptable; first })
300
+
301
+ @macro describe_indent(whitespace) (if whitespace == "" then "none" else if !contains(whitespace, " ") then { value whitespace; len; str } " tab(s)" else if !contains(whitespace, "\t") then { value whitespace; len; str } " spaces" else "a mix of tabs and spaces")
302
+ in rs_line rule rs-indent warn "Expected indentation of { { value expected_indent: @expected_indent; value @describe_indent(expected_indent) } }, found { { value line_indent: @line_indent; value @describe_indent(line_indent) } }." {
303
+ option unit "auto" | string = "auto" "one level of indentation, or \"auto\" (tabs if the code around uses them, else 4 spaces)"
304
+ indent in parentheses, brackets, braces
305
+ report change
306
+ # Lines inside multi-line strings and block comments are their own business.
307
+ when containing(rs_strings_and_comments) == null
308
+ when !{ value text_or_list: @rs_allowed_indents; value needle: @line_indent; value contains(text_or_list, needle) }
309
+ fix as @expected_indent slice(text, -1)
310
+ }
311
+
312
+ # `# [x]` -> `#[x]`, `#[x ]` -> `#[x]`
313
+ in rs_code match rs_attribute_space /(?<=#!?)[ \t]+(?=\[)/
314
+ in rs_attribute_space rule rs-attribute-spacing warn "No space between `#` and `[`." fix as ""
315
+
316
+ }
package/std/rust.dx ADDED
@@ -0,0 +1,34 @@
1
+ # .rs: Rust's structure. Strings, chars and comments are layers, so
2
+ # brackets, quotes and comments inside them never confuse anything else.
3
+ # Brings the Rust layout rule definitions (`.rs/format`), all off.
4
+
5
+ import .core
6
+
7
+ lang rust "*.rs" {
8
+ match parentheses
9
+ match brackets
10
+ match braces
11
+ }
12
+
13
+ group rs_code .rust, parentheses, brackets, braces
14
+
15
+ in rs_code {
16
+ match rs_line_comment /\/\// until /$/
17
+ match rs_block_comment /\/\*/ until /\*\// {
18
+ match rs_block_comment
19
+ }
20
+
21
+ match rs_string /(?:\bb)?"/ until /"/ {
22
+ match rs_escape /\\(?:.|\n)/
23
+ }
24
+
25
+ # Raw strings cannot escape anything, so they have no children.
26
+ match rs_raw_string /\bb?r"/ until /"/
27
+ match rs_raw_string_hash /\bb?r#"/ until /"#/
28
+ match rs_raw_string_double_hash /\bb?r##"/ until /"##/
29
+
30
+ # 'a' and '\n' are chars; 'a on its own is a lifetime and stays code.
31
+ match rs_char /(?:\bb)?'(?:\\(?:u\{[0-9a-fA-F]{1,6}\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'/
32
+ }
33
+
34
+ import .rs/format
package/std/toml.dx ADDED
@@ -0,0 +1,19 @@
1
+ # std/toml: TOML structure.
2
+
3
+ import .core
4
+
5
+ lang toml "*.toml" {
6
+ match brackets
7
+ match braces
8
+ }
9
+
10
+ group toml_code .toml, brackets, braces
11
+
12
+ in toml_code {
13
+ match toml_comment /#/ until /$/
14
+ match toml_multiline_string /"""/ until /"""/
15
+ match toml_multiline_literal /'''/ until /'''/
16
+ match toml_string /"/ until /"|$/
17
+ match toml_literal /'/ until /'|$/
18
+ }
19
+ in toml_string, toml_multiline_string match toml_escape /\\(?:.|\n)/
@@ -0,0 +1,87 @@
1
+ import .ts/values/inspection
2
+ define {
3
+
4
+ fn ts_value_print_literal_parts(parts) = parts != null && len(parts) > 0
5
+ && any(parts, arm => any(arm, part => !contains(part.traits, "parameter")))
6
+ && all(parts, arm => all(filter(arm, part => !contains(part.traits, "parameter")),
7
+ part => contains(part.traits, "literal") || part.identity_known && part.identity.kind == "value"))
8
+ # Widening only changes literals: structure would collapse to `object`, so keep
9
+ # the handle's shape unless every printed constituent is a literal.
10
+ fn arithmetic_type_name(n) = {
11
+ value written: ts_value_print_written(n)
12
+ value parts: value_constituents(n)
13
+ value if written != null then written
14
+ else if ts_value_print_literal_parts(parts) then ts_value_print(value_widened(n) ?? parts)
15
+ else ts_value_print_handle(n)
16
+ }
17
+ fn arithmetic_has_type(arms, kinds) = any(arms, arm =>
18
+ any(arm.parts, part => any(kinds, kind => contains(part.traits, kind))))
19
+ fn arithmetic_known(arms) = arms != null && all(arms, arm => any(arm.parts, part =>
20
+ any(["string", "number", "bigint", "boolean", "symbol", "null", "undefined", "any", "unknown", "object", "structural", "callable"],
21
+ trait => contains(part.traits, trait) && !contains(part.uncertain_traits, trait))))
22
+ fn arithmetic_object_type(arm) = any(arm.parts, part =>
23
+ any(["object", "structural", "callable", "constructible"], trait => contains(part.traits, trait)))
24
+ && !arithmetic_has_type([arm], ["string", "number", "bigint", "boolean", "symbol", "null", "undefined", "any", "unknown"])
25
+ fn arithmetic_regexp(arm) = if any(arm.parts, part => part.carrier.description.name == "RegExp") then true
26
+ else if all(arm.parts, part => part.carrier_known) then false else null
27
+ fn arithmetic_string_like(options) = ([]
28
+ (if options.allowAny then ["`any`"] else [])
29
+ (if options.allowBoolean then ["`boolean`"] else [])
30
+ (if options.allowNullish then ["`null`"] else [])
31
+ (if options.allowRegExp then ["`RegExp`"] else [])
32
+ (if options.allowNullish then ["`undefined`"] else []))
33
+ fn arithmetic_operand_errors(subject, t, other, options) =
34
+ if count(t) == 0 || arithmetic_has_type(t, ["symbol", "unknown"])
35
+ || !options.allowAny && arithmetic_has_type(t, ["any"])
36
+ || !options.allowBoolean && arithmetic_has_type(t, ["boolean"])
37
+ || !options.allowNullish && arithmetic_has_type(t, ["null", "undefined"])
38
+ then [{ node: subject, type: arithmetic_type_name(subject) }]
39
+ else {
40
+ value t
41
+ filter (arm) arithmetic_object_type(arm)
42
+ filter (arm) {
43
+ value regexp: arithmetic_regexp(arm)
44
+ value regexp == false || regexp == true && (!options.allowRegExp || arithmetic_has_type(other, ["number"]))
45
+ }
46
+ map (arm) { node: subject, type: if count(t) == 1 then arithmetic_type_name(subject) else ts_value_print_handle(arm.handle) }
47
+ }
48
+
49
+ fn arithmetic_pair_error(left, right, options) = when {
50
+ !options.allowNumberAndString && (arithmetic_has_type(left, ["string"]) && arithmetic_has_type(right, ["number", "bigint"])
51
+ || arithmetic_has_type(right, ["string"]) && arithmetic_has_type(left, ["number", "bigint"])) then "mismatched"
52
+ arithmetic_has_type(left, ["number"]) && arithmetic_has_type(right, ["bigint"])
53
+ || arithmetic_has_type(right, ["number"]) && arithmetic_has_type(left, ["bigint"]) then "numeric"
54
+ else then null
55
+ }
56
+
57
+ in ts_node let arithmetic_type value_parts(node)
58
+
59
+ in ts_binary, ts_assignment rule es-restrict-plus-operands {
60
+ option allowAny boolean = true "allow any operands"
61
+ option allowBoolean boolean = true "allow boolean operands"
62
+ option allowNullish boolean = true "allow null and undefined operands"
63
+ option allowNumberAndString boolean = true "allow strings with numbers or bigints"
64
+ option allowRegExp boolean = true "allow regular expression operands"
65
+ option skipCompoundAssignments boolean = false "skip compound assignments"
66
+ when operator == "+" || operator == "+=" && !skipCompoundAssignments
67
+ let settings { allowAny: allowAny, allowBoolean: allowBoolean, allowNullish: allowNullish, allowNumberAndString: allowNumberAndString, allowRegExp: allowRegExp }
68
+ let left_type left.arithmetic_type
69
+ let right_type right.arithmetic_type
70
+ when arithmetic_known(left_type) && arithmetic_known(right_type)
71
+ let errors (arithmetic_operand_errors(left.node, left_type, right_type, settings)
72
+ arithmetic_operand_errors(right.node, right_type, left_type, settings))
73
+ let pair_error if len(errors) == 0 then arithmetic_pair_error(left_type, right_type, settings) else null
74
+ let string_types arithmetic_string_like(settings)
75
+ let string_like (if len(string_types) == 0 then "string" else "string, allowing a string + "
76
+ (if len(string_types) == 1 then first(string_types) else "any of: " join(string_types, ", ")))
77
+ each finding in errors (if pair_error != null then [{ node: node, pair: pair_error }] else []) {
78
+ report at finding.node
79
+ warn when {
80
+ finding.pair == "numeric" then "Numeric '+' operations must either be both bigints or both numbers. Got `{arithmetic_type_name(left.node)}` + `{arithmetic_type_name(right.node)}`."
81
+ finding.pair == "mismatched" then "Operands of '+' operations must be a number or {string_like}. Got `{arithmetic_type_name(left.node)}` + `{arithmetic_type_name(right.node)}`."
82
+ else then "Invalid operand for a '+' operation. Operands must each be a number or {string_like}. Got `{finding.type}`."
83
+ }
84
+ }
85
+ }
86
+
87
+ }