Skip to content

check if import collides with exported local name #7583

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 1 commit into from
Mar 18, 2016
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
12 changes: 9 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15063,10 +15063,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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that previously we would correctly catch unexported functions, right? Like:

import {f} from "f1"
function f() { }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes and we still do this

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 :
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/functionAndImportNameConflict.errors.txt
Original file line number Diff line number Diff line change
@@ -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() {
}
21 changes: 21 additions & 0 deletions tests/baselines/reference/functionAndImportNameConflict.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions tests/cases/compiler/functionAndImportNameConflict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @module: commonjs
// @filename: f1.ts
export function f() {
}

// @filename: f2.ts
import {f} from './f1';
export function f() {
}