From f20f8b164f225a3c854ddbf9651b16a515082dd7 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Fri, 29 Sep 2017 11:46:52 -0700 Subject: [PATCH] Remove unused module metadata collection. --- .../babel-core/src/config/option-manager.js | 2 - packages/babel-core/src/config/removed.js | 6 + .../src/transformation/file/index.js | 32 +--- .../src/transformation/file/metadata.js | 140 --------------- packages/babel-core/test/api.js | 169 ------------------ 5 files changed, 8 insertions(+), 341 deletions(-) delete mode 100644 packages/babel-core/src/transformation/file/metadata.js diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index ab614ac93d..7c7289889d 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -41,7 +41,6 @@ const optionNames = new Set([ "ignore", "only", "code", - "metadata", "ast", "extends", "comments", @@ -562,7 +561,6 @@ function createInitialOptions() { babelrc: true, filename: "unknown", code: true, - metadata: true, ast: true, comments: true, compact: "auto", diff --git a/packages/babel-core/src/config/removed.js b/packages/babel-core/src/config/removed.js index 8d441c5fee..e4205bf31e 100644 --- a/packages/babel-core/src/config/removed.js +++ b/packages/babel-core/src/config/removed.js @@ -61,4 +61,10 @@ export default { version: 6, message: "Use `babel-plugin-module-resolver@3`'s 'resolvePath' options", }, + + metadata: { + version: 6, + message: + "Generated plugin metadata is always included in the output result", + }, }; diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index e9dccd5a2a..2797f68b95 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -1,7 +1,6 @@ /* global BabelFileResult, BabelParserOptions, BabelFileMetadata */ import getHelper from "babel-helpers"; -import * as metadataVisitor from "./metadata"; import convertSourceMap from "convert-source-map"; import PluginPass from "../plugin-pass"; import { NodePath, Hub, Scope } from "babel-traverse"; @@ -66,16 +65,7 @@ export default class File { } } - this.metadata = { - modules: { - imports: [], - exports: { - exported: [], - specifiers: [], - }, - }, - }; - + this.metadata = {}; this.declarations = {}; this.path = null; @@ -109,19 +99,6 @@ export default class File { return this._map.get(key); } - getMetadata() { - let has = false; - for (const node of (this.ast.program.body: Array)) { - if (t.isModuleDeclaration(node)) { - has = true; - break; - } - } - if (has) { - this.path.traverse(metadataVisitor, this); - } - } - getModuleName(): ?string { const opts = this.opts; if (!opts.moduleIds) { @@ -332,7 +309,6 @@ export default class File { }).setContext(); this.scope = this.path.scope; this.ast = ast; - this.getMetadata(); } addAst(ast) { @@ -458,7 +434,7 @@ export default class File { makeResult({ code, map, ast, ignored }: BabelFileResult): BabelFileResult { const result = { - metadata: null, + metadata: this.metadata, options: this.opts, ignored: !!ignored, code: null, @@ -474,10 +450,6 @@ export default class File { result.ast = ast; } - if (this.opts.metadata) { - result.metadata = this.metadata; - } - return result; } diff --git a/packages/babel-core/src/transformation/file/metadata.js b/packages/babel-core/src/transformation/file/metadata.js deleted file mode 100644 index 5fcb0efa79..0000000000 --- a/packages/babel-core/src/transformation/file/metadata.js +++ /dev/null @@ -1,140 +0,0 @@ -import * as t from "babel-types"; - -export const ModuleDeclaration = { - enter(path, file) { - const { node } = path; - if (node.source) { - node.source.value = file.resolveModuleSource(node.source.value); - } - }, -}; - -export const ImportDeclaration = { - exit(path, file) { - const { node } = path; - - const specifiers = []; - const imported = []; - file.metadata.modules.imports.push({ - source: node.source.value, - imported, - specifiers, - }); - - for (const specifier of (path.get("specifiers"): Array)) { - const local = specifier.node.local.name; - - if (specifier.isImportDefaultSpecifier()) { - imported.push("default"); - specifiers.push({ - kind: "named", - imported: "default", - local, - }); - } - - if (specifier.isImportSpecifier()) { - const importedName = specifier.node.imported.name; - imported.push(importedName); - specifiers.push({ - kind: "named", - imported: importedName, - local, - }); - } - - if (specifier.isImportNamespaceSpecifier()) { - imported.push("*"); - specifiers.push({ - kind: "namespace", - local, - }); - } - } - }, -}; - -export function ExportDeclaration(path, file) { - const { node } = path; - - const source = node.source ? node.source.value : null; - const exports = file.metadata.modules.exports; - - // export function foo() {} - // export let foo = "bar"; - const declar = path.get("declaration"); - if (declar.isStatement()) { - const bindings = declar.getBindingIdentifiers(); - - for (const name in bindings) { - exports.exported.push(name); - exports.specifiers.push({ - kind: "local", - local: name, - exported: path.isExportDefaultDeclaration() ? "default" : name, - }); - } - } - - if (path.isExportNamedDeclaration() && node.specifiers) { - for (const specifier of (node.specifiers: Array)) { - const exported = specifier.exported.name; - exports.exported.push(exported); - - // export foo from "bar"; - if (t.isExportDefaultSpecifier(specifier)) { - exports.specifiers.push({ - kind: "external", - local: exported, - exported, - source, - }); - } - - // export * as foo from "bar"; - if (t.isExportNamespaceSpecifier(specifier)) { - exports.specifiers.push({ - kind: "external-namespace", - exported, - source, - }); - } - - const local = specifier.local; - if (!local) continue; - - // export { foo } from "bar"; - // export { foo as bar } from "bar"; - if (source) { - exports.specifiers.push({ - kind: "external", - local: local.name, - exported, - source, - }); - } - - // export { foo }; - // export { foo as bar }; - if (!source) { - exports.specifiers.push({ - kind: "local", - local: local.name, - exported, - }); - } - } - } - - // export * from "bar"; - if (path.isExportAllDeclaration()) { - exports.specifiers.push({ - kind: "external-all", - source, - }); - } -} - -export function Scope(path) { - path.skip(); -} diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index e703d87140..b9c0f76d4d 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -434,175 +434,6 @@ describe("api", function() { }); }); - it("modules metadata", function() { - return Promise.all([ - // eslint-disable-next-line max-len - transformAsync( - 'import { externalName as localName } from "external";', - ).then(function(result) { - assert.deepEqual(result.metadata.modules.imports[0], { - source: "external", - imported: ["externalName"], - specifiers: [ - { - kind: "named", - imported: "externalName", - local: "localName", - }, - ], - }); - }), - - transformAsync('import * as localName2 from "external";').then(function( - result, - ) { - assert.deepEqual(result.metadata.modules.imports[0], { - source: "external", - imported: ["*"], - specifiers: [ - { - kind: "namespace", - local: "localName2", - }, - ], - }); - }), - - transformAsync('import localName3 from "external";').then(function( - result, - ) { - assert.deepEqual(result.metadata.modules.imports[0], { - source: "external", - imported: ["default"], - specifiers: [ - { - kind: "named", - imported: "default", - local: "localName3", - }, - ], - }); - }), - - transformAsync('export * as externalName1 from "external";', { - plugins: [require("../../babel-plugin-syntax-export-extensions")], - }).then(function(result) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["externalName1"], - specifiers: [ - { - kind: "external-namespace", - exported: "externalName1", - source: "external", - }, - ], - }); - }), - - transformAsync('export externalName2 from "external";', { - plugins: [require("../../babel-plugin-syntax-export-extensions")], - }).then(function(result) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["externalName2"], - specifiers: [ - { - kind: "external", - local: "externalName2", - exported: "externalName2", - source: "external", - }, - ], - }); - }), - - transformAsync("export function namedFunction() {}").then(function( - result, - ) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["namedFunction"], - specifiers: [ - { - kind: "local", - local: "namedFunction", - exported: "namedFunction", - }, - ], - }); - }), - - transformAsync('export var foo = "bar";').then(function(result) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["foo"], - specifiers: [ - { - kind: "local", - local: "foo", - exported: "foo", - }, - ], - }); - }), - - transformAsync("export { localName as externalName3 };").then(function( - result, - ) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["externalName3"], - specifiers: [ - { - kind: "local", - local: "localName", - exported: "externalName3", - }, - ], - }); - }), - - transformAsync('export { externalName4 } from "external";').then(function( - result, - ) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["externalName4"], - specifiers: [ - { - kind: "external", - local: "externalName4", - exported: "externalName4", - source: "external", - }, - ], - }); - }), - - transformAsync('export * from "external";').then(function(result) { - assert.deepEqual(result.metadata.modules.exports, { - exported: [], - specifiers: [ - { - kind: "external-all", - source: "external", - }, - ], - }); - }), - - transformAsync( - "export default function defaultFunction() {}", - ).then(function(result) { - assert.deepEqual(result.metadata.modules.exports, { - exported: ["defaultFunction"], - specifiers: [ - { - kind: "local", - local: "defaultFunction", - exported: "default", - }, - ], - }); - }), - ]); - }); - it("ignore option", function() { return Promise.all([ transformAsync("", {