This commit is contained in:
parent
9b4c33d44e
commit
fd3a2c285a
@ -125,7 +125,8 @@ export function replaceWith(replacement) {
|
||||
if (this.isNodeType("Statement") && t.isExpression(replacement)) {
|
||||
if (
|
||||
!this.canHaveVariableDeclarationOrExpression() &&
|
||||
!this.canSwapBetweenExpressionAndStatement(replacement)
|
||||
!this.canSwapBetweenExpressionAndStatement(replacement) &&
|
||||
!this.parentPath.isExportDefaultDeclaration()
|
||||
) {
|
||||
// replacing a statement with an expression so wrap it in an expression statement
|
||||
replacement = t.expressionStatement(replacement);
|
||||
|
||||
28
packages/babel-traverse/test/replacement.js
Normal file
28
packages/babel-traverse/test/replacement.js
Normal file
@ -0,0 +1,28 @@
|
||||
import traverse from "../lib";
|
||||
import assert from "assert";
|
||||
import { parse } from "babylon";
|
||||
import * as t from "babel-types";
|
||||
|
||||
describe("path/replacement", function () {
|
||||
describe("replaceWith", function () {
|
||||
const ast = parse("export default function() {};", { sourceType: "module" });
|
||||
|
||||
it("replaces declaration in ExportDefaultDeclaration node", function() {
|
||||
traverse(ast, {
|
||||
FunctionDeclaration(path) {
|
||||
path.replaceWith(t.arrayExpression([
|
||||
t.functionExpression(
|
||||
path.node.id,
|
||||
path.node.params,
|
||||
path.node.body,
|
||||
path.node.generator,
|
||||
path.node.async
|
||||
),
|
||||
]));
|
||||
},
|
||||
});
|
||||
|
||||
assert(ast.program.body[0].declaration.type == "ArrayExpression");
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user