diff --git a/packages/babel-core/README.md b/packages/babel-core/README.md index a176d469a5..719f1c856a 100644 --- a/packages/babel-core/README.md +++ b/packages/babel-core/README.md @@ -111,7 +111,6 @@ Following is a table of the options you can use: | `plugins` | `[]` | List of [plugins](https://babeljs.io/docs/plugins/) to load and use | | `presets` | `[]` | List of [presets](https://babeljs.io/docs/plugins/#presets) (a set of plugins) to load and use | | `retainLines` | `false` | Retain line numbers. This will lead to wacky code but is handy for scenarios where you can't use source maps. (**NOTE:** This will not retain the columns) | -| `resolveModuleSource` | `null` | Resolve a module source ie. `import "SOURCE";` to a custom value. Called as `resolveModuleSource(source, filename)` | | `shouldPrintComment` | `null` | An optional callback that controls whether a comment should be output or not. Called as `shouldPrintComment(commentContents)`. **NOTE:** This overrides the `comment` option when used | | `sourceFileName` | `(filenameRelative)` | Set `sources[0]` on returned source map | | `sourceMaps` | `false` | If truthy, adds a `map` property to returned output. If set to `"inline"`, a comment with a sourceMappingURL directive is added to the bottom of the returned code. If set to `"both"` then a `map` property is returned as well as a source map comment appended. **This does not emit sourcemap files by itself!** To have sourcemaps emitted using the CLI, you must pass it the `--source-maps` option | diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index 6bd6757080..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", @@ -57,7 +56,6 @@ const optionNames = new Set([ "sourceType", "auxiliaryCommentBefore", "auxiliaryCommentAfter", - "resolveModuleSource", "getModuleId", "moduleRoot", "moduleIds", @@ -450,9 +448,10 @@ function normalizeOptions(config) { // check for an unknown option if (!optionNames.has(key)) { if (removed[key]) { + const { message, version = 5 } = removed[key]; + throw new ReferenceError( - `Using removed Babel 5 option: ${alias}.${key} - ${removed[key] - .message}`, + `Using removed Babel ${version} option: ${alias}.${key} - ${message}`, ); } else { // eslint-disable-next-line max-len @@ -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 751209e8e6..e4205bf31e 100644 --- a/packages/babel-core/src/config/removed.js +++ b/packages/babel-core/src/config/removed.js @@ -56,4 +56,15 @@ export default { whitelist: { message: "Put the specific transforms you want in the `plugins` option", }, + + resolveModuleSource: { + 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/index.js b/packages/babel-core/src/index.js index 35e8e5c77f..fe616d5260 100644 --- a/packages/babel-core/src/index.js +++ b/packages/babel-core/src/index.js @@ -31,11 +31,10 @@ export function Plugin(alias) { export { transform, - analyse, transformFromAst, transformFile, transformFileSync, -} from "./transformation/pipeline"; +} from "./transformation"; /** * Recommended set of compilable extensions. Not used in babel-core directly, but meant as diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index 38947d5397..b827c4b0bc 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"; @@ -9,7 +8,6 @@ import sourceMap from "source-map"; import generate from "babel-generator"; import { codeFrameColumns } from "babel-code-frame"; import traverse from "babel-traverse"; -import Store from "../store"; import { parse } from "babylon"; import * as t from "babel-types"; import buildDebug from "debug"; @@ -38,7 +36,7 @@ const errorVisitor = { }, }; -export default class File extends Store { +export default class File { constructor({ options, passes }: ResolvedConfig) { if (!INTERNAL_PLUGINS) { // Lazy-init the internal plugin to remove the init-time circular dependency between plugins being @@ -49,8 +47,7 @@ export default class File extends Store { }).passes[0]; } - super(); - + this._map = new Map(); this.pluginPasses = passes; this.opts = options; @@ -68,23 +65,8 @@ export default class File extends Store { } } - this.metadata = { - usedHelpers: [], - marked: [], - modules: { - imports: [], - exports: { - exported: [], - specifiers: [], - }, - }, - }; - - this.dynamicImportTypes = {}; - this.dynamicImportIds = {}; - this.dynamicImports = []; + this.metadata = {}; this.declarations = {}; - this.usedHelpers = {}; this.path = null; this.ast = {}; @@ -95,16 +77,10 @@ export default class File extends Store { this.hub = new Hub(this); } - static helpers: Array; - pluginPasses: Array>; parserOpts: BabelParserOptions; opts: Object; - dynamicImportTypes: Object; - dynamicImportIds: Object; - dynamicImports: Array; declarations: Object; - usedHelpers: Object; path: NodePath; ast: Object; scope: Scope; @@ -113,17 +89,12 @@ export default class File extends Store { code: string; shebang: string; - 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); - } + set(key: string, val) { + this._map.set(key, val); + } + + get(key: string): any { + return this._map.get(key); } getModuleName(): ?string { @@ -170,11 +141,9 @@ export default class File extends Store { } } + // TODO: Remove this before 7.x's official release. Leaving it in for now to + // prevent unnecessary breakage between beta versions. resolveModuleSource(source: string): string { - const resolveModuleSource = this.opts.resolveModuleSource; - if (resolveModuleSource) { - source = resolveModuleSource(source, this.opts.filename); - } return source; } @@ -191,11 +160,6 @@ export default class File extends Store { const declar = this.declarations[name]; if (declar) return declar; - if (!this.usedHelpers[name]) { - this.metadata.usedHelpers.push(name); - this.usedHelpers[name] = true; - } - const generator = this.get("helperGenerator"); const runtime = this.get("helpersNamespace"); if (generator) { @@ -343,7 +307,6 @@ export default class File extends Store { }).setContext(); this.scope = this.path.scope; this.ast = ast; - this.getMetadata(); } addAst(ast) { @@ -469,7 +432,7 @@ export default class File extends Store { makeResult({ code, map, ast, ignored }: BabelFileResult): BabelFileResult { const result = { - metadata: null, + metadata: this.metadata, options: this.opts, ignored: !!ignored, code: null, @@ -485,10 +448,6 @@ export default class File extends Store { 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/src/transformation/pipeline.js b/packages/babel-core/src/transformation/index.js similarity index 86% rename from packages/babel-core/src/transformation/pipeline.js rename to packages/babel-core/src/transformation/index.js index a0b1db373e..b6d3de6a06 100644 --- a/packages/babel-core/src/transformation/pipeline.js +++ b/packages/babel-core/src/transformation/index.js @@ -1,23 +1,10 @@ -/* global BabelFileResult, BabelFileMetadata */ +/* global BabelFileResult */ import fs from "fs"; import * as t from "babel-types"; import File from "./file"; import loadConfig from "../config"; -export function analyse( - code: string, - opts: Object = {}, - visitor?: Object, -): ?BabelFileMetadata { - opts.code = false; - if (visitor) { - opts.plugins = opts.plugins || []; - opts.plugins.push({ visitor }); - } - return transform(code, opts).metadata; -} - export function transform(code: string, opts?: Object): BabelFileResult { const config = loadConfig(opts); if (config === null) return null; diff --git a/packages/babel-core/src/transformation/plugin-pass.js b/packages/babel-core/src/transformation/plugin-pass.js index c0a6626cf5..edde9acd07 100644 --- a/packages/babel-core/src/transformation/plugin-pass.js +++ b/packages/babel-core/src/transformation/plugin-pass.js @@ -1,9 +1,8 @@ -import Store from "./store"; import File from "./file"; -export default class PluginPass extends Store { +export default class PluginPass { constructor(file: File, key: string, options: Object = {}) { - super(); + this._map = new Map(); this.key = key; this.file = file; @@ -14,6 +13,14 @@ export default class PluginPass extends Store { file: File; opts: Object; + set(key: string, val) { + this._map.set(key, val); + } + + get(key: string): any { + return this._map.get(key); + } + addHelper(...args) { return this.file.addHelper(...args); } diff --git a/packages/babel-core/src/transformation/store.js b/packages/babel-core/src/transformation/store.js deleted file mode 100644 index bcf5bc7e4b..0000000000 --- a/packages/babel-core/src/transformation/store.js +++ /dev/null @@ -1,15 +0,0 @@ -export default class Store { - constructor() { - this._map = new Map(); - } - - set(key: string, val) { - this._map.set(key, val); - } - - get(key: string): any { - if (this._map.has(key)) { - return this._map.get(key); - } - } -} diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 2be88b495a..b9c0f76d4d 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -103,38 +103,6 @@ describe("parser and generator options", function() { }); describe("api", function() { - it("analyze", function() { - assert.equal(babel.analyse("foobar;").marked.length, 0); - - assert.equal( - babel.analyse("foobar;", { - plugins: [ - new Plugin({ - visitor: { - Program: function(path) { - path.mark("category", "foobar"); - }, - }, - }), - ], - }).marked[0].message, - "foobar", - ); - - assert.equal( - babel.analyse( - "foobar;", - {}, - { - Program: function(path) { - path.mark("category", "foobar"); - }, - }, - ).marked[0].message, - "foobar", - ); - }); - it("exposes the resolvePlugin method", function() { assert.throws( () => babel.resolvePlugin("nonexistent-plugin"), @@ -466,195 +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('import localName from "./array";', { - resolveModuleSource: function() { - return "override-source"; - }, - }).then(function(result) { - assert.deepEqual(result.metadata.modules.imports, [ - { - source: "override-source", - imported: ["default"], - specifiers: [ - { - kind: "named", - imported: "default", - local: "localName", - }, - ], - }, - ]); - }), - - 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("", { @@ -780,23 +559,6 @@ describe("api", function() { }); }); - it("resolveModuleSource option", function() { - /* eslint-disable max-len */ - const actual = - 'import foo from "foo-import-default";\nimport "foo-import-bare";\nexport { foo } from "foo-export-named";'; - const expected = - 'import foo from "resolved/foo-import-default";\nimport "resolved/foo-import-bare";\nexport { foo } from "resolved/foo-export-named";'; - /* eslint-enable max-len */ - - return transformAsync(actual, { - resolveModuleSource: function(originalSource) { - return "resolved/" + originalSource; - }, - }).then(function(result) { - assert.equal(result.code.trim(), expected); - }); - }); - describe("buildExternalHelpers", function() { describe("smoke tests", function() { it("builds external helpers in global output type", function() { diff --git a/packages/babel-helper-module-imports/src/import-builder.js b/packages/babel-helper-module-imports/src/import-builder.js index bbde6fdef9..0eda1c8663 100644 --- a/packages/babel-helper-module-imports/src/import-builder.js +++ b/packages/babel-helper-module-imports/src/import-builder.js @@ -26,21 +26,17 @@ export default class ImportBuilder { } import() { - const importedSource = this._file.resolveModuleSource(this._importedSource); - this._statements.push( - t.importDeclaration([], t.stringLiteral(importedSource)), + t.importDeclaration([], t.stringLiteral(this._importedSource)), ); return this; } require() { - const importedSource = this._file.resolveModuleSource(this._importedSource); - this._statements.push( t.expressionStatement( t.callExpression(t.identifier("require"), [ - t.stringLiteral(importedSource), + t.stringLiteral(this._importedSource), ]), ), ); diff --git a/packages/babel-traverse/src/path/index.js b/packages/babel-traverse/src/path/index.js index 43725fc344..5c835cb1f0 100644 --- a/packages/babel-traverse/src/path/index.js +++ b/packages/babel-traverse/src/path/index.js @@ -126,14 +126,6 @@ export default class NodePath { traverse(this.node, visitor, this.scope, state, this); } - mark(type: string, message: string) { - this.hub.file.metadata.marked.push({ - type, - message, - loc: this.node.loc, - }); - } - set(key: string, node: Object) { t.validate(this.node, key, node); this.node[key] = node;