Skip to content

Commit

Permalink
Prevent looking up symbol for as const from triggering an error (#48464)
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Mar 28, 2022
1 parent df7ed82 commit a5dae37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors1.ts
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

////class Tex {
//// type = 'Text' as /**/const;
////}

verify.goToDefinition("", []);
verify.noErrors();
8 changes: 8 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors2.ts
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

////class Tex {
//// type = </**/const>'Text';
////}

verify.goToDefinition("", []);
verify.noErrors();
10 changes: 10 additions & 0 deletions tests/cases/fourslash/asConstRefsNoErrors3.ts
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

// @checkJs: true
// @Filename: file.js
////class Tex {
//// type = (/** @type {/**/const} */'Text');
////}

verify.goToDefinition("", []);
verify.noErrors();

0 comments on commit a5dae37

Please sign in to comment.