From a5dae37943a35cefb6f3cea5000debe3782bc020 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 28 Mar 2022 12:53:36 -0700 Subject: [PATCH] Prevent looking up symbol for as const from triggering an error (#48464) --- src/compiler/checker.ts | 10 ++++++++++ tests/cases/fourslash/asConstRefsNoErrors1.ts | 8 ++++++++ tests/cases/fourslash/asConstRefsNoErrors2.ts | 8 ++++++++ tests/cases/fourslash/asConstRefsNoErrors3.ts | 10 ++++++++++ 4 files changed, 36 insertions(+) create mode 100644 tests/cases/fourslash/asConstRefsNoErrors1.ts create mode 100644 tests/cases/fourslash/asConstRefsNoErrors2.ts create mode 100644 tests/cases/fourslash/asConstRefsNoErrors3.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1cd7e00d0dd6c..bbf3dbe7c177e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1790,6 +1790,11 @@ namespace ts { } } + function isConstAssertion(location: Node) { + return (isAssertionExpression(location) && isConstTypeReference(location.type)) + || (isJSDocTypeTag(location) && isConstTypeReference(location.typeExpression)); + } + /** * Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and * the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with @@ -1831,6 +1836,11 @@ namespace ts { let isInExternalModule = false; loop: while (location) { + if (name === "const" && isConstAssertion(location)) { + // `const` in an `as const` has no symbol, but issues no error because there is no *actual* lookup of the type + // (it refers to the constant type of the expression instead) + return undefined; + } // Locals of a source file are not in scope (because they get merged into the global symbol table) if (location.locals && !isGlobalSourceFile(location)) { if (result = lookup(location.locals, name, meaning)) { diff --git a/tests/cases/fourslash/asConstRefsNoErrors1.ts b/tests/cases/fourslash/asConstRefsNoErrors1.ts new file mode 100644 index 0000000000000..6bb63914c8c4c --- /dev/null +++ b/tests/cases/fourslash/asConstRefsNoErrors1.ts @@ -0,0 +1,8 @@ +/// + +////class Tex { +//// type = 'Text' as /**/const; +////} + +verify.goToDefinition("", []); +verify.noErrors(); \ No newline at end of file diff --git a/tests/cases/fourslash/asConstRefsNoErrors2.ts b/tests/cases/fourslash/asConstRefsNoErrors2.ts new file mode 100644 index 0000000000000..74ea6a4e8929c --- /dev/null +++ b/tests/cases/fourslash/asConstRefsNoErrors2.ts @@ -0,0 +1,8 @@ +/// + +////class Tex { +//// type = 'Text'; +////} + +verify.goToDefinition("", []); +verify.noErrors(); \ No newline at end of file diff --git a/tests/cases/fourslash/asConstRefsNoErrors3.ts b/tests/cases/fourslash/asConstRefsNoErrors3.ts new file mode 100644 index 0000000000000..be450befafd38 --- /dev/null +++ b/tests/cases/fourslash/asConstRefsNoErrors3.ts @@ -0,0 +1,10 @@ +/// + +// @checkJs: true +// @Filename: file.js +////class Tex { +//// type = (/** @type {/**/const} */'Text'); +////} + +verify.goToDefinition("", []); +verify.noErrors(); \ No newline at end of file