Skip to content
Open
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
35 changes: 32 additions & 3 deletions packages/babel-helper-create-class-features-plugin/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,49 @@ export function injectInitialization(
renamer(referenceVisitor, { scope: constructor.scope });
}

const TAG_LAST_INJECTED = "babel_class_constructor_last_injected";
function getInjectionPath(paths: NodePath[]) {
for (const path of paths) {
if (path.getData(TAG_LAST_INJECTED)) {
path.setData(TAG_LAST_INJECTED, null);
return path;
}
}
}

let paths;
if (isDerived) {
const bareSupers: NodePath<t.CallExpression>[] = [];
constructor.traverse(findBareSupers, bareSupers);
let isFirst = true;
for (const bareSuper of bareSupers) {
const inStmt = bareSuper.parentPath.isStatement();
const path =
getInjectionPath(
(inStmt ? bareSuper.parentPath : bareSuper).getAllNextSiblings(),
) || bareSuper;

if (isFirst) {
bareSuper.insertAfter(nodes);
paths = path.insertAfter(nodes);
isFirst = false;
} else {
bareSuper.insertAfter(nodes.map(n => t.cloneNode(n)));
paths = path.insertAfter(nodes.map(n => t.cloneNode(n)));
}

let last = paths[paths.length - 1];
if (!inStmt && last.isThisExpression()) {
last = paths[paths.length - 2];
}

last.setData(TAG_LAST_INJECTED, true);
}
} else {
constructor.get("body").unshiftContainer("body", nodes);
const path = getInjectionPath(constructor.get("body.body"));
paths = path
? path.insertAfter(nodes)
: constructor.get("body").unshiftContainer("body", nodes);

paths[paths.length - 1].setData(TAG_LAST_INJECTED, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Foo {
}

class Foo2 extends Foo {
constructor(private readonly incoming: number) {}
value = this.incoming;
}

class Foo3 extends Foo {
constructor(private readonly incoming: number) {
super();
}
value = this.incoming;
}
Comment on lines +9 to +14

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add a test with super() in an expression position?

a ? super() : b;


class Foo4 extends Foo {
constructor(private readonly incoming: number) {
if (1) {
super();
} else {
super();
}
}
value = this.incoming;
}


class Foo5 extends Foo {
constructor(private readonly incoming: number) {
var c = a ? super() : b;
}
value = this.incoming;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"transform-typescript",
{
"allowDeclareFields": true
}
],
"proposal-class-properties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Foo {}
class Foo2 extends Foo {
constructor(incoming) {}
}
class Foo3 extends Foo {
constructor(incoming) {
super();
this.incoming = incoming;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated, but in Babel 8 we should emit this based on the setPublicClassFields assumption.

babelHelpers.defineProperty(this, "value", this.incoming);
}
}
class Foo4 extends Foo {
constructor(incoming) {
if (1) {
super();
this.incoming = incoming;
babelHelpers.defineProperty(this, "value", this.incoming);
} else {
super();
this.incoming = incoming;
babelHelpers.defineProperty(this, "value", this.incoming);
}
}
}
class Foo5 extends Foo {
constructor(incoming) {
var c = a ? (super(), (this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming)), this) : b;

@liuxingbaoyu liuxingbaoyu May 6, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

var c = a ? (super(), (this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming)), this) : b;
vs
var c = a ? (super(), this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming), this) : b;
There is actually a potential bug here when more than two plugins inject initialization statements.
I tried to fix it, but had some difficulty, so I decided to defer to a future PR, which should affect very few people and introduce no new regressions.

The root of the problem is when we use insertAfter, it will insert a SequenceExpression even if it is already in a SequenceExpression.
I'm not sure if we should modify insertAfter or we should handle it manually in the plugin.
I tried to modify insertAfter, which caused a lot of CI errors. I tried to fix some of them, but there were still some difficulties.
I hope to be able to look into them after we reach a consensus.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don-t those two examples have the same behavior anyway? One just has unnecessary parentheses.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it works fine when enabling these two plugins. If someone uses three plugins that will inject initialization statements, I think it will stay wrong before this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {
constructor(private readonly incoming: number) {}
value = this.incoming;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"transform-typescript",
{
"allowDeclareFields": true
}
],
"proposal-class-properties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo {
constructor(incoming) {
this.incoming = incoming;
babelHelpers.defineProperty(this, "value", this.incoming);
}
}
4 changes: 2 additions & 2 deletions packages/babel-traverse/src/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Scope from "../scope";
import { validate } from "@babel/types";
import * as t from "@babel/types";
import { path as pathCache } from "../cache";
import generator from "@babel/generator";
import generate from "@babel/generator";

// NodePath is split across many files.
import * as NodePath_ancestry from "./ancestry";
Expand Down Expand Up @@ -168,7 +168,7 @@ class NodePath<T extends t.Node = t.Node> {
}

toString() {
return generator(this.node).code;
return generate(this.node).code;
}

get inList() {
Expand Down
8 changes: 3 additions & 5 deletions packages/babel-traverse/src/path/modification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function _containerInsert<N extends t.Node>(
const path = this.getSibling(to) as NodePath<N>;
paths.push(path);

if (this.context && this.context.queue) {
if (this.context?.queue) {
path.pushContext(this.context);
}
}
Expand Down Expand Up @@ -154,10 +154,8 @@ function isAlmostConstantAssignment(

// If the variable is defined in the current scope and only assigned here,
// we can be sure that its value won't change.
return (
blockScope.hasOwnBinding(node.left.name) &&
blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1
);
const binding = blockScope.getOwnBinding(node.left.name);
return binding && binding.constantViolations.length <= 1;
}

/**
Expand Down
15 changes: 6 additions & 9 deletions packages/babel-types/src/clone/cloneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ function cloneNodeInternal<T extends t.Node>(
return newNode;
}

function maybeCloneComments<T extends t.Comment>(
comments: ReadonlyArray<T> | null,
function maybeCloneComments(
comments: t.Comment[] | null,
deep: boolean,
withoutLoc: boolean,
commentsCache: Map<T, T>,
): ReadonlyArray<T> | null {
if (!comments || !deep) {
commentsCache: Map<t.Comment, t.Comment>,
): t.Comment[] | null {
if (!comments?.length || !deep) {
return comments;
}
return comments.map(comment => {
Expand All @@ -159,10 +159,7 @@ function maybeCloneComments<T extends t.Comment>(

const { type, value, loc } = comment;

const ret = { type, value, loc } as T;
if (withoutLoc) {
ret.loc = null;
}
const ret = { type, value, loc: withoutLoc ? null : loc } as t.Comment;

commentsCache.set(comment, ret);

Expand Down