Skip to content

Commit 8917ddf

Browse files
Merge pull request microsoft#9151 from Microsoft/commonjsShorthands
Fix emit for shorthand properties when they refer to CommonJS exports.
2 parents 13a0f59 + e8a7e0c commit 8917ddf

9 files changed

+120
-4
lines changed

src/compiler/emitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,9 +2150,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
21502150
}
21512151

21522152
// Return true if identifier resolves to an exported member of a namespace
2153-
function isNamespaceExportReference(node: Identifier) {
2153+
function isExportReference(node: Identifier) {
21542154
const container = resolver.getReferencedExportContainer(node);
2155-
return container && container.kind !== SyntaxKind.SourceFile;
2155+
return !!container;
21562156
}
21572157

21582158
// Return true if identifier resolves to an imported identifier
@@ -2185,10 +2185,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
21852185
// const foo_1 = require('./foo');
21862186
// exports.baz = { foo: foo_1.foo };
21872187
//
2188-
if (languageVersion < ScriptTarget.ES6 || (modulekind !== ModuleKind.ES6 && isImportedReference(node.name)) || isNamespaceExportReference(node.name) ) {
2188+
if (languageVersion < ScriptTarget.ES6 || (modulekind !== ModuleKind.ES6 && isImportedReference(node.name)) || isExportReference(node.name)) {
21892189
// Emit identifier as an identifier
21902190
write(": ");
2191-
emit(node.name);
2191+
emitExpressionIdentifier(node.name);
21922192
}
21932193

21942194
if (languageVersion >= ScriptTarget.ES6 && node.objectAssignmentInitializer) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [shorthandOfExportedEntity01_targetES2015_CommonJS.ts]
2+
3+
export const test = "test";
4+
5+
export function foo () {
6+
const x = { test };
7+
}
8+
9+
10+
//// [shorthandOfExportedEntity01_targetES2015_CommonJS.js]
11+
"use strict";
12+
exports.test = "test";
13+
function foo() {
14+
const x = { test: exports.test };
15+
}
16+
exports.foo = foo;
17+
18+
19+
//// [shorthandOfExportedEntity01_targetES2015_CommonJS.d.ts]
20+
export declare const test: string;
21+
export declare function foo(): void;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/shorthandOfExportedEntity01_targetES2015_CommonJS.ts ===
2+
3+
export const test = "test";
4+
>test : Symbol(test, Decl(shorthandOfExportedEntity01_targetES2015_CommonJS.ts, 1, 12))
5+
6+
export function foo () {
7+
>foo : Symbol(foo, Decl(shorthandOfExportedEntity01_targetES2015_CommonJS.ts, 1, 27))
8+
9+
const x = { test };
10+
>x : Symbol(x, Decl(shorthandOfExportedEntity01_targetES2015_CommonJS.ts, 4, 7))
11+
>test : Symbol(test, Decl(shorthandOfExportedEntity01_targetES2015_CommonJS.ts, 4, 13))
12+
}
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/shorthandOfExportedEntity01_targetES2015_CommonJS.ts ===
2+
3+
export const test = "test";
4+
>test : string
5+
>"test" : string
6+
7+
export function foo () {
8+
>foo : () => void
9+
10+
const x = { test };
11+
>x : { test: string; }
12+
>{ test } : { test: string; }
13+
>test : string
14+
}
15+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [shorthandOfExportedEntity02_targetES5_CommonJS.ts]
2+
3+
export const test = "test";
4+
5+
export function foo () {
6+
const x = { test };
7+
}
8+
9+
10+
//// [shorthandOfExportedEntity02_targetES5_CommonJS.js]
11+
"use strict";
12+
exports.test = "test";
13+
function foo() {
14+
var x = { test: exports.test };
15+
}
16+
exports.foo = foo;
17+
18+
19+
//// [shorthandOfExportedEntity02_targetES5_CommonJS.d.ts]
20+
export declare const test: string;
21+
export declare function foo(): void;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/shorthandOfExportedEntity02_targetES5_CommonJS.ts ===
2+
3+
export const test = "test";
4+
>test : Symbol(test, Decl(shorthandOfExportedEntity02_targetES5_CommonJS.ts, 1, 12))
5+
6+
export function foo () {
7+
>foo : Symbol(foo, Decl(shorthandOfExportedEntity02_targetES5_CommonJS.ts, 1, 27))
8+
9+
const x = { test };
10+
>x : Symbol(x, Decl(shorthandOfExportedEntity02_targetES5_CommonJS.ts, 4, 7))
11+
>test : Symbol(test, Decl(shorthandOfExportedEntity02_targetES5_CommonJS.ts, 4, 13))
12+
}
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/shorthandOfExportedEntity02_targetES5_CommonJS.ts ===
2+
3+
export const test = "test";
4+
>test : string
5+
>"test" : string
6+
7+
export function foo () {
8+
>foo : () => void
9+
10+
const x = { test };
11+
>x : { test: string; }
12+
>{ test } : { test: string; }
13+
>test : string
14+
}
15+
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+
// @declaration: true
4+
5+
export const test = "test";
6+
7+
export function foo () {
8+
const x = { test };
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: ES5
2+
// @module: commonjs
3+
// @declaration: true
4+
5+
export const test = "test";
6+
7+
export function foo () {
8+
const x = { test };
9+
}

0 commit comments

Comments
 (0)