| 1 |
import { parse } from "@babel/parser"; |
| 2 |
import * as t from "@babel/types"; |
| 3 |
|
| 4 |
import _traverse from "../lib/index.js"; |
| 5 |
import _generate from "@babel/generator"; |
| 6 |
const traverse = _traverse.default || _traverse; |
| 7 |
const generate = _generate.default || _generate; |
| 8 |
|
| 9 |
describe("path/replacement", function () { |
| 10 |
describe("replaceWith", function () { |
| 11 |
it("replaces declaration in ExportDefaultDeclaration node", function () { |
| 12 |
const ast = parse("export default function() {};", { |
| 13 |
sourceType: "module", |
| 14 |
}); |
| 15 |
traverse(ast, { |
| 16 |
FunctionDeclaration(path) { |
| 17 |
path.replaceWith( |
| 18 |
t.arrayExpression([ |
| 19 |
t.functionExpression( |
| 20 |
path.node.id, |
| 21 |
path.node.params, |
| 22 |
path.node.body, |
| 23 |
path.node.generator, |
| 24 |
path.node.async, |
| 25 |
), |
| 26 |
]), |
| 27 |
); |
| 28 |
}, |
| 29 |
}); |
| 30 |
|
| 31 |
expect(ast.program.body[0].declaration.type).toBe("ArrayExpression"); |
| 32 |
}); |
| 33 |
|
| 34 |
it("throws error when trying to replace Program with a non-Program node", function () { |
| 35 |
const ast = parse("var x = 3;"); |
| 36 |
expect(function () { |
| 37 |
traverse(ast, { |
| 38 |
Program(path) { |
| 39 |
path.replaceWith(t.identifier("a")); |
| 40 |
}, |
| 41 |
}); |
| 42 |
}).toThrow( |
| 43 |
/You can only replace a Program root node with another Program node/, |
| 44 |
); |
| 45 |
}); |
| 46 |
|
| 47 |
it("throws error when used with an array of nodes", function () { |
| 48 |
const ast = parse("function abc() {}; var test = 17;"); |
| 49 |
expect(function () { |
| 50 |
traverse(ast, { |
| 51 |
NumericLiteral(path) { |
| 52 |
path.replaceWith([ |
| 53 |
t.identifier("should"), |
| 54 |
t.identifier("never"), |
| 55 |
t.identifier("happen"), |
| 56 |
]); |
| 57 |
}, |
| 58 |
}); |
| 59 |
}).toThrow( |
| 60 |
/Don't use `path\.replaceWith\(\)` with an array of nodes, use `path\.replaceWithMultiple\(\)`/, |
| 61 |
); |
| 62 |
}); |
| 63 |
|
| 64 |
it("throws error when used with source string", function () { |
| 65 |
const ast = parse( |
| 66 |
"(function() { var x = 3; var y = 17; var c = x + y; })();", |
| 67 |
); |
| 68 |
expect(function () { |
| 69 |
traverse(ast, { |
| 70 |
BinaryExpression(path) { |
| 71 |
path.replaceWith("17 + 23"); |
| 72 |
}, |
| 73 |
}); |
| 74 |
}).toThrow( |
| 75 |
/Don't use `path\.replaceWith\(\)` with a source string, use `path\.replaceWithSourceString\(\)`/, |
| 76 |
); |
| 77 |
}); |
| 78 |
|
| 79 |
it("throws error when trying to replace removed node", function () { |
| 80 |
const ast = parse("var z = 'abc';"); |
| 81 |
expect(function () { |
| 82 |
traverse(ast, { |
| 83 |
StringLiteral(path) { |
| 84 |
path.remove(); |
| 85 |
path.replaceWith(t.identifier("p")); |
| 86 |
}, |
| 87 |
}); |
| 88 |
}).toThrow(/You can't replace this node, we've already removed it/); |
| 89 |
}); |
| 90 |
|
| 91 |
it("throws error when passed a falsy value", function () { |
| 92 |
const ast = parse("var z = 'abc';"); |
| 93 |
expect(function () { |
| 94 |
traverse(ast, { |
| 95 |
StringLiteral(path) { |
| 96 |
path.replaceWith(); |
| 97 |
}, |
| 98 |
}); |
| 99 |
}).toThrow( |
| 100 |
/You passed `path\.replaceWith\(\)` a falsy node, use `path\.remove\(\)` instead/, |
| 101 |
); |
| 102 |
}); |
| 103 |
|
| 104 |
it("does not revisit the replaced node if it is the node being replaced", () => { |
| 105 |
const ast = parse(`var x;`); |
| 106 |
let visitCounter = 0; |
| 107 |
traverse(ast, { |
| 108 |
VariableDeclaration(path) { |
| 109 |
visitCounter++; |
| 110 |
if (visitCounter > 1) { |
| 111 |
return true; |
| 112 |
} |
| 113 |
path.replaceWith(path.node); |
| 114 |
}, |
| 115 |
}); |
| 116 |
expect(visitCounter).toBe(1); |
| 117 |
}); |
| 118 |
|
| 119 |
|
| 120 |
it("updates pathCache with the replaced node", () => { |
| 121 |
const ast = parse(`() => (a?.b)?.c`, { |
| 122 |
createParenthesizedExpressions: true, |
| 123 |
}); |
| 124 |
traverse(ast, { |
| 125 |
OptionalMemberExpression(path) { |
| 126 |
path.node.type = "MemberExpression"; |
| 127 |
|
| 128 |
path.replaceWith(t.cloneNode(path.node)); |
| 129 |
path.parentPath.ensureBlock(); |
| 130 |
|
| 131 |
const aQuestionDotBNode = path.node.object.expression; |
| 132 |
|
| 133 |
aQuestionDotBNode.type = "MemberExpression"; |
| 134 |
}, |
| 135 |
ParenthesizedExpression(path) { |
| 136 |
path.replaceWith(path.node.expression); |
| 137 |
}, |
| 138 |
}); |
| 139 |
expect(generate(ast).code).toMatchInlineSnapshot(` |
| 140 |
"() => { |
| 141 |
return a.b.c; |
| 142 |
};" |
| 143 |
`); |
| 144 |
}); |
| 145 |
}); |
| 146 |
describe("replaceWithMultiple", () => { |
| 147 |
it("does not add extra parentheses for a JSXElement with a JSXElement parent", () => { |
| 148 |
const ast = parse(`<div><span><p></p><h></h></span></div>`, { |
| 149 |
plugins: ["jsx"], |
| 150 |
}); |
| 151 |
traverse(ast, { |
| 152 |
JSXElement: path => { |
| 153 |
if (path.node.openingElement.name.name === "span") { |
| 154 |
path.replaceWithMultiple(path.node.children.filter(t.isJSXElement)); |
| 155 |
} |
| 156 |
}, |
| 157 |
}); |
| 158 |
expect(generate(ast).code).toBe("<div><p></p><h></h></div>;"); |
| 159 |
}); |
| 160 |
it("does not revisit one of new nodes if it is the node being replaced and is the head of nodes", () => { |
| 161 |
|
| 162 |
const ast = parse(`var x;`); |
| 163 |
let visitCounter = 0; |
| 164 |
traverse(ast, { |
| 165 |
VariableDeclaration(path) { |
| 166 |
visitCounter++; |
| 167 |
if (visitCounter > 1) { |
| 168 |
return true; |
| 169 |
} |
| 170 |
path.replaceWithMultiple([path.node, t.emptyStatement()]); |
| 171 |
}, |
| 172 |
}); |
| 173 |
expect(visitCounter).toBe(1); |
| 174 |
}); |
| 175 |
it("does not revisit one of new nodes if it is the node being replaced and is the tail of nodes", () => { |
| 176 |
|
| 177 |
const ast = parse(`var x;`); |
| 178 |
let visitCounter = 0; |
| 179 |
traverse(ast, { |
| 180 |
VariableDeclaration(path) { |
| 181 |
visitCounter++; |
| 182 |
if (visitCounter > 1) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
path.replaceWithMultiple([t.emptyStatement(), path.node]); |
| 186 |
}, |
| 187 |
}); |
| 188 |
expect(visitCounter).toBe(1); |
| 189 |
}); |
| 190 |
}); |
| 191 |
}); |