Fixed buildExternalHelpers tool for var and module output types (#6260)
This commit is contained in:
parent
fc1e1c5668
commit
4519f95a29
@ -66,36 +66,41 @@ function buildModule(namespace, builder) {
|
||||
const body = [];
|
||||
builder(body);
|
||||
|
||||
const module = body
|
||||
.map(helper => {
|
||||
const originalIdentifier = helper.expression.left.property.name;
|
||||
const isKeywordHelper = keywordHelpers.indexOf(originalIdentifier) !== -1;
|
||||
const helperIndentifier = isKeywordHelper
|
||||
? `_${originalIdentifier}`
|
||||
: originalIdentifier;
|
||||
const module = body.map(helperNode => {
|
||||
const possibleAssignment = t.isExpressionStatement(helperNode)
|
||||
? helperNode.expression
|
||||
: helperNode;
|
||||
|
||||
const variableDeclaration = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
t.identifier(helperIndentifier),
|
||||
helper.expression.right,
|
||||
const isExportedHelper =
|
||||
t.isAssignmentExpression(possibleAssignment) &&
|
||||
t.isMemberExpression(possibleAssignment.left) &&
|
||||
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
|
||||
? variableDeclaration
|
||||
: t.exportNamedDeclaration(variableDeclaration, []);
|
||||
})
|
||||
.concat(
|
||||
t.exportNamedDeclaration(
|
||||
null,
|
||||
keywordHelpers.map(keywordHelper =>
|
||||
t.exportSpecifier(
|
||||
t.identifier(`_${keywordHelper}`),
|
||||
t.identifier(keywordHelper),
|
||||
),
|
||||
),
|
||||
),
|
||||
return t.exportNamedDeclaration(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(t.identifier(identifier), exportedHelper.right),
|
||||
]),
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
return t.program(module);
|
||||
}
|
||||
@ -133,9 +138,10 @@ function buildVar(namespace, builder) {
|
||||
t.variableDeclarator(namespace, t.objectExpression([])),
|
||||
]),
|
||||
);
|
||||
const tree = t.program(body);
|
||||
builder(body);
|
||||
body.push(t.expressionStatement(namespace));
|
||||
return t.program(body);
|
||||
return tree;
|
||||
}
|
||||
|
||||
function buildHelpers(body, namespace, whitelist) {
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import * as babel from "../lib/index";
|
||||
import buildExternalHelpers from "../lib/tools/build-external-helpers";
|
||||
import sourceMap from "source-map";
|
||||
import assert from "assert";
|
||||
import path from "path";
|
||||
@ -817,26 +816,44 @@ describe("api", 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() {
|
||||
const script = buildExternalHelpers();
|
||||
const script = babel.buildExternalHelpers();
|
||||
assert.ok(script.indexOf("classCallCheck") >= -1);
|
||||
assert.ok(script.indexOf("inherits") >= 0);
|
||||
});
|
||||
|
||||
it("whitelist", function() {
|
||||
const script = buildExternalHelpers(["inherits"]);
|
||||
const script = babel.buildExternalHelpers(["inherits"]);
|
||||
assert.ok(script.indexOf("classCallCheck") === -1);
|
||||
assert.ok(script.indexOf("inherits") >= 0);
|
||||
});
|
||||
|
||||
it("empty whitelist", function() {
|
||||
const script = buildExternalHelpers([]);
|
||||
const script = babel.buildExternalHelpers([]);
|
||||
assert.ok(script.indexOf("classCallCheck") === -1);
|
||||
assert.ok(script.indexOf("inherits") === -1);
|
||||
});
|
||||
|
||||
it("underscored", function() {
|
||||
const script = buildExternalHelpers(["typeof"]);
|
||||
const script = babel.buildExternalHelpers(["typeof"]);
|
||||
assert.ok(script.indexOf("typeof") >= 0);
|
||||
});
|
||||
});
|
||||
|
||||
@ -27,7 +27,7 @@ function getHelperMetadata(file) {
|
||||
|
||||
traverse(file, {
|
||||
ImportDeclaration(child) {
|
||||
throw child.buildCodeFrameError("Helpers may import anything.");
|
||||
throw child.buildCodeFrameError("Helpers may not import anything.");
|
||||
},
|
||||
ExportDefaultDeclaration(child) {
|
||||
const decl = child.get("declaration");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user