From a31c8d5861fc677b462eb84e7670722460abed82 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 6 Oct 2016 21:28:50 -0700 Subject: [PATCH] exported name should be defined on the source file level --- src/compiler/emitter.ts | 7 ++- ...atMatchExportedNameViaExportDeclaration.js | 40 +++++++++++++++ ...chExportedNameViaExportDeclaration.symbols | 43 ++++++++++++++++ ...atchExportedNameViaExportDeclaration.types | 51 +++++++++++++++++++ ...atMatchExportedNameViaExportDeclaration.ts | 20 ++++++++ 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.js create mode 100644 tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.symbols create mode 100644 tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.types create mode 100644 tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 2c3f5e0b4c8..dadd4616d23 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2651,7 +2651,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge return false; } - return !exportEquals && exportSpecifiers && (node).text in exportSpecifiers; + if (exportEquals || !exportSpecifiers || !((node).text in exportSpecifiers)) { + return false; + } + // check that referenced declaration is declared on source file level + const declaration = resolver.getReferencedValueDeclaration(node); + return declaration && getEnclosingBlockScopeContainer(declaration).kind === SyntaxKind.SourceFile; } function emitPrefixUnaryExpression(node: PrefixUnaryExpression) { diff --git a/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.js b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.js new file mode 100644 index 00000000000..1ade920cdd4 --- /dev/null +++ b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.js @@ -0,0 +1,40 @@ +//// [localNameThatMatchExportedNameViaExportDeclaration.ts] + +export { my } + +var my: any; + +my += my; + +function doSome1(my: any) { + my = +my; + return my; +} + +function doSome2() { + const internal = (my: any) => { + my = +my; + return my; + }; + return internal("1"); +} + + +//// [localNameThatMatchExportedNameViaExportDeclaration.js] +define(["require", "exports"], function (require, exports) { + "use strict"; + var my; + exports.my = my; + exports.my = my += my; + function doSome1(my) { + my = +my; + return my; + } + function doSome2() { + var internal = function (my) { + my = +my; + return my; + }; + return internal("1"); + } +}); diff --git a/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.symbols b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.symbols new file mode 100644 index 00000000000..0cb8f828eda --- /dev/null +++ b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.symbols @@ -0,0 +1,43 @@ +=== tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts === + +export { my } +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 1, 8)) + +var my: any; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 3, 3)) + +my += my; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 3, 3)) +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 3, 3)) + +function doSome1(my: any) { +>doSome1 : Symbol(doSome1, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 5, 9)) +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 7, 17)) + + my = +my; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 7, 17)) +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 7, 17)) + + return my; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 7, 17)) +} + +function doSome2() { +>doSome2 : Symbol(doSome2, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 10, 1)) + + const internal = (my: any) => { +>internal : Symbol(internal, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 9)) +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 22)) + + my = +my; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 22)) +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 22)) + + return my; +>my : Symbol(my, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 22)) + + }; + return internal("1"); +>internal : Symbol(internal, Decl(localNameThatMatchExportedNameViaExportDeclaration.ts, 13, 9)) +} + diff --git a/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.types b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.types new file mode 100644 index 00000000000..af6ab86afbb --- /dev/null +++ b/tests/baselines/reference/localNameThatMatchExportedNameViaExportDeclaration.types @@ -0,0 +1,51 @@ +=== tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts === + +export { my } +>my : any + +var my: any; +>my : any + +my += my; +>my += my : any +>my : any +>my : any + +function doSome1(my: any) { +>doSome1 : (my: any) => any +>my : any + + my = +my; +>my = +my : number +>my : any +>+my : number +>my : any + + return my; +>my : any +} + +function doSome2() { +>doSome2 : () => any + + const internal = (my: any) => { +>internal : (my: any) => any +>(my: any) => { my = +my; return my; } : (my: any) => any +>my : any + + my = +my; +>my = +my : number +>my : any +>+my : number +>my : any + + return my; +>my : any + + }; + return internal("1"); +>internal("1") : any +>internal : (my: any) => any +>"1" : string +} + diff --git a/tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts b/tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts new file mode 100644 index 00000000000..491509605b4 --- /dev/null +++ b/tests/cases/compiler/localNameThatMatchExportedNameViaExportDeclaration.ts @@ -0,0 +1,20 @@ +// @module: amd + +export { my } + +var my: any; + +my += my; + +function doSome1(my: any) { + my = +my; + return my; +} + +function doSome2() { + const internal = (my: any) => { + my = +my; + return my; + }; + return internal("1"); +}