concise methods oh my god this is so good
This commit is contained in:
parent
8d1b5c8ccb
commit
23d6fb6bb1
@ -12,7 +12,7 @@ var util = require("../../util");
|
||||
var fs = require("fs");
|
||||
|
||||
sourceMapSupport.install({
|
||||
retrieveSourceMap: function (source) {
|
||||
retrieveSourceMap(source) {
|
||||
var map = maps && maps[source];
|
||||
if (map) {
|
||||
return {
|
||||
|
||||
@ -15,7 +15,7 @@ function detect(ast) {
|
||||
};
|
||||
|
||||
traverse(ast, {
|
||||
enter: function (node, parent) {
|
||||
enter(node, parent) {
|
||||
if (SYNTAX_KEYS[node.type]) {
|
||||
detectedSyntax(SYNTAX_KEYS[node.type]);
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ exports.SwitchStatement = function (node, print) {
|
||||
|
||||
print.sequence(node.cases, {
|
||||
indent: true,
|
||||
addNewlines: function (leading, cas) {
|
||||
addNewlines(leading, cas) {
|
||||
if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
|
||||
}
|
||||
});
|
||||
|
||||
@ -46,7 +46,7 @@ var isType = function (node) {
|
||||
};
|
||||
|
||||
exports.nodes = {
|
||||
AssignmentExpression: function (node) {
|
||||
AssignmentExpression(node) {
|
||||
var state = crawl(node.right);
|
||||
if ((state.hasCall && state.hasHelper) || state.hasFunction) {
|
||||
return {
|
||||
@ -56,13 +56,13 @@ exports.nodes = {
|
||||
}
|
||||
},
|
||||
|
||||
SwitchCase: function (node, parent) {
|
||||
SwitchCase(node, parent) {
|
||||
return {
|
||||
before: node.consequent.length || parent.cases[0] === node
|
||||
};
|
||||
},
|
||||
|
||||
LogicalExpression: function (node) {
|
||||
LogicalExpression(node) {
|
||||
if (t.isFunction(node.left) || t.isFunction(node.right)) {
|
||||
return {
|
||||
after: true
|
||||
@ -70,7 +70,7 @@ exports.nodes = {
|
||||
}
|
||||
},
|
||||
|
||||
Literal: function (node) {
|
||||
Literal(node) {
|
||||
if (node.value === "use strict") {
|
||||
return {
|
||||
after: true
|
||||
@ -78,7 +78,7 @@ exports.nodes = {
|
||||
}
|
||||
},
|
||||
|
||||
CallExpression: function (node) {
|
||||
CallExpression(node) {
|
||||
if (t.isFunction(node.callee) || isHelper(node)) {
|
||||
return {
|
||||
before: true,
|
||||
@ -87,7 +87,7 @@ exports.nodes = {
|
||||
}
|
||||
},
|
||||
|
||||
VariableDeclaration: function (node) {
|
||||
VariableDeclaration(node) {
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
var declar = node.declarations[i];
|
||||
|
||||
@ -106,7 +106,7 @@ exports.nodes = {
|
||||
}
|
||||
},
|
||||
|
||||
IfStatement: function (node) {
|
||||
IfStatement(node) {
|
||||
if (t.isBlockStatement(node.consequent)) {
|
||||
return {
|
||||
before: true,
|
||||
@ -126,15 +126,15 @@ exports.nodes.SpreadProperty = function (node, parent) {
|
||||
};
|
||||
|
||||
exports.list = {
|
||||
VariableDeclaration: function (node) {
|
||||
VariableDeclaration(node) {
|
||||
return map(node.declarations, "init");
|
||||
},
|
||||
|
||||
ArrayExpression: function (node) {
|
||||
ArrayExpression(node) {
|
||||
return node.elements;
|
||||
},
|
||||
|
||||
ObjectExpression: function (node) {
|
||||
ObjectExpression(node) {
|
||||
return node.properties;
|
||||
}
|
||||
};
|
||||
|
||||
@ -77,7 +77,7 @@ module.exports = function (lines, lineNumber, colNumber) {
|
||||
start: start + 1,
|
||||
before: " ",
|
||||
after: " | ",
|
||||
transform: function (params) {
|
||||
transform(params) {
|
||||
if (params.number !== lineNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -417,7 +417,7 @@ File.prototype.call = function (key) {
|
||||
};
|
||||
|
||||
var checkTransformerVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
checkNode(state.stack, node, scope);
|
||||
}
|
||||
};
|
||||
|
||||
@ -33,7 +33,7 @@ module.exports = function (exports, opts) {
|
||||
};
|
||||
|
||||
exports.JSXMemberExpression = {
|
||||
exit: function (node) {
|
||||
exit(node) {
|
||||
node.computed = t.isLiteral(node.property);
|
||||
node.type = "MemberExpression";
|
||||
}
|
||||
@ -44,21 +44,21 @@ module.exports = function (exports, opts) {
|
||||
};
|
||||
|
||||
exports.JSXAttribute = {
|
||||
enter: function (node) {
|
||||
enter(node) {
|
||||
var value = node.value;
|
||||
if (t.isLiteral(value) && isString(value.value)) {
|
||||
value.value = value.value.replace(/\n\s+/g, " ");
|
||||
}
|
||||
},
|
||||
|
||||
exit: function (node) {
|
||||
exit(node) {
|
||||
var value = node.value || t.literal(true);
|
||||
return t.inherits(t.property("init", node.name, value), node);
|
||||
}
|
||||
};
|
||||
|
||||
exports.JSXOpeningElement = {
|
||||
exit: function (node, parent, scope, file) {
|
||||
exit(node, parent, scope, file) {
|
||||
var tagExpr = node.name;
|
||||
var args = [];
|
||||
|
||||
@ -146,7 +146,7 @@ module.exports = function (exports, opts) {
|
||||
};
|
||||
|
||||
exports.JSXElement = {
|
||||
exit: function (node) {
|
||||
exit(node) {
|
||||
var callExpr = node.openingElement;
|
||||
|
||||
for (var i = 0; i < node.children.length; i++) {
|
||||
|
||||
@ -4,7 +4,7 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
// check if this node is a referenced identifier that matches the same as our
|
||||
// function id
|
||||
if (!t.isReferencedIdentifier(node, parent, { name: state.name })) return;
|
||||
@ -99,7 +99,7 @@ exports.bare = function (node, parent, scope) {
|
||||
|
||||
var id;
|
||||
if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) {
|
||||
// { foo: function () {} };
|
||||
// { foo() {} };
|
||||
id = parent.key;
|
||||
} else if (t.isVariableDeclarator(parent)) {
|
||||
// var foo = function () {};
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
var t = require("../../types");
|
||||
|
||||
var visitor = {
|
||||
enter: function (node) {
|
||||
enter(node) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
|
||||
@ -96,7 +96,7 @@ ReplaceSupers.prototype.replace = function () {
|
||||
};
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
var topLevel = state.topLevel;
|
||||
var self = state.self;
|
||||
|
||||
|
||||
@ -40,7 +40,7 @@ DefaultFormatter.prototype.bumpImportOccurences = function (node) {
|
||||
};
|
||||
|
||||
var exportsVisitor = {
|
||||
enter: function (node, parent, scope, formatter) {
|
||||
enter(node, parent, scope, formatter) {
|
||||
var declar = node && node.declaration;
|
||||
if (t.isExportDeclaration(node)) {
|
||||
formatter.hasLocalImports = true;
|
||||
@ -65,7 +65,7 @@ DefaultFormatter.prototype.getLocalExports = function () {
|
||||
};
|
||||
|
||||
var importsVisitor = {
|
||||
enter: function (node, parent, scope, formatter) {
|
||||
enter(node, parent, scope, formatter) {
|
||||
if (t.isImportDeclaration(node)) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, t.getBindingIdentifiers(node));
|
||||
@ -79,7 +79,7 @@ DefaultFormatter.prototype.getLocalImports = function () {
|
||||
};
|
||||
|
||||
var remapVisitor = {
|
||||
enter: function (node, parent, scope, formatter) {
|
||||
enter(node, parent, scope, formatter) {
|
||||
if (t.isUpdateExpression(node) && formatter.isLocalReference(node.argument, scope)) {
|
||||
this.skip();
|
||||
|
||||
|
||||
@ -68,7 +68,7 @@ SystemFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
};
|
||||
|
||||
var runnerSettersVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (node._importSource === state.source) {
|
||||
if (t.isVariableDeclaration(node)) {
|
||||
each(node.declarations, function (declar) {
|
||||
@ -103,7 +103,7 @@ SystemFormatter.prototype.buildRunnerSetters = function (block, hoistDeclarators
|
||||
};
|
||||
|
||||
var hoistVariablesVisitor = {
|
||||
enter: function (node, parent, scope, hoistDeclarators) {
|
||||
enter(node, parent, scope, hoistDeclarators) {
|
||||
if (t.isFunction(node)) {
|
||||
// nothing inside is accessible
|
||||
return this.skip();
|
||||
@ -148,7 +148,7 @@ var hoistVariablesVisitor = {
|
||||
};
|
||||
|
||||
var hoistFunctionsVisitor = {
|
||||
enter: function (node, parent, scope, handlerBody) {
|
||||
enter(node, parent, scope, handlerBody) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isFunctionDeclaration(node) || node._blockHoist) {
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
var t = require("../../../types");
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
|
||||
var declared = state.letRefs[node.name];
|
||||
|
||||
@ -266,7 +266,7 @@ BlockScoping.prototype.wrapClosure = function () {
|
||||
};
|
||||
|
||||
var letReferenceFunctionVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
// not a direct reference
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
|
||||
@ -282,7 +282,7 @@ var letReferenceFunctionVisitor = {
|
||||
};
|
||||
|
||||
var letReferenceBlockVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) {
|
||||
scope.traverse(node, letReferenceFunctionVisitor, state);
|
||||
return this.skip();
|
||||
@ -351,7 +351,7 @@ var loopNodeTo = function (node) {
|
||||
};
|
||||
|
||||
var loopVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
var replace;
|
||||
|
||||
if (t.isLoop(node)) {
|
||||
@ -403,7 +403,7 @@ var loopVisitor = {
|
||||
};
|
||||
|
||||
var loopLabelVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isLabeledStatement(node)) {
|
||||
state.innerLabels.push(node.label.name);
|
||||
}
|
||||
@ -436,7 +436,7 @@ BlockScoping.prototype.checkLoop = function () {
|
||||
};
|
||||
|
||||
var hoistVarDeclarationsVisitor = {
|
||||
enter: function (node, parent, scope, self) {
|
||||
enter(node, parent, scope, self) {
|
||||
if (t.isForStatement(node)) {
|
||||
if (isVar(node.init, node)) {
|
||||
node.init = t.sequenceExpression(self.pushDeclar(node.init));
|
||||
|
||||
@ -8,7 +8,7 @@ exports.check = function (node) {
|
||||
};
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) {
|
||||
var ids = t.getBindingIdentifiers(node);
|
||||
|
||||
|
||||
@ -38,7 +38,7 @@ exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
var breakVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isLoop(node)) {
|
||||
state.ignoreLabeless = true;
|
||||
scope.traverse(node, breakVisitor, state);
|
||||
|
||||
@ -15,7 +15,7 @@ var hasDefaults = function (node) {
|
||||
};
|
||||
|
||||
var iifeVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
if (!state.scope.hasOwnBinding(node.name)) return;
|
||||
if (state.scope.bindingIdentifierEquals(node.name, node)) return;
|
||||
|
||||
@ -305,7 +305,7 @@ TailCallTransformer.prototype.subTransformCallExpression = function (node) {
|
||||
|
||||
// looks for and replaces tail recursion calls
|
||||
var firstPass = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isIfStatement(node)) {
|
||||
if (t.isReturnStatement(node.alternate)) {
|
||||
t.ensureBlock(node, "alternate");
|
||||
@ -335,7 +335,7 @@ var firstPass = {
|
||||
// hoists up function declarations, replaces `this` and `arguments` and marks
|
||||
// them as needed
|
||||
var secondPass = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isThisExpression(node)) {
|
||||
state.needsThis = true;
|
||||
return state.getThisId();
|
||||
@ -357,7 +357,7 @@ var secondPass = {
|
||||
|
||||
// optimizes recursion by removing `this` and `arguments` if they aren't used
|
||||
var thirdPass = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isExpressionStatement(node)) return;
|
||||
|
||||
var expr = node.expression;
|
||||
|
||||
@ -12,7 +12,7 @@ var MATH_POW = t.memberExpression(t.identifier("Math"), t.identifier("pow"));
|
||||
build(exports, {
|
||||
operator: "**",
|
||||
|
||||
build: function (left, right) {
|
||||
build(left, right) {
|
||||
return t.callExpression(MATH_POW, [left, right]);
|
||||
}
|
||||
});
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
var t = require("../../../types");
|
||||
|
||||
var functionChildrenVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node) && !node._aliasFunction) {
|
||||
return this.skip();
|
||||
}
|
||||
@ -25,7 +25,7 @@ var functionChildrenVisitor = {
|
||||
};
|
||||
|
||||
var functionVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!node._aliasFunction) {
|
||||
if (t.isFunction(node)) {
|
||||
// stop traversal of this node as it'll be hit again by this transformer
|
||||
@ -47,10 +47,10 @@ var go = function (getBody, node, scope) {
|
||||
var thisId;
|
||||
|
||||
var state = {
|
||||
getArgumentsId: function () {
|
||||
getArgumentsId() {
|
||||
return argumentsId = argumentsId || scope.generateUidIdentifier("arguments");
|
||||
},
|
||||
getThisId: function () {
|
||||
getThisId() {
|
||||
return thisId = thisId || scope.generateUidIdentifier("this");
|
||||
}
|
||||
};
|
||||
|
||||
@ -13,7 +13,7 @@ var values = require("lodash/object/values");
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = {
|
||||
exit: function (node) {
|
||||
exit(node) {
|
||||
var hasChange = false;
|
||||
for (var i = 0; i < node.body.length; i++) {
|
||||
var bodyNode = node.body[i];
|
||||
|
||||
@ -17,7 +17,7 @@ exports.ExpressionStatement = function (node) {
|
||||
};
|
||||
|
||||
exports.IfStatement = {
|
||||
exit: function (node) {
|
||||
exit(node) {
|
||||
// todo: in scenarios where we can just return the consequent or
|
||||
// alternate we should drop the block statement if it contains no
|
||||
// block scoped variables
|
||||
|
||||
@ -10,11 +10,11 @@ exports.manipulateOptions = function (opts) {
|
||||
exports.optional = true;
|
||||
|
||||
require("../../helpers/build-react-transformer")(exports, {
|
||||
pre: function (state) {
|
||||
pre(state) {
|
||||
state.callee = state.tagExpr;
|
||||
},
|
||||
|
||||
post: function (state) {
|
||||
post(state) {
|
||||
if (react.isCompatTag(state.tagName)) {
|
||||
state.call = t.callExpression(
|
||||
t.memberExpression(
|
||||
|
||||
@ -27,7 +27,7 @@ exports.Program = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
require("../../helpers/build-react-transformer")(exports, {
|
||||
pre: function (state) {
|
||||
pre(state) {
|
||||
var tagName = state.tagName;
|
||||
var args = state.args;
|
||||
if (react.isCompatTag(tagName)) {
|
||||
@ -37,7 +37,7 @@ require("../../helpers/build-react-transformer")(exports, {
|
||||
}
|
||||
},
|
||||
|
||||
post: function (state, file) {
|
||||
post(state, file) {
|
||||
state.callee = file.get("jsxIdentifier");
|
||||
}
|
||||
});
|
||||
|
||||
@ -8,7 +8,7 @@ exports.check = function (node) {
|
||||
};
|
||||
|
||||
exports.Program = {
|
||||
enter: function (ast) {
|
||||
enter(ast) {
|
||||
regenerator.transform(ast);
|
||||
this.stop();
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ var ALIASABLE_CONSTRUCTORS = [
|
||||
];
|
||||
|
||||
var astVisitor = {
|
||||
enter: function (node, parent, scope, file) {
|
||||
enter(node, parent, scope, file) {
|
||||
var prop;
|
||||
|
||||
if (t.isMemberExpression(node) && t.isReferenced(node, parent)) {
|
||||
|
||||
@ -7,7 +7,7 @@ var t = require("../../../types");
|
||||
exports.playground = true;
|
||||
|
||||
build(exports, {
|
||||
is: function (node, file) {
|
||||
is(node, file) {
|
||||
var is = t.isAssignmentExpression(node) && node.operator === "||=";
|
||||
if (is) {
|
||||
var left = node.left;
|
||||
@ -18,7 +18,7 @@ build(exports, {
|
||||
}
|
||||
},
|
||||
|
||||
build: function (node) {
|
||||
build(node) {
|
||||
return t.unaryExpression("!", node, true);
|
||||
}
|
||||
});
|
||||
|
||||
@ -6,13 +6,13 @@ var t = require("../../../types");
|
||||
exports.playground = true;
|
||||
|
||||
build(exports, {
|
||||
is: function (node) {
|
||||
is(node) {
|
||||
var is = t.isAssignmentExpression(node) && node.operator === "?=";
|
||||
if (is) t.assertMemberExpression(node.left);
|
||||
return is;
|
||||
},
|
||||
|
||||
build: function (node, file) {
|
||||
build(node, file) {
|
||||
return t.unaryExpression(
|
||||
"!",
|
||||
t.callExpression(
|
||||
|
||||
@ -5,7 +5,7 @@ var t = require("../../../types");
|
||||
exports.playground = true;
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) return this.skip();
|
||||
|
||||
if (t.isReturnStatement(node) && node.argument) {
|
||||
|
||||
@ -60,11 +60,11 @@ TraversalPath.prototype.flatten = function () {
|
||||
};
|
||||
|
||||
Object.defineProperty(TraversalPath.prototype, "node", {
|
||||
get: function () {
|
||||
get() {
|
||||
return this.container[this.key];
|
||||
},
|
||||
|
||||
set: function (replacement) {
|
||||
set(replacement) {
|
||||
var isArray = Array.isArray(replacement);
|
||||
|
||||
// inherit comments from original node to the first replacement node
|
||||
|
||||
@ -181,7 +181,7 @@ Scope.prototype.rename = function (oldName, newName) {
|
||||
var scope = info.scope;
|
||||
|
||||
scope.traverse(scope.block, {
|
||||
enter: function (node, parent, scope) {
|
||||
enter(node, parent, scope) {
|
||||
if (t.isReferencedIdentifier(node, parent) && node.name === oldName) {
|
||||
node.name = newName;
|
||||
} else if (t.isDeclaration(node)) {
|
||||
@ -372,7 +372,7 @@ Scope.prototype.registerVariableDeclaration = function (declar) {
|
||||
};
|
||||
|
||||
var functionVariableVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFor(node)) {
|
||||
each(t.FOR_INIT_KEYS, function (key) {
|
||||
var declar = node[key];
|
||||
@ -413,7 +413,7 @@ Scope.prototype.hasGlobal = function (name) {
|
||||
};
|
||||
|
||||
var programReferenceVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isReferencedIdentifier(node, parent) && !scope.hasBinding(node.name)) {
|
||||
state.addGlobal(node);
|
||||
} else if (t.isLabeledStatement(node)) {
|
||||
@ -425,7 +425,7 @@ var programReferenceVisitor = {
|
||||
};
|
||||
|
||||
var blockVariableVisitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunctionDeclaration(node) || t.isBlockScoped(node)) {
|
||||
state.registerDeclaration(node);
|
||||
} else if (t.isScope(node, parent)) {
|
||||
|
||||
@ -65,7 +65,7 @@ exports.booleanify = function (val) {
|
||||
};
|
||||
|
||||
var templateVisitor = {
|
||||
enter: function (node, parent, scope, nodes) {
|
||||
enter(node, parent, scope, nodes) {
|
||||
if (t.isExpressionStatement(node)) {
|
||||
node = node.expression;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user