Skip to content

Commit

Permalink
Fix moving comments of removed nodes (#15427)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 30, 2023
1 parent fad4e77 commit d917995
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,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 = /*#__PURE__*/function (BB) {
Expand All @@ -20,6 +23,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

0 comments on commit d917995

Please sign in to comment.