more updates to estree and new acorn
This commit is contained in:
parent
df7524d909
commit
6a1b33b443
7
Makefile
7
Makefile
@ -8,7 +8,7 @@ BABEL_CMD = node_modules/babel/bin/babel
|
||||
|
||||
export NODE_ENV = test
|
||||
|
||||
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core build-core-test
|
||||
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser test-parser publish build bootstrap publish-core publish-runtime build-core watch-core build-core-test
|
||||
|
||||
build-core:
|
||||
node $(BABEL_CMD) src/babel --out-dir lib/babel --copy-files
|
||||
@ -42,7 +42,7 @@ clean:
|
||||
test-clean:
|
||||
rm -rf test/tmp
|
||||
|
||||
test:
|
||||
test: test-parser
|
||||
node $(MOCHA_CMD)
|
||||
make test-clean
|
||||
|
||||
@ -55,6 +55,9 @@ test-cov:
|
||||
make build-core-test
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
|
||||
test-parser:
|
||||
node vendor/acorn/test/run.js
|
||||
|
||||
test-travis: bootstrap build test
|
||||
|
||||
test-browser:
|
||||
|
||||
@ -17,7 +17,6 @@ program.option("-p, --print [code]", "Evaluate script and print result");
|
||||
program.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
|
||||
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]");
|
||||
program.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
program.option("-g, --playground", "Enable playground support");
|
||||
program.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
program.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
program.option("-o, --optional [optional]", "List of optional transformers to enable", util.list);
|
||||
@ -32,7 +31,6 @@ program.parse(process.argv);
|
||||
babel.register({
|
||||
experimental: program.experimental,
|
||||
extensions: program.extensions,
|
||||
playground: program.playground,
|
||||
blacklist: program.blacklist,
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
@ -47,8 +45,7 @@ var _eval = function (code, filename) {
|
||||
blacklist: ["useStrict"].concat(program.blacklist || []),
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
experimental: program.experimental,
|
||||
playground: program.playground
|
||||
experimental: program.experimental
|
||||
}).code;
|
||||
|
||||
return vm.runInThisContext(code, {
|
||||
|
||||
@ -6,7 +6,7 @@ import fs from "fs";
|
||||
export { util };
|
||||
export { canCompile } from "../util";
|
||||
|
||||
export { default as acorn } from "../../acorn";
|
||||
export { default as acorn } from "../../../vendor/acorn";
|
||||
export { default as Transformer } from "../transformation/transformer";
|
||||
export { default as transform } from "../transformation";
|
||||
export { default as traverse } from "../traversal";
|
||||
|
||||
@ -57,6 +57,10 @@ export function ThisExpression() {
|
||||
this.push("this");
|
||||
}
|
||||
|
||||
export function SuperExpression() {
|
||||
this.push("super");
|
||||
}
|
||||
|
||||
export function CallExpression(node, print) {
|
||||
print(node.callee);
|
||||
|
||||
|
||||
@ -21,11 +21,13 @@ export function _method(node, print) {
|
||||
var kind = node.kind;
|
||||
var key = node.key;
|
||||
|
||||
if (!kind || kind === "init") {
|
||||
if (kind === "method" || kind === "init") {
|
||||
if (value.generator) {
|
||||
this.push("*");
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
if (kind === "get" || kind === "set") {
|
||||
this.push(kind + " ");
|
||||
}
|
||||
|
||||
|
||||
@ -2,13 +2,17 @@ import each from "lodash/collection/each";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function ImportSpecifier(node, print) {
|
||||
if (t.isSpecifierDefault(node)) {
|
||||
print(t.getSpecifierName(node));
|
||||
} else {
|
||||
return ExportSpecifier.apply(this, arguments);
|
||||
print(node.local);
|
||||
if (node.imported && node.local !== node.imported) {
|
||||
this.push(" as ");
|
||||
print(node.imported);
|
||||
}
|
||||
}
|
||||
|
||||
export function ImportDefaultSpecifier(node, print) {
|
||||
print(node.local);
|
||||
}
|
||||
|
||||
export function ExportSpecifier(node, print) {
|
||||
print(node.local);
|
||||
if (node.exported && node.local !== node.exported) {
|
||||
@ -17,8 +21,8 @@ export function ExportSpecifier(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, source) {
|
||||
this.push("export * from");
|
||||
export function ExportAllDeclaration(node, print) {
|
||||
this.push("export * from ");
|
||||
print(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
@ -39,9 +43,6 @@ function ExportDeclaration(node, print) {
|
||||
if (node.declaration) {
|
||||
print(node.declaration);
|
||||
if (t.isStatement(node.declaration)) return;
|
||||
} else {
|
||||
if (specifiers.length === 1 && t.isExportBatchSpecifier(specifiers[0])) {
|
||||
print(specifiers[0]);
|
||||
} else {
|
||||
this.push("{");
|
||||
if (specifiers.length) {
|
||||
@ -50,7 +51,6 @@ function ExportDeclaration(node, print) {
|
||||
this.space();
|
||||
}
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
if (node.source) {
|
||||
this.push(" from ");
|
||||
@ -81,7 +81,7 @@ export function ImportDeclaration(node, print) {
|
||||
|
||||
var isDefault = t.isSpecifierDefault(spec);
|
||||
|
||||
if (!isDefault && spec.type !== "ImportBatchSpecifier" && !foundImportSpecifier) {
|
||||
if (!isDefault && spec.type !== "ImportNamespaceSpecifier" && !foundImportSpecifier) {
|
||||
foundImportSpecifier = true;
|
||||
this.push("{ ");
|
||||
}
|
||||
@ -100,7 +100,7 @@ export function ImportDeclaration(node, print) {
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function ImportBatchSpecifier(node, print) {
|
||||
export function ImportNamespaceSpecifier(node, print) {
|
||||
this.push("* as ");
|
||||
print(node.name);
|
||||
}
|
||||
|
||||
@ -212,7 +212,7 @@ export function PrivateDeclaration(node, print) {
|
||||
|
||||
export function VariableDeclarator(node, print) {
|
||||
print(node.id);
|
||||
print(node.id.typeAnnotation);
|
||||
//print(node.id.typeAnnotation);
|
||||
if (node.init) {
|
||||
this.space();
|
||||
this.push("=");
|
||||
|
||||
@ -81,7 +81,7 @@ export default class Whitespace {
|
||||
}
|
||||
}
|
||||
|
||||
if (endToken && endToken.type.type === "eof") {
|
||||
if (endToken && endToken.type.label === "eof") {
|
||||
return 1;
|
||||
} else {
|
||||
var lines = this.getNewlinesBetween(startToken, endToken);
|
||||
|
||||
@ -1,25 +1,30 @@
|
||||
import normalizeAst from "./normalize-ast";
|
||||
import estraverse from "estraverse";
|
||||
import codeFrame from "./code-frame";
|
||||
import acorn from "../../acorn";
|
||||
import acorn from "../../../vendor/acorn";
|
||||
|
||||
export default function (opts, code, callback) {
|
||||
try {
|
||||
var comments = [];
|
||||
var tokens = [];
|
||||
|
||||
var ast = acorn.parse(code, {
|
||||
var parseOpts = {
|
||||
allowImportExportEverywhere: opts.looseModules,
|
||||
allowReturnOutsideFunction: opts.looseModules,
|
||||
transformers: opts.transformers || {},
|
||||
ecmaVersion: 6,
|
||||
strictMode: opts.strictMode,
|
||||
onComment: comments,
|
||||
locations: true,
|
||||
plugins: opts.plugins || [],
|
||||
features: opts.features || {},
|
||||
plugins: opts.plugins || {},
|
||||
onToken: tokens,
|
||||
ranges: true
|
||||
});
|
||||
};
|
||||
|
||||
parseOpts.plugins.flow = true;
|
||||
parseOpts.plugins.jsx = true;
|
||||
|
||||
var ast = acorn.parse(code, parseOpts);
|
||||
|
||||
estraverse.attachComments(ast, comments, tokens);
|
||||
|
||||
|
||||
@ -28,4 +28,26 @@ def("RestElement")
|
||||
.build("argument")
|
||||
.field("argument", def("expression"));
|
||||
|
||||
def("ExportDefaultDeclaration")
|
||||
.bases("Declaration")
|
||||
.build("declaration")
|
||||
.field("declaration", or(
|
||||
def("Declaration"),
|
||||
def("Expression"),
|
||||
null
|
||||
));
|
||||
|
||||
def("ExportNamedDeclaration")
|
||||
.bases("Declaration")
|
||||
.build("declaration")
|
||||
.field("declaration", or(
|
||||
def("Declaration"),
|
||||
def("Expression"),
|
||||
null
|
||||
))
|
||||
.field("specifiers", [or(
|
||||
def("ExportSpecifier")
|
||||
)])
|
||||
.field("source", or(def("ModuleSpecifier"), null));
|
||||
|
||||
types.finalize();
|
||||
|
||||
@ -21,9 +21,6 @@ import path from "path";
|
||||
import each from "lodash/collection/each";
|
||||
import * as t from "../../types";
|
||||
|
||||
import "../../parser/jsx";
|
||||
import "../../parser/flow";
|
||||
|
||||
var checkTransformerVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
checkNode(state.stack, node, scope);
|
||||
@ -184,7 +181,7 @@ export default class File {
|
||||
each(transform.transformers, function (transformer, key) {
|
||||
var pass = transformers[key] = transformer.buildPass(file);
|
||||
|
||||
if (pass.canTransform) {
|
||||
if (pass.canTransform()) {
|
||||
stack.push(pass);
|
||||
|
||||
if (transformer.metadata.secondPass) {
|
||||
@ -270,14 +267,14 @@ export default class File {
|
||||
if (!id) {
|
||||
id = this.dynamicImportIds[name] = this.scope.generateUidIdentifier(name);
|
||||
|
||||
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
|
||||
var specifiers = [t.importDefaultSpecifier(id)];
|
||||
var declar = t.importDeclaration(specifiers, t.literal(source));
|
||||
declar._blockHoist = 3;
|
||||
|
||||
this.dynamicImported.push(declar);
|
||||
if (noDefault) this.dynamicImportedNoDefault.push(declar);
|
||||
|
||||
if (this.transformers["es6.modules"].canTransform) {
|
||||
if (this.transformers["es6.modules"].canTransform()) {
|
||||
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
|
||||
} else {
|
||||
this.dynamicImports.push(declar);
|
||||
@ -390,24 +387,14 @@ export default class File {
|
||||
plugins: {}
|
||||
};
|
||||
|
||||
var transformers = parseOpts.transformers = {};
|
||||
var features = parseOpts.features = {};
|
||||
for (var key in this.transformers) {
|
||||
var transformer = this.transformers[key];
|
||||
|
||||
transformers[key] = transformer.canTransform;
|
||||
|
||||
if (transformer.parser) {
|
||||
parseOpts.plugins[key] = true;
|
||||
}
|
||||
features[key] = transformer.canTransform();
|
||||
}
|
||||
|
||||
parseOpts.looseModules = this.isLoose("es6.modules");
|
||||
parseOpts.strictMode = transformers.strict;
|
||||
|
||||
if (!opts.standardOnly) {
|
||||
parseOpts.plugins.jsx = true;
|
||||
parseOpts.plugins.flow = true;
|
||||
}
|
||||
parseOpts.strictMode = features.strict;
|
||||
|
||||
//
|
||||
|
||||
@ -441,7 +428,7 @@ export default class File {
|
||||
this.lastStatements = t.getLastStatements(ast.program);
|
||||
|
||||
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
if (modFormatter.init && this.transformers["es6.modules"].canTransform) {
|
||||
if (modFormatter.init && this.transformers["es6.modules"].canTransform()) {
|
||||
modFormatter.init();
|
||||
}
|
||||
|
||||
|
||||
@ -252,8 +252,8 @@ export default function (exports, opts) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.ExportDeclaration = function (node, parent, scope, file) {
|
||||
if (node.default && react.isCreateClass(node.declaration)) {
|
||||
exports.ExportDefaultDeclaration = function (node, parent, scope, file) {
|
||||
if (react.isCreateClass(node.declaration)) {
|
||||
addDisplayName(file.opts.basename, node.declaration);
|
||||
}
|
||||
};
|
||||
|
||||
@ -5,16 +5,12 @@ import * as t from "../../types";
|
||||
|
||||
|
||||
function isIllegalBareSuper(node, parent) {
|
||||
if (!isSuper(node, parent)) return false;
|
||||
if (!t.isSuperExpression(node)) return false;
|
||||
if (t.isMemberExpression(parent, { computed: false })) return false;
|
||||
if (t.isCallExpression(parent, { callee: node })) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isSuper(node, parent) {
|
||||
return t.isIdentifier(node, { name: "super" }) && t.isReferenced(node, parent);
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
var topLevel = state.topLevel;
|
||||
@ -193,13 +189,13 @@ export default class ReplaceSupers {
|
||||
|
||||
looseHandle(path: TraversalPath, getThisReference: Function) {
|
||||
var node = path.node;
|
||||
if (path.isIdentifier({ name: "super" })) {
|
||||
if (path.isSuperExpression()) {
|
||||
this.hasSuper = true;
|
||||
return this.getLooseSuperProperty(node, path.parent);
|
||||
} else if (path.isCallExpression()) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
if (!t.isSuperExpression(callee.object)) return;
|
||||
|
||||
// super.test(); -> objectRef.prototype.MethodName.call(this);
|
||||
this.hasSuper = true;
|
||||
@ -228,7 +224,7 @@ export default class ReplaceSupers {
|
||||
|
||||
if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (isSuper(callee, node)) {
|
||||
if (t.isSuperExpression(callee)) {
|
||||
// super(); -> _get(Object.getPrototypeOf(objectRef), "MethodName", this).call(this);
|
||||
property = methodNode.key;
|
||||
computed = methodNode.computed;
|
||||
@ -241,17 +237,17 @@ export default class ReplaceSupers {
|
||||
var methodName = methodNode.key.name || "METHOD_NAME";
|
||||
throw this.file.errorWithNode(node, messages.get("classesIllegalSuperCall", methodName));
|
||||
}
|
||||
} else if (t.isMemberExpression(callee) && isSuper(callee.object, callee)) {
|
||||
} else if (t.isMemberExpression(callee) && t.isSuperExpression(callee.object)) {
|
||||
// super.test(); -> _get(Object.getPrototypeOf(objectRef.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
args = node.arguments;
|
||||
}
|
||||
} else if (t.isMemberExpression(node) && isSuper(node.object, node)) {
|
||||
} else if (t.isMemberExpression(node) && t.isSuperExpression(node.object)) {
|
||||
// super.name; -> _get(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
} else if (t.isAssignmentExpression(node) && isSuper(node.left.object, node.left) && methodNode.kind === "set") {
|
||||
} else if (t.isAssignmentExpression(node) && t.isSuperExpression(node.left.object) && methodNode.kind === "set") {
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
this.hasSuper = true;
|
||||
return this.setSuperProperty(node.left.property, node.right, node.left.computed, getThisReference());
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import * as messages from "../../messages";
|
||||
import traverse from "../../traversal";
|
||||
import extend from "lodash/object/extend";
|
||||
import object from "../../helpers/object";
|
||||
import * as util from "../../util";
|
||||
@ -51,17 +52,17 @@ var importsVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var exportsVisitor = {
|
||||
var exportsVisitor = traverse.explode({
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
var declar = this.get("declaration");
|
||||
formatter.hasLocalImports = true;
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
extend(formatter.localExports, declar.getBindingIdentifiers());
|
||||
}
|
||||
|
||||
if (!node.default) {
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
|
||||
@ -70,7 +71,7 @@ var exportsVisitor = {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
export default class DefaultFormatter {
|
||||
constructor(file) {
|
||||
@ -94,15 +95,17 @@ export default class DefaultFormatter {
|
||||
}
|
||||
|
||||
doDefaultExportInterop(node) {
|
||||
return node.default && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
return t.isExportDefaultDeclaration(node) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
bumpImportOccurences(node) {
|
||||
var source = node.source.value;
|
||||
var occurs = this.localImportOccurences;
|
||||
occurs[source] ||= 0;
|
||||
if (node.specifiers) {
|
||||
occurs[source] += node.specifiers.length;
|
||||
}
|
||||
}
|
||||
|
||||
getLocalExports() {
|
||||
this.file.path.traverse(exportsVisitor, this);
|
||||
@ -211,31 +214,29 @@ export default class DefaultFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
exportAllDeclaration(node, nodes) {
|
||||
var nodes = [];
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
nodes.push(this.buildExportsWildcard(ref, node));
|
||||
return nodes;
|
||||
}
|
||||
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (node.source) {
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(this.buildExportsWildcard(ref, node));
|
||||
} else {
|
||||
if (t.isSpecifierDefault(specifier) && !this.noInteropRequireExport) {
|
||||
// importing a default so we need to normalize it
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
} else {
|
||||
ref = t.memberExpression(ref, t.getSpecifierId(specifier));
|
||||
ref = t.memberExpression(ref, specifier.local);
|
||||
}
|
||||
|
||||
// export { foo } from "test";
|
||||
nodes.push(this.buildExportsAssignment(
|
||||
node.local,
|
||||
ref,
|
||||
node
|
||||
));
|
||||
}
|
||||
nodes.push(this.buildExportsAssignment(specifier.exported, ref, node));
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(this.buildExportsAssignment(specifier.local, specifier.exported, node));
|
||||
nodes.push(this.buildExportsAssignment(specifier.exported, specifier.local, node));
|
||||
}
|
||||
}
|
||||
|
||||
@ -259,7 +260,7 @@ export default class DefaultFormatter {
|
||||
|
||||
var id = declar.id;
|
||||
|
||||
if (node.default) {
|
||||
if (t.isExportDefaultDeclaration(node)) {
|
||||
id = t.identifier("default");
|
||||
}
|
||||
|
||||
|
||||
@ -74,18 +74,20 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
if (includes(this.file.dynamicImportedNoDefault, node)) {
|
||||
// Prevent unnecessary renaming of dynamic imports.
|
||||
this.ids[node.source.value] = ref;
|
||||
} else if (t.isImportBatchSpecifier(specifier)) {
|
||||
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
} else if (!includes(this.file.dynamicImported, node) && t.isSpecifierDefault(specifier) && !this.noInteropRequireImport) {
|
||||
// import foo from "foo";
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
} else {
|
||||
// import {foo} from "foo";
|
||||
ref = t.memberExpression(ref, t.getSpecifierId(specifier), false);
|
||||
// import { foo } from "foo";
|
||||
var imported = specifier.imported;
|
||||
if (t.isSpecifierDefault(specifier)) imported = t.identifier("default");
|
||||
ref = t.memberExpression(ref, imported);
|
||||
}
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(node.local, ref)
|
||||
t.variableDeclarator(specifier.local, ref)
|
||||
]));
|
||||
}
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
};
|
||||
|
||||
importSpecifier(specifier, node, nodes) {
|
||||
var variableName = node.local;
|
||||
var variableName = specifier.local;
|
||||
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
|
||||
@ -33,8 +33,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
}
|
||||
nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)]));
|
||||
} else {
|
||||
if (specifier.type === "ImportBatchSpecifier") {
|
||||
|
||||
if (t.isImportNamespaceSpecifier(specifier)) {
|
||||
if (!this.noInteropRequireImport) {
|
||||
ref = t.callExpression(this.file.addHelper("interop-require-wildcard"), [ref]);
|
||||
}
|
||||
@ -48,7 +47,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
variableName,
|
||||
t.memberExpression(ref, t.getSpecifierId(specifier))
|
||||
t.memberExpression(ref, specifier.imported)
|
||||
)
|
||||
]));
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ export default class IgnoreFormatter {
|
||||
if (declar) nodes.push(t.inherits(declar, node));
|
||||
}
|
||||
|
||||
exportAllDeclaration() {}
|
||||
importDeclaration() {}
|
||||
importSpecifier() {}
|
||||
exportSpecifier() {}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
{
|
||||
"blacklist": ["useStrict", "es6.blockScoping", "regenerator"],
|
||||
"loose": ["es6.modules"]
|
||||
"loose": ["es6.modules"],
|
||||
"breakConfig": true
|
||||
}
|
||||
|
||||
@ -12,12 +12,10 @@ export default class TransformerPass {
|
||||
this.shouldRun = !transformer.check;
|
||||
this.handlers = transformer.handlers;
|
||||
this.file = file;
|
||||
|
||||
this.canTransform = this._canTransform();
|
||||
this.ran = false;
|
||||
}
|
||||
|
||||
_canTransform(): boolean {
|
||||
canTransform(): boolean {
|
||||
var transformer = this.transformer;
|
||||
|
||||
var opts = this.file.opts;
|
||||
@ -34,9 +32,6 @@ export default class TransformerPass {
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length) return includes(whitelist, key);
|
||||
|
||||
// react
|
||||
if (transformer.metadata.react && opts.react) return true;
|
||||
|
||||
// experimental
|
||||
if (transformer.metadata.experimental && opts.experimental) return true;
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ import isFunction from "lodash/lang/isFunction";
|
||||
import traverse from "../traversal";
|
||||
import isObject from "lodash/lang/isObject";
|
||||
import assign from "lodash/object/assign";
|
||||
import acorn from "../../../../acorn";
|
||||
import acorn from "../../../vendor/acorn";
|
||||
import File from "./file";
|
||||
import each from "lodash/collection/each";
|
||||
|
||||
@ -33,10 +33,6 @@ export default class Transformer {
|
||||
this.handlers = this.normalize(transformer);
|
||||
this.opts ||= {};
|
||||
this.key = transformerKey;
|
||||
|
||||
if (this.parser) {
|
||||
acorn.plugins[key] = this.parser(acorn.Parser.prototype, acorn.tokTypes);
|
||||
}
|
||||
}
|
||||
|
||||
normalize(transformer: Object): Object {
|
||||
|
||||
@ -44,7 +44,7 @@ export function check(node) {
|
||||
export function VariableDeclaration(node, parent, scope, file) {
|
||||
if (!isLet(node, parent)) return;
|
||||
|
||||
if (isLetInitable(node) && file.transformers["es6.blockScopingTDZ"].canTransform) {
|
||||
if (isLetInitable(node) && file.transformers["es6.blockScopingTDZ"].canTransform()) {
|
||||
var nodes = [node];
|
||||
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
|
||||
@ -33,7 +33,7 @@ var verifyConstructorVisitor = traverse.explode({
|
||||
|
||||
CallExpression: {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.get("callee").isIdentifier({ name: "super" })) {
|
||||
if (this.get("callee").isSuperExpression()) {
|
||||
state.hasBareSuper = true;
|
||||
|
||||
if (!state.hasSuper) {
|
||||
@ -275,7 +275,7 @@ class ClassTransformer {
|
||||
|
||||
var kind = node.kind;
|
||||
|
||||
if (kind === "") {
|
||||
if (kind === "method") {
|
||||
nameMethod.property(node, this.file, this.scope);
|
||||
|
||||
if (this.isLoose) {
|
||||
|
||||
@ -25,7 +25,15 @@ export function ImportDeclaration(node, parent, scope, file) {
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, parent, scope, file) {
|
||||
return file.moduleFormatter.exportAllDeclaration(node, parent);
|
||||
var nodes = [];
|
||||
file.moduleFormatter.exportAllDeclaration(node, nodes, parent);
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export function ExportDefaultDeclaration(node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
file.moduleFormatter.exportDeclaration(node, nodes, parent);
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export function ExportNamedDeclaration(node, parent, scope, file) {
|
||||
@ -33,7 +41,6 @@ export function ExportNamedDeclaration(node, parent, scope, file) {
|
||||
if (this.get("declaration").isTypeAlias()) return;
|
||||
|
||||
var nodes = [];
|
||||
var i;
|
||||
|
||||
if (node.declaration) {
|
||||
// make sure variable exports have an initializer
|
||||
@ -45,13 +52,13 @@ export function ExportNamedDeclaration(node, parent, scope, file) {
|
||||
|
||||
file.moduleFormatter.exportDeclaration(node, nodes, parent);
|
||||
} else if (node.specifiers) {
|
||||
for (i = 0; i < node.specifiers.length; i++) {
|
||||
for (let i = 0; i < node.specifiers.length; i++) {
|
||||
file.moduleFormatter.exportSpecifier(node.specifiers[i], node, nodes, parent);
|
||||
}
|
||||
}
|
||||
|
||||
if (node._blockHoist) {
|
||||
for (i = 0; i < nodes.length; i++) {
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
nodes[i]._blockHoist = node._blockHoist;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
import ReplaceSupers from "../../helpers/replace-supers";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isIdentifier(node, { name: "super" });
|
||||
}
|
||||
export var check = t.isSuperExpression;
|
||||
|
||||
function Property(node, scope, getObjectRef, file) {
|
||||
if (!node.method) return;
|
||||
|
||||
@ -62,7 +62,7 @@ exports.Function = function (node, parent, scope, file) {
|
||||
param.traverse(iifeVisitor, state);
|
||||
}
|
||||
|
||||
if (file.transformers["es6.blockScopingTDZ"].canTransform && param.isIdentifier()) {
|
||||
if (file.transformers["es6.blockScopingTDZ"].canTransform() && param.isIdentifier()) {
|
||||
pushDefNode(param.node, t.identifier("undefined"), i);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
export var metadata = {
|
||||
experimental: true,
|
||||
optional: true
|
||||
};
|
||||
|
||||
export function check() {
|
||||
return false;
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
export default {
|
||||
"es7.asyncFunctions": require("./es7/async-functions"),
|
||||
|
||||
strict: require("./other/strict"),
|
||||
|
||||
_validation: require("./internal/validation"),
|
||||
|
||||
@ -5,7 +5,7 @@ export function Program(program, parent, scope, file) {
|
||||
program.body = file.dynamicImports.concat(program.body);
|
||||
});
|
||||
|
||||
if (!file.transformers["es6.modules"].canTransform) return;
|
||||
if (!file.transformers["es6.modules"].canTransform()) return;
|
||||
|
||||
if (file.moduleFormatter.transform) {
|
||||
file.moduleFormatter.transform(program);
|
||||
|
||||
@ -29,8 +29,8 @@ export function ExportDefaultDeclaration(node, parent, scope) {
|
||||
|
||||
if (t.isClassDeclaration(declar)) {
|
||||
// export default class Foo {};
|
||||
this.node = [getDeclar(), node];
|
||||
node.declaration = declar.id;
|
||||
return [getDeclar(), node];
|
||||
} else if (t.isClassExpression(declar)) {
|
||||
// export default class {};
|
||||
var temp = scope.generateUidIdentifier("default");
|
||||
@ -59,12 +59,12 @@ export function ExportNamedDeclaration(node, parent, scope) {
|
||||
|
||||
if (t.isClassDeclaration(declar)) {
|
||||
// export class Foo {}
|
||||
node.specifiers = [t.importSpecifier(declar.id, declar.id)];
|
||||
node.specifiers = [t.exportSpecifier(declar.id, declar.id)];
|
||||
node.declaration = null;
|
||||
return [getDeclar(), node];
|
||||
} else if (t.isFunctionDeclaration(declar)) {
|
||||
// export function Foo() {}
|
||||
node.specifiers = [t.importSpecifier(declar.id, declar.id)];
|
||||
node.specifiers = [t.exportSpecifier(declar.id, declar.id)];
|
||||
node.declaration = null;
|
||||
node._blockHoist = 2;
|
||||
return [getDeclar(), node];
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function Program(program, parent, scope, file) {
|
||||
if (file.transformers.strict.canTransform) {
|
||||
if (file.transformers.strict.canTransform()) {
|
||||
var directive = file.get("existingStrictDirective");
|
||||
|
||||
if (!directive) {
|
||||
|
||||
@ -2,7 +2,7 @@ import remapAsyncToGenerator from "../../helpers/remap-async-to-generator";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function manipulateOptions(opts) {
|
||||
opts.experimental = true;
|
||||
opts.optional.push("es7.asyncFunctions");
|
||||
opts.blacklist.push("regenerator");
|
||||
}
|
||||
|
||||
|
||||
@ -199,7 +199,7 @@ export default class Scope {
|
||||
*/
|
||||
|
||||
generateTempBasedOnNode(node: Object): ?Object {
|
||||
if (t.isThisExpression(node)) {
|
||||
if (t.isThisExpression(node) || t.isSuperExpression(node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@ -67,6 +67,7 @@
|
||||
"SequenceExpression": ["Expression"],
|
||||
"TaggedTemplateExpression": ["Expression"],
|
||||
"ThisExpression": ["Expression"],
|
||||
"SuperExpression": ["Expression"],
|
||||
"UpdateExpression": ["Expression"],
|
||||
"VirtualPropertyExpression": ["Expression"],
|
||||
"JSXEmptyExpression": ["Expression"],
|
||||
|
||||
@ -80,8 +80,8 @@
|
||||
},
|
||||
|
||||
"ImportSpecifier": {
|
||||
"id": null,
|
||||
"name": null
|
||||
"local": null,
|
||||
"imported": null
|
||||
},
|
||||
|
||||
"LabeledStatement": {
|
||||
|
||||
@ -38,6 +38,7 @@ getBindingIdentifiers.keys = {
|
||||
AssignmentExpression: ["left"],
|
||||
ImportSpecifier: ["local"],
|
||||
ImportNamespaceSpecifier: ["local"],
|
||||
ImportDefaultSpecifier: ["local"],
|
||||
VariableDeclarator: ["id"],
|
||||
FunctionDeclaration: ["id"],
|
||||
FunctionExpression: ["id"],
|
||||
|
||||
@ -7,10 +7,11 @@ import * as t from "./index";
|
||||
*/
|
||||
|
||||
export function isReferenced(node: Object, parent: Object): boolean {
|
||||
switch (parent.type) {
|
||||
// yes: PARENT[NODE]
|
||||
// yes: NODE.child
|
||||
// no: parent.CHILD
|
||||
if (t.isMemberExpression(parent)) {
|
||||
case "MemberExpression":
|
||||
if (parent.property === node && parent.computed) {
|
||||
return true;
|
||||
} else if (parent.object === node) {
|
||||
@ -18,90 +19,77 @@ export function isReferenced(node: Object, parent: Object): boolean {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// yes: { [NODE]: "" }
|
||||
// no: { NODE: "" }
|
||||
if (t.isProperty(parent) && parent.key === node) {
|
||||
case "Property":
|
||||
if (parent.key === node) {
|
||||
return parent.computed;
|
||||
}
|
||||
|
||||
// no: var NODE = init;
|
||||
// yes: var id = NODE;
|
||||
if (t.isVariableDeclarator(parent)) {
|
||||
case "VariableDeclarator":
|
||||
return parent.id !== node;
|
||||
}
|
||||
|
||||
// no: function NODE() {}
|
||||
// no: function foo(NODE) {}
|
||||
if (t.isFunction(parent)) {
|
||||
case "ArrowFunctionExpression":
|
||||
case "FunctionDeclaration":
|
||||
case "FunctionExpression":
|
||||
for (var i = 0; i < parent.params.length; i++) {
|
||||
var param = parent.params[i];
|
||||
if (param === node) return false;
|
||||
}
|
||||
|
||||
return parent.id !== node;
|
||||
}
|
||||
|
||||
// no: export { foo as NODE };
|
||||
if (t.isExportSpecifier(parent, { name: node })) {
|
||||
return false;
|
||||
}
|
||||
case "ExportSpecifier":
|
||||
return parent.exported !== node;
|
||||
|
||||
// no: import { NODE as foo } from "foo";
|
||||
if (t.isImportSpecifier(parent, { id: node })) {
|
||||
return false;
|
||||
}
|
||||
case "ImportSpecifier":
|
||||
return parent.imported !== node;
|
||||
|
||||
// no: class NODE {}
|
||||
if (t.isClass(parent)) {
|
||||
case "ClassDeclaration":
|
||||
case "ClassExpression":
|
||||
return parent.id !== node;
|
||||
}
|
||||
|
||||
// yes: class { [NODE](){} }
|
||||
if (t.isMethodDefinition(parent)) {
|
||||
case "MethodDefinition":
|
||||
return parent.key === node && parent.computed;
|
||||
}
|
||||
|
||||
// no: NODE: for (;;) {}
|
||||
if (t.isLabeledStatement(parent)) {
|
||||
case "LabeledStatement":
|
||||
return false;
|
||||
}
|
||||
|
||||
// no: try {} catch (NODE) {}
|
||||
if (t.isCatchClause(parent)) {
|
||||
case "CatchClause":
|
||||
return parent.param !== node;
|
||||
}
|
||||
|
||||
// no: function foo(...NODE) {}
|
||||
if (t.isRestElement(parent)) {
|
||||
case "RestElement":
|
||||
return false;
|
||||
}
|
||||
|
||||
// no: [NODE = foo] = [];
|
||||
// yes: [foo = NODE] = [];
|
||||
if (t.isAssignmentPattern(parent)) {
|
||||
case "AssignmentPattern":
|
||||
return parent.right === node;
|
||||
}
|
||||
|
||||
// no: [NODE] = [];
|
||||
// no: ({ NODE }) = [];
|
||||
if (t.isPattern(parent)) {
|
||||
case "ObjectPattern":
|
||||
case "ArrayPattern":
|
||||
return false;
|
||||
}
|
||||
|
||||
// no: import NODE from "bar";
|
||||
if (t.isImportSpecifier(parent)) {
|
||||
case "ImportSpecifier":
|
||||
return false;
|
||||
}
|
||||
|
||||
// no: import * as NODE from "foo";
|
||||
if (t.isImportBatchSpecifier(parent)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// no: class Foo { private NODE; }
|
||||
if (t.isPrivateDeclaration(parent)) {
|
||||
case "ImportNamespaceSpecifier":
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -154,7 +142,7 @@ export function isVar(node: Object): boolean {
|
||||
*/
|
||||
|
||||
export function isSpecifierDefault(specifier: Object): boolean {
|
||||
return specifier.default || t.isIdentifier(specifier.id) && specifier.id.name === "default";
|
||||
return t.isImportDefaultSpecifier(specifier) || t.isIdentifier(specifier.imported || specifier.exported, { name: "default" });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -22,7 +22,6 @@
|
||||
"DebuggerStatement": [],
|
||||
"DoWhileStatement": ["body", "test"],
|
||||
"EmptyStatement": [],
|
||||
"ExportBatchSpecifier": [],
|
||||
"ExportAllDeclaration": ["source"],
|
||||
"ExportDefaultDeclaration": ["declaration"],
|
||||
"ExportNamedDeclaration": ["declaration", "specifiers", "source"],
|
||||
@ -36,6 +35,7 @@
|
||||
"FunctionExpression": ["id", "params", "defaults", "rest", "body", "returnType", "typeParameters"],
|
||||
"Identifier": ["typeAnnotation"],
|
||||
"IfStatement": ["test", "consequent", "alternate"],
|
||||
"ImportDefaultSpecifier": ["local"],
|
||||
"ImportNamespaceSpecifier": ["local"],
|
||||
"ImportDeclaration": ["specifiers", "source"],
|
||||
"ImportSpecifier": ["imported", "local"],
|
||||
@ -55,6 +55,7 @@
|
||||
"SequenceExpression": ["expressions"],
|
||||
"SpreadElement": ["argument"],
|
||||
"SpreadProperty": ["argument"],
|
||||
"SuperExpression": [],
|
||||
"SwitchCase": ["test", "consequent"],
|
||||
"SwitchStatement": ["discriminant", "cases"],
|
||||
"TaggedTemplateExpression": ["tag", "quasi"],
|
||||
|
||||
@ -73,7 +73,6 @@ var run = function (task, done) {
|
||||
|
||||
if (execCode) {
|
||||
result = transform(execCode, getOpts(exec));
|
||||
checkAst(result, exec);
|
||||
execCode = result.code;
|
||||
|
||||
try {
|
||||
@ -94,13 +93,14 @@ var run = function (task, done) {
|
||||
err.message += codeFrame(execCode);
|
||||
throw err;
|
||||
}
|
||||
|
||||
checkAst(result, exec);
|
||||
}
|
||||
|
||||
var actualCode = actual.code;
|
||||
var expectCode = expect.code;
|
||||
if (!execCode || actualCode) {
|
||||
result = transform(actualCode, getOpts(actual));
|
||||
checkAst(result, actual);
|
||||
actualCode = result.code.trim();
|
||||
|
||||
try {
|
||||
@ -109,6 +109,8 @@ var run = function (task, done) {
|
||||
//require("fs").writeFileSync(expect.loc, actualCode);
|
||||
throw err;
|
||||
}
|
||||
|
||||
checkAst(result, actual);
|
||||
}
|
||||
|
||||
if (task.sourceMap) {
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
class Obj {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
assert.doesNotThrow(function() {
|
||||
new Obj();
|
||||
});
|
||||
@ -1,5 +1,4 @@
|
||||
{
|
||||
"experimental": true,
|
||||
"whitelist": ["flow"],
|
||||
"whitelist": ["flow", "es7.asyncFunctions"],
|
||||
"noCheckAst": true
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
export { foo as foo };
|
||||
export { foo };
|
||||
|
||||
export { bar as bar };
|
||||
export { bar };
|
||||
|
||||
function foo() {}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user