From 4519f95a297e2834c6a7044fec5239f11dffeeb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 19 Sep 2017 20:44:40 +0200 Subject: [PATCH] Fixed buildExternalHelpers tool for var and module output types (#6260) --- .../src/tools/build-external-helpers.js | 58 ++++++++++--------- packages/babel-core/test/api.js | 27 +++++++-- packages/babel-helpers/src/index.js | 2 +- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/packages/babel-core/src/tools/build-external-helpers.js b/packages/babel-core/src/tools/build-external-helpers.js index ea04163924..79df23e379 100644 --- a/packages/babel-core/src/tools/build-external-helpers.js +++ b/packages/babel-core/src/tools/build-external-helpers.js @@ -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) { diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 596a0fe6c7..99259ce4c1 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -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); }); }); diff --git a/packages/babel-helpers/src/index.js b/packages/babel-helpers/src/index.js index 0f7e05a4ac..8f067fc137 100644 --- a/packages/babel-helpers/src/index.js +++ b/packages/babel-helpers/src/index.js @@ -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");