@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,157 @@
1
+ # .ts/style/classes: internal part of .ts/style.
2
+ import .ts/constants
3
+
4
+ define {
5
+
6
+ fn member_display_name(name, key) = if name != "" && key is (ts_string, ts_number, ts_bigint, ts_boolean, ts_null) && !re_test(name, /^[\p{Alphabetic}_$][\p{Alphabetic}\p{Number}_$]*$/) then '"' name '"' else name
7
+ in ts_method, ts_class_property, ts_parameter let style_public_modifier {
8
+ value modifier.nodes
9
+ find (token) token.text == "public"
10
+ }
11
+ in ts_method, ts_class_property, ts_parameter let style_public_range {
12
+ value keyword: style_public_modifier
13
+ value next: token(keyword, "after").start
14
+ value comments: overlapping(ts_comment, keyword.end_offset, next)
15
+ value end: first(comments).offset ?? next
16
+ value if keyword != null then { start: keyword.offset, end: end } else null
17
+ }
18
+ in ts_decorator let after_decorator space_after.end
19
+ in ts_computed_key {
20
+ let member_key_node expression.node
21
+ let member_key_text expression
22
+ let member_key_offset expression.offset
23
+ }
24
+ in ts_method, ts_class_property, ts_method_signature let style_member_name {
25
+ value name: static_name.nonempty ?? key.member_key_text.nonempty ?? key
26
+ value key_2: key.member_key_node.nonempty ?? key.node
27
+ value member_display_name(name, key_2)
28
+ }
29
+ in ts_parameter let style_member_name member_display_name(name, name.node)
30
+ in ts_method, ts_class_property let style_key_offset key.member_key_offset.nonempty ?? key.offset
31
+ in ts_parameter let style_key_offset name.offset
32
+ in ts_method, ts_class_property let style_key_end key.member_key_node.end_offset ?? key.end_offset
33
+ in ts_parameter let style_key_end name.end_offset
34
+ in ts_method, ts_class_property let style_head_end if key is ts_computed_key then token(key.member_key_node, "after").end else key.node.end_offset
35
+ in ts_parameter let style_head_end name.end_offset
36
+ in ts_method, ts_class_property, ts_parameter let style_head_offset { value [] decorator.after_decorator; last; nonempty } ?? offset
37
+ in ts_method, ts_class_property, ts_parameter let style_member { name: style_member_name, head: style_head_offset, end: style_head_end, public: style_public_modifier, key: style_key_offset, key_end: style_key_end }
38
+ in ts_method let class_constructor parent is ts_class && key is (ts_identifier, ts_string) && static_name == "constructor" && !contains([] modifier, "static") && kind != "get" && kind != "set"
39
+ in ts_class_property rule es-explicit-member-accessibility:property {
40
+ option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
41
+ option ignoredMethodNames string[] = [] "Ignored method names."
42
+ option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
43
+ let check overrides.properties.nonempty ?? accessibility
44
+ let shape style_member
45
+ when !re_test(key, /^#/) && (check == "explicit" && !{ value @modifiers; has_accessibility } || check == "no-public" && contains([] modifier, "public"))
46
+ report at if check == "no-public" then shape.public else { start: shape.head, end: shape.end }
47
+ warn "{if check == 'no-public' then 'Public' else 'Missing'} accessibility modifier on class property {shape.name}."
48
+ let edit if check == "no-public" then style_public_range else null
49
+ fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as ""
50
+ }
51
+ in ts_method rule es-explicit-member-accessibility:method {
52
+ option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
53
+ option ignoredMethodNames string[] = [] "Ignored method names."
54
+ option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
55
+ let check {
56
+ value (if class_constructor then overrides.constructors else if kind == "get" || kind == "set" then overrides.accessors else overrides.methods)
57
+ nonempty
58
+ } ?? accessibility
59
+ let shape style_member
60
+ when parent is ts_class && !re_test(key, /^#/) && !contains(ignoredMethodNames, shape.name)
61
+ when check == "explicit" && !{ value @modifiers; has_accessibility } || check == "no-public" && contains([] modifier, "public")
62
+ report at if check == "no-public" then shape.public else { start: shape.head, end: shape.end }
63
+ warn "{if check == 'no-public' then 'Public' else 'Missing'} accessibility modifier on {if kind == 'get' then 'get property accessor' else if kind == 'set' then 'set property accessor' else 'method definition'} {shape.name}."
64
+ let edit if check == "no-public" then style_public_range else null
65
+ fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as ""
66
+ }
67
+ in ts_parameter rule es-explicit-member-accessibility:parameter {
68
+ option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
69
+ option ignoredMethodNames string[] = [] "Ignored method names."
70
+ option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
71
+ let check overrides.parameterProperties.nonempty ?? accessibility
72
+ let shape style_member
73
+ when contains([] modifier, "readonly") && parent is ts_method
74
+ when check == "explicit" && !{ value @modifiers; has_accessibility } || check == "no-public" && contains([] modifier, "public")
75
+ report at if check == "no-public" then shape.public else { start: shape.head, end: shape.end }
76
+ warn "{if check == 'no-public' then 'Public' else 'Missing'} accessibility modifier on parameter property {shape.name}."
77
+ let edit if check == "no-public" then style_public_range else null
78
+ fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as ""
79
+ }
80
+
81
+ # Getters that return a literal: a readonly field says it better.
82
+ in ts_number, ts_bigint, ts_string, ts_regex, ts_null, ts_boolean let member_literal true
83
+ in ts_template let member_literal count(expression) == 0
84
+ in ts_tagged_template let member_literal quasi.member_literal
85
+ in ts_parenthesized let member_literal expression.member_literal
86
+ in ts_return let returned_member_literal argument.member_literal == true
87
+ in ts_block let first_member_literal first([] body.returned_member_literal) == true && first(body.nodes) is ts_return
88
+ in ts_computed_key let member_static_name ts_runtime_string(expression.node)
89
+ in ts_method, ts_class_property let member_static_name static_name.nonempty ?? key.member_static_name
90
+ in ts_method let setter_member_name if kind == "set" then member_static_name else null
91
+ in ts_member, ts_index let written_this_member if object is ts_this && write_site != null then static_name else null
92
+ in style_tree_node let constructor_write_names if is (ts_function, ts_arrow, ts_method, ts_class) then [] else [] written_this_member { value [] children.constructor_write_names; fold ([]) (names, child) names child }
93
+ in ts_method let constructor_member_writes if class_constructor then body.constructor_write_names else []
94
+ in ts_class let constructor_member_writes { value [] member.constructor_member_writes; fold ([]) (names, child) names child }
95
+ in ts_class let setter_member_names [] member.setter_member_name
96
+ in ts_method let literal_member_candidate kind == "get" && !contains([] modifier, "override") && body.first_member_literal && (member_static_name == null || !contains(parent.setter_member_names, member_static_name))
97
+ in ts_class_property let literal_member_candidate contains([] modifier, "readonly") && !contains([] modifier, "declare") && !contains([] modifier, "override") && init.member_literal == true && (member_static_name == null || !contains(parent.constructor_member_writes, member_static_name))
98
+ in ts_method.key rule es-class-literal-property-style {
99
+ option mode ("fields" | "getters") = "fields" "Preferred literal member declaration."
100
+ when mode == "fields" && parent.literal_member_candidate
101
+ let shape parent.style_member
102
+ report at { start: shape.key, end: shape.key_end }
103
+ warn "Literals should be exposed using readonly fields."
104
+ }
105
+ in ts_class_property.key rule es-class-literal-property-style:getters {
106
+ option mode ("fields" | "getters") = "fields" "Preferred literal member declaration."
107
+ when mode == "getters" && parent.literal_member_candidate
108
+ let shape parent.style_member
109
+ report at { start: shape.key, end: shape.key_end }
110
+ warn "Literals should be exposed using getters."
111
+ }
112
+
113
+ in ts_parameter let is_parameter_property { value @modifiers; re_test /\b(?:public|private|protected|readonly)\b/ }
114
+ in ts_method, ts_class_property let is_static contains([] modifier, "static")
115
+ in ts_method let constructor_parameter_properties any([] parameter.is_parameter_property, p => p == true)
116
+ in ts_method let class_constructor_only class_constructor && !constructor_parameter_properties
117
+ in ts_class_property, ts_index_signature, ts_static_block, ts_empty let class_constructor_only false
118
+ in ts_method let class_static_only if class_constructor then !constructor_parameter_properties else is_static && !contains([] modifier, "abstract")
119
+ in ts_class_property let class_static_only is_static && !contains([] modifier, "abstract")
120
+ in ts_index_signature let class_static_only false
121
+ in ts_static_block, ts_empty let class_static_only true
122
+ in ts_class let class_shape if count(member) == 0 then "empty" else if all([] member.class_constructor_only, m => m == true) then "constructor" else if all([] member.class_static_only, m => type_of(m) == "null" || m == true) then "static" else null
123
+ in ts_class let no_extraneous_class_checked true
124
+ @macro extraneous_class_rule(rule-id, shape, message) {
125
+ in ts_class rule rule-id {
126
+ when extends == null && class_shape == shape && no_extraneous_class_checked
127
+ report at if name != null && parent is (ts_program, ts_export, ts_block, ts_namespace) then name.node else node
128
+ warn message
129
+ }
130
+ }
131
+ @extraneous_class_rule(es-no-extraneous-class:empty, "empty", "Unexpected empty class.")
132
+ @extraneous_class_rule(es-no-extraneous-class:constructor, "constructor", "Unexpected class with only a constructor.")
133
+ @extraneous_class_rule(es-no-extraneous-class:static, "static", "Unexpected class with only static properties.")
134
+
135
+ # Constructors that do nothing a missing one would not.
136
+ in ts_identifier let constructor_forwarding_key {spread: false, name: text}
137
+ in ts_rest, ts_spread let constructor_forwarding_key if argument is ts_identifier then {spread: true, name: argument} else null
138
+ in ts_parenthesized forward constructor_forwarding_key, constructor_super_call from expression
139
+ in ts_parameter let constructor_forwarding_key if init == null then name.constructor_forwarding_key else null
140
+ in ts_parameter let constructor_parameter_exempt is_parameter_property || count(decorator) > 0
141
+ in ts_call let constructor_super_call if callee is ts_super then node else null
142
+ in ts_expression_statement let constructor_super_call expression.constructor_super_call
143
+ in ts_block let constructor_super_call if count(body) == 1 then first([] body.constructor_super_call) else null
144
+ fn constructor_spreads_arguments(arg) = arg is ts_spread && arg.argument is ts_identifier && arg.argument == "arguments"
145
+ in ts_method rule es-no-useless-constructor {
146
+ # A public constructor of a derived class may widen a protected one.
147
+ when class_constructor && body != null && count(decorator) == 0 && !{ value @modifiers; re_test /\b(?:private|protected)\b/ }
148
+ when !(ts_class.extends != null && { value @modifiers; re_test /\bpublic\b/ })
149
+ when !any([] parameter.constructor_parameter_exempt)
150
+ let super_call body.constructor_super_call
151
+ when if ts_class.extends == null then count(body.body) == 0 else super_call != null && count(parameter) == count([] parameter.constructor_forwarding_key)
152
+ && (count(super_call.argument) == 1 && { value super_call.argument.nodes; first; constructor_spreads_arguments } || count(parameter) == count(super_call.argument) && [] parameter.constructor_forwarding_key == [] super_call.argument.constructor_forwarding_key)
153
+ report at { start: offset, end: key.end_offset }
154
+ warn "Useless constructor."
155
+ }
156
+
157
+ }
@@ -0,0 +1,75 @@
1
+ # .ts/style/comments: internal part of .ts/style.
2
+
3
+ define {
4
+
5
+ # ---- comments --------------------------------------------------------------------
6
+ fn tslint_comment_edit(source, n) = {
7
+ value line_start: position(n.offset).js_line_start
8
+ value prefix: byte_slice(source, line_start, n.offset)
9
+ value tail: n.after
10
+ value tail_bytes: byte_len(tail)
11
+ value tail_end: n.end_offset + tail_bytes
12
+ value after_end: tail_end + 1
13
+ value after_tail: byte_slice(source, tail_end, after_end)
14
+ value newline: if after_tail == "\r" && position(after_end).line == position(tail_end).line then 2 else if re_test(after_tail, /^[\r\n]/) then 1 else 0
15
+ value standalone: re_test(prefix, /^[ \t]*$/) && re_test(tail, /^[ \t]*$/)
16
+ value trimmed_prefix: re_replace(prefix, /[ \t]+$/, "")
17
+ # Inline multiline comments can carry ASI-significant line breaks.
18
+ value if standalone then { start: line_start, end: n.end_offset + byte_len(tail) + newline, text: "" }
19
+ else if n is ts_line_comment then { start: line_start + byte_len(trimmed_prefix), end: n.end_offset, text: "" }
20
+ else if re_test(n.text, /[\r\n]/) then null
21
+ else {
22
+ start: n.offset,
23
+ end: n.end_offset,
24
+ text: if re_test(prefix, /\S$/) && re_test(tail, /^\S/) && !re_test(tail, /^[;,)}\]]/) then " " else ""
25
+ }
26
+ }
27
+ in ts_comment rule es-ban-tslint-comment {
28
+ let content re_replace(text, /^\/\/|^\/\*|\*\/$/, "")
29
+ let normalised re_replace(content, /^\s+|\s+$/, "")
30
+ when re_test(content, /^\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)/)
31
+ let edit tslint_comment_edit(source, node)
32
+ warn "tslint comment detected: \"{if node is ts_line_comment then '// ' normalised else '/* ' normalised ' */'}\""
33
+ fix at edit if edit != null as edit.text
34
+ }
35
+
36
+ # Only directives before the first code token are triple-slash references.
37
+ in ts_line_comment rule es-triple-slash-reference {
38
+ option lib ("always" | "never") = "always" "When lib directives are allowed."
39
+ option path ("always" | "never") = "never" "When path directives are allowed."
40
+ option types ("always" | "never" | "prefer-import") = "prefer-import" "When types directives are allowed."
41
+ let directive re_replace(text, /^\/\/\/\s*<reference\s*(types|path|lib)\s*=[\s\S]*$/, "$1")
42
+ let module re_replace(text, /^\/\/\/\s*<reference\s*(?:types|path|lib)\s*=\s*["'|](.*)["'|][\s\S]*$/, "$1")
43
+ when re_test(text, /^\/\/\/\s*<reference\s*(?:types|path|lib)\s*=\s*["'|].*["'|]/)
44
+ when { value source; byte_slice 0 offset; re_test /^(?:\s|\/\/[^\r\n]*(?:\r?\n|$)|\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)*$/ }
45
+ when directive == "lib" && lib == "never" || directive == "path" && path == "never" || directive == "types" && (types == "never" || types == "prefer-import" && { value result: "(?:import|from|require\\s*\\()\\s*[\"']" re_escape(module) "[\"']"; value re_test(source, result) })
46
+ warn "Do not use a triple slash reference for {module}, use `import` style instead."
47
+ }
48
+
49
+ # JSDoc lines whose `*` is not one column right of the opening `/**`. The
50
+ # matchers never take the `*` of the closing `*/` (the comment's own end).
51
+ in ts_block_comment match jsdoc_line /^[ \t]*\*(?!\/)/
52
+ in ts_block_comment match jsdoc_close_indent /^[ \t]+(?=\*\/)/
53
+ in ts_block_comment match jsdoc_blank_run /^(?:[ \t]*\r?\n)+/
54
+ # Blank lines inside the block have no `*` at all.
55
+ in jsdoc_blank_run rule es-check-alignment:blank {
56
+ when re_test(parent_text, /^\/\*\*[^*]/)
57
+ warn "Expected JSDoc block to be aligned."
58
+ }
59
+ # `/**` then anything but another `*` (`/*****` banners are not JSDoc).
60
+ @macro is_jsdoc re_test(parent_text, /^\/\*\*[^*]/)
61
+ in jsdoc_line rule es-check-alignment {
62
+ when @is_jsdoc && text != parent_indent " *"
63
+ warn "Expected JSDoc block to be aligned."
64
+ }
65
+ in jsdoc_close_indent rule es-check-alignment:close {
66
+ when @is_jsdoc && text != parent_indent " "
67
+ warn "Expected JSDoc block to be aligned."
68
+ }
69
+ in ts_block_comment match jsdoc_typed_tag /^[ \t]*\*[ \t]*@(?:param|arg|argument|returns?|property|prop|throws|yields)[ \t]+\{/
70
+ in jsdoc_typed_tag rule es-no-types {
71
+ when @is_jsdoc
72
+ warn "Types are not permitted on @{re_replace(text, /^.*@(\w+).*$/, '$1')}."
73
+ }
74
+
75
+ }
@@ -0,0 +1,82 @@
1
+ # .ts/style/declarations: internal part of .ts/style.
2
+
3
+ import .ts/modules
4
+
5
+ define {
6
+
7
+ # ---- declarations --------------------------------------------------------------
8
+ # Annotations the initializer already states, with widening exceptions.
9
+ fn primitive_call_types(name) = get({ BigInt: ["bigint"], Boolean: ["boolean"], Number: ["number"], String: ["string"], Symbol: ["symbol"], RegExp: ["RegExp"] }, name)
10
+ in ts_number let primitive_inference ["bigint", "number"]
11
+ in ts_string let primitive_inference ["bigint", "string"]
12
+ in ts_bigint let primitive_inference ["bigint"]
13
+ in ts_boolean let primitive_inference ["bigint", "boolean"]
14
+ in ts_null let primitive_inference ["bigint", "null"]
15
+ in ts_regex let primitive_inference ["bigint", "RegExp"]
16
+ in ts_template let primitive_inference ["string"]
17
+ in ts_identifier let primitive_inference if contains(["Infinity", "NaN"], text) then ["number"] else if text == "undefined" then ["undefined"] else []
18
+ in ts_call let primitive_inference if callee is ts_identifier then primitive_call_types(callee) else []
19
+ in ts_new let primitive_inference if callee == "RegExp" then ["RegExp"] else []
20
+ in ts_number, ts_string, ts_bigint, ts_boolean, ts_null, ts_regex, ts_template, ts_identifier, ts_call, ts_new let primitive_direct primitive_inference
21
+ in ts_unary let primitive_inference if operator == "!" then ["boolean"] else if operator == "void" then ["undefined"] else if operator == "-" then filter(argument.primitive_direct, t => contains(["bigint", "number"], t)) else if operator == "+" && contains(argument.primitive_direct, "number") then ["number"] else []
22
+ in ts_parenthesized forward primitive_inference, primitive_direct from expression
23
+ in ts_declarator, ts_class_property, ts_parameter let inferred_annotation if type != null && contains(init.primitive_inference, type) then type else null
24
+ # `readonly` (and optional) properties keep their annotation: it widens.
25
+ in ts_class_property let annotation_widens contains([] modifier, "readonly") || optional != null
26
+ group inferrable_annotation ts_declarator, ts_class_property, ts_parameter
27
+ in inferrable_annotation let no_inferrable_types_checked true
28
+ in inferrable_annotation rule es-no-inferrable-types {
29
+ when inferred_annotation != null && (annotation_widens.nonempty ?? false) != true
30
+ when no_inferrable_types_checked
31
+ report at { start: if is ts_parameter then name.offset else offset, end: end_offset }
32
+ warn "Type {inferred_annotation} trivially inferred from a {inferred_annotation} literal, remove type annotation."
33
+ let annotation_colon type.token_neighbours.before
34
+ let annotation_start if is ts_parameter && node.optional != null then node.optional.offset else annotation_colon.start
35
+ let annotation_comments overlapping(ts_comment, annotation_start, type.end_offset)
36
+ fix at { start: annotation_start, end: type.end_offset } if annotation_colon.text == ":" && count(annotation_comments) == 0 as ""
37
+ }
38
+
39
+ # `const x: Map<K, V> = new Map()`: the arguments belong on the constructor.
40
+ in ts_new let constructor_arguments if callee is ts_identifier then { name: callee, callee: callee.node, count: count(type_argument), node: node } else null
41
+ in ts_parenthesized forward constructor_arguments from expression
42
+ in ts_type_reference let annotation_arguments if name is ts_identifier then { name: name, count: count(type_argument), node: node, typed_array: contains(["Float32Array", "Float64Array", "Int16Array", "Int32Array", "Int8Array", "Uint16Array", "Uint32Array", "Uint8Array", "Uint8ClampedArray"], name) && name.reference_kind == null } else null
43
+ in ts_new, ts_type_reference let style_generic_span {
44
+ value first_argument: first(type_argument.nodes)
45
+ value last_argument: last(type_argument.nodes)
46
+ value closing_tokens: overlapping(ts_close_angle, last_argument.end_offset, end_offset)
47
+ value closing: first(closing_tokens)
48
+ value { start: first_argument.token_neighbours.before.start, end: closing.end_offset }
49
+ }
50
+ fn generic_constructor_replacement(source, owner, construction, annotation, mode) = if mode == "constructor" then {
51
+ value colon: annotation.node.token_neighbours.before
52
+ value generics: annotation.node.style_generic_span
53
+ value comments: overlapping(ts_comment, colon.start, generics.start)
54
+ value unsafe_comments: filter(comments, c => starts_with(c, "//"))
55
+ value relocated_comments: join(comments, "")
56
+ value arguments: byte_slice(source, generics.start, generics.end)
57
+ value between: byte_slice(source, annotation.node.end_offset, construction.callee.end_offset)
58
+ value missing_parens: construction.node.end_offset == construction.callee.end_offset
59
+ value if colon.text == ":" && count(unsafe_comments) == 0 then { start: colon.start, end: construction.callee.end_offset, text: between relocated_comments arguments (if missing_parens then "()" else "") } else null
60
+ } else {
61
+ value binding: if owner is ts_class_property then owner.key.node else owner.name.node
62
+ value marker: binding.token_neighbours.after
63
+ value start: if contains(["?", "!"], marker.text) then marker.end else binding.end_offset
64
+ value generics: construction.node.style_generic_span
65
+ value arguments: byte_slice(source, generics.start, generics.end)
66
+ value between: byte_slice(source, start, generics.start)
67
+ value { start: start, end: generics.end, text: ": " construction.name arguments between }
68
+ }
69
+ in ts_declarator, ts_class_property, ts_parameter rule es-consistent-generic-constructors {
70
+ option mode ("constructor" | "type-annotation") = "constructor" "Preferred location for generic arguments."
71
+ when not ts_parameter || parent is (ts_function, ts_method)
72
+ let construction init.constructor_arguments
73
+ let annotation type.annotation_arguments
74
+ when construction != null
75
+ when if mode == "constructor" then annotation != null && annotation.name == construction.name && !annotation.typed_array && annotation.count > 0 && construction.count == 0 else type == null && construction.count > 0
76
+ when mode != "constructor" || ts_compiler_option(node, "isolatedDeclarations") != true
77
+ let replacement generic_constructor_replacement(source, node, construction, annotation, mode)
78
+ warn if mode == "constructor" then "The generic type arguments should be specified as part of the constructor type arguments." else "The generic type arguments should be specified as part of the type annotation."
79
+ fix at { start: replacement.start ?? offset, end: replacement.end ?? offset } if replacement != null as replacement.text
80
+ }
81
+
82
+ }
@@ -0,0 +1,125 @@
1
+ # .ts/style/expressions: internal part of .ts/style.
2
+
3
+ define {
4
+
5
+ # ---- expressions -----------------------------------------------------------------------
6
+ in ts_assignment rule es-no-multi-assign {
7
+ option ignoreNonDeclaration boolean = false "Allow chained assignments outside declaration initializers."
8
+ let owner { value parent.node; walk (current) if current is ts_parenthesized then current.parent.node else null; last }
9
+ when owner is ts_declarator && unparenthesised_node(owner.init.node) == node
10
+ || owner is ts_class_property && unparenthesised_node(owner.init.node) == node
11
+ || !ignoreNonDeclaration && owner is ts_assignment && unparenthesised_node(owner.right.node) == node
12
+ warn "Unexpected chained assignment."
13
+ }
14
+
15
+ in ts_member, ts_index let loop_collection_read if object not ts_this && write_site == null then object else null
16
+ in ts_identifier let loop_index_use { start: offset, end: end_offset, array: if is (ts_node.property, ts_node.index) then parent.loop_collection_read else null }
17
+ in ts_identifier let loop_index_uses [] references.loop_index_use
18
+ in ts_number let loop_zero literal_value.value == "0"
19
+ in ts_declarator let counted_loop_binding if name is ts_identifier && init.loop_zero == true then { name: name, uses: name.loop_index_uses } else null
20
+ in ts_variable_declaration let counted_loop_initialisation if kind != "const" && count(declaration) == 1 then first([] declaration.counted_loop_binding) else null
21
+ in ts_member let array_length_object if property is ts_identifier && property == "length" then object else null
22
+ in ts_index let array_length_object if index is ts_identifier && index == "length" then object else null
23
+ in ts_binary let counted_loop_test if operator == "<" then { name: left, array: right.array_length_object } else null
24
+ in ts_number let loop_increment_unit literal_value.value == "1"
25
+ in ts_binary let counted_loop_increment if operator == "+" then { left: left, right: right, left_unit: left.loop_increment_unit, right_unit: right.loop_increment_unit } else null
26
+ in ts_update let incremented_loop_name if operator == "++" then argument else null
27
+ fn increments_name(sum, name) = sum != null && (sum.left == name && sum.right_unit == true || sum.right == name && sum.left_unit == true)
28
+ in ts_assignment let incremented_loop_name if operator == "+=" && right.loop_increment_unit == true || operator == "=" && increments_name(right.counted_loop_increment, left) then left else null
29
+ in ts_for rule es-prefer-for-of {
30
+ let binding init.counted_loop_initialisation
31
+ let comparison test.counted_loop_test
32
+ when binding != null && comparison.array != null && comparison.name == binding.name && update.incremented_loop_name == binding.name
33
+ when all(binding.uses, use => use.start < body.offset || use.end > body.end_offset || use.array == comparison.array)
34
+ warn "Expected a `for-of` loop instead of a `for` loop with this simple iteration."
35
+ }
36
+ in style_tree_node let assignment_node_start offset
37
+ in style_tree_node let assignment_call_start if parent is ts_parenthesized then parent.assignment_call_start else if parent is ts_call then parent.assignment_node_start else null
38
+ in ts_parenthesized let assignment_inner_start expression.assignment_value_start
39
+ in style_tree_node let assignment_value_start assignment_inner_start.nonempty ?? offset
40
+ in ts_parameter let assignment_property_name if is_parameter_property && name is ts_identifier then name else null
41
+ in ts_method let assignment_constructor_properties [] parameter.assignment_property_name
42
+ in ts_method let assignment_is_constructor key == "constructor"
43
+ in ts_method, ts_function, ts_arrow let assignment_function_node { node: node, constructor: assignment_is_constructor == true, properties: assignment_constructor_properties.nonempty ?? [], call: if is ts_arrow then assignment_call_start else null, outer: parent.assignment_nearest_function }
44
+ in style_tree_node let assignment_nearest_function if is (ts_method, ts_function, ts_arrow) then assignment_function_node else parent.assignment_nearest_function
45
+ fn assignment_owner(owner) = if owner.node is ts_arrow && owner.call != null then owner.outer else owner
46
+ in ts_class_property let assignment_property_node { init: init.assignment_value_start }
47
+ in style_tree_node let assignment_nearest_property if is ts_class_property then assignment_property_node else parent.assignment_nearest_property
48
+ in ts_identifier let assignment_parameter_value if reference_kind == "parameter" then text else null
49
+ in ts_as, ts_non_null, ts_parenthesized let assignment_parameter_value expression.assignment_parameter_value
50
+ in ts_member let assignment_this_name if object is ts_this then property else null
51
+ in ts_index let assignment_this_name if object is ts_this then index.static_name else null
52
+ fn assignment_constructor(owner) = owner.node is ts_method && owner.constructor
53
+ fn assignment_field_initialiser(owner, property) = property != null && (owner == null || owner.node is ts_arrow && owner.call == property.init)
54
+ fn parameter_assignment_entry(start, owner, nearest, property, left_name, left_parameter, right_parameter, op) = {
55
+ start: start,
56
+ prior: if assignment_constructor(owner) then if left_parameter != null then left_parameter else if !contains(["=", "??=", "&&=", "||="], op) then left_name else null else null,
57
+ field_write: if !assignment_constructor(owner) && assignment_field_initialiser(nearest, property) then left_name else null,
58
+ candidate: assignment_constructor(owner) && left_name != null && left_name == right_parameter && contains(owner.properties, left_name) && contains(["=", "??=", "&&=", "||="], op),
59
+ name: left_name
60
+ }
61
+ in ts_assignment let parameter_assignment_entry {
62
+ value assignment_nearest_function
63
+ owner: assignment_owner
64
+ value left_parameter: if left is ts_identifier then left.assignment_parameter_value else null
65
+ value parameter_assignment_entry(offset, owner, assignment_nearest_function, assignment_nearest_property, left.assignment_this_name, left_parameter, right.assignment_parameter_value, operator)
66
+ }
67
+ in style_tree_node let parameter_assignment_entries if is ts_class then [] else [] parameter_assignment_entry { value [] children.parameter_assignment_entries; fold ([]) (entries, child) entries child }
68
+ in ts_class let parameter_assignment_catalog { value [] children.parameter_assignment_entries; fold ([]) (entries, child) entries child }
69
+ in ts_assignment rule es-no-unnecessary-parameter-property-assignment {
70
+ when ts_class.node != null
71
+ let entry parameter_assignment_entry
72
+ when entry.candidate && !any(ts_class.parameter_assignment_catalog, prior => prior.field_write == entry.name || prior.start < entry.start && prior.prior == entry.name)
73
+ warn "This assignment is unnecessary since it is already assigned by a parameter property."
74
+ }
75
+
76
+ in ts_binary rule es-no-confusing-non-null-assertion {
77
+ when contains(["==", "===", "in", "instanceof"], operator) && re_test(left, /!\s*$/)
78
+ warn if operator == "in" || operator == "instanceof" then "Confusing combination of non-null assertion and `{operator}` operator like `a! {operator} b`, which might be misinterpreted as `!(a {operator} b)`." else "Confusing combination of non-null assertion and equality test like `a! == b`, which looks very similar to `a !== b`."
79
+ }
80
+ in ts_assignment rule es-no-confusing-non-null-assertion:assign {
81
+ when operator == "=" && re_test(left, /!\s*$/)
82
+ warn "Confusing combination of non-null assertion and assignment like `a! = b`, which looks very similar to `a != b`."
83
+ }
84
+
85
+ in ts_call rule es-no-require-imports {
86
+ option allow string[] = [] "module paths (regexes) that may be required"
87
+ option allowAsImport boolean = false "allow `import x = require(...)`"
88
+ when callee == "require" && (callee.resolved == "global" || parent is ts_import_equals)
89
+ when !(allowAsImport && parent is ts_import_equals)
90
+ when !any(allow, p => { value { value first([] argument.literal_value).value; nonempty } ?? "\u{0}"; re_test p } && first([] argument.literal_value).type == "string")
91
+ warn "A `require()` style import is forbidden."
92
+ }
93
+ in ts_call rule es-no-var-requires {
94
+ when callee == "require" && callee.resolved == "global" && parent is (ts_declarator, ts_call, ts_member, ts_index, ts_new, ts_as, ts_type_assertion)
95
+ warn "Require statement not part of import statement."
96
+ }
97
+
98
+ in ts_array, ts_arrow, ts_binary, ts_class, ts_function, ts_identifier, ts_member, ts_index, ts_meta_property, ts_object, ts_sequence, ts_template, ts_this, ts_number, ts_string, ts_boolean, ts_bigint, ts_null, ts_regex let statement_effect false
99
+ in ts_unary let statement_effect contains(["delete", "void"], operator)
100
+ in ts_logical let statement_effect { kind: "logical", right: right.statement_effect }
101
+ in ts_conditional let statement_effect { kind: "conditional", left: consequent.statement_effect, right: alternate.statement_effect }
102
+ in ts_tagged_template let statement_effect { kind: "tagged" }
103
+ in ts_jsx_element, ts_jsx_fragment let statement_effect { kind: "jsx" }
104
+ in ts_parenthesized, ts_as, ts_type_assertion, ts_non_null, ts_instantiation forward statement_effect from expression
105
+ fn expression_allowed(shape, short, ternary, tagged, jsx) = {
106
+ value shape
107
+ walk (part) if part.kind == "logical" && short then part.right
108
+ else if part.kind == "conditional" && ternary then [part.left, part.right] else []
109
+ all (part) if type_of(part) == "bool" then part
110
+ else if part.kind == "logical" then short
111
+ else if part.kind == "conditional" then ternary
112
+ else if part.kind == "tagged" then tagged
113
+ else if part.kind == "jsx" then !jsx else true
114
+ }
115
+ in ts_expression_statement rule es-no-unused-expressions {
116
+ option allowShortCircuit boolean = false "Allow effectful short circuit expressions."
117
+ option allowTernary boolean = false "Allow ternaries with effectful branches."
118
+ option allowTaggedTemplates boolean = false "Allow tagged templates."
119
+ option enforceForJSX boolean = false "Report unused JSX expressions."
120
+ option ignoreDirectives boolean = false "Ignore directive prologues."
121
+ when !(directive_statement && (ecma_version >= 5 || ignoreDirectives)) && !expression_allowed(expression.statement_effect, allowShortCircuit, allowTernary, allowTaggedTemplates, enforceForJSX)
122
+ warn "Expected an assignment or function call and instead saw an expression."
123
+ }
124
+
125
+ }
@@ -0,0 +1,44 @@
1
+ # .ts/style/imports: internal part of .ts/style.
2
+ import .ts/expression-fixes
3
+ import .ts/style/type-imports
4
+
5
+ define {
6
+
7
+ in ts_import_specifier let type_only_specifier kind == "type"
8
+ fn import_type_side_effect_fix(source, n) = {
9
+ value items: n.specifier.nodes
10
+ value qualifiers: map(items, item => {
11
+ value gap: byte_slice(source, item.kind.end_offset, item.imported.offset)
12
+ value whitespace: re_find(gap, /^\s*/)
13
+ value { start: item.kind.offset, end: item.kind.end_offset + byte_len(whitespace), text: "" }
14
+ })
15
+ value edits: [{ start: n.first_token.end, end: n.first_token.end, text: " type" }] qualifiers
16
+ value expression_fix_edits(n.text, n.offset, edits)
17
+ }
18
+ in ts_import rule es-no-import-type-side-effects {
19
+ when kind != "type" && default_import == null && namespace == null && count(specifier) > 0
20
+ when { value [] specifier.type_only_specifier; filter (t) t; count } == count(specifier)
21
+ let replacement import_type_side_effect_fix(source, node)
22
+ warn "TypeScript will only remove the inline type specifiers which will leave behind a side effect import at runtime. Convert this to a top-level type qualifier to properly remove the entire import."
23
+ fix at node if first_token.text == "import" && expression_fix_retains_comments(node, replacement) as replacement
24
+ }
25
+
26
+ in ts_export let empty_module_export declaration == null && expression == null && from == null && namespace == null && count(specifier) == 0 && { value result: "{{"; value contains(text, result) }
27
+ in ts_export let module_item !empty_module_export
28
+ in ts_import, ts_import_equals, ts_export_assignment let module_item true
29
+ in ts_program let has_module_item any([] body.module_item, x => x == true)
30
+ in ts_export rule es-no-useless-empty-export {
31
+ when !re_test(file, /\.d\.[cm]?ts$/) && empty_module_export && parent.has_module_item
32
+ warn "Empty export does nothing and can be removed."
33
+ fix at node if expression_fix_retains_comments(node, "") as ""
34
+ }
35
+
36
+ # n/no-sync: `fooSync()` inside functions (top-level code may block).
37
+ in ts_identifier let sync_argument_name if re_test(text, /^[\w$]*Sync$/) then text else null
38
+ in ts_call let sync_method_name { value [re_find(callee, /[\w$]*Sync$/)] argument.sync_argument_name; compact; first }
39
+ in ts_call rule es-no-sync {
40
+ when sync_method_name != null && (ts_function != null || ts_arrow != null || ts_method != null)
41
+ warn "Unexpected sync method: '{sync_method_name}'."
42
+ }
43
+
44
+ }