Skip to content

Commit b5f81f9

Browse files
committed
Fix emit when type import merges with local value
1 parent c179d9a commit b5f81f9

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

Jakefile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ function runConsoleTests(defaultReporter, runInParallel) {
925925
}
926926
}
927927

928-
var testTimeout = 20000;
928+
var testTimeout = 22000;
929929
desc("Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true.");
930930
task("runtests-parallel", ["build-rules", "tests", builtLocalDirectory], function () {
931931
runConsoleTests('min', /*runInParallel*/ true);

src/compiler/checker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,8 +1473,15 @@ namespace ts {
14731473
}
14741474
}
14751475

1476+
/**
1477+
* Indicates that a symbol is an alias that does not merge with a local declaration.
1478+
*/
1479+
function isNonLocalAlias(symbol: Symbol, excludes = SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace) {
1480+
return symbol && (symbol.flags & (SymbolFlags.Alias | excludes)) === SymbolFlags.Alias;
1481+
}
1482+
14761483
function resolveSymbol(symbol: Symbol, dontResolveAlias?: boolean): Symbol {
1477-
const shouldResolve = !dontResolveAlias && symbol && symbol.flags & SymbolFlags.Alias && !(symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace));
1484+
const shouldResolve = !dontResolveAlias && isNonLocalAlias(symbol);
14781485
return shouldResolve ? resolveAlias(symbol) : symbol;
14791486
}
14801487

@@ -12015,7 +12022,9 @@ namespace ts {
1201512022
return getTypeOfSymbol(symbol);
1201612023
}
1201712024

12018-
if (symbol.flags & SymbolFlags.Alias && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
12025+
// We should only mark aliases as referenced if there isn't a local value declaration
12026+
// for the symbol.
12027+
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
1201912028
markAliasSymbolAsReferenced(symbol);
1202012029
}
1202112030

@@ -22864,7 +22873,9 @@ namespace ts {
2286422873
node = getParseTreeNode(node, isIdentifier);
2286522874
if (node) {
2286622875
const symbol = getReferencedValueSymbol(node);
22867-
if (symbol && symbol.flags & SymbolFlags.Alias) {
22876+
// We should only get the declaration of an alias if there isn't a local value
22877+
// declaration for the symbol
22878+
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value)) {
2286822879
return getDeclarationOfAliasSymbol(symbol);
2286922880
}
2287022881
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/symbolMergeValueAndImportedType.ts] ////
2+
3+
//// [main.ts]
4+
import { X } from "./other";
5+
const X = 42;
6+
console.log('X is ' + X);
7+
//// [other.ts]
8+
export type X = {};
9+
10+
//// [other.js]
11+
"use strict";
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
//// [main.js]
14+
"use strict";
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
const X = 42;
17+
console.log('X is ' + X);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/main.ts ===
2+
import { X } from "./other";
3+
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))
4+
5+
const X = 42;
6+
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))
7+
8+
console.log('X is ' + X);
9+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
10+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
11+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
12+
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))
13+
14+
=== tests/cases/compiler/other.ts ===
15+
export type X = {};
16+
>X : Symbol(X, Decl(other.ts, 0, 0))
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/main.ts ===
2+
import { X } from "./other";
3+
>X : 42
4+
5+
const X = 42;
6+
>X : 42
7+
>42 : 42
8+
9+
console.log('X is ' + X);
10+
>console.log('X is ' + X) : void
11+
>console.log : (message?: any, ...optionalParams: any[]) => void
12+
>console : Console
13+
>log : (message?: any, ...optionalParams: any[]) => void
14+
>'X is ' + X : string
15+
>'X is ' : "X is "
16+
>X : 42
17+
18+
=== tests/cases/compiler/other.ts ===
19+
export type X = {};
20+
>X : X
21+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: es2015
2+
// @module: commonjs
3+
// @lib: es2015,dom
4+
// @filename: main.ts
5+
import { X } from "./other";
6+
const X = 42;
7+
console.log('X is ' + X);
8+
// @filename: other.ts
9+
export type X = {};

0 commit comments

Comments
 (0)