Make .insert{Before,After} work by default when the parent is an eport declaration (#7040)

This commit is contained in:
Nicolò Ribaudo 2017-12-19 00:05:29 +01:00 committed by GitHub
parent 17b37b5013
commit 68476b6ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 38 deletions

View File

@ -89,11 +89,8 @@ function plainFunction(path: NodePath, callId: Object) {
}); });
if (isDeclaration) { if (isDeclaration) {
const basePath = path.parentPath.isExportDeclaration()
? path.parentPath
: path;
basePath.insertAfter(container[1]);
path.replaceWith(container[0]); path.replaceWith(container[0]);
path.insertAfter(container[1]);
} else { } else {
const retFunction = container.callee.body.body[1].argument; const retFunction = container.callee.body.body[1].argument;
if (!functionId) { if (!functionId) {

View File

@ -228,17 +228,11 @@ export default function(api, options) {
if (path.isClassExpression()) { if (path.isClassExpression()) {
path.scope.push({ id: ref }); path.scope.push({ id: ref });
path.replaceWith(t.assignmentExpression("=", ref, path.node)); path.replaceWith(t.assignmentExpression("=", ref, path.node));
} else { } else if (!path.node.id) {
// path.isClassDeclaration() // Anonymous class declaration
if (!path.node.id) {
path.node.id = ref; path.node.id = ref;
} }
if (path.parentPath.isExportDeclaration()) {
path = path.parentPath;
}
}
path.insertAfter(nodes); path.insertAfter(nodes);
}, },
}, },

View File

@ -14,25 +14,14 @@ export default function transpileEnum(path, t) {
switch (path.parent.type) { switch (path.parent.type) {
case "BlockStatement": case "BlockStatement":
case "ExportNamedDeclaration":
case "Program": { case "Program": {
const isGlobal = t.isProgram(path.parent); // && !path.parent.body.some(t.isModuleDeclaration); path.insertAfter(fill);
if (seen(path.parentPath)) { if (seen(path.parentPath)) {
path.replaceWith(fill);
} else {
path.replaceWithMultiple([
makeVar(node.id, t, isGlobal ? "var" : "let"),
fill,
]);
}
break;
}
case "ExportNamedDeclaration": {
path.parentPath.insertAfter(fill);
if (seen(path.parentPath.parentPath)) {
path.remove(); path.remove();
} else { } else {
path.replaceWith(makeVar(node.id, t, "let")); const isGlobal = t.isProgram(path.parent); // && !path.parent.body.some(t.isModuleDeclaration);
path.replaceWith(makeVar(node.id, t, isGlobal ? "var" : "let"));
} }
break; break;
} }
@ -42,6 +31,10 @@ export default function transpileEnum(path, t) {
} }
function seen(parentPath: Path<Node>) { function seen(parentPath: Path<Node>) {
if (parentPath.isExportDeclaration()) {
return seen(parentPath.parentPath);
}
if (parentPath.getData(name)) { if (parentPath.getData(name)) {
return true; return true;
} else { } else {

View File

@ -128,12 +128,6 @@ export default class PathHoister {
} }
} }
// We can't insert before/after a child of an export declaration, so move up
// to the declaration itself.
if (path.parentPath.isExportDeclaration()) {
path = path.parentPath;
}
return path; return path;
} }

View File

@ -16,7 +16,8 @@ export function insertBefore(nodes) {
if ( if (
this.parentPath.isExpressionStatement() || this.parentPath.isExpressionStatement() ||
this.parentPath.isLabeledStatement() this.parentPath.isLabeledStatement() ||
this.parentPath.isExportDeclaration()
) { ) {
return this.parentPath.insertBefore(nodes); return this.parentPath.insertBefore(nodes);
} else if ( } else if (
@ -96,7 +97,8 @@ export function insertAfter(nodes) {
if ( if (
this.parentPath.isExpressionStatement() || this.parentPath.isExpressionStatement() ||
this.parentPath.isLabeledStatement() this.parentPath.isLabeledStatement() ||
this.parentPath.isExportDeclaration()
) { ) {
return this.parentPath.insertAfter(nodes); return this.parentPath.insertAfter(nodes);
} else if ( } else if (

View File

@ -4,8 +4,8 @@ import { parse } from "babylon";
import generate from "@babel/generator"; import generate from "@babel/generator";
import * as t from "@babel/types"; import * as t from "@babel/types";
function getPath(code) { function getPath(code, parserOpts) {
const ast = parse(code); const ast = parse(code, parserOpts);
let path; let path;
traverse(ast, { traverse(ast, {
Program: function(_path) { Program: function(_path) {
@ -118,6 +118,41 @@ describe("modification", function() {
"if (x) {\n b\n\n for (var i = 0; i < 0; i++) {}\n}", "if (x) {\n b\n\n for (var i = 0; i < 0; i++) {}\n}",
); );
}); });
describe("when the parent is an export declaration inserts the node before", function() {
it("the ExportNamedDeclaration", function() {
const bodyPath = getPath("export function a() {}", {
sourceType: "module",
}).parentPath;
const fnPath = bodyPath.get("body.0.declaration");
fnPath.insertBefore(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.0").node, t.identifier("x"));
});
it("the ExportDefaultDeclaration, if a declaration is exported", function() {
const bodyPath = getPath("export default function () {}", {
sourceType: "module",
}).parentPath;
const fnPath = bodyPath.get("body.0.declaration");
fnPath.insertBefore(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.0").node, t.identifier("x"));
});
it("the exported expression", function() {
const bodyPath = getPath("export default 2;", {
sourceType: "module",
}).parentPath;
const path = bodyPath.get("body.0.declaration");
path.insertBefore(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.0").node, t.identifier("x"));
});
});
}); });
describe("insertAfter", function() { describe("insertAfter", function() {
@ -170,5 +205,40 @@ describe("modification", function() {
"if (x) {\n for (var i = 0; i < 0; i++) {}\n\n b\n}", "if (x) {\n for (var i = 0; i < 0; i++) {}\n\n b\n}",
); );
}); });
describe("when the parent is an export declaration inserts the node after", function() {
it("the ExportNamedDeclaration", function() {
const bodyPath = getPath("export function a() {}", {
sourceType: "module",
}).parentPath;
const fnPath = bodyPath.get("body.0.declaration");
fnPath.insertAfter(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.1").node, t.identifier("x"));
});
it("the ExportDefaultDeclaration, if a declaration is exported", function() {
const bodyPath = getPath("export default function () {}", {
sourceType: "module",
}).parentPath;
const fnPath = bodyPath.get("body.0.declaration");
fnPath.insertAfter(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.1").node, t.identifier("x"));
});
it("the exported expression", function() {
const bodyPath = getPath("export default 2;", {
sourceType: "module",
}).parentPath;
const path = bodyPath.get("body.0.declaration");
path.insertAfter(t.identifier("x"));
assert.equal(bodyPath.get("body").length, 2);
assert.deepEqual(bodyPath.get("body.1").node, t.identifier("x"));
});
});
}); });
}); });