Skip to content

Enable substitution for object literal shorthand property assignments in the system transform #21106

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
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
55 changes: 55 additions & 0 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ts {
context.onSubstituteNode = onSubstituteNode;
context.onEmitNode = onEmitNode;
context.enableSubstitution(SyntaxKind.Identifier); // Substitutes expression identifiers for imported symbols.
context.enableSubstitution(SyntaxKind.ShorthandPropertyAssignment); // Substitutes expression identifiers for imported symbols
context.enableSubstitution(SyntaxKind.BinaryExpression); // Substitutes assignments to exported symbols.
context.enableSubstitution(SyntaxKind.PrefixUnaryExpression); // Substitutes updates to exported symbols.
context.enableSubstitution(SyntaxKind.PostfixUnaryExpression); // Substitutes updates to exported symbols.
Expand Down Expand Up @@ -1625,10 +1626,64 @@ namespace ts {
if (hint === EmitHint.Expression) {
return substituteExpression(<Expression>node);
}
else if (hint === EmitHint.Unspecified) {
return substituteUnspecified(node);
}

return node;
}

/**
* Substitute the node, if necessary.
*
* @param node The node to substitute.
*/
function substituteUnspecified(node: Node) {
switch (node.kind) {
case SyntaxKind.ShorthandPropertyAssignment:
return substituteShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
}
return node;
}
/**
* Substitution for a ShorthandPropertyAssignment whose name that may contain an imported or exported symbol.
*
* @param node The node to substitute.
*/
function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment) {
const name = node.name;
if (!isGeneratedIdentifier(name) && !isLocalName(name)) {
const importDeclaration = resolver.getReferencedImportDeclaration(name);
if (importDeclaration) {
if (isImportClause(importDeclaration)) {
return setTextRange(
createPropertyAssignment(
getSynthesizedClone(name),
createPropertyAccess(
getGeneratedNameForNode(importDeclaration.parent),
createIdentifier("default")
)
),
/*location*/ node
);
}
else if (isImportSpecifier(importDeclaration)) {
return setTextRange(
createPropertyAssignment(
getSynthesizedClone(name),
createPropertyAccess(
getGeneratedNameForNode(importDeclaration.parent.parent.parent),
getSynthesizedClone(importDeclaration.propertyName || importDeclaration.name)
),
),
/*location*/ node
);
}
}
}
return node;
}

/**
* Substitute the expression, if necessary.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/systemObjectShorthandRename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/systemObjectShorthandRename.ts] ////

//// [x.ts]
export const x = 'X'
//// [index.ts]
import {x} from './x.js'

const x2 = {x}
const a = {x2}

const x3 = x
const b = {x3}

//// [x.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var x;
return {
setters: [],
execute: function () {
exports_1("x", x = 'X');
}
};
});
//// [index.js]
System.register(["./x.js"], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var x_js_1, x2, a, x3, b;
return {
setters: [
function (x_js_1_1) {
x_js_1 = x_js_1_1;
}
],
execute: function () {
x2 = { x: x_js_1.x };
a = { x2 };
x3 = x_js_1.x;
b = { x3 };
}
};
});
24 changes: 24 additions & 0 deletions tests/baselines/reference/systemObjectShorthandRename.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/x.ts ===
export const x = 'X'
>x : Symbol(x, Decl(x.ts, 0, 12))

=== tests/cases/compiler/index.ts ===
import {x} from './x.js'
>x : Symbol(x, Decl(index.ts, 0, 8))

const x2 = {x}
>x2 : Symbol(x2, Decl(index.ts, 2, 5))
>x : Symbol(x, Decl(index.ts, 2, 12))

const a = {x2}
>a : Symbol(a, Decl(index.ts, 3, 5))
>x2 : Symbol(x2, Decl(index.ts, 3, 11))

const x3 = x
>x3 : Symbol(x3, Decl(index.ts, 5, 5))
>x : Symbol(x, Decl(index.ts, 0, 8))

const b = {x3}
>b : Symbol(b, Decl(index.ts, 6, 5))
>x3 : Symbol(x3, Decl(index.ts, 6, 11))

28 changes: 28 additions & 0 deletions tests/baselines/reference/systemObjectShorthandRename.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/x.ts ===
export const x = 'X'
>x : "X"
>'X' : "X"

=== tests/cases/compiler/index.ts ===
import {x} from './x.js'
>x : "X"

const x2 = {x}
>x2 : { x: string; }
>{x} : { x: string; }
>x : string

const a = {x2}
>a : { x2: { x: string; }; }
>{x2} : { x2: { x: string; }; }
>x2 : { x: string; }

const x3 = x
>x3 : "X"
>x : "X"

const b = {x3}
>b : { x3: string; }
>{x3} : { x3: string; }
>x3 : string

12 changes: 12 additions & 0 deletions tests/cases/compiler/systemObjectShorthandRename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @module: system
// @target: es2015
// @filename: x.ts
export const x = 'X'
// @filename: index.ts
import {x} from './x.js'

const x2 = {x}
const a = {x2}

const x3 = x
const b = {x3}