Skip to content

Commit

Permalink
Remove .initializer fallback for accessor properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 8, 2022
1 parent 7be6258 commit 7bbb3f9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts

Large diffs are not rendered by default.

49 changes: 17 additions & 32 deletions packages/babel-helpers/src/helpers/applyDecs2203.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,6 @@ function assertValidReturnValue(kind, value) {
if (value.init !== undefined) {
assertCallable(value.init, "accessor.init");
}
if (value.initializer !== undefined) {
assertCallable(value.initializer, "accessor.initializer");
}
} else if (type !== "function") {
var hint;
if (kind === 0 /* FIELD */) {
Expand All @@ -285,18 +282,6 @@ function assertValidReturnValue(kind, value) {
}
}

function getInit(desc) {
var initializer;
if (
(initializer = desc.init) == null &&
(initializer = desc.initializer) &&
typeof console !== "undefined"
) {
console.warn(".initializer has been renamed to .init as of March 2022");
}
return initializer;
}

function applyMemberDec(
ret,
base,
Expand All @@ -310,7 +295,7 @@ function applyMemberDec(
) {
var decs = decInfo[0];

var desc, initializer, value;
var desc, init, value;

if (isPrivate) {
if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
Expand Down Expand Up @@ -367,9 +352,9 @@ function applyMemberDec(
assertValidReturnValue(kind, newValue);

if (kind === 0 /* FIELD */) {
initializer = newValue;
init = newValue;
} else if (kind === 1 /* ACCESSOR */) {
initializer = getInit(newValue);
init = newValue.init;
get = newValue.get || value.get;
set = newValue.set || value.set;

Expand Down Expand Up @@ -401,7 +386,7 @@ function applyMemberDec(
if (kind === 0 /* FIELD */) {
newInit = newValue;
} else if (kind === 1 /* ACCESSOR */) {
newInit = getInit(newValue);
newInit = newValue.init;
get = newValue.get || value.get;
set = newValue.set || value.set;

Expand All @@ -411,28 +396,28 @@ function applyMemberDec(
}

if (newInit !== void 0) {
if (initializer === void 0) {
initializer = newInit;
} else if (typeof initializer === "function") {
initializer = [initializer, newInit];
if (init === void 0) {
init = newInit;
} else if (typeof init === "function") {
init = [init, newInit];
} else {
initializer.push(newInit);
init.push(newInit);
}
}
}
}
}

if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
if (initializer === void 0) {
if (init === void 0) {
// If the initializer was void 0, sub in a dummy initializer
initializer = function (instance, init) {
init = function (instance, init) {
return init;
};
} else if (typeof initializer !== "function") {
var ownInitializers = initializer;
} else if (typeof init !== "function") {
var ownInitializers = init;

initializer = function (instance, init) {
init = function (instance, init) {
var value = init;

for (var i = 0; i < ownInitializers.length; i++) {
Expand All @@ -442,14 +427,14 @@ function applyMemberDec(
return value;
};
} else {
var originalInitializer = initializer;
var originalInitializer = init;

initializer = function (instance, init) {
init = function (instance, init) {
return originalInitializer.call(instance, init);
};
}

ret.push(initializer);
ret.push(init);
}

if (kind !== 0 /* FIELD */) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function logClassDecoratorRun(a, b, c) {
};
}

function logAccessorDecoratorRun(a, b, c, d) {
function decorator(a, b, c, d) {
push(a);
return function (el, { addInitializer }) {
push(b);
Expand All @@ -25,20 +25,20 @@ function logAccessorDecoratorRun(a, b, c, d) {
@logClassDecoratorRun(0, 19, 29)
@logClassDecoratorRun(1, 18, 28)
class A {
@logAccessorDecoratorRun(2, 15, 31, 35)
@logAccessorDecoratorRun(3, 14, 30, 34)
@decorator(2, 15, 31, 35)
@decorator(3, 14, 30, 34)
accessor a;

@logAccessorDecoratorRun(4, 11, 21, 25)
@logAccessorDecoratorRun(5, 10, 20, 24)
@decorator(4, 11, 21, 25)
@decorator(5, 10, 20, 24)
static accessor b;

@logAccessorDecoratorRun(6, 13, 23, 27)
@logAccessorDecoratorRun(7, 12, 22, 26)
@decorator(6, 13, 23, 27)
@decorator(7, 12, 22, 26)
static accessor #c;

@logAccessorDecoratorRun(8, 17, 33, 37)
@logAccessorDecoratorRun(9, 16, 32, 36)
@decorator(8, 17, 33, 37)
@decorator(9, 16, 32, 36)
accessor #d;
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let init = false;
let initializer = false;

function decorator() {
return {
get init() {
init = true;
return () => {};
},
get initializer() {
initializer = true;
return () => {};
}
};
}

class A {
@decorator
accessor x;
}

new A();

expect(init).toBe(true);
expect(initializer).toBe(false);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let init = false;
let initializer = false;

function decorator() {
return {
get init() {
init = true;
return () => {};
},
get initializer() {
initializer = true;
return () => {};
}
};
}

class A {
@decorator
accessor x;
}

new A();

expect(init).toBe(true);
expect(initializer).toBe(false);

0 comments on commit 7bbb3f9

Please sign in to comment.