Fixed buildExternalHelpers tool for var and module output types (#6260)

This commit is contained in:
Mateusz Burzyński 2017-09-19 20:44:40 +02:00 committed by Henry Zhu
parent fc1e1c5668
commit 4519f95a29
3 changed files with 55 additions and 32 deletions

View File

@ -66,36 +66,41 @@ function buildModule(namespace, builder) {
const body = []; const body = [];
builder(body); builder(body);
const module = body const module = body.map(helperNode => {
.map(helper => { const possibleAssignment = t.isExpressionStatement(helperNode)
const originalIdentifier = helper.expression.left.property.name; ? helperNode.expression
const isKeywordHelper = keywordHelpers.indexOf(originalIdentifier) !== -1; : helperNode;
const helperIndentifier = isKeywordHelper
? `_${originalIdentifier}`
: originalIdentifier;
const variableDeclaration = t.variableDeclaration("var", [ const isExportedHelper =
t.variableDeclarator( t.isAssignmentExpression(possibleAssignment) &&
t.identifier(helperIndentifier), t.isMemberExpression(possibleAssignment.left) &&
helper.expression.right, possibleAssignment.left.object.name === namespace.name;
if (!isExportedHelper) {
return helperNode;
}
const exportedHelper = possibleAssignment;
const identifier = exportedHelper.left.property.name;
const isKeywordHelper = keywordHelpers.indexOf(identifier) !== -1;
if (isKeywordHelper) {
return t.exportNamedDeclaration(null, [
t.exportSpecifier(
t.identifier(`_${identifier}`),
t.identifier(identifier),
), ),
]); ]);
}
return isKeywordHelper return t.exportNamedDeclaration(
? variableDeclaration t.variableDeclaration("var", [
: t.exportNamedDeclaration(variableDeclaration, []); t.variableDeclarator(t.identifier(identifier), exportedHelper.right),
}) ]),
.concat( [],
t.exportNamedDeclaration(
null,
keywordHelpers.map(keywordHelper =>
t.exportSpecifier(
t.identifier(`_${keywordHelper}`),
t.identifier(keywordHelper),
),
),
),
); );
});
return t.program(module); return t.program(module);
} }
@ -133,9 +138,10 @@ function buildVar(namespace, builder) {
t.variableDeclarator(namespace, t.objectExpression([])), t.variableDeclarator(namespace, t.objectExpression([])),
]), ]),
); );
const tree = t.program(body);
builder(body); builder(body);
body.push(t.expressionStatement(namespace)); body.push(t.expressionStatement(namespace));
return t.program(body); return tree;
} }
function buildHelpers(body, namespace, whitelist) { function buildHelpers(body, namespace, whitelist) {

View File

@ -1,5 +1,4 @@
import * as babel from "../lib/index"; import * as babel from "../lib/index";
import buildExternalHelpers from "../lib/tools/build-external-helpers";
import sourceMap from "source-map"; import sourceMap from "source-map";
import assert from "assert"; import assert from "assert";
import path from "path"; import path from "path";
@ -817,26 +816,44 @@ describe("api", function() {
}); });
describe("buildExternalHelpers", function() { describe("buildExternalHelpers", function() {
describe("smoke tests", function() {
it("builds external helpers in global output type", function() {
babel.buildExternalHelpers(null, "global");
});
it("builds external helpers in module output type", function() {
babel.buildExternalHelpers(null, "module");
});
it("builds external helpers in umd output type", function() {
babel.buildExternalHelpers(null, "umd");
});
it("builds external helpers in var output type", function() {
babel.buildExternalHelpers(null, "var");
});
});
it("all", function() { it("all", function() {
const script = buildExternalHelpers(); const script = babel.buildExternalHelpers();
assert.ok(script.indexOf("classCallCheck") >= -1); assert.ok(script.indexOf("classCallCheck") >= -1);
assert.ok(script.indexOf("inherits") >= 0); assert.ok(script.indexOf("inherits") >= 0);
}); });
it("whitelist", function() { it("whitelist", function() {
const script = buildExternalHelpers(["inherits"]); const script = babel.buildExternalHelpers(["inherits"]);
assert.ok(script.indexOf("classCallCheck") === -1); assert.ok(script.indexOf("classCallCheck") === -1);
assert.ok(script.indexOf("inherits") >= 0); assert.ok(script.indexOf("inherits") >= 0);
}); });
it("empty whitelist", function() { it("empty whitelist", function() {
const script = buildExternalHelpers([]); const script = babel.buildExternalHelpers([]);
assert.ok(script.indexOf("classCallCheck") === -1); assert.ok(script.indexOf("classCallCheck") === -1);
assert.ok(script.indexOf("inherits") === -1); assert.ok(script.indexOf("inherits") === -1);
}); });
it("underscored", function() { it("underscored", function() {
const script = buildExternalHelpers(["typeof"]); const script = babel.buildExternalHelpers(["typeof"]);
assert.ok(script.indexOf("typeof") >= 0); assert.ok(script.indexOf("typeof") >= 0);
}); });
}); });

View File

@ -27,7 +27,7 @@ function getHelperMetadata(file) {
traverse(file, { traverse(file, {
ImportDeclaration(child) { ImportDeclaration(child) {
throw child.buildCodeFrameError("Helpers may import anything."); throw child.buildCodeFrameError("Helpers may not import anything.");
}, },
ExportDefaultDeclaration(child) { ExportDefaultDeclaration(child) {
const decl = child.get("declaration"); const decl = child.get("declaration");