Remove unused module metadata collection.
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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<Object>)) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Object>)) {
|
||||
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<Object>)) {
|
||||
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();
|
||||
}
|
||||
@@ -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("", {
|
||||
|
||||
Reference in New Issue
Block a user