Skip to content

Fix emit when type import merges with local value #16282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ function runConsoleTests(defaultReporter, runInParallel) {
}
}

var defaultTestTimeout = 20000;
var defaultTestTimeout = 22000;
desc("Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true.");
task("runtests-parallel", ["build-rules", "tests", builtLocalDirectory], function () {
runConsoleTests('min', /*runInParallel*/ true);
Expand Down
17 changes: 14 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,15 @@ namespace ts {
}
}

/**
* Indicates that a symbol is an alias that does not merge with a local declaration.
*/
function isNonLocalAlias(symbol: Symbol, excludes = SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace) {
return symbol && (symbol.flags & (SymbolFlags.Alias | excludes)) === SymbolFlags.Alias;
}

function resolveSymbol(symbol: Symbol, dontResolveAlias?: boolean): Symbol {
const shouldResolve = !dontResolveAlias && symbol && symbol.flags & SymbolFlags.Alias && !(symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace));
const shouldResolve = !dontResolveAlias && isNonLocalAlias(symbol);
return shouldResolve ? resolveAlias(symbol) : symbol;
}

Expand Down Expand Up @@ -12020,7 +12027,9 @@ namespace ts {
return getTypeOfSymbol(symbol);
}

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

Expand Down Expand Up @@ -22926,7 +22935,9 @@ namespace ts {
node = getParseTreeNode(node, isIdentifier);
if (node) {
const symbol = getReferencedValueSymbol(node);
if (symbol && symbol.flags & SymbolFlags.Alias) {
// We should only get the declaration of an alias if there isn't a local value
// declaration for the symbol
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value)) {
return getDeclarationOfAliasSymbol(symbol);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/symbolMergeValueAndImportedType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/symbolMergeValueAndImportedType.ts] ////

//// [main.ts]
import { X } from "./other";
const X = 42;
console.log('X is ' + X);
//// [other.ts]
export type X = {};

//// [other.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//// [main.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const X = 42;
console.log('X is ' + X);
17 changes: 17 additions & 0 deletions tests/baselines/reference/symbolMergeValueAndImportedType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/main.ts ===
import { X } from "./other";
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))

const X = 42;
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))

console.log('X is ' + X);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>X : Symbol(X, Decl(main.ts, 0, 8), Decl(main.ts, 1, 5))

=== tests/cases/compiler/other.ts ===
export type X = {};
>X : Symbol(X, Decl(other.ts, 0, 0))

21 changes: 21 additions & 0 deletions tests/baselines/reference/symbolMergeValueAndImportedType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/main.ts ===
import { X } from "./other";
>X : 42

const X = 42;
>X : 42
>42 : 42

console.log('X is ' + X);
>console.log('X is ' + X) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>'X is ' + X : string
>'X is ' : "X is "
>X : 42

=== tests/cases/compiler/other.ts ===
export type X = {};
>X : X

9 changes: 9 additions & 0 deletions tests/cases/compiler/symbolMergeValueAndImportedType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: es2015
// @module: commonjs
// @lib: es2015,dom
// @filename: main.ts
import { X } from "./other";
const X = 42;
console.log('X is ' + X);
// @filename: other.ts
export type X = {};