move more transformers into plugins
This commit is contained in:
parent
91161ae9a1
commit
822eb47ee7
@ -34,11 +34,15 @@
|
|||||||
"babel-plugin-dead-code-elimination": "^1.0.1",
|
"babel-plugin-dead-code-elimination": "^1.0.1",
|
||||||
"babel-plugin-eval": "^1.0.1",
|
"babel-plugin-eval": "^1.0.1",
|
||||||
"babel-plugin-inline-environment-variables": "^1.0.1",
|
"babel-plugin-inline-environment-variables": "^1.0.1",
|
||||||
|
"babel-plugin-jscript": "^1.0.1",
|
||||||
"babel-plugin-member-expression-literals": "^1.0.1",
|
"babel-plugin-member-expression-literals": "^1.0.1",
|
||||||
"babel-plugin-property-literals": "^1.0.1",
|
"babel-plugin-property-literals": "^1.0.1",
|
||||||
"babel-plugin-proto-to-assign": "^1.0.3",
|
"babel-plugin-proto-to-assign": "^1.0.3",
|
||||||
|
"babel-plugin-react-constant-elements": "^1.0.3",
|
||||||
"babel-plugin-remove-console": "^1.0.1",
|
"babel-plugin-remove-console": "^1.0.1",
|
||||||
"babel-plugin-remove-debugger": "^1.0.1",
|
"babel-plugin-remove-debugger": "^1.0.1",
|
||||||
|
"babel-plugin-runtime": "^1.0.5",
|
||||||
|
"babel-plugin-undeclared-variables-check": "^1.0.1",
|
||||||
"babel-plugin-undefined-to-void": "^1.1.6",
|
"babel-plugin-undefined-to-void": "^1.1.6",
|
||||||
"bluebird": "^2.9.25",
|
"bluebird": "^2.9.25",
|
||||||
"chalk": "^1.0.0",
|
"chalk": "^1.0.0",
|
||||||
|
|||||||
@ -24,11 +24,11 @@ export default {
|
|||||||
"es7.trailingFunctionCommas": require("./es7/trailing-function-commas"),
|
"es7.trailingFunctionCommas": require("./es7/trailing-function-commas"),
|
||||||
"es7.asyncFunctions": require("./es7/async-functions"),
|
"es7.asyncFunctions": require("./es7/async-functions"),
|
||||||
"es7.decorators": require("./es7/decorators"),
|
"es7.decorators": require("./es7/decorators"),
|
||||||
"validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"),
|
"validation.undeclaredVariableCheck": require("babel-plugin-undeclared-variables-check"),
|
||||||
"validation.react": require("./validation/react"),
|
"validation.react": require("./validation/react"),
|
||||||
"es6.arrowFunctions": require("./es6/arrow-functions"),
|
"es6.arrowFunctions": require("./es6/arrow-functions"),
|
||||||
"spec.blockScopedFunctions": require("./spec/block-scoped-functions"),
|
"spec.blockScopedFunctions": require("./spec/block-scoped-functions"),
|
||||||
"optimisation.react.constantElements": require("./optimisation/react.constant-elements"),
|
"optimisation.react.constantElements": require("babel-plugin-react-constant-elements"),
|
||||||
"optimisation.react.inlineElements": require("./optimisation/react.inline-elements"),
|
"optimisation.react.inlineElements": require("./optimisation/react.inline-elements"),
|
||||||
"es7.comprehensions": require("./es7/comprehensions"),
|
"es7.comprehensions": require("./es7/comprehensions"),
|
||||||
"es6.classes": require("./es6/classes"),
|
"es6.classes": require("./es6/classes"),
|
||||||
@ -67,7 +67,7 @@ export default {
|
|||||||
// hissy fit
|
// hissy fit
|
||||||
|
|
||||||
//- builtin-modules
|
//- builtin-modules
|
||||||
runtime: require("./other/runtime"),
|
runtime: require("babel-plugin-runtime"),
|
||||||
"es6.modules": require("./es6/modules"),
|
"es6.modules": require("./es6/modules"),
|
||||||
_moduleFormatter: require("./internal/module-formatter"),
|
_moduleFormatter: require("./internal/module-formatter"),
|
||||||
|
|
||||||
@ -82,6 +82,6 @@ export default {
|
|||||||
"minification.memberExpressionLiterals": require("babel-plugin-member-expression-literals"),
|
"minification.memberExpressionLiterals": require("babel-plugin-member-expression-literals"),
|
||||||
"minification.propertyLiterals": require("babel-plugin-property-literals"),
|
"minification.propertyLiterals": require("babel-plugin-property-literals"),
|
||||||
_blockHoist: require("./internal/block-hoist"),
|
_blockHoist: require("./internal/block-hoist"),
|
||||||
jscript: require("./other/jscript"),
|
jscript: require("babel-plugin-jscript"),
|
||||||
flow: require("./other/flow"),
|
flow: require("./other/flow"),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,42 +0,0 @@
|
|||||||
export var metadata = {
|
|
||||||
optional: true
|
|
||||||
};
|
|
||||||
|
|
||||||
var immutabilityVisitor = {
|
|
||||||
enter(node, parent, scope, state) {
|
|
||||||
var stop = () => {
|
|
||||||
state.isImmutable = false;
|
|
||||||
this.stop();
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.isJSXClosingElement()) {
|
|
||||||
this.skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isJSXIdentifier({ name: "ref" }) && this.parentPath.isJSXAttribute({ name: node })) {
|
|
||||||
return stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isJSXIdentifier() || this.isIdentifier() || this.isJSXMemberExpression()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.isImmutable()) stop();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export var visitor = {
|
|
||||||
JSXElement(node) {
|
|
||||||
if (node._hoisted) return;
|
|
||||||
|
|
||||||
var state = { isImmutable: true };
|
|
||||||
this.traverse(immutabilityVisitor, state);
|
|
||||||
|
|
||||||
if (state.isImmutable) {
|
|
||||||
this.hoist();
|
|
||||||
} else {
|
|
||||||
node._hoisted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
import * as t from "../../../types";
|
|
||||||
|
|
||||||
export var metadata = {
|
|
||||||
group: "builtin-trailing",
|
|
||||||
optional: true
|
|
||||||
};
|
|
||||||
|
|
||||||
export var visitor = {
|
|
||||||
FunctionExpression: {
|
|
||||||
exit(node) {
|
|
||||||
if (!node.id) return;
|
|
||||||
node._ignoreUserWhitespace = true;
|
|
||||||
|
|
||||||
return t.callExpression(
|
|
||||||
t.functionExpression(null, [], t.blockStatement([
|
|
||||||
t.toStatement(node),
|
|
||||||
t.returnStatement(node.id)
|
|
||||||
])),
|
|
||||||
[]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,168 +0,0 @@
|
|||||||
{
|
|
||||||
"builtins": {
|
|
||||||
"Symbol": "symbol",
|
|
||||||
"Promise": "promise",
|
|
||||||
"Map": "map",
|
|
||||||
"WeakMap": "weak-map",
|
|
||||||
"Set": "set",
|
|
||||||
"WeakSet": "weak-set"
|
|
||||||
},
|
|
||||||
|
|
||||||
"methods": {
|
|
||||||
"Array": {
|
|
||||||
"concat": "array/concat",
|
|
||||||
"copyWithin": "array/copy-within",
|
|
||||||
"entries": "array/entries",
|
|
||||||
"every": "array/every",
|
|
||||||
"fill": "array/fill",
|
|
||||||
"filter": "array/filter",
|
|
||||||
"findIndex": "array/find-index",
|
|
||||||
"find": "array/find",
|
|
||||||
"forEach": "array/for-each",
|
|
||||||
"from": "array/from",
|
|
||||||
"includes": "array/includes",
|
|
||||||
"indexOf": "array/index-of",
|
|
||||||
"join": "array/join",
|
|
||||||
"keys": "array/keys",
|
|
||||||
"lastIndexOf": "array/last-index-of",
|
|
||||||
"map": "array/map",
|
|
||||||
"of": "array/of",
|
|
||||||
"pop": "array/pop",
|
|
||||||
"push": "array/push",
|
|
||||||
"reduceRight": "array/reduce-right",
|
|
||||||
"reduce": "array/reduce",
|
|
||||||
"reverse": "array/reverse",
|
|
||||||
"shift": "array/shift",
|
|
||||||
"slice": "array/slice",
|
|
||||||
"some": "array/some",
|
|
||||||
"sort": "array/sort",
|
|
||||||
"splice": "array/splice",
|
|
||||||
"turn": "array/turn",
|
|
||||||
"unshift": "array/unshift",
|
|
||||||
"values": "array/values"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Object": {
|
|
||||||
"assign": "object/assign",
|
|
||||||
"classof": "object/classof",
|
|
||||||
"create": "object/create",
|
|
||||||
"define": "object/define",
|
|
||||||
"defineProperties": "object/define-properties",
|
|
||||||
"defineProperty": "object/define-property",
|
|
||||||
"entries": "object/entries",
|
|
||||||
"freeze": "object/freeze",
|
|
||||||
"getOwnPropertyDescriptor": "object/get-own-property-descriptor",
|
|
||||||
"getOwnPropertyDescriptors": "object/get-own-property-descriptors",
|
|
||||||
"getOwnPropertyNames": "object/get-own-property-names",
|
|
||||||
"getOwnPropertySymbols": "object/get-own-property-symbols",
|
|
||||||
"getPrototypePf": "object/get-prototype-of",
|
|
||||||
"index": "object/index",
|
|
||||||
"isExtensible": "object/is-extensible",
|
|
||||||
"isFrozen": "object/is-frozen",
|
|
||||||
"isObject": "object/is-object",
|
|
||||||
"isSealed": "object/is-sealed",
|
|
||||||
"is": "object/is",
|
|
||||||
"keys": "object/keys",
|
|
||||||
"make": "object/make",
|
|
||||||
"preventExtensions": "object/prevent-extensions",
|
|
||||||
"seal": "object/seal",
|
|
||||||
"setPrototypeOf": "object/set-prototype-of",
|
|
||||||
"values": "object/values"
|
|
||||||
},
|
|
||||||
|
|
||||||
"RegExp": {
|
|
||||||
"escape": "regexp/escape"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Function": {
|
|
||||||
"only": "function/only",
|
|
||||||
"part": "function/part"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Math": {
|
|
||||||
"acosh": "math/acosh",
|
|
||||||
"asinh": "math/asinh",
|
|
||||||
"atanh": "math/atanh",
|
|
||||||
"cbrt": "math/cbrt",
|
|
||||||
"clz32": "math/clz32",
|
|
||||||
"cosh": "math/cosh",
|
|
||||||
"expm1": "math/expm1",
|
|
||||||
"fround": "math/fround",
|
|
||||||
"hypot": "math/hypot",
|
|
||||||
"pot": "math/pot",
|
|
||||||
"imul": "math/imul",
|
|
||||||
"log10": "math/log10",
|
|
||||||
"log1p": "math/log1p",
|
|
||||||
"log2": "math/log2",
|
|
||||||
"sign": "math/sign",
|
|
||||||
"sinh": "math/sinh",
|
|
||||||
"tanh": "math/tanh",
|
|
||||||
"trunc": "math/trunc"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Date": {
|
|
||||||
"addLocale": "date/add-locale",
|
|
||||||
"formatUTC": "date/format-utc",
|
|
||||||
"format": "date/format"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Symbol": {
|
|
||||||
"for": "symbol/for",
|
|
||||||
"hasInstance": "symbol/has-instance",
|
|
||||||
"is-concat-spreadable": "symbol/is-concat-spreadable",
|
|
||||||
"iterator": "symbol/iterator",
|
|
||||||
"keyFor": "symbol/key-for",
|
|
||||||
"match": "symbol/match",
|
|
||||||
"replace": "symbol/replace",
|
|
||||||
"search": "symbol/search",
|
|
||||||
"species": "symbol/species",
|
|
||||||
"split": "symbol/split",
|
|
||||||
"toPrimitive": "symbol/to-primitive",
|
|
||||||
"toStringTag": "symbol/to-string-tag",
|
|
||||||
"unscopables": "symbol/unscopables"
|
|
||||||
},
|
|
||||||
|
|
||||||
"String": {
|
|
||||||
"at": "string/at",
|
|
||||||
"codePointAt": "string/code-point-at",
|
|
||||||
"endsWith": "string/ends-with",
|
|
||||||
"escapeHTML": "string/escape-html",
|
|
||||||
"fromCodePoint": "string/from-code-point",
|
|
||||||
"includes": "string/includes",
|
|
||||||
"raw": "string/raw",
|
|
||||||
"repeat": "string/repeat",
|
|
||||||
"startsWith": "string/starts-with",
|
|
||||||
"unescapeHTML": "string/unescape-html"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Number": {
|
|
||||||
"EPSILON": "number/epsilon",
|
|
||||||
"isFinite": "number/is-finite",
|
|
||||||
"isInteger": "number/is-integer",
|
|
||||||
"isNaN": "number/is-nan",
|
|
||||||
"isSafeInteger": "number/is-safe-integer",
|
|
||||||
"MAX_SAFE_INTEGER": "number/max-safe-integer",
|
|
||||||
"MIN_SAFE_INTEGER": "number/min-safe-integer",
|
|
||||||
"parseFloat": "number/parse-float",
|
|
||||||
"parseInt": "number/parse-int",
|
|
||||||
"random": "number/random"
|
|
||||||
},
|
|
||||||
|
|
||||||
"Reflect": {
|
|
||||||
"apply": "reflect/apply",
|
|
||||||
"construct": "reflect/construct",
|
|
||||||
"defineProperty": "reflect/define-property",
|
|
||||||
"deleteProperty": "reflect/delete-property",
|
|
||||||
"enumerate": "reflect/enumerate",
|
|
||||||
"getOwnPropertyDescriptor": "reflect/get-own-property-descriptor",
|
|
||||||
"getPrototypeOf": "reflect/get-prototype-of",
|
|
||||||
"get": "reflect/get",
|
|
||||||
"has": "reflect/has",
|
|
||||||
"isExtensible": "reflect/is-extensible",
|
|
||||||
"ownKeys": "reflect/own-keys",
|
|
||||||
"preventExtensions": "reflect/prevent-extensions",
|
|
||||||
"setPrototypeOf": "reflect/set-prototype-of",
|
|
||||||
"set": "reflect/set"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,102 +0,0 @@
|
|||||||
import has from "lodash/object/has";
|
|
||||||
import * as t from "../../../../types";
|
|
||||||
import definitions from "./definitions";
|
|
||||||
|
|
||||||
const RUNTIME_MODULE_NAME = "babel-runtime";
|
|
||||||
|
|
||||||
export var metadata = {
|
|
||||||
optional: true,
|
|
||||||
group: "builtin-post-modules"
|
|
||||||
};
|
|
||||||
|
|
||||||
export function pre(file) {
|
|
||||||
file.set("helperGenerator", function (name) {
|
|
||||||
return file.addImport(`${RUNTIME_MODULE_NAME}/helpers/${name}`, name, "absoluteDefault");
|
|
||||||
});
|
|
||||||
|
|
||||||
file.setDynamic("regeneratorIdentifier", function () {
|
|
||||||
return file.addImport(`${RUNTIME_MODULE_NAME}/regenerator`, "regeneratorRuntime", "absoluteDefault");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export var visitor = {
|
|
||||||
ReferencedIdentifier(node, parent, scope, file) {
|
|
||||||
if (node.name === "regeneratorRuntime") {
|
|
||||||
return file.get("regeneratorIdentifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isMemberExpression(parent)) return;
|
|
||||||
if (!has(definitions.builtins, node.name)) return;
|
|
||||||
if (scope.getBindingIdentifier(node.name)) return;
|
|
||||||
|
|
||||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
|
||||||
var modulePath = definitions.builtins[node.name];
|
|
||||||
return file.addImport(`${RUNTIME_MODULE_NAME}/core-js/${modulePath}`, node.name, "absoluteDefault");
|
|
||||||
},
|
|
||||||
|
|
||||||
CallExpression(node, parent, scope, file) {
|
|
||||||
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
|
|
||||||
|
|
||||||
if (node.arguments.length) return;
|
|
||||||
|
|
||||||
var callee = node.callee;
|
|
||||||
if (!t.isMemberExpression(callee)) return;
|
|
||||||
if (!callee.computed) return;
|
|
||||||
if (!this.get("callee.property").matchesPattern("Symbol.iterator")) return;
|
|
||||||
|
|
||||||
return t.callExpression(file.addImport(`${RUNTIME_MODULE_NAME}/core-js/get-iterator`, "getIterator", "absoluteDefault"), [callee.object]);
|
|
||||||
},
|
|
||||||
|
|
||||||
BinaryExpression(node, parent, scope, file) {
|
|
||||||
// Symbol.iterator in arr -> core.$for.isIterable(arr)
|
|
||||||
|
|
||||||
if (node.operator !== "in") return;
|
|
||||||
if (!this.get("left").matchesPattern("Symbol.iterator")) return;
|
|
||||||
|
|
||||||
return t.callExpression(
|
|
||||||
file.addImport(`${RUNTIME_MODULE_NAME}/core-js/is-iterable`, "isIterable", "absoluteDefault"),
|
|
||||||
[node.right]
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
MemberExpression: {
|
|
||||||
enter(node, parent, scope, file) {
|
|
||||||
// Array.from -> _core.Array.from
|
|
||||||
|
|
||||||
if (!this.isReferenced()) return;
|
|
||||||
|
|
||||||
var obj = node.object;
|
|
||||||
var prop = node.property;
|
|
||||||
|
|
||||||
if (!t.isReferenced(obj, node)) return;
|
|
||||||
|
|
||||||
if (node.computed) return;
|
|
||||||
|
|
||||||
if (!has(definitions.methods, obj.name)) return;
|
|
||||||
|
|
||||||
var methods = definitions.methods[obj.name];
|
|
||||||
if (!has(methods, prop.name)) return;
|
|
||||||
|
|
||||||
if (scope.getBindingIdentifier(obj.name)) return;
|
|
||||||
|
|
||||||
var modulePath = methods[prop.name];
|
|
||||||
return file.addImport(`${RUNTIME_MODULE_NAME}/core-js/${modulePath}`, `${obj.name}$${prop.name}`, "absoluteDefault");
|
|
||||||
},
|
|
||||||
|
|
||||||
exit(node, parent, scope, file) {
|
|
||||||
if (!this.isReferenced()) return;
|
|
||||||
|
|
||||||
var prop = node.property;
|
|
||||||
var obj = node.object;
|
|
||||||
|
|
||||||
if (!has(definitions.builtins, obj.name)) return;
|
|
||||||
if (scope.getBindingIdentifier(obj.name)) return;
|
|
||||||
|
|
||||||
var modulePath = definitions.builtins[obj.name];
|
|
||||||
return t.memberExpression(
|
|
||||||
file.addImport(`${RUNTIME_MODULE_NAME}/core-js/${modulePath}`, `${obj.name}`, "absoluteDefault"),
|
|
||||||
prop
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,45 +0,0 @@
|
|||||||
import levenshtein from "leven";
|
|
||||||
import * as messages from "../../../messages";
|
|
||||||
|
|
||||||
export var metadata = {
|
|
||||||
optional: true
|
|
||||||
};
|
|
||||||
|
|
||||||
export var visitor = {
|
|
||||||
ReferencedIdentifier(node, parent, scope) {
|
|
||||||
var binding = scope.getBinding(node.name);
|
|
||||||
if (binding && binding.kind === "type" && !this.parentPath.isFlow()) {
|
|
||||||
throw this.errorWithNode(messages.get("undeclaredVariableType", node.name), ReferenceError);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scope.hasBinding(node.name)) return;
|
|
||||||
|
|
||||||
// get the closest declaration to offer as a suggestion
|
|
||||||
// the variable name may have just been mistyped
|
|
||||||
|
|
||||||
var bindings = scope.getAllBindings();
|
|
||||||
|
|
||||||
var closest;
|
|
||||||
var shortest = -1;
|
|
||||||
|
|
||||||
for (var name in bindings) {
|
|
||||||
var distance = levenshtein(node.name, name);
|
|
||||||
if (distance <= 0 || distance > 3) continue;
|
|
||||||
if (distance <= shortest) continue;
|
|
||||||
|
|
||||||
closest = name;
|
|
||||||
shortest = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
var msg;
|
|
||||||
if (closest) {
|
|
||||||
msg = messages.get("undeclaredVariableSuggestion", node.name, closest);
|
|
||||||
} else {
|
|
||||||
msg = messages.get("undeclaredVariable", node.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
throw this.errorWithNode(msg, ReferenceError);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Loading…
x
Reference in New Issue
Block a user