From 17d66a1d4cf13c745dc19416d24138a01749cef4 Mon Sep 17 00:00:00 2001 From: Daiki Nishikawa Date: Mon, 10 Jul 2023 21:16:57 +0900 Subject: [PATCH] fix(rome_js_analyze): skip `this` reference identifier in the semantic analyzer (#4675) --- CHANGELOG.md | 4 ++++ .../noUndeclaredVariables.ts | 13 ++++++++++++- .../noUndeclaredVariables.ts.snap | 19 ++++++++++++++++--- crates/rome_js_semantic/src/events.rs | 4 ++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eb46e4efb5..0485a7666d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -272,6 +272,10 @@ if no error diagnostics are emitted. This rule's code action emits an invalid AST, so I fixed using JsxString instead of JsStringLiteral +- Fix [noUndeclaredVariables](https://docs.rome.tools/lint/rules/noundeclaredvariables/)'s false positive diagnostics ([#4675](https://github.com/rome/tools/issues/4675)) + + The semantic analyzer no longer handles `this` reference identifier in the semantic analyzer. + ### Parser - Add support for decorators in class method parameters, example: diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts b/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts index 353e59722e2..97ab9fda490 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts +++ b/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts @@ -11,5 +11,16 @@ export type WhateverDefault = `Hello ${S}` // Const assertions are valid const fruits = ["banana"] as const; +class X { + f() { + this.g; + type T1 = typeof this.g; + type T2 = X['g']; + } + + g() { + } +} + // Invalid -export type Invalid = `Hello ${T}` \ No newline at end of file +export type Invalid = `Hello ${T}` diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap b/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap index 832a398abe2..b0621477a65 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap +++ b/crates/rome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap @@ -17,19 +17,32 @@ export type WhateverDefault = `Hello ${S}` // Const assertions are valid const fruits = ["banana"] as const; +class X { + f() { + this.g; + type T1 = typeof this.g; + type T2 = X['g']; + } + + g() { + } +} + // Invalid export type Invalid = `Hello ${T}` + ``` # Diagnostics ``` -noUndeclaredVariables.ts:15:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noUndeclaredVariables.ts:26:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! The T variable is undeclared - 14 │ // Invalid - > 15 │ export type Invalid = `Hello ${T}` + 25 │ // Invalid + > 26 │ export type Invalid = `Hello ${T}` │ ^ + 27 │ ``` diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index b3700a4f197..b410cdefe2c 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -479,6 +479,10 @@ impl SemanticEventExtractor { // SAFETY: kind check above let reference = JsReferenceIdentifier::unwrap_cast(node.clone()); let name_token = reference.value_token().ok()?; + // skip `this` reference representing the class instance + if name_token.token_text_trimmed() == "this" { + return None; + } ( name_token.token_text_trimmed(), self.is_js_reference_identifier_exported(node),