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

Fix moving comments of removed nodes #15427

Merged
merged 5 commits into from
Mar 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,12 @@ export function buildFieldsInitNodes(
pureStaticNodes: pureStaticNodes.filter(Boolean),
wrapClass(path: NodePath<t.Class>) {
for (const prop of props) {
// Delete leading comments so that they don't get attached as
// trailing comments of the previous sibling.
// When transforming props, we explicitly attach their leading
// comments to the transformed node with `inheritPropComments`
// above.
prop.node.leadingComments = null;
prop.remove();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class C {
// Output should not use `_initialiseProps`

constructor(T) {
// Output should not use `_initialiseProps`
babelHelpers.defineProperty(this, "y", 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class C {
// Output should not use `_initialiseProps`

constructor(T) {
// Output should not use `_initialiseProps`
this.y = 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class C {
// Output should not use `_initialiseProps`

constructor(T) {
// Output should not use `_initialiseProps`
this.x = void 0;
this.y = 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class C {
// Output should not use `_initialiseProps`

constructor(T) {
// Output should not use `_initialiseProps`
babelHelpers.defineProperty(this, "x", void 0);
babelHelpers.defineProperty(this, "y", 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export class C2 {}

// Not-even E

// everything removed
export { C2 as C3 }; // only C2->C3
var BB;
Expand All @@ -18,6 +21,5 @@ function foo() {}
export { BB2 as BB3, foo }; // only BB2->BB3 and foo

// export an interface before declaration

// everything removed
export { C2 as C4 }; // only C2->C4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @module
*/

import '../message-bus';

/**
* @typedef {Function} PromiseResolveCb
*/
export type PromiseResolveCb<T> = (value?: T | PromiseLike<T>) => void;

/**
* @typedef {Function} PromiseRejectCb
*/
export type PromiseRejectCb = (reason?: any) => void;

var a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @module
*/

import '../message-bus';

/**
* @typedef {Function} PromiseResolveCb
*/

/**
* @typedef {Function} PromiseRejectCb
*/

var a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @module
*/

import '../message-bus';

/**
* @typedef {Function} PromiseResolveCb
*/
export type PromiseResolveCb<T> = (value?: T | PromiseLike<T>) => void;

/**
* @typedef {Function} PromiseRejectCb
*/
export type PromiseRejectCb = (reason?: any) => void;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @module
*/

import '../message-bus';

/**
* @typedef {Function} PromiseResolveCb
*/

/**
* @typedef {Function} PromiseRejectCb
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @module
*/

/**
* @typedef {Function} PromiseResolveCb
*/
export type PromiseResolveCb<T> = (value?: T | PromiseLike<T>) => void;

/**
* @typedef {Function} PromiseRejectCb
*/
export type PromiseRejectCb = (reason?: any) => void;

var a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @module
*/

/**
* @typedef {Function} PromiseResolveCb
*/

/**
* @typedef {Function} PromiseRejectCb
*/

var a;
export {};
32 changes: 28 additions & 4 deletions packages/babel-traverse/src/path/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,37 @@ export function shareCommentsWithSiblings(this: NodePath) {
const next = this.getSibling(this.key + 1);
const hasPrev = Boolean(prev.node);
const hasNext = Boolean(next.node);
if (hasPrev && !hasNext) {
prev.addComments("trailing", trailing);
} else if (hasNext && !hasPrev) {
next.addComments("leading", leading);

if (hasPrev) {
if (leading) {
prev.addComments(
"trailing",
removeIfExisting(leading, prev.node.trailingComments),
);
}
if (trailing && !hasNext) prev.addComments("trailing", trailing);
}
if (hasNext) {
if (trailing) {
next.addComments(
"leading",
removeIfExisting(trailing, next.node.leadingComments),
);
}
if (leading && !hasPrev) next.addComments("leading", leading);
}
}

function removeIfExisting<T>(list: T[], toRemove?: T[]): T[] {
if (!toRemove) return list;
let lastFoundIndex = -1;
return list.filter(el => {
const i = toRemove.indexOf(el, lastFoundIndex);
if (i === -1) return true;
lastFoundIndex = i;
});
}

export function addComment(
this: NodePath,
type: t.CommentTypeShorthand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{
hasPrev;
/* top */
/* left */ /* right */
/* left */
/* right */
/* bottom */
}

{
/* top */
/* left */ /* right */
Expand All @@ -16,7 +16,6 @@
hasPrev;
/* top */
/* left */

/* right */
/* bottom */
hasNext;
Expand Down