Skip to content

Commit

Permalink
Fix elided var handling in declaration emit visibility checks (#58605)
Browse files Browse the repository at this point in the history
Co-authored-by: TypeScript Bot <typescriptbot@microsoft.com>
  • Loading branch information
weswigham and typescript-bot authored May 21, 2024
1 parent 11b73ec commit 6f72e24
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5862,9 +5862,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return { accessibility: SymbolAccessibility.Accessible };
}

if (!symbol) {
return {
accessibility: SymbolAccessibility.NotResolved,
errorSymbolName: getTextOfNode(firstIdentifier),
errorNode: firstIdentifier,
};
}
// Verify if the symbol is accessible
return (symbol && hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible)) || {
accessibility: SymbolAccessibility.NotResolved,
return hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible) || {
accessibility: SymbolAccessibility.NotAccessible,
errorSymbolName: getTextOfNode(firstIdentifier),
errorNode: firstIdentifier,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
declarationEmitInvalidExport.ts(4,30): error TS4081: Exported type alias 'MyClass' has or is using private name 'myClass'.
declarationEmitInvalidExport.ts(5,1): error TS1128: Declaration or statement expected.


==== declarationEmitInvalidExport.ts (1 errors) ====
==== declarationEmitInvalidExport.ts (2 errors) ====
if (false) {
export var myClass = 0;
}
export type MyClass = typeof myClass;
~~~~~~~
!!! error TS4081: Exported type alias 'MyClass' has or is using private name 'myClass'.
}
~
!!! error TS1128: Declaration or statement expected.
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/declarationEmitInvalidExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ Object.defineProperty(exports, "__esModule", { value: true });
if (false) {
exports.myClass = 0;
}


//// [declarationEmitInvalidExport.d.ts]
export type MyClass = typeof myClass;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declarationEmitVarInElidedBlock.ts(4,22): error TS4025: Exported variable 'b' has or is using private name 'a'.


==== declarationEmitVarInElidedBlock.ts (1 errors) ====
{
var a = "";
}
export let b: typeof a;
~
!!! error TS4025: Exported variable 'b' has or is using private name 'a'.
11 changes: 11 additions & 0 deletions tests/baselines/reference/declarationEmitVarInElidedBlock.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/declarationEmitVarInElidedBlock.ts] ////

=== declarationEmitVarInElidedBlock.ts ===
{
var a = "";
>a : Symbol(a, Decl(declarationEmitVarInElidedBlock.ts, 1, 7))
}
export let b: typeof a;
>b : Symbol(b, Decl(declarationEmitVarInElidedBlock.ts, 3, 10))
>a : Symbol(a, Decl(declarationEmitVarInElidedBlock.ts, 1, 7))

16 changes: 16 additions & 0 deletions tests/baselines/reference/declarationEmitVarInElidedBlock.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/compiler/declarationEmitVarInElidedBlock.ts] ////

=== declarationEmitVarInElidedBlock.ts ===
{
var a = "";
>a : string
> : ^^^^^^
>"" : ""
> : ^^
}
export let b: typeof a;
>b : string
> : ^^^^^^
>a : string
> : ^^^^^^

6 changes: 6 additions & 0 deletions tests/cases/compiler/declarationEmitVarInElidedBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @declaration: true
// @emitDeclarationOnly: true
{
var a = "";
}
export let b: typeof a;

0 comments on commit 6f72e24

Please sign in to comment.