clean up file processing API
This commit is contained in:
parent
23bead9226
commit
32f19aff99
@ -482,32 +482,30 @@ export default class File {
|
||||
if (modFormatter.init && this.transformers["es6.modules"].canTransform()) {
|
||||
modFormatter.init();
|
||||
}
|
||||
this.populateModuleMetadata();
|
||||
this.log.debug("End module formatter init");
|
||||
}
|
||||
|
||||
populateModuleMetadata() {
|
||||
var modules = {};
|
||||
this.metadata.modules = modules;
|
||||
}
|
||||
|
||||
transform(dependencyGraph) {
|
||||
this.call("pre");
|
||||
for (var pass of (this.transformerStack: Array)) {
|
||||
pass.transform();
|
||||
}
|
||||
this.call("post");
|
||||
|
||||
return this.generate();
|
||||
}
|
||||
|
||||
wrap(code, callback) {
|
||||
code = code + "";
|
||||
|
||||
try {
|
||||
if (this.shouldIgnore()) {
|
||||
return {
|
||||
metadata: this.metadata,
|
||||
ignored: true,
|
||||
code: code,
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
}
|
||||
|
||||
callback();
|
||||
|
||||
return this.generate();
|
||||
return callback();
|
||||
} catch (err) {
|
||||
if (err._babel) {
|
||||
throw err;
|
||||
@ -540,11 +538,11 @@ export default class File {
|
||||
code = (code || "") + "";
|
||||
code = this.parseInputSourceMap(code);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
if (parseCode) {
|
||||
this.parseShebang();
|
||||
this.addAst(this.parse(this.code));
|
||||
}
|
||||
parseCode() {
|
||||
this.parseShebang();
|
||||
this.addAst(this.parse(this.code));
|
||||
}
|
||||
|
||||
shouldIgnore() {
|
||||
@ -581,28 +579,37 @@ export default class File {
|
||||
}
|
||||
}
|
||||
|
||||
generate(): {
|
||||
usedHelpers?: Array<string>;
|
||||
code: string;
|
||||
map?: Object;
|
||||
ast?: Object;
|
||||
} {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
makeResult({ code, map = null, ast, ignored }) {
|
||||
var result = {
|
||||
metadata: this.metadata,
|
||||
code: "",
|
||||
map: null,
|
||||
ast: null
|
||||
metadata: null,
|
||||
ignored: !!ignored,
|
||||
code: null,
|
||||
ast: null,
|
||||
map: map
|
||||
};
|
||||
|
||||
if (this.opts.metadataUsedHelpers) {
|
||||
if (this.opts.code) {
|
||||
result.code = code;
|
||||
}
|
||||
|
||||
if (this.opts.ast) {
|
||||
result.ast = ast;
|
||||
}
|
||||
|
||||
if (this.opts.metadata) {
|
||||
result.metadata = this.metadata;
|
||||
result.metadata.usedHelpers = Object.keys(this.usedHelpers);
|
||||
}
|
||||
|
||||
if (opts.ast) result.ast = ast;
|
||||
if (!opts.code) return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
generate() {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
var result = { ast };
|
||||
if (!opts.code) return this.makeResult(result);
|
||||
|
||||
this.log.debug("Generation start");
|
||||
|
||||
@ -629,6 +636,6 @@ export default class File {
|
||||
result.map = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
return this.makeResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,6 +139,12 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"metadata": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"ast": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
@ -179,6 +185,7 @@
|
||||
},
|
||||
|
||||
"metadataUsedHelpers": {
|
||||
"deprecated": "Not required anymore as this is enabled by default",
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"hidden": true
|
||||
|
||||
@ -69,44 +69,44 @@ var remapVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var importsVisitor = {
|
||||
ImportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, this.getBindingIdentifiers());
|
||||
var metadataVisitor = {
|
||||
ModuleDeclaration(node, parent, scope, formatter) {
|
||||
if (node.source) {
|
||||
node.source.value = formatter.file.resolveModuleSource(node.source.value);
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
var exportsVisitor = {
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalExports = true;
|
||||
ImportDeclaration(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, this.getBindingIdentifiers());
|
||||
},
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
var bindings = declar.getBindingIdentifiers()
|
||||
for (var name in bindings) {
|
||||
var binding = bindings[name];
|
||||
formatter._addExport(name, binding);
|
||||
}
|
||||
ExportDeclaration(node, parent, scope, formatter) {
|
||||
formatter.hasLocalExports = true;
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
var bindings = declar.getBindingIdentifiers()
|
||||
for (var name in bindings) {
|
||||
var binding = bindings[name];
|
||||
formatter._addExport(name, binding);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isExportNamedDeclaration() && node.specifiers) {
|
||||
for (var i = 0; i < node.specifiers.length; i++) {
|
||||
var specifier = node.specifiers[i];
|
||||
var local = specifier.local;
|
||||
if (!local) continue;
|
||||
if (this.isExportNamedDeclaration() && node.specifiers) {
|
||||
for (var i = 0; i < node.specifiers.length; i++) {
|
||||
var specifier = node.specifiers[i];
|
||||
var local = specifier.local;
|
||||
if (!local) continue;
|
||||
|
||||
formatter._addExport(local.name, specifier.exported);
|
||||
}
|
||||
formatter._addExport(local.name, specifier.exported);
|
||||
}
|
||||
}
|
||||
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
var onlyDefault = node.specifiers && node.specifiers.length === 1 && t.isSpecifierDefault(node.specifiers[0]);
|
||||
if (!onlyDefault) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
var onlyDefault = node.specifiers && node.specifiers.length === 1 && t.isSpecifierDefault(node.specifiers[0]);
|
||||
if (!onlyDefault) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -128,8 +128,7 @@ export default class DefaultFormatter {
|
||||
this.localExports = object();
|
||||
this.localImports = object();
|
||||
|
||||
this.getLocalExports();
|
||||
this.getLocalImports();
|
||||
this.getMetadata();
|
||||
}
|
||||
|
||||
isModuleType(node, type) {
|
||||
@ -145,12 +144,8 @@ export default class DefaultFormatter {
|
||||
return (t.isExportDefaultDeclaration(node) || t.isSpecifierDefault(node)) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
getLocalExports() {
|
||||
this.file.path.traverse(exportsVisitor, this);
|
||||
}
|
||||
|
||||
getLocalImports() {
|
||||
this.file.path.traverse(importsVisitor, this);
|
||||
getMetadata() {
|
||||
this.file.path.traverse(metadataVisitor, this);
|
||||
}
|
||||
|
||||
remapAssignments() {
|
||||
|
||||
@ -57,10 +57,21 @@ export default class TransformerPipeline {
|
||||
return true;
|
||||
}
|
||||
|
||||
pretransform(code: string, opts?: Object) {
|
||||
var file = new File(opts, this);
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file;
|
||||
});
|
||||
}
|
||||
|
||||
transform(code: string, opts?: Object) {
|
||||
var file = new File(opts, this);
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code, true);
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
@ -71,6 +82,7 @@ export default class TransformerPipeline {
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code);
|
||||
file.addAst(ast);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user