@inthedark/dxlint 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +40 -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,192 @@
|
|
|
1
|
+
# .ts/style/ordering: member rank, optionality and name order.
|
|
2
|
+
|
|
3
|
+
import .ts/constants
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
group ordering_container ts_class, ts_interface, ts_type_literal
|
|
8
|
+
group ordering_member_node ts_method, ts_class_property, ts_method_signature, ts_property_signature, ts_call_signature, ts_index_signature, ts_static_block, ts_empty
|
|
9
|
+
group ordering_target ordering_container, ordering_member_node
|
|
10
|
+
in ordering_container let ordering_is_container true
|
|
11
|
+
in ts_class let ordering_context if parent is (ts_program, ts_block, ts_export, ts_namespace) then "class-declaration" else "class-expression"
|
|
12
|
+
in ts_interface let ordering_context "interface"
|
|
13
|
+
in ts_type_literal let ordering_context "type-literal"
|
|
14
|
+
in ts_method, ts_class_property, ts_property_signature, ts_index_signature let ordering_modifiers [] modifier
|
|
15
|
+
in ts_method, ts_class_property let ordering_decorated count(decorator) > 0
|
|
16
|
+
in ts_method let ordering_kind when {
|
|
17
|
+
parent is ts_class && !contains([] modifier, "static") && key not ts_computed_key && static_name == "constructor" then "constructor"
|
|
18
|
+
kind == "get" || kind == "set" then kind
|
|
19
|
+
else then "method"
|
|
20
|
+
}
|
|
21
|
+
in ts_method_signature let ordering_kind if kind == "get" || kind == "set" then kind else "method"
|
|
22
|
+
in ts_call_signature let ordering_kind if kind == "new" then "constructor" else "call-signature"
|
|
23
|
+
in ts_class_property let ordering_kind when {
|
|
24
|
+
contains([] modifier, "accessor") then "accessor"
|
|
25
|
+
init.returned_function == true then "method"
|
|
26
|
+
contains([] modifier, "readonly") then "readonly-field"
|
|
27
|
+
else then "field"
|
|
28
|
+
}
|
|
29
|
+
in ts_property_signature let ordering_kind if contains([] modifier, "readonly") then "readonly-field" else "field"
|
|
30
|
+
in ts_index_signature let ordering_kind if contains([] modifier, "readonly") then "readonly-signature" else "signature"
|
|
31
|
+
in ts_static_block let ordering_kind "static-initialization"
|
|
32
|
+
in ts_method, ts_class_property, ts_method_signature, ts_property_signature let ordering_key { text: static_name.nonempty ?? key.member_key_text.nonempty ?? key, private: key is ts_private_name }
|
|
33
|
+
in ts_parameter let ordering_parameter_name if name is ts_identifier then name else null
|
|
34
|
+
in ts_index_signature let ordering_name { value [] parameter.ordering_parameter_name; first; nonempty } ?? "(index signature)"
|
|
35
|
+
in ts_call_signature let ordering_name if kind == "new" then "new" else "call"
|
|
36
|
+
in ts_static_block let ordering_name "static block"
|
|
37
|
+
in ts_method, ts_method_signature, ts_class_property, ts_property_signature let ordering_name when {
|
|
38
|
+
ordering_kind == "constructor" then "constructor"
|
|
39
|
+
ordering_key.private then re_replace(ordering_key.text, /^#+/, "")
|
|
40
|
+
else then ordering_key.text
|
|
41
|
+
}
|
|
42
|
+
in ts_method let ordering_skip body == null && !contains([] modifier, "abstract")
|
|
43
|
+
in ts_class_property let ordering_initialiser init != null
|
|
44
|
+
in ts_member let ordering_this_dependency if object is ts_this then re_replace(property, /^#+/, "") else null
|
|
45
|
+
in ts_index let ordering_this_dependency if object is ts_this then ts_runtime_string(index.node) else null
|
|
46
|
+
in style_tree_node let ordering_dependencies if is (ts_function, ts_arrow, ts_method, ts_class) then [] else {
|
|
47
|
+
value [] ordering_this_dependency { value [] children.ordering_dependencies; fold ([]) (names, child) names child }
|
|
48
|
+
unique
|
|
49
|
+
}
|
|
50
|
+
in ts_class_property let ordering_initialiser_dependencies init.ordering_dependencies.nonempty ?? []
|
|
51
|
+
in ts_method, ts_class_property, ts_method_signature, ts_property_signature let ordering_optional_member optional != null
|
|
52
|
+
in ordering_member_node let ordering_access if ordering_key.private == true then "#private" else {
|
|
53
|
+
value ["public", "protected", "private"]
|
|
54
|
+
filter (access) contains(ordering_modifiers, access)
|
|
55
|
+
first
|
|
56
|
+
nonempty
|
|
57
|
+
} ?? "public"
|
|
58
|
+
in ordering_member_node let ordering_scope when {
|
|
59
|
+
ordering_decorated == true then "decorated"
|
|
60
|
+
contains(ordering_modifiers, "static") then "static"
|
|
61
|
+
contains(ordering_modifiers, "abstract") then "abstract"
|
|
62
|
+
else then "instance"
|
|
63
|
+
}
|
|
64
|
+
fn ordering_standard_rank(kind, class_member, access, scope) = (
|
|
65
|
+
if kind == null then null else get({ signature: 0, "readonly-signature": 0, "call-signature": 100, field: 200, "readonly-field": 200, "static-initialization": 300, constructor: 400, accessor: 500, get: 600, set: 700, method: 800 }, kind)
|
|
66
|
+
+ (if !class_member || contains(["signature", "readonly-signature", "call-signature", "static-initialization"], kind) then 0 else
|
|
67
|
+
(when {
|
|
68
|
+
kind == "constructor" then 0
|
|
69
|
+
scope == "abstract" && contains(["private", "#private"], access) then 80
|
|
70
|
+
scope == "decorated" && access == "#private" then 90
|
|
71
|
+
else then index_of(["static", "decorated", "instance", "abstract"], scope) * 10
|
|
72
|
+
})
|
|
73
|
+
+ (if kind != "constructor" && (scope == "abstract" && contains(["private", "#private"], access) || scope == "decorated" && access == "#private") then 0 else index_of(["public", "protected", "private", "#private"], access)))
|
|
74
|
+
)
|
|
75
|
+
in ordering_member_node let member_ordering_rank if ordering_skip == true then null else ordering_standard_rank(ordering_kind, parent is ts_class, ordering_access, ordering_scope)
|
|
76
|
+
fn ordering_standard_label(kind, class_member, access, scope) = {
|
|
77
|
+
value when {
|
|
78
|
+
kind == "readonly-field" then "field"
|
|
79
|
+
kind == "readonly-signature" then "signature"
|
|
80
|
+
else then kind
|
|
81
|
+
}
|
|
82
|
+
ordering_category_label class_member access scope
|
|
83
|
+
}
|
|
84
|
+
fn ordering_category_label(kind, class_member, access, scope) = when {
|
|
85
|
+
!class_member || contains(["signature", "call-signature", "static-initialization"], kind) then kind
|
|
86
|
+
kind == "constructor" then access " constructor"
|
|
87
|
+
scope == "abstract" && contains(["private", "#private"], access) || scope == "decorated" && access == "#private" then scope " " kind
|
|
88
|
+
else then access " " scope " " kind
|
|
89
|
+
}
|
|
90
|
+
in ordering_member_node let member_ordering_label ordering_standard_label(ordering_kind, parent is ts_class, ordering_access, ordering_scope)
|
|
91
|
+
in ordering_member_node let ordering_member {
|
|
92
|
+
index: index, name: ordering_name.nonempty ?? "", optional: ordering_optional_member == true,
|
|
93
|
+
rank: member_ordering_rank, label: member_ordering_label,
|
|
94
|
+
init: ordering_initialiser == true, dependencies: ordering_initialiser_dependencies.nonempty ?? []
|
|
95
|
+
}
|
|
96
|
+
in ordering_container let ordering_members [] member.ordering_member
|
|
97
|
+
fn ordering_member_end(n) = {
|
|
98
|
+
value after: token(n, "after")
|
|
99
|
+
value if n is (ts_property_signature, ts_method_signature, ts_call_signature, ts_index_signature)
|
|
100
|
+
&& after != null && contains([";", ","], after.text) then after.end else n.end_offset
|
|
101
|
+
}
|
|
102
|
+
fn ordering_at_rank(states, partition, rank) = find(states, state => state.partition == partition && state.rank == rank)
|
|
103
|
+
fn ordering_frontier(states, partition, rank) = {
|
|
104
|
+
value states
|
|
105
|
+
fold null (best, state) if state.partition == partition && state.rank > rank && (best == null || state.rank < best.rank) then state else best
|
|
106
|
+
}
|
|
107
|
+
# Displaced members must not become explanations for later failures.
|
|
108
|
+
fn ordering_frontiers(states, row, partition) = if row.rank == null || ordering_at_rank(states, partition, row.rank) != null || ordering_frontier(states, partition, row.rank) != null then states else states [{ partition: partition, rank: row.rank, label: row.label }]
|
|
109
|
+
fn ordering_alpha_bad(name, previous_name, mode) = name != "" && previous_name != null && name != previous_name && (if starts_with(mode, "natural") then { value mode; ends_with: ends_with "case-insensitive"; value compare_text(name, previous_name, true, ends_with) } <= 0 else { value mode; ends_with_2: ends_with "case-insensitive"; value compare_text(name, previous_name, false, ends_with_2) } < 0)
|
|
110
|
+
fn ordering_alpha_blocked(row, before, rows, optionality) = row.init && any(rows, earlier =>
|
|
111
|
+
earlier.index >= before.index && earlier.index < row.index && earlier.rank == row.rank && earlier.init
|
|
112
|
+
&& (optionality == "none" || earlier.optional == row.optional) && contains(row.dependencies, earlier.name))
|
|
113
|
+
fn ordering_alpha_previous(states, row, previous_row, reset) = if reset && previous_row.rank != row.rank then [] else states
|
|
114
|
+
fn ordering_alpha_states(states, row, partition, rows, mode, optionality, enabled) = (
|
|
115
|
+
if !enabled || row.name == "" || {
|
|
116
|
+
value previous_name: ordering_at_rank(states, partition, row.rank).name
|
|
117
|
+
value ordering_alpha_bad(row.name, previous_name, mode)
|
|
118
|
+
}
|
|
119
|
+
&& {
|
|
120
|
+
value states
|
|
121
|
+
before: ordering_at_rank partition row.rank
|
|
122
|
+
value ordering_alpha_blocked(row, before, rows, optionality)
|
|
123
|
+
} then states
|
|
124
|
+
else filter(states, state => state.partition != partition || state.rank != row.rank) [{ partition: partition, rank: row.rank, name: row.name, index: row.index }]
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
@macro ordering_rule(rule-id, context) {
|
|
128
|
+
in ordering_target rule rule-id {
|
|
129
|
+
default option order enum(alphabetically, alphabetically-case-insensitive, as-written, natural, natural-case-insensitive) = as-written "Name order within a rank."
|
|
130
|
+
option optionality enum(none, optional-first, required-first) = none "Optional and required member order."
|
|
131
|
+
let owner if ordering_is_container == true then node else parent.node
|
|
132
|
+
let row ordering_member
|
|
133
|
+
let checked row != null && row.rank != null
|
|
134
|
+
let preferred optionality == "optional-first"
|
|
135
|
+
let partition if optionality == "none" || row.optional == preferred then 0 else 1
|
|
136
|
+
let first_index previous.first_index.nonempty ?? if checked then row.index else null
|
|
137
|
+
let first_optional when {
|
|
138
|
+
previous.first_index != null then previous.first_optional
|
|
139
|
+
checked then row.optional
|
|
140
|
+
else then null
|
|
141
|
+
}
|
|
142
|
+
let phase_start previous.phase_start.nonempty ?? if checked && row.optional != preferred then row.index else null
|
|
143
|
+
let contains_preferred checked && row.optional == preferred || next.contains_preferred == true
|
|
144
|
+
let optionality_error optionality != "none" && checked && row.optional != preferred && next.contains_preferred == true
|
|
145
|
+
&& row.index == (if first_optional != preferred then first_index else phase_start)
|
|
146
|
+
let optionality_bad if ordering_is_container == true then any(children.optionality_error, bad => bad == true) else parent.optionality_bad
|
|
147
|
+
let frontiers { value previous.frontiers.nonempty ?? []; ordering_frontiers row partition }
|
|
148
|
+
let before_frontiers previous.frontiers.nonempty ?? []
|
|
149
|
+
let frontier ordering_frontier(before_frontiers, partition, row.rank)
|
|
150
|
+
let rank_bad checked && frontier != null
|
|
151
|
+
let rank_status if checked then { partition: partition, bad: rank_bad } else null
|
|
152
|
+
let bad_partitions if ordering_is_container == true then {
|
|
153
|
+
value children.rank_status
|
|
154
|
+
filter (status) status.bad
|
|
155
|
+
map (status) status.partition
|
|
156
|
+
unique
|
|
157
|
+
} else parent.bad_partitions
|
|
158
|
+
let alpha_enabled checked && order != "as-written" && !optionality_bad
|
|
159
|
+
let alpha_states {
|
|
160
|
+
value states: previous.alpha_states.nonempty ?? []
|
|
161
|
+
value bad_partitions
|
|
162
|
+
reset: contains partition
|
|
163
|
+
value ordering_alpha_previous(states, row, previous.row, reset)
|
|
164
|
+
ordering_alpha_states row partition owner.ordering_members order optionality alpha_enabled
|
|
165
|
+
}
|
|
166
|
+
let before_alpha {
|
|
167
|
+
value states: previous.alpha_states.nonempty ?? []
|
|
168
|
+
value bad_partitions
|
|
169
|
+
reset: contains partition
|
|
170
|
+
value ordering_alpha_previous(states, row, previous.row, reset)
|
|
171
|
+
}
|
|
172
|
+
let alpha_before ordering_at_rank(before_alpha, partition, row.rank)
|
|
173
|
+
let alpha_bad alpha_enabled && ordering_alpha_bad(row.name, alpha_before.name, order)
|
|
174
|
+
let alpha_blocked alpha_bad && ordering_alpha_blocked(row, alpha_before, owner.ordering_members, optionality)
|
|
175
|
+
when owner.ordering_context == context && checked
|
|
176
|
+
report at { start: offset, end: ordering_member_end(node) }
|
|
177
|
+
each failure in if optionality_bad then (if optionality_error then ["optionality"] else [])
|
|
178
|
+
else (if rank_bad then ["rank"] else []) (if alpha_bad && !alpha_blocked then ["alpha"] else []) {
|
|
179
|
+
warn when {
|
|
180
|
+
failure == "optionality" then "Member " row.name " should be declared after all " (if preferred then "optional" else "required") " members."
|
|
181
|
+
failure == "rank" then "Member " row.name " should be declared before all " frontier.label " definitions."
|
|
182
|
+
else then "Member " row.name " should be declared before member " alpha_before.name "."
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
@ordering_rule(es-member-ordering:class-declaration, "class-declaration")
|
|
188
|
+
@ordering_rule(es-member-ordering:class-expression, "class-expression")
|
|
189
|
+
@ordering_rule(es-member-ordering:interface, "interface")
|
|
190
|
+
@ordering_rule(es-member-ordering:type-literal, "type-literal")
|
|
191
|
+
|
|
192
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# .ts/style/returns: internal part of .ts/style.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_as, ts_type_assertion, ts_jsx_expression, ts_jsx_spread_attribute, ts_new let return_accepts_value "all"
|
|
6
|
+
in ts_declarator, ts_class_property let return_accepts_value if type != null then "all" else ""
|
|
7
|
+
in ts_parameter let return_accepts_value if type != null then "init" else ""
|
|
8
|
+
in style_tree_node let return_parent_typed if parent is ts_parenthesized then parent.return_parent_typed else parent.return_accepts_value == "all" || parent.return_accepts_value == "init" && is ts_node.init || parent is ts_call && not ts_call.callee
|
|
9
|
+
in ts_declarator let return_context_name if name is ts_identifier then name else ""
|
|
10
|
+
in ts_class_property, ts_property let return_context_name if key is ts_identifier then key else ""
|
|
11
|
+
in style_tree_node let return_parent_info if parent is ts_parenthesized then parent.return_parent_info else { node: parent.node, name: parent.return_context_name }
|
|
12
|
+
in ts_function let return_own_name name
|
|
13
|
+
in ts_method let return_own_name if key is ts_identifier then key else ""
|
|
14
|
+
in ts_function, ts_arrow, ts_method let return_annotation return_type != null
|
|
15
|
+
in ts_declarator, ts_class_property let return_annotation type != null
|
|
16
|
+
in style_tree_node let return_ancestor_context if is (ts_declarator, ts_class_property) then return_annotation else if is ts_expression_statement then false else return_annotation == true || parent.return_ancestor_context
|
|
17
|
+
in ts_arrow let return_concise body not ts_block
|
|
18
|
+
in style_tree_node let return_ancestor_typed if parent is ts_parenthesized then parent.return_ancestor_typed else (parent is ts_return || is ts_arrow.body && parent.return_concise) && parent.return_ancestor_context == true
|
|
19
|
+
in ts_property, ts_method let return_object_typed parent is ts_object && parent.return_value_typed
|
|
20
|
+
in style_tree_node let return_property_typed if parent is ts_parenthesized then parent.return_property_typed else parent.return_object_typed == true
|
|
21
|
+
in style_tree_node let return_value_typed if parent is ts_parenthesized then parent.return_value_typed else return_parent_typed || return_property_typed || parent is ts_array && parent.return_value_typed || parent is ts_return && return_ancestor_typed
|
|
22
|
+
in ts_function, ts_arrow let returned_function true
|
|
23
|
+
in ts_parenthesized let returned_function expression.returned_function
|
|
24
|
+
in ts_as, ts_type_assertion let returned_const_assertion type == "const"
|
|
25
|
+
in ts_satisfies, ts_parenthesized let returned_const_assertion expression.returned_const_assertion
|
|
26
|
+
in ts_unary let returned_void operator == "void"
|
|
27
|
+
in ts_parenthesized let returned_void expression.returned_void
|
|
28
|
+
in ts_return let return_function argument.returned_function == true
|
|
29
|
+
in style_tree_node let function_returns_any if is ts_return then true else if is (ts_function, ts_arrow, ts_method, ts_class) then false else any(children.function_returns_any, any => any)
|
|
30
|
+
# Cheap aggregates; the returned values themselves are only collected where a
|
|
31
|
+
# caller needs them.
|
|
32
|
+
in style_tree_node let function_returns_functions if is ts_return then return_function else if is (ts_function, ts_arrow, ts_method, ts_class) then true else all(children.nodes, child => !child.function_returns_any || child.function_returns_functions)
|
|
33
|
+
in ts_return let return_value { value: argument.node, function: argument.returned_function == true }
|
|
34
|
+
in style_tree_node let function_return_values if is ts_return then [return_value] else if is (ts_function, ts_arrow, ts_method, ts_class) then [] else { value [] children.function_return_values; fold ([]) (values, child) values child }
|
|
35
|
+
in ts_method let return_method_valid key == "constructor" || kind == "set"
|
|
36
|
+
|
|
37
|
+
# ESLint reports the function head: the member or node start up to the parameter
|
|
38
|
+
# list's opening paren. It does not know accessor properties, so a function they
|
|
39
|
+
# hold reports from the function itself.
|
|
40
|
+
in ts_function, ts_arrow, ts_method let return_report_site when {
|
|
41
|
+
parent.boundary_accessor != true then function_head_site(node)
|
|
42
|
+
is ts_arrow then operator.node
|
|
43
|
+
else then { start: offset, end: function_head_site(node).end }
|
|
44
|
+
}
|
|
45
|
+
in ts_function, ts_arrow, ts_method let return_declaration is ts_function && return_parent_info.node is (ts_program, ts_block, ts_export, ts_namespace)
|
|
46
|
+
in ts_function, ts_arrow, ts_method let return_context_typed !return_declaration && (return_parent_typed || return_property_typed) || is ts_method && return_object_typed
|
|
47
|
+
in ts_function, ts_arrow, ts_method let explicit_return_shape {
|
|
48
|
+
name: return_own_name.nonempty ?? return_parent_info.name.nonempty ?? "",
|
|
49
|
+
valid: return_annotation || return_method_valid == true,
|
|
50
|
+
typed: return_context_typed || !return_declaration && return_ancestor_typed,
|
|
51
|
+
context_typed: return_context_typed,
|
|
52
|
+
ancestor_typed: !return_declaration && return_ancestor_typed,
|
|
53
|
+
free_expression: !return_declaration && return_parent_info.node != null && return_parent_info.node not (ts_declarator, ts_class_property, ts_export, ts_class),
|
|
54
|
+
higher_order: is ts_arrow && body.returned_function == true || body.function_returns_any && body.function_returns_functions,
|
|
55
|
+
const_assertion: is ts_arrow && body.returned_const_assertion == true,
|
|
56
|
+
iife: return_parent_info.node is ts_call,
|
|
57
|
+
void_arrow: is ts_arrow && body.returned_void == true
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@macro explicit_return_rule(rule-id, return_context) {
|
|
61
|
+
in return_context rule rule-id {
|
|
62
|
+
option allowConciseArrowFunctionExpressionsStartingWithVoid boolean = false "Allow concise arrows starting with void."
|
|
63
|
+
option allowDirectConstAssertionInArrowFunctions boolean = true "Allow concise arrows returning const assertions."
|
|
64
|
+
option allowedNames string[] = [] "Allowed function and member names."
|
|
65
|
+
option allowExpressions boolean = false "Allow function expressions outside declarations."
|
|
66
|
+
option allowFunctionsWithoutTypeParameters boolean = false "Allow nongeneric functions."
|
|
67
|
+
option allowHigherOrderFunctions boolean = true "Allow functions returning only functions."
|
|
68
|
+
option allowIIFEs boolean = false "Allow functions directly within calls."
|
|
69
|
+
option allowTypedFunctionExpressions boolean = true "Allow contextually typed function expressions."
|
|
70
|
+
let shape explicit_return_shape
|
|
71
|
+
when !shape.valid && !contains(allowedNames, shape.name)
|
|
72
|
+
when !(allowFunctionsWithoutTypeParameters && count(type_parameter) == 0 || allowIIFEs && shape.iife || allowHigherOrderFunctions && shape.higher_order || allowConciseArrowFunctionExpressionsStartingWithVoid && shape.void_arrow)
|
|
73
|
+
when !(allowTypedFunctionExpressions && (shape.typed || allowExpressions && shape.free_expression || allowDirectConstAssertionInArrowFunctions && shape.const_assertion))
|
|
74
|
+
report at return_report_site
|
|
75
|
+
warn "Missing return type on function."
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
@explicit_return_rule(es-explicit-function-return-type, ts_function)
|
|
79
|
+
@explicit_return_rule(es-explicit-function-return-type:method, ts_method)
|
|
80
|
+
@explicit_return_rule(es-explicit-function-return-type:arrow, ts_arrow)
|
|
81
|
+
|
|
82
|
+
# explicit-module-boundary-types: what other modules see needs types.
|
|
83
|
+
group style_boundary_export ts_export, ts_export_assignment
|
|
84
|
+
in ts_class_property, ts_method let boundary_private contains([] modifier, "private") || key is ts_private_name
|
|
85
|
+
in ts_function, ts_arrow, ts_method let boundary_returned_functions if is ts_arrow && body not ts_block then if body.returned_function == true then body.nodes else [] else if body.function_returns_any && body.function_returns_functions then map(body.function_return_values, value => value.value) else []
|
|
86
|
+
in ts_assignment let boundary_written_value right.node
|
|
87
|
+
in ts_identifier let boundary_binding_owner parent.node
|
|
88
|
+
in ts_identifier let boundary_identifier_refs if reference_kind != null && !contains(["parameter", "catch", "import"], reference_kind) then [] declaration.boundary_binding_owner [] declaration.references.writer.boundary_written_value else []
|
|
89
|
+
in ts_class let boundary_structural_refs member.nodes
|
|
90
|
+
in ts_object let boundary_structural_refs property.nodes
|
|
91
|
+
in ts_array let boundary_structural_refs element.nodes
|
|
92
|
+
in ts_variable_declaration let boundary_structural_refs declaration.nodes
|
|
93
|
+
in ts_declarator let boundary_structural_refs init.nodes
|
|
94
|
+
in ts_class_property let boundary_structural_refs if boundary_private then [] else init.nodes
|
|
95
|
+
in ts_property let boundary_structural_refs if value != null then value.nodes else if key is ts_identifier then key.nodes else []
|
|
96
|
+
in ts_parenthesized let boundary_structural_refs expression.nodes
|
|
97
|
+
in ts_function, ts_arrow, ts_method let boundary_structural_refs if boundary_private == true then [] else boundary_returned_functions
|
|
98
|
+
in style_tree_node let boundary_next boundary_structural_refs.nonempty ?? boundary_identifier_refs.nonempty ?? []
|
|
99
|
+
in ts_export_specifier let boundary_export_value local.node
|
|
100
|
+
in ts_export let boundary_export_roots if from == null then declaration.nodes [] expression.node [] specifier.boundary_export_value else []
|
|
101
|
+
in ts_export_assignment let boundary_export_roots expression.nodes
|
|
102
|
+
in ts_program let boundary_roots { value descendants(style_boundary_export); fold ([]) (roots, n) roots n.boundary_export_roots }
|
|
103
|
+
in ts_program let boundary_export_nodes walk(boundary_roots, n => n.boundary_next)
|
|
104
|
+
in ts_declarator let boundary_context_name if name is ts_identifier then name else ""
|
|
105
|
+
in ts_class_property let boundary_context_name member_static_name.nonempty ?? ""
|
|
106
|
+
in style_tree_node let boundary_parent_name if parent is ts_parenthesized then parent.boundary_parent_name else parent.boundary_context_name
|
|
107
|
+
in ts_function let boundary_function_name if return_declaration then name.nonempty ?? "" else boundary_parent_name.nonempty ?? ""
|
|
108
|
+
in ts_arrow let boundary_function_name boundary_parent_name.nonempty ?? ""
|
|
109
|
+
in ts_method let boundary_function_name member_static_name.nonempty ?? ""
|
|
110
|
+
in ts_function, ts_method let boundary_overload_entry { name: boundary_function_name, empty: body == null, exported: parent is ts_export, default: parent.boundary_default_export == true }
|
|
111
|
+
in ts_export let boundary_default_export kind == "default"
|
|
112
|
+
in ts_export let boundary_overload_entry declaration.boundary_overload_entry
|
|
113
|
+
in style_tree_node let boundary_overload_entries if is ts_export then parent.boundary_overload_entries else [] children.boundary_overload_entry
|
|
114
|
+
in ts_function, ts_arrow, ts_method let boundary_has_overload any(parent.boundary_overload_entries, entry => entry.empty && (parent is ts_export && entry.exported && entry.default == parent.boundary_default_export && (entry.default || entry.name == boundary_function_name) || parent not ts_export && !entry.exported && entry.name == boundary_function_name))
|
|
115
|
+
in ts_method let boundary_empty_method body == null
|
|
116
|
+
in ts_class_property let boundary_accessor contains([] modifier, "accessor")
|
|
117
|
+
in ts_function, ts_arrow, ts_method let boundary_report_site
|
|
118
|
+
if boundary_empty_method == true then { start: function_head_site(node).end, end: end_offset } else return_report_site
|
|
119
|
+
in ts_function, ts_arrow, ts_method let boundary_function_reachable boundary_private != true && contains(ts_program.boundary_export_nodes, node)
|
|
120
|
+
in ts_parameter let boundary_argument {
|
|
121
|
+
eligible: init == null && (type == null || type == "any"),
|
|
122
|
+
any: type == "any",
|
|
123
|
+
offset: name.offset,
|
|
124
|
+
end: if modifier == null then end_offset else name.end_offset,
|
|
125
|
+
name: name.unified_parameter_name,
|
|
126
|
+
pattern: if name is ts_rest then "Rest" else if name is ts_array_pattern then "Array pattern" else "Object pattern"
|
|
127
|
+
}
|
|
128
|
+
@macro boundary_rule(rule-id, boundary_context, parameter_check) {
|
|
129
|
+
in boundary_context rule rule-id {
|
|
130
|
+
option allowArgumentsExplicitlyTypedAsAny boolean = false "Allow explicit any parameter types."
|
|
131
|
+
option allowDirectConstAssertionInArrowFunctions boolean = true "Allow concise arrows returning const assertions."
|
|
132
|
+
option allowedNames string[] = [] "Allowed function and member names."
|
|
133
|
+
option allowHigherOrderFunctions boolean = true "Allow functions returning only functions."
|
|
134
|
+
option allowTypedFunctionExpressions boolean = true "Allow contextually typed functions."
|
|
135
|
+
option allowOverloadFunctions boolean = false "Allow implementations with overload signatures."
|
|
136
|
+
let boundary_shape if parameter_check then parent.explicit_return_shape else explicit_return_shape
|
|
137
|
+
let boundary_name if parameter_check then parent.boundary_function_name else boundary_function_name
|
|
138
|
+
let empty_method if parameter_check then parent.boundary_empty_method else boundary_empty_method
|
|
139
|
+
let overload if parameter_check then parent.boundary_has_overload else boundary_has_overload
|
|
140
|
+
when if parameter_check then parent.boundary_function_reachable && boundary_argument.eligible else boundary_function_reachable
|
|
141
|
+
when empty_method == true || !(contains(allowedNames, boundary_name) || boundary_shape.ancestor_typed || allowTypedFunctionExpressions && boundary_shape.context_typed || allowOverloadFunctions && overload)
|
|
142
|
+
when if parameter_check then !(allowArgumentsExplicitlyTypedAsAny && boundary_argument.any) else !boundary_shape.valid && !(allowHigherOrderFunctions && boundary_shape.higher_order || allowDirectConstAssertionInArrowFunctions && boundary_shape.const_assertion)
|
|
143
|
+
report at if parameter_check then { start: boundary_argument.offset, end: boundary_argument.end } else boundary_report_site
|
|
144
|
+
warn if parameter_check then (if boundary_argument.name != null then "Argument '" boundary_argument.name "'" else boundary_argument.pattern " argument") " should be typed" (if boundary_argument.any then " with a non-any type" else "") "." else "Missing return type on function."
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
@boundary_rule(es-explicit-module-boundary-types, ts_function, false)
|
|
148
|
+
@boundary_rule(es-explicit-module-boundary-types:method, ts_method, false)
|
|
149
|
+
@boundary_rule(es-explicit-module-boundary-types:arrow, ts_arrow, false)
|
|
150
|
+
@boundary_rule(es-explicit-module-boundary-types:argument, ts_parameter, true)
|
|
151
|
+
|
|
152
|
+
}
|