Skip to content
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

Inline const enum in import equals declaration #46664

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 7 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41611,22 +41611,19 @@ namespace ts {
return getNodeLinks(node).enumMemberValue;
}

function canHaveConstantValue(node: Node): node is EnumMember | AccessExpression {
switch (node.kind) {
case SyntaxKind.EnumMember:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
return true;
}
return false;
function canHaveConstantValue(node: Node): node is EnumMember | EntityName | AccessExpression {
return isEnumMember(node) || isEntityName(node) || isAccessExpression(node);
}

function getConstantValue(node: EnumMember | AccessExpression): string | number | undefined {
function getConstantValue(node: EnumMember | EntityName | AccessExpression): string | number | undefined {
if (node.kind === SyntaxKind.EnumMember) {
return getEnumMemberValue(node);
}

const symbol = getNodeLinks(node).resolvedSymbol;
// Cached name resolution result of import equals declaration/access expression
const symbol = isEntityName(node)
? isDeclaration(node.parent) && resolveAlias(getSymbolOfNode(node.parent))
: getNodeLinks(node).resolvedSymbol;
if (symbol && (symbol.flags & SymbolFlags.EnumMember)) {
// inline property\index accesses only for const enums
const member = symbol.valueDeclaration as EnumMember;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace ts {
const left = createExpressionFromEntityName(factory, node.left);
// TODO(rbuckton): Does this need to be parented?
const right = setParent(setTextRange(factory.cloneNode(node.right), node.right), node.right.parent);
return setTextRange(factory.createPropertyAccessExpression(left, right), node);
return setOriginalNode(setTextRange(factory.createPropertyAccessExpression(left, right), node), node);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

transformers/ts.ts replaces import equals entity names with synthetic variable declarations + property access:

const moduleReference = createExpressionFromEntityName(factory, node.moduleReference as EntityName);

I experimented with resolving the synthetic property access, but settled on saving and resolving the original entity name instead?

}
else {
// TODO(rbuckton): Does this need to be parented?
Expand Down
7 changes: 3 additions & 4 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3342,10 +3342,9 @@ namespace ts {

const substitute = typeof constantValue === "string" ? factory.createStringLiteral(constantValue) : factory.createNumericLiteral(constantValue);
if (!compilerOptions.removeComments) {
const originalNode = getOriginalNode(node, isAccessExpression);
const propertyName = isPropertyAccessExpression(originalNode)
? declarationNameToString(originalNode.name)
: getTextOfNode(originalNode.argumentExpression);
const propertyName = getPropertyNameForPropertyNameNode(
isPropertyAccessExpression(node) ? node.name : (node.argumentExpression as PropertyName)
);

addSyntheticTrailingComment(substitute, SyntaxKind.MultiLineCommentTrivia, ` ${propertyName} `);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/constEnumPropertyAccess1.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var o = {
1: true
};
var a = 1 /* A */;
var a1 = 1 /* "A" */;
var a1 = 1 /* A */;
Copy link

Choose a reason for hiding this comment

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

why changed?

I think it caused by following to:

var g = o[1 /* A */];
class C {
[1 /* A */]() { }
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/constEnumSyntheticNodesComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function verify(a) {
switch (a) {
case 0 /* A */:
return assert(a);
case 1 /* "B" */:
case 1 /* B */:
return assert(a);
case 2 /* `C` */:
case 2 /* C */:
return assert(a);
case 3 /* "\u{44}" */:
case 3 /* D */:
return assert(a);
}
}
12 changes: 6 additions & 6 deletions tests/baselines/reference/constEnumToStringWithComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ let c1 = Foo["C"].toString();

//// [constEnumToStringWithComments.js]
var x0 = 100 /* X */.toString();
var x1 = 100 /* "X" */.toString();
var x1 = 100 /* X */.toString();
var y0 = 0.5 /* Y */.toString();
var y1 = 0.5 /* "Y" */.toString();
var y1 = 0.5 /* Y */.toString();
var z0 = 2 /* Z */.toString();
var z1 = 2 /* "Z" */.toString();
var z1 = 2 /* Z */.toString();
var a0 = -1 /* A */.toString();
var a1 = -1 /* "A" */.toString();
var a1 = -1 /* A */.toString();
var b0 = -1.5 /* B */.toString();
var b1 = -1.5 /* "B" */.toString();
var b1 = -1.5 /* B */.toString();
var c0 = -1 /* C */.toString();
var c1 = -1 /* "C" */.toString();
var c1 = -1 /* C */.toString();
4 changes: 2 additions & 2 deletions tests/baselines/reference/constEnums.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ function foo(x) {
case -1 /* Q */:
case 0 /* R */:
case 0 /* S */:
case 11 /* "T" */:
case 11 /* `U` */:
case 11 /* T */:
case 11 /* U */:
case 11 /* V */:
case 11 /* W */:
case 100 /* W1 */:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var s = test[0];
var n = test[1];
var s1 = test[0 /* zero */];
var n1 = test[1 /* one */];
var s2 = test[0 /* "zero" */];
var n2 = test[1 /* "one" */];
var s2 = test[0 /* zero */];
var n2 = test[1 /* one */];
var numbersNotConst;
(function (numbersNotConst) {
numbersNotConst[numbersNotConst["zero"] = 0] = "zero";
Expand Down