more es6 modules
This commit is contained in:
parent
e945f0d10f
commit
4c77d04b56
@ -20,12 +20,12 @@ var checkTransformerVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var checkNode = function (stack, node, scope) {
|
||||
function checkNode(stack, node, scope) {
|
||||
each(stack, function (pass) {
|
||||
if (pass.shouldRun) return;
|
||||
pass.checkNode(node, scope);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export default class File {
|
||||
constructor(opts) {
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
import isString from "lodash/lang/isString";
|
||||
import * as messages from "../../messages";
|
||||
import esutils from "esutils";
|
||||
import react from "./react";
|
||||
import * as react from "./react";
|
||||
import t from "../../types";
|
||||
|
||||
module.exports = function (exports, opts) {
|
||||
|
||||
@ -5,7 +5,7 @@ import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import t from "../../types";
|
||||
|
||||
exports.push = function (mutatorMap, key, kind, computed, value) {
|
||||
export function push(mutatorMap, key, kind, computed, value) {
|
||||
var alias;
|
||||
|
||||
if (t.isIdentifier(key)) {
|
||||
@ -31,9 +31,9 @@ exports.push = function (mutatorMap, key, kind, computed, value) {
|
||||
}
|
||||
|
||||
map[kind] = value;
|
||||
};
|
||||
}
|
||||
|
||||
exports.build = function (mutatorMap) {
|
||||
export function build(mutatorMap) {
|
||||
var objExpr = t.objectExpression([]);
|
||||
|
||||
each(mutatorMap, function (map) {
|
||||
@ -70,4 +70,4 @@ exports.build = function (mutatorMap) {
|
||||
});
|
||||
|
||||
return objExpr;
|
||||
};
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ var visit = function (node, name, scope) {
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.property = function (node, file, scope) {
|
||||
export function property(node, file, scope) {
|
||||
var key = t.toComputedKey(node, node.key);
|
||||
if (!t.isLiteral(key)) return node; // we can't set a function id with this
|
||||
|
||||
@ -99,9 +99,9 @@ exports.property = function (node, file, scope) {
|
||||
var method = node.value;
|
||||
var state = visit(method, name, scope);
|
||||
node.value = wrap(state, method, id, scope);
|
||||
};
|
||||
}
|
||||
|
||||
exports.bare = function (node, parent, scope) {
|
||||
export function bare(node, parent, scope) {
|
||||
// has an `id` so we don't need to infer one
|
||||
if (node.id) return;
|
||||
|
||||
@ -123,4 +123,4 @@ exports.bare = function (node, parent, scope) {
|
||||
|
||||
var state = visit(node, name, scope);
|
||||
return wrap(state, node, id, scope);
|
||||
};
|
||||
}
|
||||
|
||||
10
src/babel/transformation/helpers/react.js
vendored
10
src/babel/transformation/helpers/react.js
vendored
@ -2,7 +2,7 @@ import t from "../../types";
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
|
||||
exports.isCreateClass = function (node) {
|
||||
export function isCreateClass(node) {
|
||||
if (!node || !t.isCallExpression(node)) return false;
|
||||
|
||||
// not React.createClass call member object
|
||||
@ -17,10 +17,10 @@ exports.isCreateClass = function (node) {
|
||||
if (!t.isObjectExpression(first)) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
exports.isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
export var isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
|
||||
exports.isCompatTag = function (tagName) {
|
||||
export function isCompatTag(tagName) {
|
||||
return tagName && /^[a-z]|\-/.test(tagName);
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import t from "../../types";
|
||||
|
||||
exports.has = function (node) {
|
||||
export function has(node) {
|
||||
var first = node.body[0];
|
||||
return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" });
|
||||
};
|
||||
}
|
||||
|
||||
exports.wrap = function (node, callback) {
|
||||
export function wrap(node, callback) {
|
||||
var useStrictNode;
|
||||
if (exports.has(node)) {
|
||||
useStrictNode = node.body.shift();
|
||||
@ -16,4 +16,4 @@ exports.wrap = function (node, callback) {
|
||||
if (useStrictNode) {
|
||||
node.body.unshift(useStrictNode);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import defineMap from "../../helpers/define-map";
|
||||
import * as defineMap from "../../helpers/define-map";
|
||||
import t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isArrowFunctionExpression;
|
||||
export var check = t.isArrowFunctionExpression;
|
||||
|
||||
export function ArrowFunctionExpression(node) {
|
||||
t.ensureBlock(node);
|
||||
|
||||
@ -26,11 +26,9 @@ var visitor = {
|
||||
}
|
||||
};
|
||||
|
||||
exports.optional = true;
|
||||
export var optional = true;
|
||||
|
||||
exports.Loop =
|
||||
exports.Program =
|
||||
exports.BlockStatement = function (node, parent, scope, file) {
|
||||
export function BlockStatement(node, parent, scope, file) {
|
||||
var letRefs = node._letReferences;
|
||||
if (!letRefs) return;
|
||||
|
||||
@ -40,4 +38,6 @@ exports.BlockStatement = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
scope.traverse(node, visitor, state);
|
||||
};
|
||||
}
|
||||
|
||||
export { BlockStatement as Program, BlockStatement as Loop };
|
||||
|
||||
@ -5,11 +5,7 @@ import t from "../../../types";
|
||||
import values from "lodash/object/values";
|
||||
import extend from "lodash/object/extend";
|
||||
|
||||
exports.check = function (node) {
|
||||
return t.isVariableDeclaration(node) && (node.kind === "let" || node.kind === "const");
|
||||
};
|
||||
|
||||
var isLet = function (node, parent) {
|
||||
function isLet(node, parent) {
|
||||
if (!t.isVariableDeclaration(node)) return false;
|
||||
if (node._let) return true;
|
||||
if (node.kind !== "let") return false;
|
||||
@ -25,23 +21,27 @@ var isLet = function (node, parent) {
|
||||
node._let = true;
|
||||
node.kind = "var";
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
var isLetInitable = function (node, parent) {
|
||||
function isLetInitable(node, parent) {
|
||||
return !t.isFor(parent) || !t.isFor(parent, { left: node });
|
||||
};
|
||||
}
|
||||
|
||||
var isVar = function (node, parent) {
|
||||
function isVar(node, parent) {
|
||||
return t.isVariableDeclaration(node, { kind: "var" }) && !isLet(node, parent);
|
||||
};
|
||||
}
|
||||
|
||||
var standardizeLets = function (declars) {
|
||||
function standardizeLets(declars) {
|
||||
for (var i = 0; i < declars.length; i++) {
|
||||
delete declars[i]._let;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, scope, file) {
|
||||
export function check(node) {
|
||||
return t.isVariableDeclaration(node) && (node.kind === "let" || node.kind === "const");
|
||||
}
|
||||
|
||||
export function VariableDeclaration(node, parent, scope, file) {
|
||||
if (!isLet(node, parent)) return;
|
||||
|
||||
if (isLetInitable(node) && file.transformers["es6.blockScopingTDZ"].canRun()) {
|
||||
@ -61,9 +61,9 @@ exports.VariableDeclaration = function (node, parent, scope, file) {
|
||||
|
||||
return nodes;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
exports.Loop = function (node, parent, scope, file) {
|
||||
export function Loop(node, parent, scope, file) {
|
||||
var init = node.left || node.init;
|
||||
if (isLet(init, node)) {
|
||||
t.ensureBlock(node);
|
||||
@ -71,15 +71,16 @@ exports.Loop = function (node, parent, scope, file) {
|
||||
}
|
||||
var blockScoping = new BlockScoping(node, node.body, parent, scope, file);
|
||||
blockScoping.run();
|
||||
};
|
||||
}
|
||||
|
||||
exports.Program =
|
||||
exports.BlockStatement = function (block, parent, scope, file) {
|
||||
export function BlockStatement(block, parent, scope, file) {
|
||||
if (!t.isLoop(parent)) {
|
||||
var blockScoping = new BlockScoping(false, block, parent, scope, file);
|
||||
blockScoping.run();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { BlockStatement as Program };
|
||||
|
||||
function replace(node, parent, scope, remaps) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import ReplaceSupers from "../../helpers/replace-supers";
|
||||
import nameMethod from "../../helpers/name-method";
|
||||
import defineMap from "../../helpers/define-map";
|
||||
import * as nameMethod from "../../helpers/name-method";
|
||||
import * as defineMap from "../../helpers/define-map";
|
||||
import * as messages from "../../../messages";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isClass;
|
||||
export var check = t.isClass;
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, scope, file) {
|
||||
export function ClassDeclaration(node, parent, scope, file) {
|
||||
return new ClassTransformer(node, file, scope, true).run();
|
||||
};
|
||||
}
|
||||
|
||||
exports.ClassExpression = function (node, parent, scope, file) {
|
||||
export function ClassExpression(node, parent, scope, file) {
|
||||
if (!node.id) {
|
||||
if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) {
|
||||
// var o = { foo: class {} };
|
||||
@ -25,7 +25,7 @@ exports.ClassExpression = function (node, parent, scope, file) {
|
||||
}
|
||||
|
||||
return new ClassTransformer(node, file, scope, false).run();
|
||||
};
|
||||
}
|
||||
|
||||
class ClassTransformer {
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import * as messages from "../../../messages";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = function (node) {
|
||||
export function check(node) {
|
||||
return t.isVariableDeclaration(node, { kind: "const" });
|
||||
};
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
@ -36,13 +36,13 @@ var visitor = {
|
||||
}
|
||||
};
|
||||
|
||||
exports.Scopable = function (node, parent, scope, file) {
|
||||
export function Scopable(node, parent, scope, file) {
|
||||
scope.traverse(node, visitor, {
|
||||
constants: scope.getAllBindingsOfKind("const"),
|
||||
file: file
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
exports.VariableDeclaration = function (node) {
|
||||
export function VariableDeclaration(node) {
|
||||
if (node.kind === "const") node.kind = "let";
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import * as messages from "../../../messages";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isPattern;
|
||||
export var check = t.isPattern;
|
||||
|
||||
exports.ForInStatement =
|
||||
exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
export function ForOfStatement(node, parent, scope, file) {
|
||||
var left = node.left;
|
||||
|
||||
if (t.isPattern(left)) {
|
||||
@ -50,7 +49,9 @@ exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
|
||||
var block = node.body;
|
||||
block.body = nodes.concat(block.body);
|
||||
};
|
||||
}
|
||||
|
||||
export { ForOfStatement as ForInStatement };
|
||||
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
@ -83,7 +84,7 @@ exports.Function = function (node, parent, scope, file) {
|
||||
block.body = nodes.concat(block.body);
|
||||
};
|
||||
|
||||
exports.CatchClause = function (node, parent, scope, file) {
|
||||
export function CatchClause(node, parent, scope, file) {
|
||||
var pattern = node.param;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
@ -103,9 +104,9 @@ exports.CatchClause = function (node, parent, scope, file) {
|
||||
node.body.body = nodes.concat(node.body.body);
|
||||
|
||||
return node;
|
||||
};
|
||||
}
|
||||
|
||||
exports.ExpressionStatement = function (node, parent, scope, file) {
|
||||
export function ExpressionStatement(node, parent, scope, file) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
if (!t.isPattern(expr.left)) return;
|
||||
@ -127,9 +128,9 @@ exports.ExpressionStatement = function (node, parent, scope, file) {
|
||||
destructuring.init(expr.left, ref);
|
||||
|
||||
return nodes;
|
||||
};
|
||||
}
|
||||
|
||||
exports.AssignmentExpression = function (node, parent, scope, file) {
|
||||
export function AssignmentExpression(node, parent, scope, file) {
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
var ref = scope.generateUidIdentifier("temp");
|
||||
@ -152,18 +153,18 @@ exports.AssignmentExpression = function (node, parent, scope, file) {
|
||||
nodes.push(ref);
|
||||
|
||||
return t.toSequenceExpression(nodes, scope);
|
||||
};
|
||||
}
|
||||
|
||||
var variableDeclarationHasPattern = function (node) {
|
||||
function variableDeclarationHasPattern(node) {
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
if (t.isPattern(node.declarations[i].id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, scope, file) {
|
||||
export function VariableDeclaration(node, parent, scope, file) {
|
||||
if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
|
||||
if (!variableDeclarationHasPattern(node)) return;
|
||||
|
||||
|
||||
@ -2,9 +2,9 @@ import * as messages from "../../../messages";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isForOfStatement;
|
||||
export var check = t.isForOfStatement;
|
||||
|
||||
exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
export function ForOfStatement(node, parent, scope, file) {
|
||||
var callback = spec;
|
||||
if (file.isLoose("es6.forOf")) callback = loose;
|
||||
|
||||
@ -33,7 +33,7 @@ exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
loop._scopeInfo = node._scopeInfo;
|
||||
|
||||
return build.node;
|
||||
};
|
||||
}
|
||||
|
||||
var breakVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
|
||||
@ -2,7 +2,7 @@ import isNumber from "lodash/lang/isNumber";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isRestElement;
|
||||
export var check = t.isRestElement;
|
||||
|
||||
var memberExpressionOptimisationVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
import includes from "lodash/collection/includes";
|
||||
import t from "../../../types";
|
||||
|
||||
exports.check = t.isSpreadElement;
|
||||
|
||||
function getSpreadLiteral(spread, scope) {
|
||||
return scope.toArray(spread.argument, true);
|
||||
}
|
||||
@ -42,6 +40,8 @@ function build(props, scope) {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export var check = t.isSpreadElement;
|
||||
|
||||
export function ArrayExpression(node, parent, scope) {
|
||||
var elements = node.elements;
|
||||
if (!hasSpread(elements)) return;
|
||||
|
||||
@ -76,16 +76,17 @@ var go = function (getBody, node, scope) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.Program = function (node, parent, scope) {
|
||||
export function Program(node, parent, scope) {
|
||||
go(function () {
|
||||
return node.body;
|
||||
}, node, scope);
|
||||
};
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node, parent, scope) {
|
||||
export function FunctionDeclaration(node, parent, scope) {
|
||||
go(function () {
|
||||
t.ensureBlock(node);
|
||||
return node.body.body;
|
||||
}, node, scope);
|
||||
};
|
||||
}
|
||||
|
||||
export { FunctionDeclaration as FunctionExpression };
|
||||
|
||||
@ -9,8 +9,7 @@ import values from "lodash/object/values";
|
||||
// - 2 Priority over normal nodes
|
||||
// - 3 We want this to be at the **very** top
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = {
|
||||
export var BlockStatement = {
|
||||
exit(node) {
|
||||
var hasChange = false;
|
||||
for (var i = 0; i < node.body.length; i++) {
|
||||
@ -29,3 +28,5 @@ exports.Program = {
|
||||
node.body = flatten(values(nodePriorities).reverse());
|
||||
}
|
||||
};
|
||||
|
||||
export { BlockStatement as Program };
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import useStrict from "../../helpers/use-strict";
|
||||
import * as useStrict from "../../helpers/use-strict";
|
||||
import t from "../../../types";
|
||||
|
||||
export var secondPass = true;
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = function (node, parent, scope, file) {
|
||||
export function BlockStatement(node, parent, scope, file) {
|
||||
if (!node._declarations) return;
|
||||
|
||||
useStrict.wrap(node, function () {
|
||||
@ -31,4 +30,6 @@ exports.Program = function (node, parent, scope, file) {
|
||||
|
||||
node._declarations = null;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export { BlockStatement as Program };
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import useStrict from "../../helpers/use-strict";
|
||||
import * as useStrict from "../../helpers/use-strict";
|
||||
|
||||
export function Program(program, parent, scope, file) {
|
||||
if (!file.transformers["es6.modules"].canRun()) return;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import react from "../../helpers/react";
|
||||
import * as react from "../../helpers/react";
|
||||
import t from "../../../types";
|
||||
|
||||
export function manipulateOptions(opts) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import react from "../../helpers/react";
|
||||
import * as react from "../../helpers/react";
|
||||
import t from "../../../types";
|
||||
|
||||
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
|
||||
|
||||
@ -8,10 +8,11 @@ export function Program(program) {
|
||||
}
|
||||
}
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function () {
|
||||
export function FunctionExpression() {
|
||||
this.skip();
|
||||
};
|
||||
}
|
||||
|
||||
export { FunctionExpression as FunctionDeclaration };
|
||||
|
||||
export function ThisExpression() {
|
||||
return t.identifier("undefined");
|
||||
|
||||
@ -16,8 +16,7 @@ var visitor = {
|
||||
}
|
||||
};
|
||||
|
||||
exports.Property =
|
||||
exports.MethodDefinition = function (node, parent, scope, file) {
|
||||
export function MethodDefinition(node, parent, scope, file) {
|
||||
if (node.kind !== "memo") return;
|
||||
node.kind = "get";
|
||||
|
||||
@ -38,4 +37,6 @@ exports.MethodDefinition = function (node, parent, scope, file) {
|
||||
scope.traverse(value, visitor, state);
|
||||
|
||||
return node;
|
||||
};
|
||||
}
|
||||
|
||||
export { MethodDefinition as Property };
|
||||
|
||||
@ -3,11 +3,12 @@ import t from "../../../types";
|
||||
|
||||
export { isFor as check } from "../../../types";
|
||||
|
||||
exports.ForInStatement =
|
||||
exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
export function ForOfStatement(node, parent, scope, file) {
|
||||
var left = node.left;
|
||||
if (t.isVariableDeclaration(left)) {
|
||||
var declar = left.declarations[0];
|
||||
if (declar.init) throw file.errorWithNode(declar, messages.get("noAssignmentsInForHead"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { ForOfStatement as ForInStatement };
|
||||
|
||||
@ -4,9 +4,10 @@ export function check(node) {
|
||||
return node.kind === "set";
|
||||
}
|
||||
|
||||
exports.MethodDefinition =
|
||||
exports.Property = function (node, parent, scope, file) {
|
||||
export function Property(node, parent, scope, file) {
|
||||
if (node.kind === "set" && node.value.params.length !== 1) {
|
||||
throw file.errorWithNode(node.value, messages.get("settersInvalidParamLength"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { Property as MethodDefinition };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user