-
Notifications
You must be signed in to change notification settings - Fork 81
Open
Description
Turns out that this lost space stems from generation against our locationless synthetic AST nodes, and it's fairly easy to fix:
diff --git i/packages/evasive-transform/src/transform-code.js w/packages/evasive-transform/src/trans
form-code.js
index 1ef096d9d..f186e7ee5 100644
--- i/packages/evasive-transform/src/transform-code.js
+++ w/packages/evasive-transform/src/transform-code.js
@@ -1,6 +1,32 @@
const evadeRegexp = /import\s*\(|<!--|-->/g;
const importRegexp = /import(\s*\()/g;
+/**
+ * Copy the location from one AST node to another (round-tripping through JSON
+ * to sever references), updating the target's end position as if it had zero
+ * length.
+ *
+ * @param {import('@babel/types').Node} target
+ * @param {import('@babel/types').Node} src
+ */
+const adoptStartFrom = (target, src) => {
+ try {
+ const srcLoc = src.loc;
+ if (!srcLoc) return;
+ const loc = /** @type {typeof srcLoc} */ (
+ JSON.parse(JSON.stringify(srcLoc))
+ );
+ const start = loc?.start;
+ target.loc = loc;
+ if (!start) return;
+ target.loc.end = /** @type {typeof start} */ (
+ JSON.parse(JSON.stringify(start))
+ );
+ } catch (_err) {
+ // Ignore errors; this is purely opportunistic.
+ }
+};
+
/**
* Creates a BinaryExpression adding two expressions
*
@@ -36,6 +62,7 @@ export const evadeStrings = p => {
expr = !expr
? { type: 'StringLiteral', value: part }
: addStringToExpressions(expr, part);
+ if (lastIndex === 0) adoptStartFrom(expr, p.node);
lastIndex = index;
}
if (expr) {
@@ -128,11 +155,14 @@ export const evadeTemplates = p => {
newQuasis[newQuasis.length - 1].tail = true;
}
- p.replaceWith({
+ /** @type {import('@babel/types').Node} */
+ const replacement = {
type: 'TemplateLiteral',
quasis: newQuasis,
expressions: newExpressions,
- });
+ };
+ adoptStartFrom(replacement, p.node);
+ p.replaceWith(replacement);
};
/**Originally posted by @gibson042 in #3026 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels