Skip to content

Commit 610a705

Browse files
committed
feat(semantic): add to indicate referenced by
1 parent 5d17675 commit 610a705

13 files changed

Lines changed: 37 additions & 57 deletions

File tree

crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oxc_ast::{
1010
};
1111
use oxc_diagnostics::OxcDiagnostic;
1212
use oxc_macros::declare_oxc_lint;
13-
use oxc_semantic::SymbolId;
13+
use oxc_semantic::{Reference, SymbolId};
1414
use oxc_span::{CompactStr, GetSpan, Span};
1515

1616
use crate::{
@@ -309,23 +309,7 @@ fn is_only_has_type_references(symbol_id: SymbolId, ctx: &LintContext) -> bool {
309309
if peekable_iter.peek().is_none() {
310310
return false;
311311
}
312-
peekable_iter.all(|reference| {
313-
if reference.is_type() {
314-
return true;
315-
} else if reference.is_read() {
316-
for node in ctx.nodes().iter_parents(reference.node_id()).skip(1) {
317-
return match node.kind() {
318-
// CASE 1:
319-
// `type T = typeof foo` will create a value reference because "foo" must be a value type
320-
// however this value reference is safe to use with type-only imports
321-
AstKind::TSTypeQuery(_) => true,
322-
AstKind::TSTypeName(_) | AstKind::TSQualifiedName(_) => continue,
323-
_ => false,
324-
};
325-
}
326-
}
327-
false
328-
})
312+
peekable_iter.all(Reference::is_type)
329313
}
330314

331315
struct FixOptions<'a, 'b> {

crates/oxc_semantic/src/builder.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,15 +1715,17 @@ impl<'a> SemanticBuilder<'a> {
17151715
AstKind::TSInterfaceHeritage(_) => {
17161716
self.current_reference_flag = ReferenceFlag::Type;
17171717
}
1718+
AstKind::TSTypeQuery(_) => {
1719+
// type A = typeof a;
1720+
// ^^^^^^^^
1721+
self.current_reference_flag = ReferenceFlag::Read | ReferenceFlag::TSTypeQuery;
1722+
}
17181723
AstKind::TSTypeName(_) => {
17191724
match self.nodes.parent_kind(self.current_node_id) {
17201725
Some(
1721-
// type A = typeof a;
1722-
// ^^^^^^^^
1723-
AstKind::TSTypeQuery(_)
17241726
// import A = a;
17251727
// ^
1726-
| AstKind::TSModuleReference(_)
1728+
AstKind::TSModuleReference(_),
17271729
) => {
17281730
self.current_reference_flag = ReferenceFlag::Read;
17291731
}
@@ -1732,7 +1734,9 @@ impl<'a> SemanticBuilder<'a> {
17321734
// ^^^ Keep the current reference flag
17331735
}
17341736
_ => {
1735-
self.current_reference_flag = ReferenceFlag::Type;
1737+
if !self.current_reference_flag.is_ts_type_query() {
1738+
self.current_reference_flag = ReferenceFlag::Type;
1739+
}
17361740
}
17371741
}
17381742
}
@@ -1847,7 +1851,9 @@ impl<'a> SemanticBuilder<'a> {
18471851
self.current_reference_flag -= ReferenceFlag::Read;
18481852
}
18491853
}
1850-
AstKind::MemberExpression(_) => self.current_reference_flag = ReferenceFlag::empty(),
1854+
AstKind::MemberExpression(_) | AstKind::TSTypeQuery(_) => {
1855+
self.current_reference_flag = ReferenceFlag::empty();
1856+
}
18511857
AstKind::AssignmentTarget(_) => self.current_reference_flag -= ReferenceFlag::Write,
18521858
_ => {}
18531859
}
@@ -1873,10 +1879,10 @@ impl<'a> SemanticBuilder<'a> {
18731879

18741880
/// Resolve reference flags for the current ast node.
18751881
fn resolve_reference_usages(&self) -> ReferenceFlag {
1876-
if self.current_reference_flag.is_write() || self.current_reference_flag.is_type() {
1877-
self.current_reference_flag
1878-
} else {
1882+
if self.current_reference_flag.is_empty() {
18791883
ReferenceFlag::Read
1884+
} else {
1885+
self.current_reference_flag
18801886
}
18811887
}
18821888

crates/oxc_semantic/src/reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ impl Reference {
8080
}
8181

8282
pub fn is_type(&self) -> bool {
83-
self.flag.is_type()
83+
self.flag.is_type() || self.flag.is_ts_type_query()
8484
}
8585
}

crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/type-reference.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati
4444
"node_id": 8
4545
},
4646
{
47-
"flag": "ReferenceFlag(Read)",
47+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
4848
"id": 1,
4949
"name": "A",
5050
"node_id": 13

crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-expressions/type-arguments2.snap

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-e
5252
"id": 4,
5353
"name": "T",
5454
"node": "TSTypeParameter",
55-
"references": [
56-
{
57-
"flag": "ReferenceFlag(Type)",
58-
"id": 0,
59-
"name": "T",
60-
"node_id": 31
61-
}
62-
]
55+
"references": []
6356
}
6457
]
6558
}
@@ -75,7 +68,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-e
7568
"node": "Function(makeBox)",
7669
"references": [
7770
{
78-
"flag": "ReferenceFlag(Read)",
71+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
7972
"id": 0,
8073
"name": "makeBox",
8174
"node_id": 27

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2424
"node": "VariableDeclarator",
2525
"references": [
2626
{
27-
"flag": "ReferenceFlag(Read)",
27+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
2828
"id": 0,
2929
"name": "arg",
3030
"node_id": 15

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/index-access3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
4545
"node": "VariableDeclarator",
4646
"references": [
4747
{
48-
"flag": "ReferenceFlag(Read)",
48+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
4949
"id": 0,
5050
"name": "k",
5151
"node_id": 21

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-qualified.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
3131
"node": "VariableDeclarator",
3232
"references": [
3333
{
34-
"flag": "ReferenceFlag(Read)",
34+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
3535
"id": 0,
3636
"name": "x",
3737
"node_id": 21

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-with-parameters.snap

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
5252
"id": 4,
5353
"name": "T",
5454
"node": "TSTypeParameter",
55-
"references": [
56-
{
57-
"flag": "ReferenceFlag(Type)",
58-
"id": 0,
59-
"name": "T",
60-
"node_id": 33
61-
}
62-
]
55+
"references": []
6356
}
6457
]
6558
}
@@ -75,7 +68,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
7568
"node": "Function(foo)",
7669
"references": [
7770
{
78-
"flag": "ReferenceFlag(Read)",
71+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
7972
"id": 0,
8073
"name": "foo",
8174
"node_id": 29

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
3131
"node": "VariableDeclarator",
3232
"references": [
3333
{
34-
"flag": "ReferenceFlag(Read)",
34+
"flag": "ReferenceFlag(Read | TSTypeQuery)",
3535
"id": 0,
3636
"name": "x",
3737
"node_id": 9

0 commit comments

Comments
 (0)