Merge pull request #7583 from Microsoft/colliding-local-import

check if import collides with exported local name
This commit is contained in:
Vladimir Matveev 2016-03-18 12:21:51 -07:00
commit 32178acdfe
4 changed files with 52 additions and 3 deletions

View file

@ -15077,10 +15077,16 @@ namespace ts {
const symbol = getSymbolOfNode(node);
const target = resolveAlias(symbol);
if (target !== unknownSymbol) {
// For external modules symbol represent local symbol for an alias.
// This local symbol will merge any other local declarations (excluding other aliases)
// and symbol.flags will contains combined representation for all merged declaration.
// Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have,
// otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export*
// in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names).
const excludedMeanings =
(symbol.flags & SymbolFlags.Value ? SymbolFlags.Value : 0) |
(symbol.flags & SymbolFlags.Type ? SymbolFlags.Type : 0) |
(symbol.flags & SymbolFlags.Namespace ? SymbolFlags.Namespace : 0);
(symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue) ? SymbolFlags.Value : 0) |
(symbol.flags & (SymbolFlags.Type | SymbolFlags.ExportType) ? SymbolFlags.Type : 0) |
(symbol.flags & (SymbolFlags.Namespace | SymbolFlags.ExportNamespace) ? SymbolFlags.Namespace : 0);
if (target.flags & excludedMeanings) {
const message = node.kind === SyntaxKind.ExportSpecifier ?
Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 :

View file

@ -0,0 +1,13 @@
tests/cases/compiler/f2.ts(1,9): error TS2440: Import declaration conflicts with local declaration of 'f'
==== tests/cases/compiler/f1.ts (0 errors) ====
export function f() {
}
==== tests/cases/compiler/f2.ts (1 errors) ====
import {f} from './f1';
~
!!! error TS2440: Import declaration conflicts with local declaration of 'f'
export function f() {
}

View file

@ -0,0 +1,21 @@
//// [tests/cases/compiler/functionAndImportNameConflict.ts] ////
//// [f1.ts]
export function f() {
}
//// [f2.ts]
import {f} from './f1';
export function f() {
}
//// [f1.js]
"use strict";
function f() {
}
exports.f = f;
//// [f2.js]
"use strict";
function f() {
}
exports.f = f;

View file

@ -0,0 +1,9 @@
// @module: commonjs
// @filename: f1.ts
export function f() {
}
// @filename: f2.ts
import {f} from './f1';
export function f() {
}