Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 17d66a1

Browse files
authored
fix(rome_js_analyze): skip this reference identifier in the semantic analyzer (#4675)
1 parent def7584 commit 17d66a1

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ if no error diagnostics are emitted.
272272

273273
This rule's code action emits an invalid AST, so I fixed using JsxString instead of JsStringLiteral
274274

275+
- Fix [noUndeclaredVariables](https://docs.rome.tools/lint/rules/noundeclaredvariables/)'s false positive diagnostics ([#4675](https://github.com/rome/tools/issues/4675))
276+
277+
The semantic analyzer no longer handles `this` reference identifier in the semantic analyzer.
278+
275279
### Parser
276280

277281
- Add support for decorators in class method parameters, example:

crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,16 @@ export type WhateverDefault<S extends number = 2> = `Hello ${S}`
1111
// Const assertions are valid
1212
const fruits = ["banana"] as const;
1313

14+
class X {
15+
f() {
16+
this.g;
17+
type T1 = typeof this.g;
18+
type T2 = X['g'];
19+
}
20+
21+
g() {
22+
}
23+
}
24+
1425
// Invalid
15-
export type Invalid<S extends number> = `Hello ${T}`
26+
export type Invalid<S extends number> = `Hello ${T}`

crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@ export type WhateverDefault<S extends number = 2> = `Hello ${S}`
1717
// Const assertions are valid
1818
const fruits = ["banana"] as const;
1919
20+
class X {
21+
f() {
22+
this.g;
23+
type T1 = typeof this.g;
24+
type T2 = X['g'];
25+
}
26+
27+
g() {
28+
}
29+
}
30+
2031
// Invalid
2132
export type Invalid<S extends number> = `Hello ${T}`
33+
2234
```
2335
2436
# Diagnostics
2537
```
26-
noUndeclaredVariables.ts:15:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
38+
noUndeclaredVariables.ts:26:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2739
2840
! The T variable is undeclared
2941
30-
14 │ // Invalid
31-
> 15 │ export type Invalid<S extends number> = `Hello ${T}`
42+
25 │ // Invalid
43+
> 26 │ export type Invalid<S extends number> = `Hello ${T}`
3244
│ ^
45+
27 │
3346
3447
3548
```

crates/rome_js_semantic/src/events.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ impl SemanticEventExtractor {
479479
// SAFETY: kind check above
480480
let reference = JsReferenceIdentifier::unwrap_cast(node.clone());
481481
let name_token = reference.value_token().ok()?;
482+
// skip `this` reference representing the class instance
483+
if name_token.token_text_trimmed() == "this" {
484+
return None;
485+
}
482486
(
483487
name_token.token_text_trimmed(),
484488
self.is_js_reference_identifier_exported(node),

0 commit comments

Comments
 (0)