[go: nahoru, domu]

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

Reduce element decorator temp variables #16280

Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: sort decorator info once
  • Loading branch information
JLHwung committed Feb 14, 2024
commit a91381e21fe2a50df65b555bb6d33847f2f1b848
Original file line number Diff line number Diff line change
Expand Up @@ -499,34 +499,26 @@ interface DecoratorInfo {
locals: t.Identifier | t.Identifier[] | undefined;
}

// Information about a computed property key. These must be evaluated
// interspersed with decorator expressions, which is why they get added to the
// array of DecoratorInfos later on.
interface ComputedPropInfo {
localComputedNameId: t.Identifier;
keyNode: t.Expression;
}

function isDecoratorInfo(
info: DecoratorInfo | ComputedPropInfo,
): info is DecoratorInfo {
return "decoratorsArray" in info;
}

function filteredOrderedDecoratorInfo(
info: (DecoratorInfo | ComputedPropInfo)[],
): DecoratorInfo[] {
const filtered = info.filter(isDecoratorInfo);

/**
* Sort decoration info in the application order:
* - static non-fields
* - instance non-fields
* - static fields
* - instance fields
*
* @param {DecoratorInfo[]} info
* @returns {DecoratorInfo[]} Sorted decoration info
*/
function toSortedDecoratorInfo(info: DecoratorInfo[]): DecoratorInfo[] {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this PR is not depending on tc39/proposal-decorators#524, since any normative change of decorator application ordering should be implemented within toSortedDecoratorInfo.

return [
...filtered.filter(
...info.filter(
el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER,
),
...filtered.filter(
...info.filter(
el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER,
),
...filtered.filter(el => el.isStatic && el.kind === FIELD),
...filtered.filter(el => !el.isStatic && el.kind === FIELD),
...info.filter(el => el.isStatic && el.kind === FIELD),
...info.filter(el => !el.isStatic && el.kind === FIELD),
];
}

Expand Down Expand Up @@ -555,11 +547,11 @@ function generateDecorationList(
}

function generateDecorationExprs(
info: (DecoratorInfo | ComputedPropInfo)[],
decorationInfo: DecoratorInfo[],
version: DecoratorVersionKind,
): t.ArrayExpression {
return t.arrayExpression(
filteredOrderedDecoratorInfo(info).map(el => {
decorationInfo.map(el => {
let flag = el.kind;
if (el.isStatic) {
flag +=
Expand All @@ -580,12 +572,10 @@ function generateDecorationExprs(
);
}

function extractElementLocalAssignments(
decorationInfo: (DecoratorInfo | ComputedPropInfo)[],
) {
function extractElementLocalAssignments(decorationInfo: DecoratorInfo[]) {
const localIds: t.Identifier[] = [];

for (const el of filteredOrderedDecoratorInfo(decorationInfo)) {
for (const el of decorationInfo) {
const { locals } = el;

if (Array.isArray(locals)) {
Expand Down Expand Up @@ -899,7 +889,7 @@ function transformClass(
return;
}

const elementDecoratorInfo: (DecoratorInfo | ComputedPropInfo)[] = [];
const elementDecoratorInfo: DecoratorInfo[] = [];

let constructorPath: NodePath<t.ClassMethod> | undefined;
const decoratedPrivateMethods = new Set<string>();
Expand Down Expand Up @@ -1283,13 +1273,17 @@ function transformClass(
staticFieldInitializerExpressions = [];
}

const sortedElementDecoratorInfo =
toSortedDecoratorInfo(elementDecoratorInfo);

const elementDecorations = generateDecorationExprs(
elementDecoratorInfo,
sortedElementDecoratorInfo,
version,
);

const elementLocals: t.Identifier[] =
extractElementLocalAssignments(elementDecoratorInfo);
const elementLocals: t.Identifier[] = extractElementLocalAssignments(
sortedElementDecoratorInfo,
);

if (protoInitLocal) {
elementLocals.push(protoInitLocal);
Expand Down