@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.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/bin/dxlint +15 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +29 -4
- package/rules/conventions.dx +34 -0
- package/rules/correctness.dx +33 -0
- package/rules/es-no-semi.dx +11 -0
- package/rules/format.dx +49 -0
- package/rules/recommended.dx +51 -0
- package/rules/suspicious.dx +31 -0
- package/rules/types.dx +21 -0
- package/std/c.dx +23 -0
- package/std/core/lines.dx +51 -0
- package/std/core/lists.dx +76 -0
- package/std/core.dx +30 -0
- package/std/css.dx +17 -0
- package/std/dx.dx +31 -0
- package/std/json.dx +104 -0
- package/std/markdown.dx +11 -0
- package/std/python.dx +21 -0
- package/std/rust/format.dx +316 -0
- package/std/rust.dx +34 -0
- package/std/toml.dx +19 -0
- package/std/ts/arithmetic.dx +87 -0
- package/std/ts/assertions.dx +477 -0
- package/std/ts/calls.dx +246 -0
- package/std/ts/comments.dx +10 -0
- package/std/ts/conditions.dx +531 -0
- package/std/ts/constants.dx +335 -0
- package/std/ts/deprecation.dx +185 -0
- package/std/ts/directives.dx +60 -0
- package/std/ts/effects.dx +219 -0
- package/std/ts/enums.dx +233 -0
- package/std/ts/expression-fixes.dx +67 -0
- package/std/ts/expressions.dx +270 -0
- package/std/ts/flow.dx +2 -0
- package/std/ts/format.dx +755 -0
- package/std/ts/globals.dx +106 -0
- package/std/ts/grammar.dx +488 -0
- package/std/ts/helpers.dx +102 -0
- package/std/ts/import-attributes.dx +88 -0
- package/std/ts/import-no-deprecated.dx +73 -0
- package/std/ts/indentation.dx +442 -0
- package/std/ts/jsx.dx +646 -0
- package/std/ts/layout/blocks.dx +281 -0
- package/std/ts/layout/calls.dx +79 -0
- package/std/ts/layout/expressions.dx +213 -0
- package/std/ts/layout/literals.dx +114 -0
- package/std/ts/layout/separators.dx +184 -0
- package/std/ts/layout/shared.dx +203 -0
- package/std/ts/layout/text.dx +142 -0
- package/std/ts/layout/types.dx +182 -0
- package/std/ts/layout.dx +9 -0
- package/std/ts/license.dx +54 -0
- package/std/ts/literal-text.dx +3 -0
- package/std/ts/literals.dx +95 -0
- package/std/ts/methods.dx +69 -0
- package/std/ts/modules.dx +268 -0
- package/std/ts/names.dx +348 -0
- package/std/ts/naming.dx +169 -0
- package/std/ts/narrowing.dx +96 -0
- package/std/ts/parentheses.dx +262 -0
- package/std/ts/precedence.dx +25 -0
- package/std/ts/problems/calls.dx +34 -0
- package/std/ts/problems/classes.dx +39 -0
- package/std/ts/problems/control-flow.dx +257 -0
- package/std/ts/problems/declarations.dx +67 -0
- package/std/ts/problems/duplicates.dx +70 -0
- package/std/ts/problems/expressions.dx +154 -0
- package/std/ts/problems/helpers.dx +22 -0
- package/std/ts/problems/imports.dx +43 -0
- package/std/ts/problems/non-null.dx +78 -0
- package/std/ts/problems/numeric-precision.dx +66 -0
- package/std/ts/problems/regexp.dx +66 -0
- package/std/ts/problems/statements.dx +36 -0
- package/std/ts/problems/typescript.dx +55 -0
- package/std/ts/problems/whitespace.dx +40 -0
- package/std/ts/problems.dx +20 -0
- package/std/ts/regex/checks.dx +249 -0
- package/std/ts/regex/findings.dx +209 -0
- package/std/ts/regex/grammar.dx +80 -0
- package/std/ts/regex/suggestions.dx +94 -0
- package/std/ts/regex/values.dx +378 -0
- package/std/ts/regex.dx +5 -0
- package/std/ts/restrictions/comments.dx +76 -0
- package/std/ts/restrictions.dx +811 -0
- package/std/ts/scopes.dx +68 -0
- package/std/ts/scoping/atomic.dx +40 -0
- package/std/ts/scoping/bindings.dx +126 -0
- package/std/ts/scoping/constness.dx +72 -0
- package/std/ts/scoping/globals.dx +52 -0
- package/std/ts/scoping/loops.dx +86 -0
- package/std/ts/scoping/member-uses.dx +111 -0
- package/std/ts/scoping/receivers.dx +107 -0
- package/std/ts/scoping/shadowing.dx +202 -0
- package/std/ts/scoping/usage.dx +393 -0
- package/std/ts/scoping.dx +842 -0
- package/std/ts/selectors.dx +10 -0
- package/std/ts/spreads.dx +97 -0
- package/std/ts/structure/declarations.dx +52 -0
- package/std/ts/structure/lines.dx +79 -0
- package/std/ts/structure/lists.dx +144 -0
- package/std/ts/structure/padding.dx +258 -0
- package/std/ts/structure.dx +963 -0
- package/std/ts/style/classes.dx +157 -0
- package/std/ts/style/comments.dx +75 -0
- package/std/ts/style/declarations.dx +82 -0
- package/std/ts/style/expressions.dx +125 -0
- package/std/ts/style/imports.dx +44 -0
- package/std/ts/style/ordering.dx +192 -0
- package/std/ts/style/returns.dx +152 -0
- package/std/ts/style/signatures.dx +308 -0
- package/std/ts/style/type-exports.dx +62 -0
- package/std/ts/style/type-imports.dx +166 -0
- package/std/ts/style/types.dx +474 -0
- package/std/ts/style.dx +29 -0
- package/std/ts/suggestions/accessors.dx +138 -0
- package/std/ts/suggestions/builtins.dx +110 -0
- package/std/ts/suggestions/comparisons.dx +41 -0
- package/std/ts/suggestions/computed-keys.dx +20 -0
- package/std/ts/suggestions/constructors.dx +54 -0
- package/std/ts/suggestions/control-flow.dx +204 -0
- package/std/ts/suggestions/curly.dx +124 -0
- package/std/ts/suggestions/directives.dx +79 -0
- package/std/ts/suggestions/errors.dx +106 -0
- package/std/ts/suggestions/expressions.dx +230 -0
- package/std/ts/suggestions/functions.dx +260 -0
- package/std/ts/suggestions/globals.dx +187 -0
- package/std/ts/suggestions/helpers.dx +17 -0
- package/std/ts/suggestions/object-shorthand.dx +148 -0
- package/std/ts/suggestions/object-spread.dx +96 -0
- package/std/ts/suggestions/regexp.dx +59 -0
- package/std/ts/suggestions/renames.dx +50 -0
- package/std/ts/suggestions/strings.dx +47 -0
- package/std/ts/suggestions/templates.dx +104 -0
- package/std/ts/suggestions.dx +24 -0
- package/std/ts/template-fixes.dx +22 -0
- package/std/ts/tokens.dx +124 -0
- package/std/ts/type-specifiers.dx +93 -0
- package/std/ts/typed/assignments.dx +154 -0
- package/std/ts/typed/awaitability.dx +157 -0
- package/std/ts/typed/boolean-comparisons.dx +82 -0
- package/std/ts/typed/defaults.dx +97 -0
- package/std/ts/typed/enum-consistency.dx +68 -0
- package/std/ts/typed/enums.dx +52 -0
- package/std/ts/typed/errors.dx +32 -0
- package/std/ts/typed/floating-promises.dx +147 -0
- package/std/ts/typed/for-in-arrays.dx +21 -0
- package/std/ts/typed/includes.dx +102 -0
- package/std/ts/typed/nullish-coalescing.dx +149 -0
- package/std/ts/typed/optional-chains.dx +317 -0
- package/std/ts/typed/promises.dx +205 -0
- package/std/ts/typed/qualifiers.dx +108 -0
- package/std/ts/typed/readonly-members.dx +159 -0
- package/std/ts/typed/readonly-parameters.dx +121 -0
- package/std/ts/typed/redundant-constituents.dx +171 -0
- package/std/ts/typed/regexp-exec.dx +79 -0
- package/std/ts/typed/rejection-handlers.dx +91 -0
- package/std/ts/typed/return-await.dx +116 -0
- package/std/ts/typed/return-this.dx +67 -0
- package/std/ts/typed/strict-void-return.dx +121 -0
- package/std/ts/typed/string-boundaries.dx +153 -0
- package/std/ts/typed/switches.dx +147 -0
- package/std/ts/typed/template-expressions.dx +127 -0
- package/std/ts/typed/template-values.dx +88 -0
- package/std/ts/typed/transfers.dx +106 -0
- package/std/ts/typed/type-arguments.dx +337 -0
- package/std/ts/typed/type-constituents.dx +143 -0
- package/std/ts/typed/type-conversions.dx +82 -0
- package/std/ts/typed/type-parameters.dx +280 -0
- package/std/ts/typed/unbound-method.dx +167 -0
- package/std/ts/typed/unsafe-calls.dx +73 -0
- package/std/ts/typed/unsafe-members.dx +36 -0
- package/std/ts/typed/void-expressions.dx +101 -0
- package/std/ts/typed.dx +585 -0
- package/std/ts/values/arguments.dx +13 -0
- package/std/ts/values/enums.dx +104 -0
- package/std/ts/values/inspection.dx +274 -0
- package/std/ts/values.dx +1217 -0
- package/std/ts.dx +77 -0
- package/std/whitespace.dx +37 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
# .ts/style/types: internal part of .ts/style.
|
|
2
|
+
import .ts/expression-fixes
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
# ---- type syntax -------------------------------------------------------------------
|
|
7
|
+
schema restricted_type_policy boolean | string | null | { message?: string; fixWith?: string; suggest?: string[] }
|
|
8
|
+
fn type_restriction_policy(types, name) = {
|
|
9
|
+
value key: { value keys(types); filter (key) re_replace(key, /\s+/, "") == name; last }
|
|
10
|
+
value policy: get(types, key)
|
|
11
|
+
value if type_of(policy) == "null" || policy == false then null else {
|
|
12
|
+
message: if type_of(policy) == "str" then policy else policy.message ?? "",
|
|
13
|
+
replacement: policy.fixWith.nonempty
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
fn type_restriction_warning(name, policy) = "Don't use `" name "` as a type." (if policy.message != "" then " " policy.message else "")
|
|
17
|
+
@macro restricted_type_check {
|
|
18
|
+
default option types { [key: string]: restricted_type_policy } = {} "Restricted types and their message/replacement policies."
|
|
19
|
+
let restricted_policy type_restriction_policy(types, restricted_name)
|
|
20
|
+
when restricted_policy != null
|
|
21
|
+
warn type_restriction_warning(restricted_name, restricted_policy)
|
|
22
|
+
let replacement restricted_policy.replacement
|
|
23
|
+
fix at node if replacement != null && expression_fix_retains_comments(node, replacement) as replacement
|
|
24
|
+
}
|
|
25
|
+
in ts_type_reference let restricted_type_shape count(type_argument) > 0
|
|
26
|
+
in ts_type_keyword let restricted_type_shape contains(["bigint", "boolean", "never", "null", "number", "object", "string", "symbol", "undefined", "unknown", "void"], text)
|
|
27
|
+
in ts_tuple_type let restricted_type_shape count(element) == 0
|
|
28
|
+
in ts_type_literal let restricted_type_shape count(member) == 0
|
|
29
|
+
in ts_type_reference, ts_type_keyword, ts_tuple_type, ts_type_literal rule es-no-restricted-types {
|
|
30
|
+
when restricted_type_shape
|
|
31
|
+
let restricted_name re_replace(text, /\s+/, "")
|
|
32
|
+
@restricted_type_check
|
|
33
|
+
}
|
|
34
|
+
in ts_type_reference.name rule es-no-restricted-types:name {
|
|
35
|
+
let restricted_name re_replace(text, /\s+/, "")
|
|
36
|
+
@restricted_type_check
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# Array syntax supplies one notation value; mutable and readonly policies are independent.
|
|
40
|
+
in ts_type_keyword, ts_array_type, ts_qualified_name let is_simple_type true
|
|
41
|
+
in ts_type_reference let is_simple_type count(type_argument) == 0 || name == "Array" && count(type_argument) == 1 && first([] type_argument.is_simple_type) == true
|
|
42
|
+
in ts_array_type {
|
|
43
|
+
let is_array_type true
|
|
44
|
+
let array_element_simple type.is_simple_type == true
|
|
45
|
+
let array_element_text type
|
|
46
|
+
let array_element_node type.node
|
|
47
|
+
}
|
|
48
|
+
in ts_type_reference let array_element_node first([] type_argument.node)
|
|
49
|
+
in ts_node let style_type_unwrapped if is ts_parenthesized_type then type.style_type_unwrapped else node
|
|
50
|
+
in ts_type_reference let array_type_notation
|
|
51
|
+
if re_test(name, /^(Array|ReadonlyArray|Readonly)$/) && count(type_argument) == 1 && name.reference_kind == null
|
|
52
|
+
&& (name != "Readonly" || first([] type_argument.is_array_type) == true)
|
|
53
|
+
then { readonly: name != "Array", named: true, wrapper: name, element: array_element_node, simple: first([] type_argument.is_simple_type) == true, shown: if first([] type_argument.is_simple_type) == true then first([] type_argument) else "T", suffix: if name == "Readonly" then "" else "[]" }
|
|
54
|
+
else null
|
|
55
|
+
in ts_array_type let array_type_notation
|
|
56
|
+
if parent is ts_type_operator && parent.operator == "readonly" then null
|
|
57
|
+
else { readonly: false, named: false, wrapper: "Array", element: array_element_node, simple: type.is_simple_type == true, shown: if type.is_simple_type == true then type else "T", suffix: "[]" }
|
|
58
|
+
in ts_type_operator let array_type_notation
|
|
59
|
+
if operator == "readonly" && type.is_array_type == true
|
|
60
|
+
then { readonly: true, named: false, wrapper: "ReadonlyArray", element: type.array_element_node, simple: type.array_element_simple == true, shown: if type.array_element_simple then type.array_element_text else "T", suffix: "[]" }
|
|
61
|
+
else null
|
|
62
|
+
fn array_notation_forbidden(notation, mode) = if notation.named then mode != "generic" && (mode != "array-simple" || notation.simple) else mode == "generic" || mode == "array-simple" && !notation.simple
|
|
63
|
+
fn array_notation_warning(notation, mode) =
|
|
64
|
+
if notation.named then "Array type using '{notation.wrapper}<{notation.shown}>' is forbidden{if mode == 'array-simple' then ' for simple types' else ''}. Use '{if notation.readonly then 'readonly ' else ''}{notation.shown}{notation.suffix}' instead."
|
|
65
|
+
else "Array type using '{if notation.readonly then 'readonly ' else ''}{notation.shown}[]' is forbidden{if mode == 'array-simple' then ' for non-simple types' else ''}. Use '{notation.wrapper}<{notation.shown}>' instead."
|
|
66
|
+
fn array_notation_replacement(owner, notation) = {
|
|
67
|
+
value element: if notation.named then notation.element else notation.element.style_type_unwrapped
|
|
68
|
+
value comments: overlapping(ts_comment, owner.offset, owner.end_offset)
|
|
69
|
+
value safe: all(comments, comment => element.offset <= comment.offset && comment.end_offset <= element.end_offset)
|
|
70
|
+
value postfix: if element.precedence < 20 || element is ts_infer then "({element})" else "{element}"
|
|
71
|
+
value replacement: if !notation.named then "{notation.wrapper}<{element}>"
|
|
72
|
+
else "{if notation.readonly then 'readonly ' else ''}{if notation.wrapper == 'Readonly' then element else postfix}{notation.suffix}"
|
|
73
|
+
value if !safe then null
|
|
74
|
+
else if notation.named && notation.readonly && owner.parent is ts_array_type then "({replacement})"
|
|
75
|
+
else replacement
|
|
76
|
+
}
|
|
77
|
+
in ts_type_reference, ts_array_type rule es-array-type:mutable {
|
|
78
|
+
default option mode ("array" | "array-simple" | "generic") = "array" "Mutable array notation."
|
|
79
|
+
when array_type_notation != null && !array_type_notation.readonly
|
|
80
|
+
when array_notation_forbidden(array_type_notation, mode)
|
|
81
|
+
warn array_notation_warning(array_type_notation, mode)
|
|
82
|
+
let replacement array_notation_replacement(node, array_type_notation)
|
|
83
|
+
fix if replacement != null as replacement
|
|
84
|
+
}
|
|
85
|
+
in ts_type_reference, ts_type_operator rule es-array-type:readonly {
|
|
86
|
+
default option mode ("array" | "array-simple" | "generic") = "array" "Readonly array notation."
|
|
87
|
+
when array_type_notation != null && array_type_notation.readonly
|
|
88
|
+
when array_notation_forbidden(array_type_notation, mode)
|
|
89
|
+
warn array_notation_warning(array_type_notation, mode)
|
|
90
|
+
let replacement array_notation_replacement(node, array_type_notation)
|
|
91
|
+
fix if replacement != null as replacement
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
# The global wrapper types (not local types of those names).
|
|
95
|
+
in ts_type_reference rule es-no-wrapper-object-types {
|
|
96
|
+
when re_test(name, /^(String|Number|Boolean|Object|Symbol|BigInt)$/) && parent not ts_template_type && name.reference_kind == null
|
|
97
|
+
warn "Prefer using the primitive `{lower(name)}` as a type name, rather than the upper-cased `{name}`."
|
|
98
|
+
fix at name.node if !(is ts_class.implements || is ts_interface.extends) as lower(name)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
in ts_non_null rule es-no-non-null-assertion {
|
|
102
|
+
warn "Forbidden non-null assertion."
|
|
103
|
+
}
|
|
104
|
+
in ts_declarator let initialised_declaration_end if init != null || {
|
|
105
|
+
value name
|
|
106
|
+
len: len
|
|
107
|
+
value slice(text, len)
|
|
108
|
+
trim_start
|
|
109
|
+
starts_with "!"
|
|
110
|
+
} then end_offset else null
|
|
111
|
+
in ts_identifier let binding_initialised_end if parent is ts_declarator then parent.initialised_declaration_end else null
|
|
112
|
+
in ts_identifier let binding_write_end if access == "write" || access == "read_write" then end_offset else null
|
|
113
|
+
in ts_identifier let binding_assignment_history if declaration == null then null else { initialised: declaration.binding_initialised_end, writes: [] declaration.references.binding_write_end }
|
|
114
|
+
in ts_parenthesized forward binding_assignment_history from expression
|
|
115
|
+
in ts_non_null, ts_parenthesized let nullish_left if parent is ts_parenthesized then parent.nullish_left else is ts_logical.left && parent.operator == "??"
|
|
116
|
+
fn assigned_before(history, end) = history == null || history.initialised != null && history.initialised < end || any(history.writes, n => n != null && n < end)
|
|
117
|
+
in ts_non_null rule es-no-non-null-asserted-nullish-coalescing {
|
|
118
|
+
when nullish_left && assigned_before(expression.binding_assignment_history, end_offset)
|
|
119
|
+
warn "The nullish coalescing operator is designed to handle undefined and null - using a non-null assertion is not needed."
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
in ts_type_parameter rule es-no-unnecessary-type-constraint {
|
|
123
|
+
when constraint == "any" || constraint == "unknown"
|
|
124
|
+
warn "Constraining the generic type `{name}` to `{constraint}` does nothing and is unnecessary."
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
in ts_function let function_declaration { name: name, node: node, body: body != null }
|
|
128
|
+
in ts_method let function_declaration { name: key, node: node, body: body != null }
|
|
129
|
+
in ts_export forward function_declaration from declaration
|
|
130
|
+
in style_tree_node let function_siblings if is ts_export then parent.function_siblings else [] children.function_declaration
|
|
131
|
+
in ts_function, ts_method let overload_implementation body != null && any(parent.function_siblings, d => (d.node is ts_function) == (is ts_function) && d.name == function_declaration.name && !d.body)
|
|
132
|
+
in style_tree_node let function_overload_context if is (ts_function, ts_method) then overload_implementation else parent.function_overload_context
|
|
133
|
+
in ts_type_keyword let void_keyword text == "void"
|
|
134
|
+
in ts_type_reference let has_void_argument any([] type_argument.void_keyword, v => v == true)
|
|
135
|
+
in style_tree_node let void_union_member if is ts_type_keyword then contains(["void", "never"], text) else if is ts_type_reference then has_void_argument else false
|
|
136
|
+
in ts_union let valid_void_union all([] type.void_union_member, t => t == true)
|
|
137
|
+
in ts_type_keyword {
|
|
138
|
+
let void_type_context if is ts_node.return_type then "return" else if is ts_node.type_argument then (if parent is ts_type_reference then "generic" else if parent is ts_call then "other" else "generic-other") else if is ts_type_parameter.default then "default" else if parent is ts_parameter && parent.name == "this" then "this" else if parent is ts_union then (if parent.valid_void_union || parent.function_overload_context == true then "return" else "union") else "other"
|
|
139
|
+
let void_type_generic_name { value parent.name.nonempty ?? ""; re_replace /\s+/ "" }
|
|
140
|
+
let no_invalid_void_type_checked !contains(["generic", "generic-other", "default"], void_type_context)
|
|
141
|
+
}
|
|
142
|
+
in ts_type_keyword rule es-no-invalid-void-type {
|
|
143
|
+
option allowAsThisParameter boolean = false "Whether a `this` parameter may be `void`."
|
|
144
|
+
option allowInGenericTypeArguments boolean | string[] = true "Whether `void` may be a generic type argument: for every type, or only those listed."
|
|
145
|
+
when text == "void" && void_type_context != "return"
|
|
146
|
+
let any_generic allowInGenericTypeArguments != false
|
|
147
|
+
let listed_generic if type_of(allowInGenericTypeArguments) == "list" then any(allowInGenericTypeArguments, entry => re_replace(entry, /\s+/, "") == void_type_generic_name) else false
|
|
148
|
+
let checked_option when {
|
|
149
|
+
void_type_context == "generic" then !any_generic || type_of(allowInGenericTypeArguments) == "list" && listed_generic != true
|
|
150
|
+
void_type_context == "generic-other" then allowInGenericTypeArguments != true
|
|
151
|
+
void_type_context == "default" then !any_generic
|
|
152
|
+
else then false
|
|
153
|
+
}
|
|
154
|
+
when no_invalid_void_type_checked || checked_option == true
|
|
155
|
+
when !(void_type_context == "this" && allowAsThisParameter == true)
|
|
156
|
+
warn if void_type_context == "generic" && any_generic then void_type_generic_name " may not have void as a type argument." else if void_type_context == "union" && any_generic then "void is not valid as a constituent in a union type" else if any_generic && allowAsThisParameter != true then "void is only valid as a return type or generic type argument." else if any_generic then "void is only valid as a return type or generic type argument or the type of a `this` parameter." else if allowAsThisParameter == true then "void is only valid as return type or type of `this` parameter." else "void is only valid as a return type."
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
in ts_parameter let rest_function_parameter name is ts_rest && parent is (ts_function, ts_arrow, ts_method, ts_method_signature, ts_call_signature, ts_function_type)
|
|
160
|
+
fn rest_parameter_annotation(n) =
|
|
161
|
+
n is ts_parameter.type && n.parent.rest_function_parameter == true
|
|
162
|
+
|| n is ts_type_operator.type && n.parent.operator == "readonly" && n.parent is ts_parameter.type && n.parent.parent.rest_function_parameter == true
|
|
163
|
+
in ts_array_type let rest_array_argument rest_parameter_annotation(node)
|
|
164
|
+
in ts_type_reference let rest_array_argument contains(["Array", "ReadonlyArray"], name) && rest_parameter_annotation(node)
|
|
165
|
+
in ts_type_keyword let no_explicit_any_checked true
|
|
166
|
+
in ts_type_keyword rule es-no-explicit-any {
|
|
167
|
+
option fixToUnknown boolean = false "Replace explicit any with unknown automatically."
|
|
168
|
+
option ignoreRestArgs boolean = false "Allow any in rest parameter arrays."
|
|
169
|
+
when text == "any"
|
|
170
|
+
when no_explicit_any_checked && (!ignoreRestArgs || parent.rest_array_argument != true)
|
|
171
|
+
let keyof_any parent is ts_type_operator && parent.operator == "keyof"
|
|
172
|
+
let fix_target if keyof_any then parent.node else node
|
|
173
|
+
let replacement if keyof_any then "PropertyKey" else if node is ts_parameter.type && parent.rest_function_parameter == true then "unknown[]" else "unknown"
|
|
174
|
+
warn "Unexpected any. Specify a different type."
|
|
175
|
+
fix at fix_target if fixToUnknown && expression_fix_retains_comments(fix_target, replacement) as replacement
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
in ts_number, ts_string let delete_literal_key true
|
|
179
|
+
in ts_number let delete_numeric_key true
|
|
180
|
+
in ts_parenthesized forward delete_literal_key, delete_numeric_key from expression
|
|
181
|
+
in ts_unary let delete_literal_key operator == "-" && argument.delete_numeric_key == true
|
|
182
|
+
in ts_index, ts_parenthesized let delete_operand if parent is ts_parenthesized then parent.delete_operand else parent is ts_unary && parent.operator == "delete"
|
|
183
|
+
in ts_index rule es-no-dynamic-delete {
|
|
184
|
+
when delete_operand && index.delete_literal_key != true
|
|
185
|
+
report at unparenthesised_node(index.node)
|
|
186
|
+
warn "Do not delete dynamically computed property keys."
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
in ts_namespace let namespace_is_declared contains([] modifier, "declare")
|
|
190
|
+
in style_tree_node let namespace_declared namespace_is_declared == true || parent.namespace_declared == true
|
|
191
|
+
in ts_namespace let no_namespace_checked !re_test(file, /\.d\.[cm]?ts$/)
|
|
192
|
+
in ts_namespace rule es-no-namespace {
|
|
193
|
+
when kind != "global" && name != "global" && !re_test(name, /^["']/)
|
|
194
|
+
when no_namespace_checked
|
|
195
|
+
warn "ES2015 module syntax is preferred over namespaces."
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
in ts_enum_member rule es-prefer-enum-initializers {
|
|
199
|
+
when init == null
|
|
200
|
+
warn "The value of the member '{text}' should be explicitly defined."
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
in ts_enum let enum_keys [] member.static_name
|
|
204
|
+
in ts_enum_member let enum_scope { name: parent.name, keys: parent.enum_keys }
|
|
205
|
+
in ts_identifier, ts_member, ts_index, ts_unary, ts_binary, ts_parenthesized let enum_scope parent.enum_scope
|
|
206
|
+
in ts_number, ts_string, ts_bigint, ts_boolean, ts_null, ts_regex {
|
|
207
|
+
let enum_literal_value true
|
|
208
|
+
let enum_bitwise_value true
|
|
209
|
+
let enum_bitwise_operand true
|
|
210
|
+
}
|
|
211
|
+
in ts_template {
|
|
212
|
+
let enum_literal_value count(expression) == 0
|
|
213
|
+
let enum_bitwise_value enum_literal_value
|
|
214
|
+
let enum_bitwise_operand enum_literal_value
|
|
215
|
+
}
|
|
216
|
+
in ts_identifier let enum_bitwise_operand contains(enum_scope.keys, text)
|
|
217
|
+
in ts_member, ts_index let enum_bitwise_operand object == enum_scope.name && contains(enum_scope.keys, static_name)
|
|
218
|
+
in ts_unary {
|
|
219
|
+
let enum_literal_value contains(["+", "-"], operator) && argument.enum_literal_value == true
|
|
220
|
+
let enum_bitwise_value if operator == "~" then argument.enum_bitwise_operand == true else contains(["+", "-"], operator) && argument.enum_bitwise_value == true
|
|
221
|
+
let enum_bitwise_operand contains(["+", "-", "~"], operator) && argument.enum_bitwise_operand == true
|
|
222
|
+
}
|
|
223
|
+
in ts_binary {
|
|
224
|
+
let enum_bitwise_value contains(["&", "^", "<<", ">>", ">>>", "|"], operator) && left.enum_bitwise_operand == true && right.enum_bitwise_operand == true
|
|
225
|
+
let enum_bitwise_operand enum_bitwise_value
|
|
226
|
+
}
|
|
227
|
+
in ts_parenthesized forward enum_literal_value, enum_bitwise_value, enum_bitwise_operand from expression
|
|
228
|
+
in ts_enum_member let enum_literal_value init == null || init.enum_literal_value == true
|
|
229
|
+
in ts_enum_member let enum_bitwise_value init == null || init.enum_bitwise_value == true
|
|
230
|
+
in ts_enum_member.name rule es-prefer-literal-enum-member {
|
|
231
|
+
option allowBitwiseExpressions boolean = false "Allow bitwise expressions using literals and enum members."
|
|
232
|
+
when !(if allowBitwiseExpressions then parent.enum_bitwise_value else parent.enum_literal_value)
|
|
233
|
+
warn "Explicit enum value must only be a literal value (string or number){if allowBitwiseExpressions then ' or a bitwise expression' else ''}."
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
in ts_declarator let no_this_alias_checked name is ts_identifier
|
|
237
|
+
in ts_assignment let no_this_alias_checked left is ts_identifier
|
|
238
|
+
in ts_declarator.name rule es-no-this-alias {
|
|
239
|
+
option allowDestructuring boolean = true "Allow destructuring, such as `const { props, state } = this`."
|
|
240
|
+
option allowedNames string[] = [] "Names allowed to alias this."
|
|
241
|
+
when parent.init == "this"
|
|
242
|
+
when parent.no_this_alias_checked || !allowDestructuring
|
|
243
|
+
when !(is ts_identifier && contains(allowedNames, text))
|
|
244
|
+
report at { start: offset, end: parent.type.end_offset ?? end_offset }
|
|
245
|
+
warn if is ts_identifier then "Unexpected aliasing of 'this' to local variable." else "Unexpected aliasing of members of 'this' to local variables."
|
|
246
|
+
}
|
|
247
|
+
in ts_assignment.left rule es-no-this-alias:assignment {
|
|
248
|
+
option allowDestructuring boolean = true "Allow destructuring, such as `const { props, state } = this`."
|
|
249
|
+
option allowedNames string[] = [] "Names allowed to alias this."
|
|
250
|
+
when parent.right == "this"
|
|
251
|
+
when parent.no_this_alias_checked || !allowDestructuring
|
|
252
|
+
when !(is ts_identifier && contains(allowedNames, text))
|
|
253
|
+
warn if is ts_identifier then "Unexpected aliasing of 'this' to local variable." else "Unexpected aliasing of members of 'this' to local variables."
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
fn property_modifiers(xs) = { value ["public", "protected", "private", "readonly"]; filter (m) contains(xs, m); join " " }
|
|
257
|
+
in ts_parameter, ts_class_property let parameter_modifiers property_modifiers([] modifier)
|
|
258
|
+
in ts_parameter, ts_class_property let parameter_properties_checked true
|
|
259
|
+
in ts_parameter rule es-parameter-properties {
|
|
260
|
+
option prefer ("class-property" | "parameter-property") = "class-property" "Preferred property declaration."
|
|
261
|
+
when parameter_properties_checked
|
|
262
|
+
when prefer == "class-property" && parameter_modifiers != ""
|
|
263
|
+
warn "Property {name} should be declared as a class property."
|
|
264
|
+
}
|
|
265
|
+
in ts_member let plain_this_property object is ts_this && property is ts_identifier
|
|
266
|
+
in ts_assignment let parameter_assignment_name if left.plain_this_property == true && right is ts_identifier then right else null
|
|
267
|
+
in ts_expression_statement let leading_parameter_assignment if expression.parameter_assignment_name != null && (previous == null || previous.leading_parameter_assignment != null) then expression.parameter_assignment_name else null
|
|
268
|
+
in ts_block let leading_parameter_assignments [] body.leading_parameter_assignment
|
|
269
|
+
in ts_parameter let convertible_parameter if init == null && parameter_modifiers == "" then { name: name, annotation: type } else null
|
|
270
|
+
in ts_method let property_conversion_constructor if key == "constructor" && body != null then { parameters: [] parameter.convertible_parameter, assignments: body.leading_parameter_assignments } else null
|
|
271
|
+
in ts_class let property_conversion_constructors [] member.property_conversion_constructor
|
|
272
|
+
fn conversion_parameter(parameters, name) = find(parameters, p => p.name == name)
|
|
273
|
+
fn property_conversion_matches(constructors, name, annotation) = any(constructors, c => conversion_parameter(c.parameters, name) != null && conversion_parameter(c.parameters, name).annotation == annotation && contains(c.assignments, name))
|
|
274
|
+
in ts_class_property rule es-parameter-properties:class {
|
|
275
|
+
option prefer ("class-property" | "parameter-property") = "class-property" "Preferred property declaration."
|
|
276
|
+
when parameter_properties_checked
|
|
277
|
+
when prefer == "parameter-property" && key is ts_identifier && init == null
|
|
278
|
+
when property_conversion_matches(parent.property_conversion_constructors, key, type)
|
|
279
|
+
warn "Property {key} should be declared as a parameter property."
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
fn empty_object_message(subject, option) = subject " allows any non-nullish value, including literals like `0` and `\"\"`.\n- If that's what you want, disable this lint rule with an inline comment or configure the '" option "' rule option.\n- If you want a type meaning \"any object\", you probably want `object` instead.\n- If you want a type meaning \"any value\", you probably want `unknown` instead."
|
|
283
|
+
in ts_interface, ts_type_literal let no_empty_object_type_checked true
|
|
284
|
+
in ts_interface.name rule es-no-empty-object-type:interface {
|
|
285
|
+
option allowInterfaces ("always" | "never" | "with-single-extends") = "never" "Whether to allow empty interfaces."
|
|
286
|
+
option allowWithName regex | null = null "Allow matching interface and type alias names."
|
|
287
|
+
when parent.no_empty_object_type_checked
|
|
288
|
+
when allowInterfaces != "always"
|
|
289
|
+
when count(parent.member) == 0 && count(parent.extends) <= 1
|
|
290
|
+
when !(allowInterfaces == "with-single-extends" && count(parent.extends) == 1)
|
|
291
|
+
when !(type_of(allowWithName) == "str" && re_test(text, allowWithName))
|
|
292
|
+
warn if count(parent.extends) == 1 then "An interface declaring no members is equivalent to its supertype." else empty_object_message("An empty interface declaration", "allowInterfaces")
|
|
293
|
+
}
|
|
294
|
+
in ts_type_literal rule es-no-empty-object-type:object {
|
|
295
|
+
option allowObjectTypes ("always" | "never") = "never" "Whether to allow empty object type literals."
|
|
296
|
+
option allowWithName regex | null = null "Allow matching interface and type alias names."
|
|
297
|
+
when no_empty_object_type_checked
|
|
298
|
+
when allowObjectTypes != "always"
|
|
299
|
+
when count(member) == 0 && parent not ts_intersection
|
|
300
|
+
when !(type_of(allowWithName) == "str" && parent is ts_type_alias && re_test(parent.name, allowWithName))
|
|
301
|
+
warn empty_object_message("The `{{}}` (\"empty object\") type", "allowObjectTypes")
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
# A type literal or interface that is only an index signature.
|
|
305
|
+
in ts_type_alias, ts_interface let indexed_type_name name.offset
|
|
306
|
+
in style_tree_node let indexed_type_scope if is (ts_type_alias, ts_interface) then indexed_type_name else if is (ts_index_signature, ts_property_signature, ts_parameter, ts_declarator, ts_class_property, ts_method, ts_function, ts_function_type, ts_call_signature, ts_method_signature) then null else parent.indexed_type_scope
|
|
307
|
+
in ts_identifier let indexed_syntax_refs [] declaration.offset
|
|
308
|
+
in ts_type_literal, ts_interface let indexed_syntax_refs { value [] member.indexed_type_refs; fold ([]) (ids, child) ids child }
|
|
309
|
+
in ts_type_alias, ts_index_signature, ts_mapped_type, ts_parenthesized_type let indexed_syntax_refs type.indexed_type_refs.nonempty ?? []
|
|
310
|
+
in ts_union, ts_intersection, ts_indexed_access, ts_conditional_type, ts_type_reference let indexed_syntax_refs { value [] children.indexed_type_refs; fold ([]) (ids, child) ids child }
|
|
311
|
+
in style_tree_node let indexed_type_refs indexed_syntax_refs.nonempty ?? []
|
|
312
|
+
in ts_type_alias, ts_interface let indexed_type_entry { id: name.offset, refs: indexed_type_refs }
|
|
313
|
+
in style_tree_node let indexed_type_entries [] indexed_type_entry { value [] children.indexed_type_entries; fold ([]) (entries, child) entries child }
|
|
314
|
+
in style_tree_node let indexed_type_graph if is ts_program then indexed_type_entries else parent.indexed_type_graph
|
|
315
|
+
fn expand_graph_refs(graph, ids) = { value ids { value graph; filter (entry) contains(ids, entry.id); fold ([]) (refs, entry) refs entry.refs }; unique }
|
|
316
|
+
fn expanded_graph_state(previous_state, ids, target) = { ids: ids, done: ids == previous_state.ids || contains(ids, target) }
|
|
317
|
+
fn type_reaches(graph, ids, target) = target != null && {
|
|
318
|
+
value initial_state: { ids: ids, done: contains(ids, target) }
|
|
319
|
+
value graph
|
|
320
|
+
final_state: fold initial_state (state, entry) if state.done then state else { value graph; expanded_ids: expand_graph_refs state.ids; value expanded_graph_state(state, expanded_ids, target) }
|
|
321
|
+
value final_state.ids
|
|
322
|
+
contains target
|
|
323
|
+
}
|
|
324
|
+
in ts_parameter let indexed_parameter name is ts_identifier && type != null
|
|
325
|
+
in ts_index_signature let indexed_signature type != null && any([] parameter.indexed_parameter, p => p == true)
|
|
326
|
+
in ts_type_literal, ts_interface let record_shape count(member) == 1 && first([] member.indexed_signature) == true
|
|
327
|
+
in ts_identifier let indexed_type_uses [] references.access
|
|
328
|
+
in ts_type_operator let indexed_keyof operator == "keyof"
|
|
329
|
+
in ts_type_parameter let indexed_key_parameter name != null && !contains(name.indexed_type_uses, "type") && constraint.indexed_keyof != true
|
|
330
|
+
in ts_mapped_type let record_shape type_parameter.indexed_key_parameter == true
|
|
331
|
+
in ts_type_literal, ts_interface, ts_mapped_type let indexed_record_eligible record_shape && !{
|
|
332
|
+
value ids: if is ts_mapped_type then parent.indexed_type_refs else indexed_type_refs
|
|
333
|
+
value type_reaches(indexed_type_graph, ids, indexed_type_scope)
|
|
334
|
+
}
|
|
335
|
+
in ts_parameter let indexed_parameter_type type.node
|
|
336
|
+
in ts_index_signature let indexed_conversion {
|
|
337
|
+
key: first([] parameter.indexed_parameter_type), value: type.node,
|
|
338
|
+
readonly: contains([] modifier, "readonly"), optional: null
|
|
339
|
+
}
|
|
340
|
+
in ts_type_literal, ts_interface let indexed_conversion first([] member.indexed_conversion)
|
|
341
|
+
in ts_type_literal, ts_interface let indexed_first_member first([] member.node)
|
|
342
|
+
in ts_mapped_type let indexed_conversion {
|
|
343
|
+
key: type_parameter.constraint.style_type_unwrapped, value: type.node,
|
|
344
|
+
readonly: contains([] modifier, "readonly") || contains([] modifier, "+readonly"), optional: optional
|
|
345
|
+
}
|
|
346
|
+
in ts_type_reference let indexed_conversion {
|
|
347
|
+
key: first([] type_argument.node), value: last([] type_argument.node), readonly: false, optional: null
|
|
348
|
+
}
|
|
349
|
+
fn indexed_notation_replacement(source, owner, info) = {
|
|
350
|
+
value comments: overlapping(ts_comment, owner.offset, owner.end_offset)
|
|
351
|
+
value safe: all(comments, comment =>
|
|
352
|
+
info.key.offset <= comment.offset && comment.end_offset <= info.key.end_offset
|
|
353
|
+
|| info.value != null && info.value.offset <= comment.offset && comment.end_offset <= info.value.end_offset)
|
|
354
|
+
value record: "Record<{info.key}, {info.value ?? 'any'}>"
|
|
355
|
+
value optional: if info.optional == "-?" then "Required<{record}>"
|
|
356
|
+
else if info.optional != null then "Partial<{record}>" else record
|
|
357
|
+
value result: if info.readonly then "Readonly<{optional}>" else optional
|
|
358
|
+
value if !safe || info.key == null
|
|
359
|
+
|| owner is ts_interface && (owner.extends != null || owner.parent is ts_export && owner.parent.kind == "default")
|
|
360
|
+
|| owner is ts_type_reference && (owner.name.reference_kind != null
|
|
361
|
+
|| !(info.key is ts_type_keyword && contains(["string", "number", "symbol"], info.key.text)))
|
|
362
|
+
|| owner is ts_mapped_type && (owner.name != null || contains([] owner.modifier, "-readonly")) then null
|
|
363
|
+
else if owner is ts_type_reference then "{{ [key: {info.key}]: {info.value} }}"
|
|
364
|
+
else if owner not ts_interface then result
|
|
365
|
+
else {
|
|
366
|
+
value byte_slice(source, owner.offset, owner.indexed_first_member.offset)
|
|
367
|
+
re_replace /\s*\{\s*$/ ""
|
|
368
|
+
re_replace /^(declare\s+)?interface\b/ "${{1}}type"
|
|
369
|
+
then (header) "{header} = {result};"
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
in ts_type_literal rule es-consistent-indexed-object-style {
|
|
373
|
+
option mode ("record" | "index-signature") = "record" "Preferred indexed object notation."
|
|
374
|
+
when mode == "record" && indexed_record_eligible
|
|
375
|
+
warn "A record is preferred over an index signature."
|
|
376
|
+
let replacement indexed_notation_replacement(source, node, indexed_conversion)
|
|
377
|
+
fix if replacement != null as replacement
|
|
378
|
+
}
|
|
379
|
+
in ts_interface rule es-consistent-indexed-object-style:interface {
|
|
380
|
+
option mode ("record" | "index-signature") = "record" "Preferred indexed object notation."
|
|
381
|
+
when mode == "record" && indexed_record_eligible
|
|
382
|
+
warn "A record is preferred over an index signature."
|
|
383
|
+
let replacement indexed_notation_replacement(source, node, indexed_conversion)
|
|
384
|
+
fix if replacement != null as replacement
|
|
385
|
+
}
|
|
386
|
+
in ts_mapped_type rule es-consistent-indexed-object-style:mapped {
|
|
387
|
+
option mode ("record" | "index-signature") = "record" "Preferred indexed object notation."
|
|
388
|
+
when mode == "record" && indexed_record_eligible
|
|
389
|
+
warn "A record is preferred over an index signature."
|
|
390
|
+
let replacement indexed_notation_replacement(source, node, indexed_conversion)
|
|
391
|
+
fix if replacement != null as replacement
|
|
392
|
+
}
|
|
393
|
+
in ts_type_reference rule es-consistent-indexed-object-style:record {
|
|
394
|
+
option mode ("record" | "index-signature") = "record" "Preferred indexed object notation."
|
|
395
|
+
when mode == "index-signature" && name == "Record" && count(type_argument) == 2
|
|
396
|
+
warn "An index signature is preferred over a record."
|
|
397
|
+
let replacement indexed_notation_replacement(source, node, indexed_conversion)
|
|
398
|
+
fix if replacement != null as replacement
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
in ts_type_literal let is_type_literal true
|
|
402
|
+
in ts_parenthesized_type let is_type_literal type.is_type_literal == true
|
|
403
|
+
in ts_type_alias let aliases_type_literal type.is_type_literal == true
|
|
404
|
+
in ts_type_alias let type_definition_annotation type.node
|
|
405
|
+
in ts_type_alias let type_definition_literal type.style_type_unwrapped
|
|
406
|
+
in ts_type_alias, ts_interface let type_definition_keyword name.node.token_neighbours.before
|
|
407
|
+
in ts_interface let type_definition_name name.text
|
|
408
|
+
in ts_interface let type_definition_name_end name.end_offset
|
|
409
|
+
in ts_interface let type_definition_heritage [] extends.text
|
|
410
|
+
fn type_definition_edit(source, start, end, edits) = {
|
|
411
|
+
value original: byte_slice(source, start, end)
|
|
412
|
+
value replacement: expression_fix_edits(original, start, edits)
|
|
413
|
+
value span: { offset: start, end_offset: end, text: original }
|
|
414
|
+
value if expression_fix_retains_comments(span, replacement) then { start: start, end: end, text: replacement } else null
|
|
415
|
+
}
|
|
416
|
+
fn type_definition_interface(source, n) = {
|
|
417
|
+
value keyword: n.type_definition_keyword
|
|
418
|
+
value literal: n.type_definition_literal
|
|
419
|
+
value equals_tokens: overlapping(ts_equals, n.type_definition_name_end ?? n.offset, n.type_definition_annotation.offset)
|
|
420
|
+
value equals: last(equals_tokens)
|
|
421
|
+
value comments: overlapping(ts_comment, equals.token_neighbours.before.end, equals.offset)
|
|
422
|
+
value comment_end: last(comments).end_offset ?? 0
|
|
423
|
+
value before_equals: max(equals.token_neighbours.before.end, comment_end)
|
|
424
|
+
value edits: [
|
|
425
|
+
{ start: keyword.start, end: keyword.end, text: "interface" },
|
|
426
|
+
{ start: before_equals, end: literal.offset, text: " " },
|
|
427
|
+
{ start: literal.end_offset, end: n.end_offset, text: "" }
|
|
428
|
+
]
|
|
429
|
+
value if literal == null || equals == null then null else type_definition_edit(source, keyword.start, n.end_offset, edits)
|
|
430
|
+
}
|
|
431
|
+
fn type_definition_type(source, n) = {
|
|
432
|
+
value ancestors: walk([n], ancestor => ancestor.parent.node)
|
|
433
|
+
value global_augmentation: any(ancestors, ancestor =>
|
|
434
|
+
ancestor is ts_namespace && ancestor.kind == "global" && contains(ancestor.modifier, "declare"))
|
|
435
|
+
value close_tokens: overlapping(ts_close_brace, n.last_token.start, n.last_token.end)
|
|
436
|
+
value close: last(close_tokens)
|
|
437
|
+
value open: close.paired_token
|
|
438
|
+
value last_parameter: last(n.type_parameter.nodes)
|
|
439
|
+
value angle_tokens: if last_parameter != null then overlapping(ts_close_angle, last_parameter.end_offset, open.start) else []
|
|
440
|
+
value header_end: first(angle_tokens).end_offset ?? n.type_definition_name_end
|
|
441
|
+
value keyword: n.type_definition_keyword
|
|
442
|
+
value default_export: n.parent is ts_export && n.parent.kind == "default"
|
|
443
|
+
value start: if default_export then n.parent.offset else keyword.start
|
|
444
|
+
value heritage: { value n.type_definition_heritage; map (base) " & " base; join "" }
|
|
445
|
+
value suffix: heritage (if default_export then "\nexport default " n.type_definition_name else "")
|
|
446
|
+
value edits: (if default_export then [{ start: start, end: n.offset, text: "" }] else []) [
|
|
447
|
+
{ start: keyword.start, end: keyword.end, text: "type" },
|
|
448
|
+
{ start: header_end, end: open.start, text: " = " },
|
|
449
|
+
{ start: close.end_offset, end: close.end_offset, text: suffix }
|
|
450
|
+
]
|
|
451
|
+
value if global_augmentation || open == null || header_end == null then null else type_definition_edit(source, start, close.end_offset, edits)
|
|
452
|
+
}
|
|
453
|
+
in ts_type_alias.name rule es-consistent-type-definitions {
|
|
454
|
+
option mode ("interface" | "type") = "interface" "Preferred object type definition."
|
|
455
|
+
when mode == "interface" && parent.aliases_type_literal
|
|
456
|
+
warn "Use an `interface` instead of a `type`."
|
|
457
|
+
let edit type_definition_interface(source, parent.node)
|
|
458
|
+
fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as edit.text
|
|
459
|
+
}
|
|
460
|
+
in ts_interface.name rule es-consistent-type-definitions:interface {
|
|
461
|
+
option mode ("interface" | "type") = "interface" "Preferred object type definition."
|
|
462
|
+
when mode == "type"
|
|
463
|
+
warn "Use a `type` instead of an `interface`."
|
|
464
|
+
let edit type_definition_type(source, parent.node)
|
|
465
|
+
fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as edit.text
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
in ts_namespace rule es-prefer-namespace-keyword {
|
|
469
|
+
when kind == "module" && { value name; re_replace /\..*$/ ""; is_identifier }
|
|
470
|
+
warn "Use 'namespace' instead of 'module' to declare custom TypeScript modules."
|
|
471
|
+
fix at { start: kind.offset, end: kind.end_offset } as "namespace"
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
}
|
package/std/ts/style.dx
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# .ts/style: typescript-eslint stylistic and strict rules, plus import /
|
|
2
|
+
# jsdoc / tslint ones, on the native TypeScript layers. Messages and
|
|
3
|
+
# positions follow typescript-eslint's.
|
|
4
|
+
#
|
|
5
|
+
# `[] x` turns a field or field attribute into a list whether it matched
|
|
6
|
+
# none, one or many nodes.
|
|
7
|
+
|
|
8
|
+
import .ts/helpers
|
|
9
|
+
import .ts/assertions
|
|
10
|
+
|
|
11
|
+
define {
|
|
12
|
+
|
|
13
|
+
group style_tree_node ts_node
|
|
14
|
+
|
|
15
|
+
@macro modifiers (join([] modifier, " "))
|
|
16
|
+
fn has_accessibility(m) = re_test(m, /\b(?:public|private|protected)\b/)
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
import .ts/style/comments
|
|
21
|
+
import .ts/style/types
|
|
22
|
+
import .ts/style/declarations
|
|
23
|
+
import .ts/style/ordering
|
|
24
|
+
import .ts/style/classes
|
|
25
|
+
import .ts/style/signatures
|
|
26
|
+
import .ts/style/returns
|
|
27
|
+
import .ts/style/expressions
|
|
28
|
+
import .ts/style/imports
|
|
29
|
+
import .ts/style/type-exports
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn accessor_scope(n) = {
|
|
4
|
+
value owner: n.parent.node
|
|
5
|
+
value if owner is ts_object then "object" else if owner is ts_class then "class"
|
|
6
|
+
else if owner is (ts_interface, ts_type_literal) then "type" else null
|
|
7
|
+
}
|
|
8
|
+
fn accessor_display_name(n) = {
|
|
9
|
+
value label: n.function_name ?? {
|
|
10
|
+
value kind: if n.kind == "get" then "getter" else "setter"
|
|
11
|
+
value if n.static_name == null then kind else "{kind} '{n.static_name}'"
|
|
12
|
+
}
|
|
13
|
+
# A TS method signature's message interpolates getStaticPropertyName, so a dynamic key prints 'null'.
|
|
14
|
+
value if accessor_scope(n) == "type" && !contains(label, "'") then label " 'null'" else label
|
|
15
|
+
}
|
|
16
|
+
fn accessor_static(n) = any([] n.modifier.nodes, modifier => modifier.text == "static")
|
|
17
|
+
fn accessor_key(n) = {
|
|
18
|
+
value key: n.key.node
|
|
19
|
+
value unparenthesised_node(key.expression.node ?? key).token_texts
|
|
20
|
+
}
|
|
21
|
+
fn accessor_same_key(left, right) = if left.static_name != null && right.static_name != null
|
|
22
|
+
then left.static_name == right.static_name
|
|
23
|
+
else left.static_name == null && right.static_name == null
|
|
24
|
+
&& left.key != null && right.key != null && accessor_key(left) == accessor_key(right)
|
|
25
|
+
fn accessor_pair_info(n) = {
|
|
26
|
+
value scope: accessor_scope(n)
|
|
27
|
+
value owner: n.parent.node
|
|
28
|
+
value if !contains(["get", "set"], n.kind) || scope == null then null else {
|
|
29
|
+
value members: if scope == "object" then owner.property.nodes else owner.member.nodes
|
|
30
|
+
value matches: { value members; filter (member) accessor_static(member) == accessor_static(n) && accessor_same_key(n, member) }
|
|
31
|
+
value getters: { value matches; filter (member) member.kind == "get" }
|
|
32
|
+
value setters: { value matches; filter (member) member.kind == "set" }
|
|
33
|
+
value grouping: if count(getters) != 1 || count(setters) != 1 then null else {
|
|
34
|
+
value getter: first(getters)
|
|
35
|
+
value setter: first(setters)
|
|
36
|
+
value gi: index_of(members, getter)
|
|
37
|
+
value si: index_of(members, setter)
|
|
38
|
+
value former: if gi < si then getter else setter
|
|
39
|
+
value latter: if gi < si then setter else getter
|
|
40
|
+
value if latter != n then null else {
|
|
41
|
+
former: accessor_display_name(former),
|
|
42
|
+
gap: if gi < si then si - gi > 1 else gi - si > 1,
|
|
43
|
+
getter_first: gi < si
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
value {
|
|
47
|
+
kind: n.kind, scope: scope,
|
|
48
|
+
paired: if n.kind == "get" then count(setters) > 0 else count(getters) > 0,
|
|
49
|
+
name: accessor_display_name(n), grouping: grouping, offset: n.offset
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
fn accessor_call_argument(n) = {
|
|
54
|
+
value call: n.parent.node
|
|
55
|
+
value if call not ts_call || !contains(call.argument.nodes, n) then null else {
|
|
56
|
+
value callee: unparenthesised_node(call.callee.node)
|
|
57
|
+
value if callee not (ts_member, ts_index) then null else {
|
|
58
|
+
object: callee.object.node, method: callee.static_name, index: index_of(call.argument.nodes, n)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
fn accessor_pair_call(n, names, position) = {
|
|
63
|
+
value outer: outer_parenthesised_node(n)
|
|
64
|
+
value call: accessor_call_argument(outer)
|
|
65
|
+
value object: unparenthesised_node(call.object)
|
|
66
|
+
value call != null && call.index == position && object is ts_identifier
|
|
67
|
+
&& object.ts_global_reference && contains(names, "{object.text}.{call.method}")
|
|
68
|
+
}
|
|
69
|
+
fn accessor_return_call(n, names, position) = {
|
|
70
|
+
value call: accessor_call_argument(n)
|
|
71
|
+
value object: call.object
|
|
72
|
+
value call != null && call.index == position && object is ts_identifier
|
|
73
|
+
&& contains(["readonly", "writable"], object.global) && contains(names, "{object.text}.{call.method}")
|
|
74
|
+
}
|
|
75
|
+
fn accessor_descriptor_container(n) = if n.parent is ts_property && n.parent.value.node == n
|
|
76
|
+
&& n.parent.parent is ts_object then n.parent.parent.node else null
|
|
77
|
+
fn accessor_pair_descriptor(n) = n is ts_object && (
|
|
78
|
+
accessor_pair_call(n, ["Object.defineProperty", "Reflect.defineProperty"], 2)
|
|
79
|
+
|| {
|
|
80
|
+
value container: accessor_descriptor_container(n)
|
|
81
|
+
value container != null && accessor_pair_call(container, ["Object.create", "Object.defineProperties"], 1)
|
|
82
|
+
})
|
|
83
|
+
fn accessor_return_descriptor(n) = n is ts_object && (
|
|
84
|
+
accessor_return_call(n, ["Object.defineProperty", "Reflect.defineProperty"], 2)
|
|
85
|
+
|| {
|
|
86
|
+
value container: accessor_descriptor_container(n)
|
|
87
|
+
value container != null && accessor_return_call(container, ["Object.create", "Object.defineProperties"], 1)
|
|
88
|
+
})
|
|
89
|
+
fn accessor_descriptor_info(n) = if !accessor_pair_descriptor(n) then null else {
|
|
90
|
+
value names: { value n.property.nodes; filter (property) property is ts_property && property.key is ts_identifier; map (property) property.key.text }
|
|
91
|
+
value { get: contains(names, "get"), set: contains(names, "set") }
|
|
92
|
+
}
|
|
93
|
+
fn accessor_function_kind(n) = if n is ts_method && contains(["get", "set"], n.kind) then n.kind else {
|
|
94
|
+
value holder: if n is ts_method then n
|
|
95
|
+
else if n is (ts_function, ts_arrow) && n.parent is ts_property && n.parent.value.node == n then n.parent.node else null
|
|
96
|
+
value name: holder.static_name
|
|
97
|
+
value if holder != null && contains(["get", "set"], name) && accessor_return_descriptor(holder.parent.node) then name else null
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
in ts_method, ts_method_signature let accessor_pair accessor_pair_info(node)
|
|
101
|
+
in ts_object let property_descriptor accessor_descriptor_info(node)
|
|
102
|
+
in ts_function, ts_arrow, ts_method let accessor accessor_function_kind(node)
|
|
103
|
+
in ts_node let enclosing_accessor {
|
|
104
|
+
value owner: { value walk(parent.node, ancestor => ancestor.parent.node); filter (ancestor) ancestor is (ts_function, ts_arrow, ts_method); first }
|
|
105
|
+
value accessor_function_kind(owner)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
in ts_method, ts_method_signature rule es-accessor-pairs {
|
|
109
|
+
option getWithoutSet boolean = false "require a setter for each getter"
|
|
110
|
+
option setWithoutGet boolean = true "require a getter for each setter"
|
|
111
|
+
option enforceForClassMembers boolean = true "check class accessors"
|
|
112
|
+
option enforceForTSTypes boolean = false "check type accessors"
|
|
113
|
+
when accessor_pair != null && !accessor_pair.paired
|
|
114
|
+
when accessor_pair.scope == "object" || accessor_pair.scope == "class" && enforceForClassMembers || accessor_pair.scope == "type" && enforceForTSTypes
|
|
115
|
+
when accessor_pair.kind == "get" && getWithoutSet || accessor_pair.kind == "set" && setWithoutGet
|
|
116
|
+
report at { start: offset, end: key.end_offset }
|
|
117
|
+
warn "{if accessor_pair.kind == 'set' then 'Getter' else 'Setter'} is not present for {if accessor_pair.scope == 'class' then 'class ' else if accessor_pair.scope == 'type' then 'type ' else ''}{accessor_pair.name}."
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
in ts_object rule es-accessor-pairs:descriptor {
|
|
121
|
+
option getWithoutSet boolean = false "require a setter for each getter"
|
|
122
|
+
option setWithoutGet boolean = true "require a getter for each setter"
|
|
123
|
+
when property_descriptor != null
|
|
124
|
+
when setWithoutGet && property_descriptor.set && !property_descriptor.get || getWithoutSet && property_descriptor.get && !property_descriptor.set
|
|
125
|
+
warn "{if property_descriptor.set then 'Getter' else 'Setter'} is not present in property descriptor."
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
in ts_method, ts_method_signature rule es-grouped-accessor-pairs {
|
|
129
|
+
option mode ("anyOrder" | "getBeforeSet" | "setBeforeGet") = "anyOrder" "accessor order"
|
|
130
|
+
option enforceForTSTypes boolean = false "check type accessors"
|
|
131
|
+
let group_info accessor_pair.grouping
|
|
132
|
+
when accessor_pair.scope != "type" || enforceForTSTypes
|
|
133
|
+
when group_info != null && (group_info.gap || mode == "getBeforeSet" && !group_info.getter_first || mode == "setBeforeGet" && group_info.getter_first)
|
|
134
|
+
report at { start: offset, end: key.end_offset }
|
|
135
|
+
warn if group_info.gap then "Accessor pair {group_info.former} and {accessor_pair.name} should be grouped." else "Expected {accessor_pair.name} to be before {group_info.former}."
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
}
|