@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
package/std/ts/scopes.dx
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# .ts/scopes: rules on names and their uses.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
# Each binding belongs to one variant; arrays have their own name policy.
|
|
6
|
+
in ts_identifier let no_unused_vars_checked unused_var.setter != true
|
|
7
|
+
in ts_identifier let no_unused_vars_ignored null
|
|
8
|
+
in ts_identifier let unused_binding_context if unused_var.array_element || unused_var.array_target then "array" else unused_var.def
|
|
9
|
+
fn unused_binding_names(context) = when {
|
|
10
|
+
context == "array" then "elements of array destructuring"
|
|
11
|
+
context == "catch" then "caught errors"
|
|
12
|
+
context == "parameter" then "args"
|
|
13
|
+
else then "vars"
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@macro unused_binding_options {
|
|
17
|
+
option ignorePattern regex = "" "names allowed to be unused"
|
|
18
|
+
option reportUsedIgnorePattern boolean = false "report ignored names that are used"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@macro unused_binding_checks(context, after_used) {
|
|
22
|
+
let summary ts_usage_summary
|
|
23
|
+
when summary != null && (!summary.used || reportUsedIgnorePattern)
|
|
24
|
+
let u unused_var
|
|
25
|
+
when u != null && no_unused_vars_checked && unused_binding_context == context
|
|
26
|
+
let policy if type_of(no_unused_vars_ignored) == "null" then ignorePattern else no_unused_vars_ignored
|
|
27
|
+
let pattern if type_of(policy) == "str" then policy else ""
|
|
28
|
+
let ignored ts_usage_ignored(node, pattern)
|
|
29
|
+
let used_ignored ignored && reportUsedIgnorePattern && u.used
|
|
30
|
+
when used_ignored || !ignored && !u.used && !u.exported && !u.silent && !(after_used && u.before_used_param)
|
|
31
|
+
let names unused_binding_names(context)
|
|
32
|
+
report at if used_ignored then u.ignored_report_site else u.report_site
|
|
33
|
+
warn if used_ignored then "'{text}' is marked as ignored but is used{if pattern != '' then '. Used ' names ' must not match /' pattern '/u' else ''}."
|
|
34
|
+
else "'{text}' is {u.action} but {if u.only_type then 'only used as a type' else 'never used'}{if pattern != '' then '. Allowed unused ' names ' must match /' pattern '/u' else ''}."
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
in ts_identifier rule es-no-unused-vars:variables {
|
|
38
|
+
@unused_binding_options
|
|
39
|
+
option enableAutofixRemoval { imports?: boolean } = {} "remove unused import bindings"
|
|
40
|
+
@unused_binding_checks("variable", false)
|
|
41
|
+
let removal if enableAutofixRemoval.imports == true && !used_ignored then ts_usage_import_removal(source, node, pattern) else null
|
|
42
|
+
fix at removal if removal != null as ""
|
|
43
|
+
}
|
|
44
|
+
in ts_identifier rule es-no-unused-vars:parameters {
|
|
45
|
+
@unused_binding_options
|
|
46
|
+
default option mode enum(all, after-used) = after-used "which unused parameters to check"
|
|
47
|
+
@unused_binding_checks("parameter", mode == "after-used")
|
|
48
|
+
}
|
|
49
|
+
in ts_identifier rule es-no-unused-vars:caught-errors {
|
|
50
|
+
@unused_binding_options
|
|
51
|
+
@unused_binding_checks("catch", false)
|
|
52
|
+
}
|
|
53
|
+
in ts_identifier rule es-no-unused-vars:array-elements {
|
|
54
|
+
@unused_binding_options
|
|
55
|
+
@unused_binding_checks("array", false)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
# `/* global a */` names nothing reads.
|
|
59
|
+
in ts_program rule es-no-unused-vars:globals {
|
|
60
|
+
option vars "all" | "local" = "all" "`local`: skip the global scope of scripts"
|
|
61
|
+
when vars == "all"
|
|
62
|
+
each g in unused_globals {
|
|
63
|
+
report at { start: g.offset, end: g.offset + byte_len(g.name) }
|
|
64
|
+
warn "'{g.name}' is defined but never used."
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
in ts_node let scoping_atomic_target
|
|
4
|
+
if is ts_assignment.left then { owner: parent.node, property: false }
|
|
5
|
+
else if is ts_parenthesized.expression then parent.scoping_atomic_target
|
|
6
|
+
else if is (ts_member.object, ts_index.object) && parent.scoping_atomic_target != null
|
|
7
|
+
then { owner: parent.scoping_atomic_target.owner, property: true }
|
|
8
|
+
else null
|
|
9
|
+
fn scoping_atomic_binding(assignment, reference) = {
|
|
10
|
+
value target: reference.scoping_atomic_target
|
|
11
|
+
value references: scoping_binding_references(reference)
|
|
12
|
+
value declared: reference.declaration.scoping_binding_execution_scope
|
|
13
|
+
value shared: reference.declaration == null
|
|
14
|
+
|| declared != null && any(references, other => other.scoping_execution_scope != null
|
|
15
|
+
&& other.scoping_execution_scope.id != declared.id)
|
|
16
|
+
value entry: flow_entry(assignment)
|
|
17
|
+
value reads: {
|
|
18
|
+
value references
|
|
19
|
+
filter (other) flow_entry(other) == entry
|
|
20
|
+
&& (contains(["read", "read_write"], other.access) && other.scoping_atomic_target == null
|
|
21
|
+
|| other.scoping_atomic_target != null && other.scoping_atomic_target.owner.operator != "=")
|
|
22
|
+
}
|
|
23
|
+
value if !(shared || target.property && reference.reference_kind == "parameter")
|
|
24
|
+
|| flow_crosses(entry, reads, [assignment], ["awaited", "yielded"], reads) != true then null
|
|
25
|
+
else { name: reference.declaration.text ?? reference.text, property: target.property, value: assignment.left.text }
|
|
26
|
+
}
|
|
27
|
+
fn scoping_atomic_updates(assignment) = {
|
|
28
|
+
value owner: assignment.scoping_execution_owner
|
|
29
|
+
value suspending: owner is (ts_function, ts_arrow, ts_method)
|
|
30
|
+
&& any([] owner.modifier.nodes, modifier => contains(["async", "*"], modifier.text))
|
|
31
|
+
value if !suspending then [] else {
|
|
32
|
+
value walk(assignment.left.node, child => child.children.nodes)
|
|
33
|
+
filter (reference) reference is ts_identifier && reference.scoping_atomic_target.owner == assignment
|
|
34
|
+
map (reference) scoping_atomic_binding(assignment, reference)
|
|
35
|
+
filter (binding) binding != null
|
|
36
|
+
unique
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn scoping_within(inner, outer) = inner != null && outer != null
|
|
4
|
+
&& outer.offset <= inner.offset && inner.end_offset <= outer.end_offset
|
|
5
|
+
in ts_node let scoping_program parent.scoping_program
|
|
6
|
+
in ts_program let scoping_program node
|
|
7
|
+
in ts_node let scoping_site_kind
|
|
8
|
+
if is ts_function then "function"
|
|
9
|
+
else if is ts_class then "class"
|
|
10
|
+
else if is ts_interface then "interface"
|
|
11
|
+
else if is ts_enum then "enum"
|
|
12
|
+
else if is ts_namespace then "namespace"
|
|
13
|
+
else if is ts_type_alias then "type"
|
|
14
|
+
else if is ts_parameter then "parameter"
|
|
15
|
+
else if is ts_type_parameter then "type_parameter"
|
|
16
|
+
else if is (ts_import, ts_import_specifier) then "import"
|
|
17
|
+
else parent.scoping_site_kind
|
|
18
|
+
in ts_variable_declaration let scoping_site_kind kind
|
|
19
|
+
in ts_node let scoping_declaration_scopes name_scopes(node, "declaration")
|
|
20
|
+
in ts_identifier let scoping_lexical_declaration ts_name_declared
|
|
21
|
+
in ts_node let scoping_reference_scopes name_scopes(node, "reference")
|
|
22
|
+
in ts_node let scoping_lexical_scopes name_scopes(node, "lexical")
|
|
23
|
+
fn scoping_value_scope(scope) =
|
|
24
|
+
any(scope.classes, class => contains(["module", "function", "block", "catch", "class"], class))
|
|
25
|
+
fn scoping_script_scope(scope) = scope.owner is ts_program
|
|
26
|
+
&& ts_source_type(scope.owner) == "script" && scope.owner.source_environment.root_function != true
|
|
27
|
+
fn scoping_reference_scope(n) = {
|
|
28
|
+
value n.scoping_reference_scopes
|
|
29
|
+
filter (scope) scoping_value_scope(scope)
|
|
30
|
+
first
|
|
31
|
+
}
|
|
32
|
+
fn scoping_function_scope(n, role) = {
|
|
33
|
+
value if role == "declaration" then n.scoping_declaration_scopes
|
|
34
|
+
else if role == "reference" then n.scoping_reference_scopes else n.scoping_lexical_scopes
|
|
35
|
+
filter (scope) contains(scope.classes, "function")
|
|
36
|
+
first
|
|
37
|
+
}
|
|
38
|
+
in ts_node let scoping_execution_scope
|
|
39
|
+
scoping_function_scope(node, "reference") ?? scoping_function_scope(node, "lexical")
|
|
40
|
+
in ts_node let scoping_execution_owner scoping_execution_scope.owner
|
|
41
|
+
in ts_identifier let scoping_binding_execution_scope scoping_function_scope(node, "declaration")
|
|
42
|
+
in ts_identifier let scoping_write_execution_scope
|
|
43
|
+
scoping_binding_execution_scope ?? scoping_execution_scope
|
|
44
|
+
in ts_program let scoping_reference_catalog descendants(ts_identifier)
|
|
45
|
+
in ts_node let scoping_reference_catalog parent.scoping_reference_catalog
|
|
46
|
+
fn scoping_binding_references(reference) =
|
|
47
|
+
if reference.declaration != null then reference.declaration.references
|
|
48
|
+
else if reference.resolved == "global" && contains(["readonly", "writable"], reference.global) then {
|
|
49
|
+
value reference.scoping_reference_catalog
|
|
50
|
+
filter (other) other.resolved == "global" && other.identifier_name == reference.identifier_name
|
|
51
|
+
&& contains(["readonly", "writable"], other.global)
|
|
52
|
+
} else []
|
|
53
|
+
in ts_node let scoping_binding_declarator
|
|
54
|
+
if is ts_declarator then node
|
|
55
|
+
else if is (ts_function_like, ts_program) then null
|
|
56
|
+
else parent.scoping_binding_declarator
|
|
57
|
+
fn scoping_binding_writes(declaration, iteration) = {
|
|
58
|
+
value writes: filter(declaration.references, reference => contains(["write", "read_write"], reference.access))
|
|
59
|
+
value initial: {
|
|
60
|
+
value declaration.declarations
|
|
61
|
+
filter (site) (iteration || site.scoping_site_kind == "var") && site.scoping_binding_declarator.init != null
|
|
62
|
+
|| iteration && site.scoping_binding_declarator.parent is (ts_for_in.left, ts_for_of.left)
|
|
63
|
+
}
|
|
64
|
+
value writes initial
|
|
65
|
+
}
|
|
66
|
+
fn scoping_binding_context(n) = {
|
|
67
|
+
value n
|
|
68
|
+
walk (current) if current is (ts_block, ts_for, ts_for_in, ts_for_of, ts_switch, ts_static_block, ts_program)
|
|
69
|
+
then null else current.parent.node
|
|
70
|
+
last
|
|
71
|
+
}
|
|
72
|
+
fn scoping_outside_definitions(reference) =
|
|
73
|
+
if reference.reference_kind == null
|
|
74
|
+
&& !(reference.scoping_site_kind == "var" && name_role_classes(reference, "declaration") != null
|
|
75
|
+
&& reference.scoping_binding_declarator.init != null) then []
|
|
76
|
+
else {
|
|
77
|
+
value sites: reference.declarations
|
|
78
|
+
value sites
|
|
79
|
+
filter (site) site.scoping_site_kind == "var" && {
|
|
80
|
+
value context: scoping_binding_context(site)
|
|
81
|
+
value !scoping_within(reference, context)
|
|
82
|
+
&& !any(sites, earlier => earlier.offset < site.offset
|
|
83
|
+
&& scoping_const_declaration(earlier) == scoping_const_declaration(site))
|
|
84
|
+
}
|
|
85
|
+
map (site) {
|
|
86
|
+
value position: position(site.offset)
|
|
87
|
+
value { line: position.js_line, column: position.js_column }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn scoping_definition_next(n) =
|
|
92
|
+
if n is (ts_declarator, ts_class, ts_function_like, ts_import, ts_export, ts_try) then null
|
|
93
|
+
else n.parent.node
|
|
94
|
+
in ts_identifier let scoping_definition_owners walk(node, current => scoping_definition_next(current))
|
|
95
|
+
fn scoping_definition_separate(reference, boundary) = {
|
|
96
|
+
value walk(reference, current => if current.parent.node == boundary then null else current.parent.node)
|
|
97
|
+
any (current) current.parent.node != boundary
|
|
98
|
+
&& (current is (ts_function.body, ts_function.parameter, ts_arrow.body, ts_arrow.parameter, ts_method.body, ts_method.parameter)
|
|
99
|
+
|| current is ts_class_property.init && !contains([] current.parent.modifier, "static"))
|
|
100
|
+
}
|
|
101
|
+
fn scoping_definition_initializes(reference, owner) =
|
|
102
|
+
if owner is (ts_assignment_pattern, ts_parameter) then scoping_within(reference, owner.right.node ?? owner.init.node)
|
|
103
|
+
else if owner is ts_pattern_property then owner.value is ts_assignment_pattern
|
|
104
|
+
&& scoping_within(reference, owner.value.right.node)
|
|
105
|
+
else if owner is ts_declarator then scoping_within(reference, owner.init.node)
|
|
106
|
+
|| owner.parent.parent is (ts_for_in, ts_for_of) && scoping_within(reference, owner.parent.parent.right.node)
|
|
107
|
+
else if owner is ts_class then scoping_within(reference, owner) && !any([] owner.member.nodes, member =>
|
|
108
|
+
member is ts_static_block && scoping_within(reference, member)
|
|
109
|
+
|| member is ts_class_property && contains([] member.modifier, "static") && scoping_within(reference, member.init.node))
|
|
110
|
+
else false
|
|
111
|
+
fn scoping_definition_context(reference) = if reference.reference_kind == null then null else {
|
|
112
|
+
value declaration: reference.declaration.node
|
|
113
|
+
value scope: first(declaration.scoping_declaration_scopes)
|
|
114
|
+
value separate: scope != null && scoping_definition_separate(reference, scope.owner)
|
|
115
|
+
value owners: declaration.scoping_definition_owners
|
|
116
|
+
value can_initialize: !separate && reference.access != "type"
|
|
117
|
+
value decorated: can_initialize && any(owners, owner => owner is ts_class
|
|
118
|
+
&& any([] owner.decorator.nodes, decorator => scoping_within(reference, decorator)))
|
|
119
|
+
value initialized: can_initialize && any(owners, owner => scoping_definition_initializes(reference, owner))
|
|
120
|
+
value if declaration == null || scope == null then null
|
|
121
|
+
else { kind: reference.reference_kind, before: !decorated && (reference.end_offset < declaration.end_offset || initialized),
|
|
122
|
+
separate: separate, export: reference.parent is ts_export_specifier }
|
|
123
|
+
}
|
|
124
|
+
in ts_identifier let scoping_definition_use scoping_definition_context(node)
|
|
125
|
+
|
|
126
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
in ts_node let scoping_const_writer
|
|
4
|
+
if is (ts_assignment.left, ts_declarator.name) then parent.node
|
|
5
|
+
else if is ts_assignment_pattern.left
|
|
6
|
+
|| parent is (ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_property, ts_pattern_property, ts_rest, ts_spread, ts_parenthesized)
|
|
7
|
+
then parent.scoping_const_writer
|
|
8
|
+
else null
|
|
9
|
+
fn scoping_const_base(n) = n.scoping_site_kind == "let" && n.declaration.node == n && {
|
|
10
|
+
value declarator: n.scoping_binding_declarator
|
|
11
|
+
value scope: first(n.scoping_declaration_scopes)
|
|
12
|
+
value variable: declarator.parent.node
|
|
13
|
+
value if scope == null || declarator == null || variable is ts_for.init then false
|
|
14
|
+
else !(scoping_script_scope(scope) && contains(ts_exported_names(n.scoping_program), n.text))
|
|
15
|
+
}
|
|
16
|
+
fn scoping_const_pattern_children(n) =
|
|
17
|
+
if n is ts_assignment_pattern then n.left.node
|
|
18
|
+
else if n is (ts_property, ts_pattern_property) then n.value.node ?? n.key.node
|
|
19
|
+
else n.children.nodes
|
|
20
|
+
fn scoping_const_invalid_pattern(host, scope) = {
|
|
21
|
+
value left: host.left.node
|
|
22
|
+
value direct: if left is (ts_array, ts_array_pattern) then [] left.element.nodes
|
|
23
|
+
else if left is (ts_object, ts_object_pattern) then {
|
|
24
|
+
value [] left.property.nodes
|
|
25
|
+
map (property) property.value.node ?? property.key.node
|
|
26
|
+
} else []
|
|
27
|
+
value any(direct, item => item is ts_identifier && item.declaration != null && {
|
|
28
|
+
value declared: first(item.declaration.node.scoping_declaration_scopes)
|
|
29
|
+
value declared == null || declared.id != scope.id || item.declaration.scoping_site_kind == "parameter"
|
|
30
|
+
}) || {
|
|
31
|
+
value walk(left, item => scoping_const_pattern_children(item))
|
|
32
|
+
any (item) item is (ts_member, ts_index)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
fn scoping_const_candidate(n) = if !scoping_const_base(n) then null else {
|
|
36
|
+
value declarator: n.scoping_binding_declarator
|
|
37
|
+
value initialized: declarator.init != null || declarator.parent is (ts_for_in.left, ts_for_of.left)
|
|
38
|
+
value writes: filter(n.references, reference => contains(["write", "read_write"], reference.access))
|
|
39
|
+
value writer: if initialized && count(writes) == 0 then n else if !initialized && count(writes) == 1 then first(writes) else null
|
|
40
|
+
value host: if initialized then declarator else writer.scoping_const_writer
|
|
41
|
+
value before: any(n.references, reference => reference.offset < writer.offset && reference.access == "read")
|
|
42
|
+
value declared: first(n.scoping_declaration_scopes)
|
|
43
|
+
value written: scoping_reference_scope(writer)
|
|
44
|
+
value outer: host.scoping_outer_parens
|
|
45
|
+
value valid: initialized || declared.id == written.id && outer.parent is ts_expression_statement
|
|
46
|
+
&& outer.parent.parent is (ts_program, ts_block, ts_case, ts_static_block)
|
|
47
|
+
&& !scoping_const_invalid_pattern(host, declared)
|
|
48
|
+
value if writer == null || host == null || !valid then null
|
|
49
|
+
else { offset: if before then n.offset else writer.offset, before: before, group: host }
|
|
50
|
+
}
|
|
51
|
+
fn scoping_const_group(host) = {
|
|
52
|
+
value root: if host is ts_declarator then host.name.node else host.left.node
|
|
53
|
+
value walk(root, item => item.children.nodes)
|
|
54
|
+
filter (item) item is ts_identifier
|
|
55
|
+
&& (host is ts_declarator && item.scoping_binding_declarator == host && item.declaration.node == item
|
|
56
|
+
|| host is ts_assignment && item.scoping_const_writer == host && contains(["write", "read_write"], item.access))
|
|
57
|
+
map (item) item.declaration.node
|
|
58
|
+
filter (item) item != null && scoping_const_base(item)
|
|
59
|
+
unique
|
|
60
|
+
}
|
|
61
|
+
in ts_identifier let scoping_const_state {
|
|
62
|
+
value candidate: scoping_const_candidate(node)
|
|
63
|
+
value members: scoping_const_group(candidate.group)
|
|
64
|
+
value states: map(members, member => scoping_const_candidate(member))
|
|
65
|
+
value if candidate == null then null else {
|
|
66
|
+
offset: candidate.offset, before: candidate.before,
|
|
67
|
+
all: all(states, state => state != null),
|
|
68
|
+
all_before: any(states, state => state.before == true)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn scoping_global_root(n) = {
|
|
4
|
+
value scope: first(n.scoping_declaration_scopes)
|
|
5
|
+
value scope != null && scoping_script_scope(scope)
|
|
6
|
+
}
|
|
7
|
+
fn scoping_global_bindings(n) =
|
|
8
|
+
if ts_source_type(n.scoping_program) != "script" then [] else {
|
|
9
|
+
value owners: if n is ts_variable_declaration then [] n.declaration.nodes else [n]
|
|
10
|
+
value exported: ts_exported_names(n.scoping_program)
|
|
11
|
+
value owners
|
|
12
|
+
fold ([]) (entries, owner) entries {
|
|
13
|
+
value if owner.name == null then [] else overlapping(ts_identifier, owner.name.offset, owner.name.end_offset)
|
|
14
|
+
filter (site) scoping_within(site, owner.name.node) && scoping_global_root(site)
|
|
15
|
+
map (site) {
|
|
16
|
+
kind: site.scoping_site_kind,
|
|
17
|
+
exported: contains(exported, site.identifier_name),
|
|
18
|
+
setting: ts_global_setting(n.scoping_program, site.identifier_name),
|
|
19
|
+
name: site.identifier_name, offset: owner.offset, end: site.end_offset
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
fn scoping_global_mentions(program) = {
|
|
24
|
+
value mentions: {
|
|
25
|
+
value ts_global_mentions(program)
|
|
26
|
+
map (mention) { name: mention.name, offset: mention.offset, comment: mention.comment }
|
|
27
|
+
}
|
|
28
|
+
value roots: filter(program.scoping_lexical_scopes, scope => scoping_script_scope(scope) && scoping_value_scope(scope))
|
|
29
|
+
value script: ts_source_type(program) == "script"
|
|
30
|
+
value typed: re_test(program.file, /\.(ts|tsx|mts|cts)$/)
|
|
31
|
+
value mentions
|
|
32
|
+
filter (mention) !any(mentions, earlier => earlier.offset < mention.offset
|
|
33
|
+
&& earlier.name == mention.name && earlier.comment == mention.comment)
|
|
34
|
+
map (mention) {
|
|
35
|
+
value sites: {
|
|
36
|
+
value roots
|
|
37
|
+
flat_map (scope) name_scope_declarations(program, scope.id, mention.name)
|
|
38
|
+
}
|
|
39
|
+
value setting: ts_global_setting(program, mention.name)
|
|
40
|
+
value builtin: ts_global_implicit(program, mention.name)
|
|
41
|
+
&& contains(["readonly", "writable"], setting)
|
|
42
|
+
value syntax: script && any(sites, site => site.node is ts_identifier
|
|
43
|
+
&& any(site.classes, class => contains(["value", "type", "query"], class)))
|
|
44
|
+
value {
|
|
45
|
+
name: mention.name, offset: mention.offset, builtin: builtin, syntax: syntax && !typed,
|
|
46
|
+
duplicate: any(mentions, earlier => earlier.offset < mention.offset
|
|
47
|
+
&& earlier.name == mention.name && earlier.comment != mention.comment)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn scoping_loop_immediate(function) = function is (ts_function, ts_arrow)
|
|
4
|
+
&& !any([] function.modifier.nodes, modifier => contains(["async", "*"], modifier.text))
|
|
5
|
+
&& scoping_receiver_immediate(function) != null
|
|
6
|
+
&& count(function.name.references) == 0
|
|
7
|
+
in ts_node let scoping_loop_context
|
|
8
|
+
if parent is (ts_while, ts_do_while) then parent.node
|
|
9
|
+
else if parent is ts_for then if is ts_for.init then parent.scoping_loop_context else parent.node
|
|
10
|
+
else if parent is (ts_for_in, ts_for_of) then if is (ts_for_in.right, ts_for_of.right) then parent.scoping_loop_context else parent.node
|
|
11
|
+
else if parent is ts_function_like && !scoping_loop_immediate(parent.node) then null
|
|
12
|
+
else parent.scoping_loop_context
|
|
13
|
+
fn scoping_loop_unsafe(function, loop, reference) = {
|
|
14
|
+
value declaration: reference.declaration.node
|
|
15
|
+
value kind: reference.reference_kind
|
|
16
|
+
value variable: scoping_const_declaration(declaration)
|
|
17
|
+
value local: kind == "let" && loop.offset < variable.offset && variable.end_offset < loop.end_offset
|
|
18
|
+
value border: if kind == "let" then variable.end_offset else 0
|
|
19
|
+
value top: {
|
|
20
|
+
value loop
|
|
21
|
+
walk (current) if current.scoping_loop_context.offset >= border then current.scoping_loop_context else null
|
|
22
|
+
last
|
|
23
|
+
}
|
|
24
|
+
value writes: scoping_binding_writes(declaration, true)
|
|
25
|
+
value declared: declaration.scoping_binding_execution_scope
|
|
26
|
+
value declaration != null && !scoping_within(declaration, function)
|
|
27
|
+
&& !contains(["const", "using", "await using"], kind) && !local
|
|
28
|
+
&& any(writes, write => write.offset >= top.offset
|
|
29
|
+
|| declared != null && write.scoping_write_execution_scope != null
|
|
30
|
+
&& write.scoping_write_execution_scope.id != declared.id)
|
|
31
|
+
}
|
|
32
|
+
fn scoping_loop_captured(function) = {
|
|
33
|
+
value loop: function.scoping_loop_context
|
|
34
|
+
value if loop == null || scoping_loop_immediate(function) then [] else {
|
|
35
|
+
value function.scoping_reference_catalog
|
|
36
|
+
filter (reference) scoping_within(reference, function) && scoping_loop_unsafe(function, loop, reference)
|
|
37
|
+
sort_by (reference) reference.offset
|
|
38
|
+
map (reference) reference.text
|
|
39
|
+
unique
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
in ts_node let scoping_condition_dynamic any(children.nodes, child => child.scoping_condition_dynamic)
|
|
44
|
+
in ts_call, ts_member, ts_index, ts_new, ts_tagged_template, ts_yield let scoping_condition_dynamic true
|
|
45
|
+
in ts_function, ts_arrow, ts_class let scoping_condition_dynamic false
|
|
46
|
+
fn scoping_condition_next(n, independent) =
|
|
47
|
+
if n is (ts_while, ts_do_while, ts_for) then null
|
|
48
|
+
else if n.parent is (ts_while, ts_do_while, ts_for) then
|
|
49
|
+
if n is (ts_while.test, ts_do_while.test, ts_for.test) then n.parent.node else null
|
|
50
|
+
else if n.parent is ts_binary && n.parent.scoping_condition_dynamic
|
|
51
|
+
|| !independent && n.parent is ts_conditional && n.parent.scoping_condition_dynamic
|
|
52
|
+
|| n.parent is (ts_call, ts_member, ts_index, ts_new, ts_yield, ts_function_like, ts_class, ts_block,
|
|
53
|
+
ts_expression_statement, ts_variable_declaration, ts_declarator, ts_return, ts_if, ts_switch, ts_for_in, ts_for_of, ts_program) then null
|
|
54
|
+
else n.parent.node
|
|
55
|
+
fn scoping_condition_candidate(reference, independent) = {
|
|
56
|
+
value path: walk(reference, current => scoping_condition_next(current, independent))
|
|
57
|
+
value loop: last(path)
|
|
58
|
+
value group: { value path; filter (current) current is ts_binary || !independent && current is ts_conditional; last }
|
|
59
|
+
value if loop not (ts_while, ts_do_while, ts_for) || reference.declaration == null then null
|
|
60
|
+
else { loop: loop, group: group }
|
|
61
|
+
}
|
|
62
|
+
fn scoping_condition_in_loop(n, loop) = scoping_within(n, loop) && !scoping_within(n, loop.init.node)
|
|
63
|
+
fn scoping_condition_modified(reference, loop) = {
|
|
64
|
+
value declaration: reference.declaration.node
|
|
65
|
+
value writes: scoping_binding_writes(declaration, false)
|
|
66
|
+
value any(writes, write => scoping_condition_in_loop(write, loop) || {
|
|
67
|
+
value ancestors: walk(write, current => current.parent.node)
|
|
68
|
+
value owner: {
|
|
69
|
+
value ancestors
|
|
70
|
+
filter (current) current is ts_function && current.parent is (ts_program, ts_block, ts_export)
|
|
71
|
+
first
|
|
72
|
+
}
|
|
73
|
+
value any(owner.name.references, use => scoping_condition_in_loop(use, loop))
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
fn scoping_condition_unmodified(reference, independent) = {
|
|
77
|
+
value candidate: scoping_condition_candidate(reference, independent)
|
|
78
|
+
value if candidate == null then false
|
|
79
|
+
else if candidate.group == null then !scoping_condition_modified(reference, candidate.loop)
|
|
80
|
+
else !any(reference.scoping_reference_catalog, other => scoping_within(other, candidate.group) && {
|
|
81
|
+
value own: scoping_condition_candidate(other, independent)
|
|
82
|
+
value own.group == candidate.group && scoping_condition_modified(other, candidate.loop)
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
group member_usage_access ts_member, ts_index, ts_pattern_property, ts_property
|
|
4
|
+
in ts_class let member_usage_accesses descendants(member_usage_access)
|
|
5
|
+
fn member_usage_class(n) = find(walk(n, item => item.parent.node), item => item is ts_class)
|
|
6
|
+
fn member_usage_key(n) = if n is ts_parameter then n.name.node else n.key.node
|
|
7
|
+
in ts_class let member_usage_members {
|
|
8
|
+
value member.nodes
|
|
9
|
+
flat_map (member) if member is ts_method && member.calls_constructor then {
|
|
10
|
+
value member.parameter.nodes
|
|
11
|
+
filter (parameter) any(parameter.modifier.nodes,
|
|
12
|
+
modifier => contains(["private", "protected", "public", "readonly", "override"], modifier.text))
|
|
13
|
+
} else if member is (ts_method, ts_class_property) then [member] else []
|
|
14
|
+
}
|
|
15
|
+
fn member_usage_last(owner, name, static) = find_last(owner.member_usage_members,
|
|
16
|
+
member => ts_value_key(member_usage_key(member)) == name && ts_value_static(member.modifier.nodes) == static)
|
|
17
|
+
fn member_usage_pattern_source(n) = {
|
|
18
|
+
value pattern: n.parent.node
|
|
19
|
+
value owner: pattern.parent.node
|
|
20
|
+
value source: if pattern is (ts_object, ts_object_pattern) then
|
|
21
|
+
if owner is (ts_declarator, ts_parameter) && owner.name.node == pattern then owner.init.node
|
|
22
|
+
else if owner is (ts_assignment, ts_assignment_pattern) && owner.left.node == pattern then owner.right.node
|
|
23
|
+
else null
|
|
24
|
+
else null
|
|
25
|
+
value if unparenthesised_node(source) is ts_this then source else null
|
|
26
|
+
}
|
|
27
|
+
in ts_member, ts_index {
|
|
28
|
+
let member_usage_name if is ts_member then ts_value_key(property.node) else ts_value_key(index.node)
|
|
29
|
+
let member_usage_read {
|
|
30
|
+
value writer: name_write_owner
|
|
31
|
+
value if writer == null then true
|
|
32
|
+
else if writer is (ts_update, ts_assignment) then
|
|
33
|
+
writer.operator != "=" && writer.problem_expression_value_used == true
|
|
34
|
+
else writer is ts_unary
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
in ts_pattern_property, ts_property {
|
|
38
|
+
let member_usage_name if key is ts_identifier && member_usage_pattern_source(node) != null then ts_value_key(key.node) else null
|
|
39
|
+
let member_usage_read member_usage_name != null
|
|
40
|
+
}
|
|
41
|
+
fn member_usage_named_class(name, access) = {
|
|
42
|
+
value binding: if name is ts_identifier then name_binding(name) else null
|
|
43
|
+
value classes: map(binding.declarations ?? [], declaration => declaration.parent.node)
|
|
44
|
+
value find(walk(access, item => item.parent.node), item => item is ts_class && contains(classes, item))
|
|
45
|
+
}
|
|
46
|
+
fn member_usage_annotation(type, access) =
|
|
47
|
+
if type is (ts_type_reference, ts_type_query) then {
|
|
48
|
+
class: member_usage_named_class(type.name.node, access),
|
|
49
|
+
static: type is ts_type_query,
|
|
50
|
+
} else null
|
|
51
|
+
fn member_usage_this(access) = {
|
|
52
|
+
value context: ts_value_this_context(access)
|
|
53
|
+
value if context.explicit is ts_type_reference then member_usage_annotation(context.explicit, access)
|
|
54
|
+
else if context.container is ts_class && context.owner not ts_function
|
|
55
|
+
then { class: context.container, static: context.static }
|
|
56
|
+
else null
|
|
57
|
+
}
|
|
58
|
+
fn member_usage_receiver(access) = {
|
|
59
|
+
value object: if access is (ts_member, ts_index) then access.object.node else member_usage_pattern_source(access)
|
|
60
|
+
value binding: if object is ts_identifier then name_binding(object) else null
|
|
61
|
+
value declaration: binding.node.parent.node
|
|
62
|
+
value named: member_usage_named_class(object, access)
|
|
63
|
+
value if object is ts_this then member_usage_this(access)
|
|
64
|
+
else if named != null then { class: named, static: true }
|
|
65
|
+
else if declaration is ts_declarator then
|
|
66
|
+
if declaration.init is ts_this
|
|
67
|
+
&& all(binding.references, reference => reference.name_write_owner == null || reference.name_write_owner == declaration)
|
|
68
|
+
then member_usage_this(access) else null
|
|
69
|
+
else if declaration is ts_parameter then member_usage_annotation(declaration.type.node, access)
|
|
70
|
+
else null
|
|
71
|
+
}
|
|
72
|
+
fn member_usage_hash_owner(access) = {
|
|
73
|
+
value owners: walk(access, item => item.parent.node)
|
|
74
|
+
value find(owners, owner => owner is ts_class
|
|
75
|
+
&& member_usage_last(owner, access.member_usage_name, false) != null
|
|
76
|
+
|| owner is ts_class && member_usage_last(owner, access.member_usage_name, true) != null)
|
|
77
|
+
}
|
|
78
|
+
fn member_usage_used(declaration) = {
|
|
79
|
+
value name: ts_value_key(member_usage_key(declaration))
|
|
80
|
+
value class: member_usage_class(declaration)
|
|
81
|
+
value accessor: declaration is ts_method && contains(["get", "set"], declaration.kind)
|
|
82
|
+
|| contains([] declaration.modifier, "accessor")
|
|
83
|
+
value any(class.member_usage_accesses, access =>
|
|
84
|
+
access.member_usage_name == name && (accessor || access.member_usage_read)
|
|
85
|
+
&& (if access is ts_member && access.property is ts_private_name
|
|
86
|
+
then member_usage_hash_owner(access) == class
|
|
87
|
+
else {
|
|
88
|
+
value receiver: member_usage_receiver(access)
|
|
89
|
+
value receiver.class == class && receiver.static == ts_value_static(declaration.modifier.nodes)
|
|
90
|
+
}))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
in ts_program let member_usage_typescript_file re_test(file, /\.[cm]?tsx?$/)
|
|
94
|
+
in ts_method, ts_class_property, ts_parameter let member_usage_private
|
|
95
|
+
modifier != null && contains([] modifier, "private")
|
|
96
|
+
in ts_method.key, ts_class_property.key, ts_parameter.name rule es-no-unused-private-class-members {
|
|
97
|
+
when is (ts_private_name, ts_identifier, ts_string, ts_number, ts_computed_key)
|
|
98
|
+
when parent not ts_parameter || parent.member_usage_private
|
|
99
|
+
when is ts_private_name || parent.member_usage_private
|
|
100
|
+
let typed ts_program.member_usage_typescript_file
|
|
101
|
+
when is ts_private_name || typed
|
|
102
|
+
let declaration parent.node
|
|
103
|
+
let name ts_value_key(node)
|
|
104
|
+
when name != null
|
|
105
|
+
when member_usage_last(member_usage_class(declaration), name, ts_value_static(declaration.modifier.nodes)) == declaration
|
|
106
|
+
when !member_usage_used(declaration)
|
|
107
|
+
report at if parent is ts_parameter && parent.type != null then { start: offset, end: parent.type.end_offset } else node
|
|
108
|
+
warn if typed then "Private class member '{text}' is defined but never used." else "'{text}' is defined but never used."
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
}
|