Skip to content

Reverse order of decorator-injected initializers #54269

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 1 commit into from
May 17, 2023
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
4 changes: 2 additions & 2 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,10 @@ export const esDecorateHelper: UnscopedEmitHelper = {
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.push(_);
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.push(_);
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
Expand Down
50 changes: 40 additions & 10 deletions src/testRunner/unittests/evaluation/esDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1729,12 +1729,11 @@ describe("unittests:: evaluation:: esDecorators", () => {
`;
assert.strictEqual(C.x, 2);
});
it("multiple initializer pipe-throughs applied in reverse order", () => {
it("multiple initializer pipe-throughs applied in order", () => {
const { C } = exec`
function initializer(x) { return x + 1; }
export class C {
@((t, c) => x => [...x, 3])
@((t, c) => x => [...x, 2])
@((t, c) => x => [...x, 3])
static x: number[] = [1];
}
`;
Expand Down Expand Up @@ -1797,12 +1796,11 @@ describe("unittests:: evaluation:: esDecorators", () => {
`;
assert.strictEqual(C.x, 2);
});
it("multiple initializer pipe-throughs applied in reverse order", () => {
it("multiple init pipe-throughs applied in order", () => {
const { C } = exec`
function initializer(x) { return x + 1; }
export class C {
@((t, c) => ({ init: x => [...x, 3] }))
@((t, c) => ({ init: x => [...x, 2] }))
@((t, c) => ({ init: x => [...x, 3] }))
static accessor x: number[] = [1];
}
`;
Expand Down Expand Up @@ -1861,6 +1859,38 @@ describe("unittests:: evaluation:: esDecorators", () => {
assert.throws(() => main("abc"));
});
});
it("accessor 'init' evaluation order (#54267)", () => {
const { main } = exec`
function minusTwo({ set }: any, ctx: any) {
return {
set(v) { set.call(this, v - 2); },
init(v) { return v - 2; },
};
}

function timesFour({ set }: any, ctx: any) {
return {
set(v) { set.call(this, v * 4); },
init(v) { return v * 4; }
};
}

class C {
@minusTwo @timesFour accessor x = 5;
}

export const main = () => {
const obj = new C();
const afterInit = obj.x;
obj.x = 5;
const afterSet = obj.x;
return { afterInit, afterSet };
};
`;
const { afterInit, afterSet } = main();
assert.strictEqual(afterInit, 12);
assert.strictEqual(afterSet, 12);
});
});

const nodeVersion = new ts.Version(process.versions.node);
Expand Down Expand Up @@ -2162,11 +2192,11 @@ describe("unittests:: evaluation:: esDecorators", () => {
// order and applied to the replacement class:
"static block evaluation",
"static field initializer evaluation",
"static field injected initializer evaluation 2",
"static field injected initializer evaluation 1",
"static field injected initializer evaluation 2",
"static auto-accessor initializer evaluation",
"static auto-accessor injected initializer evaluation 2",
"static auto-accessor injected initializer evaluation 1",
"static auto-accessor injected initializer evaluation 2",
// NOTE: at this point, static private fields will be installed (TODO: on the replacement class)

// finally, class extra initializers are applied in the order they were added (i.e., methods before fields,
Expand Down Expand Up @@ -2208,11 +2238,11 @@ describe("unittests:: evaluation:: esDecorators", () => {
// next, instance initializers (i.e., fields, auto-accessors, and static blocks) are evaluated in document
// order:
"instance field initializer evaluation",
"instance field injected initializer evaluation 2",
"instance field injected initializer evaluation 1",
"instance field injected initializer evaluation 2",
"instance auto-accessor initializer evaluation",
"instance auto-accessor injected initializer evaluation 2",
"instance auto-accessor injected initializer evaluation 1",
"instance auto-accessor injected initializer evaluation 2",
// NOTE: at this point, instance private fields will be installed.

// finally, statements in the constructor after the call to `super()` are evaluated:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.push(_);
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.push(_);
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.push(_);
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.push(_);
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ sourceFile:esDecorators-classDeclaration-sourceMap.ts
>>> if (result === null || typeof result !== "object") throw new TypeError("Object expected");
>>> if (_ = accept(result.get)) descriptor.get = _;
>>> if (_ = accept(result.set)) descriptor.set = _;
>>> if (_ = accept(result.init)) initializers.push(_);
>>> if (_ = accept(result.init)) initializers.unshift(_);
>>> }
>>> else if (_ = accept(result)) {
>>> if (kind === "field") initializers.push(_);
>>> if (kind === "field") initializers.unshift(_);
>>> else descriptor[key] = _;
>>> }
>>> }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ sourceFile:esDecorators-classDeclaration-sourceMap.ts
>>> if (result === null || typeof result !== "object") throw new TypeError("Object expected");
>>> if (_ = accept(result.get)) descriptor.get = _;
>>> if (_ = accept(result.set)) descriptor.set = _;
>>> if (_ = accept(result.init)) initializers.push(_);
>>> if (_ = accept(result.init)) initializers.unshift(_);
>>> }
>>> else if (_ = accept(result)) {
>>> if (kind === "field") initializers.push(_);
>>> if (kind === "field") initializers.unshift(_);
>>> else descriptor[key] = _;
>>> }
>>> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.push(_);
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.push(_);
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
Expand Down