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

Use simpler assertThisInitialized more often #16343

Merged
merged 1 commit into from Mar 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,7 +18,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
value: "foo"
});
}
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -12,7 +12,7 @@ let Foo = /*#__PURE__*/function (_Bar) {
_this = babelHelpers.callSuper(this, Foo);
babelHelpers.classPrivateFieldInitSpec(babelHelpers.assertThisInitialized(_this), _bar, "foo");
}
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -11,7 +11,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
_this = babelHelpers.callSuper(this, Foo);
babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo");
}
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
22 changes: 12 additions & 10 deletions packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -406,17 +406,20 @@ export default function transformClass(
return ref;
};

const buildAssertThisInitialized = function () {
return t.callExpression(
classState.file.addHelper("assertThisInitialized"),
[thisRef()],
);
};

for (const thisPath of classState.superThises) {
const { node, parentPath } = thisPath;
if (parentPath.isMemberExpression({ object: node })) {
thisPath.replaceWith(thisRef());
continue;
}
thisPath.replaceWith(
t.callExpression(classState.file.addHelper("assertThisInitialized"), [
thisRef(),
]),
);
thisPath.replaceWith(buildAssertThisInitialized());
}

const bareSupers: NodePath<t.CallExpression>[] = [];
Expand Down Expand Up @@ -462,10 +465,7 @@ export default function transformClass(

if (classState.isLoose) {
wrapReturn = (returnArg: t.Expression | void) => {
const thisExpr = t.callExpression(
classState.file.addHelper("assertThisInitialized"),
[thisRef()],
);
const thisExpr = buildAssertThisInitialized();
return returnArg
? t.logicalExpression("||", returnArg, thisExpr)
: thisExpr;
Expand All @@ -490,7 +490,9 @@ export default function transformClass(
body.pushContainer(
"body",
t.returnStatement(
guaranteedSuperBeforeFinish ? thisRef() : wrapReturn(),
guaranteedSuperBeforeFinish
? thisRef()
: buildAssertThisInitialized(),
),
);
}
Expand Down
Expand Up @@ -7,7 +7,7 @@ var A = /*#__PURE__*/function (_B) {
function A(track) {
var _this;
if (track !== undefined) _this = babelHelpers.callSuper(this, A, [track]);else _this = babelHelpers.callSuper(this, A);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(A, _B);
return babelHelpers.createClass(A);
Expand Down
Expand Up @@ -10,7 +10,7 @@ var A = /*#__PURE__*/function (_B) {
var _this;
babelHelpers.classCallCheck(this, A);
if (track !== undefined) _this = babelHelpers.callSuper(this, A, [track]);else _this = babelHelpers.callSuper(this, A);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(A, _B);
return babelHelpers.createClass(A);
Expand Down
Expand Up @@ -7,7 +7,7 @@ var Test = /*#__PURE__*/function (_Foo) {
babelHelpers.classCallCheck(this, Test);
(_babelHelpers$get = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Test.prototype)), "foo", _thisSuper)) === null || _babelHelpers$get === void 0 || _babelHelpers$get.bar;
(_babelHelpers$get2 = babelHelpers.get((_thisSuper2 = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Test.prototype)), "foo", _thisSuper2)) === null || _babelHelpers$get2 === void 0 || _babelHelpers$get2.call(_thisSuper2);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Test, _Foo);
return babelHelpers.createClass(Test);
Expand Down
Expand Up @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
var _this;
babelHelpers.classCallCheck(this, Foo);
if (eval("false")) _this = babelHelpers.callSuper(this, Foo);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -6,7 +6,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
babelHelpers.classCallCheck(this, Foo);
var fn = () => _this = babelHelpers.callSuper(this, Foo);
fn();
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
var _this;
babelHelpers.classCallCheck(this, Foo);
var fn = () => _this = babelHelpers.callSuper(this, Foo);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -4,7 +4,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
function Foo() {
var _this;
babelHelpers.classCallCheck(this, Foo);
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down
Expand Up @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/function (_Bar) {
var _this;
babelHelpers.classCallCheck(this, Foo);
Foo[babelHelpers.assertThisInitialized(_this)];
return babelHelpers.possibleConstructorReturn(_this);
return babelHelpers.assertThisInitialized(_this);
}
babelHelpers.inherits(Foo, _Bar);
return babelHelpers.createClass(Foo);
Expand Down