From f230497d083b97721152763d87aac4bb317f8b97 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sun, 15 Oct 2017 18:53:53 -0400 Subject: [PATCH] Use literal-based templates for more stuff. --- .../src/index.js | 137 +++++++----------- .../src/rewrite-live-references.js | 14 +- .../src/index.js | 37 +++-- .../src/vanilla.js | 12 +- 4 files changed, 82 insertions(+), 118 deletions(-) diff --git a/packages/babel-helper-module-transforms/src/index.js b/packages/babel-helper-module-transforms/src/index.js index 3b2fb229db..f6345b6fe1 100644 --- a/packages/babel-helper-module-transforms/src/index.js +++ b/packages/babel-helper-module-transforms/src/index.js @@ -102,14 +102,6 @@ export function wrapInterop( return t.callExpression(programPath.hub.file.addHelper(helper), [expr]); } -const buildNamespaceInit = template(` - var NAME = SOURCE; -`); - -const buildReexportNamespace = template(` - EXPORTS.NAME = NAMESPACE; -`); - /** * Create the runtime initialization statements for a given requested source. * These will initialize all of the runtime import/export logic that @@ -127,19 +119,19 @@ export function buildNamespaceInitStatements( // Create and assign binding to namespace object statements.push( - buildNamespaceInit({ - NAME: t.identifier(localName), - SOURCE: t.identifier(sourceMetadata.name), + template.statement`var NAME = SOURCE;`({ + NAME: localName, + SOURCE: sourceMetadata.name, }), ); } for (const exportName of sourceMetadata.reexportNamespace) { // Assign export to namespace object. statements.push( - buildReexportNamespace({ - EXPORTS: t.identifier(metadata.exportName), - NAME: t.identifier(exportName), - NAMESPACE: t.identifier(sourceMetadata.name), + template.statement`EXPORTS.NAME = NAMESPACE;`({ + EXPORTS: metadata.exportName, + NAME: exportName, + NAMESPACE: sourceMetadata.name, }), ); } @@ -153,16 +145,6 @@ export function buildNamespaceInitStatements( return statements; } -const moduleHeader = template(` - Object.defineProperty(EXPORTS, "__esModule", { - value: true, - }) -`); - -const moduleHeaderLoose = template(` - EXPORTS.__esModule = true; -`); - /** * Build an "__esModule" header statement setting the property on a given object. */ @@ -170,58 +152,44 @@ function buildESModuleHeader( metadata: ModuleMetadata, enumerable: boolean = false, ) { - if (enumerable) { - return moduleHeaderLoose({ - EXPORTS: t.identifier(metadata.exportName), - }); - } - - return moduleHeader({ - EXPORTS: t.identifier(metadata.exportName), - }); + return (enumerable + ? template.statement` + EXPORTS.__esModule = true; + ` + : template.statement` + Object.defineProperty(EXPORTS, "__esModule", { + value: true, + }); + `)({ EXPORTS: metadata.exportName }); } -const namespaceReexport = template(` - Object.keys(NAMESPACE).forEach(function(key) { - if (key === "default" || key === "__esModule") return; - VERIFY_NAME_LIST; - - Object.defineProperty(EXPORTS, key, { - enumerable: true, - get: function() { - return NAMESPACE[key]; - }, - }); - }); -`); -const buildNameListCheck = template(` - if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return; -`); - /** * Create a re-export initialization loop for a specific imported namespace. */ function buildNamespaceReexport(metadata, namespace) { - return namespaceReexport({ - NAMESPACE: t.identifier(namespace), - EXPORTS: t.identifier(metadata.exportName), + return template.statement` + Object.keys(NAMESPACE).forEach(function(key) { + if (key === "default" || key === "__esModule") return; + VERIFY_NAME_LIST; + + Object.defineProperty(EXPORTS, key, { + enumerable: true, + get: function() { + return NAMESPACE[key]; + }, + }); + }); + `({ + NAMESPACE: namespace, + EXPORTS: metadata.exportName, VERIFY_NAME_LIST: metadata.exportNameListName - ? buildNameListCheck({ - EXPORTS_LIST: t.identifier(metadata.exportNameListName), - }) + ? template` + if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return; + `({ EXPORTS_LIST: metadata.exportNameListName }) : null, }); } -const reexportGetter = template(` - Object.defineProperty(EXPORTS, EXPORT_NAME, { - enumerable: true, - get: function() { - return NAMESPACE.IMPORT_NAME; - }, - }); -`); - /** * Build a statement declaring a variable that contains all of the exported * variable names in an object so they can easily be referenced from an @@ -289,11 +257,18 @@ function buildExportInitializationStatements( for (const data of metadata.source.values()) { for (const [exportName, importName] of data.reexports) { initStatements.push( - reexportGetter({ - EXPORTS: t.identifier(metadata.exportName), - EXPORT_NAME: t.stringLiteral(exportName), - NAMESPACE: t.identifier(data.name), - IMPORT_NAME: t.identifier(importName), + template` + Object.defineProperty(EXPORTS, "EXPORT_NAME", { + enumerable: true, + get: function() { + return NAMESPACE.IMPORT_NAME; + }, + }); + `({ + EXPORTS: metadata.exportName, + EXPORT_NAME: exportName, + NAMESPACE: data.name, + IMPORT_NAME: importName, }), ); } @@ -315,22 +290,20 @@ function buildExportInitializationStatements( return initStatements; } -const initStatement = template(` - EXPORTS.NAME = VALUE; -`); - /** * Given a set of export names, create a set of nested assignments to * initialize them all to a given expression. */ function buildInitStatement(metadata, exportNames, initExpr) { return t.expressionStatement( - exportNames.reduce((acc, exportName) => { - return initStatement({ - EXPORTS: t.identifier(metadata.exportName), - NAME: t.identifier(exportName), - VALUE: acc, - }).expression; - }, initExpr), + exportNames.reduce( + (acc, exportName) => + template.expression`EXPORTS.NAME = VALUE`({ + EXPORTS: metadata.exportName, + NAME: exportName, + VALUE: acc, + }), + initExpr, + ), ); } diff --git a/packages/babel-helper-module-transforms/src/rewrite-live-references.js b/packages/babel-helper-module-transforms/src/rewrite-live-references.js index bdb53e915e..29c4e9a879 100644 --- a/packages/babel-helper-module-transforms/src/rewrite-live-references.js +++ b/packages/babel-helper-module-transforms/src/rewrite-live-references.js @@ -142,16 +142,12 @@ const buildBindingExportAssignmentExpression = ( }, localExpr); }; -const importThrow = template(` - (function() { - throw new Error('"' + NAME + '" is read-only.'); - })(); -`); - const buildImportThrow = localName => { - return importThrow({ - NAME: t.stringLiteral(localName), - }).expression; + return template.expression.ast` + (function() { + throw new Error('"' + '${localName}' + '" is read-only.'); + })() + `; }; const rewriteReferencesVisitor = { diff --git a/packages/babel-plugin-transform-class-properties/src/index.js b/packages/babel-plugin-transform-class-properties/src/index.js index 4f6f8e1b0f..0fef5b90f6 100644 --- a/packages/babel-plugin-transform-class-properties/src/index.js +++ b/packages/babel-plugin-transform-class-properties/src/index.js @@ -25,30 +25,27 @@ export default function({ types: t }, options) { }, }; - const buildObjectDefineProperty = template(` - Object.defineProperty(REF, KEY, { - configurable: true, - enumerable: true, - writable: true, - value: VALUE - }); - `); - - const buildClassPropertySpec = (ref, { key, value, computed }, scope) => - buildObjectDefineProperty({ + const buildClassPropertySpec = (ref, { key, value, computed }, scope) => { + return template.statement` + Object.defineProperty(REF, KEY, { + configurable: true, + enumerable: true, + writable: true, + value: VALUE + }); + `({ REF: ref, KEY: t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key, - VALUE: value ? value : scope.buildUndefinedNode(), + VALUE: value || scope.buildUndefinedNode(), }); + }; - const buildClassPropertyLoose = (ref, { key, value, computed }, scope) => - t.expressionStatement( - t.assignmentExpression( - "=", - t.memberExpression(ref, key, computed || t.isLiteral(key)), - value ? value : scope.buildUndefinedNode(), - ), - ); + const buildClassPropertyLoose = (ref, { key, value, computed }, scope) => { + return template.statement`MEMBER = VALUE`({ + MEMBER: t.memberExpression(ref, key, computed || t.isLiteral(key)), + VALUE: value || scope.buildUndefinedNode(), + }); + }; const buildClassProperty = loose ? buildClassPropertyLoose diff --git a/packages/babel-plugin-transform-es2015-classes/src/vanilla.js b/packages/babel-plugin-transform-es2015-classes/src/vanilla.js index 7a705466b3..27a94be41c 100644 --- a/packages/babel-plugin-transform-es2015-classes/src/vanilla.js +++ b/packages/babel-plugin-transform-es2015-classes/src/vanilla.js @@ -6,12 +6,6 @@ import * as defineMap from "@babel/helper-define-map"; import template from "@babel/template"; import * as t from "@babel/types"; -const buildDerivedConstructor = template(` - (function () { - super(...arguments); - }) -`); - const noMethodVisitor = { "FunctionExpression|FunctionDeclaration"(path) { path.skip(); @@ -212,7 +206,11 @@ export default class ClassTransformer { let params, body; if (this.isDerived) { - const constructor = buildDerivedConstructor().expression; + const constructor = template.expression.ast` + (function () { + super(...arguments); + }) + `; params = constructor.params; body = constructor.body; } else {