Fix replacing function declaration in export default (fixes #4468) (#5456)

This commit is contained in:
MrSpider 2017-04-06 17:40:31 +02:00 committed by Henry Zhu
parent 9b4c33d44e
commit fd3a2c285a
2 changed files with 30 additions and 1 deletions

View File

@ -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);

View 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");
});
});
});