6.0.0
I'm extremely stupid and didn't commit as I go. To anyone reading this I'm extremely sorry. A lot of these changes are very broad and I plan on releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm afraid I couldn't wait. If you're ever in London I'll buy you a beer (or assorted beverage!) to make up for it, also I'll kiss your feet and give you a back massage, maybe.
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-es2015-parameters",
|
||||
"version": "1.0.0",
|
||||
"version": "5.10.32",
|
||||
"description": "Compile ES2015 default and rest parameters to ES5",
|
||||
"repository": "babel/babel",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"babel-traverse": "^5.10.32",
|
||||
"babel-helper-call-delegate": "^5.0.0",
|
||||
"babel-helper-get-function-arity": "^5.0.0",
|
||||
"babel-template": "^5.10.32",
|
||||
"babel-types": "^5.10.32",
|
||||
"babel-runtime": "^5.10.32"
|
||||
},
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
]
|
||||
}
|
||||
}
|
||||
149
packages/babel-plugin-transform-es2015-parameters/src/default.js
Normal file
149
packages/babel-plugin-transform-es2015-parameters/src/default.js
Normal file
@@ -0,0 +1,149 @@
|
||||
import getFunctionArity from "babel-helper-get-function-arity";
|
||||
import callDelegate from "babel-helper-call-delegate";
|
||||
import template from "babel-template";
|
||||
import * as t from "babel-types";
|
||||
|
||||
let buildDefaultParam = template(`
|
||||
let VARIABLE_NAME =
|
||||
ARGUMENTS.length <= ARGUMENT_KEY || ARGUMENTS[ARGUMENT_KEY] === undefined ?
|
||||
DEFAULT_VALUE
|
||||
:
|
||||
ARGUMENTS[ARGUMENT_KEY];
|
||||
`);
|
||||
|
||||
let buildDefaultParamAssign = template(`
|
||||
if (VARIABLE_NAME === undefined) VARIABLE_NAME = DEFAULT_VALUE;
|
||||
`);
|
||||
|
||||
let buildCutOff = template(`
|
||||
let $0 = arguments[$1];
|
||||
`);
|
||||
|
||||
function hasDefaults(node) {
|
||||
for (let param of (node.params: Array<Object>)) {
|
||||
if (!t.isIdentifier(param)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
let iifeVisitor = {
|
||||
ReferencedIdentifier(path, state) {
|
||||
let name = path.node.name;
|
||||
if (name === "eval" || (path.scope.hasOwnBinding(name) && path.scope.getOwnBinding(name).kind !== "param")) {
|
||||
state.iife = true;
|
||||
path.stop();
|
||||
}
|
||||
},
|
||||
|
||||
Scope(path) {
|
||||
// different bindings
|
||||
path.skip();
|
||||
}
|
||||
};
|
||||
|
||||
export let visitor = {
|
||||
Function(path) {
|
||||
let { node, scope } = path;
|
||||
if (!hasDefaults(node)) return;
|
||||
|
||||
// ensure it's a block, useful for arrow functions
|
||||
path.ensureBlock();
|
||||
|
||||
let state = {
|
||||
iife: false,
|
||||
scope: scope
|
||||
};
|
||||
|
||||
let body = [];
|
||||
|
||||
//
|
||||
let argsIdentifier = t.identifier("arguments");
|
||||
argsIdentifier._shadowedFunctionLiteral = path;
|
||||
|
||||
// push a default parameter definition
|
||||
function pushDefNode(left, right, i) {
|
||||
let defNode;
|
||||
if (exceedsLastNonDefault(i) || t.isPattern(left)) {
|
||||
defNode = buildDefaultParam({
|
||||
VARIABLE_NAME: left,
|
||||
DEFAULT_VALUE: right,
|
||||
ARGUMENT_KEY: t.numberLiteral(i),
|
||||
ARGUMENTS: argsIdentifier
|
||||
});
|
||||
} else {
|
||||
defNode = buildDefaultParamAssign({
|
||||
VARIABLE_NAME: left,
|
||||
DEFAULT_VALUE: right
|
||||
});
|
||||
}
|
||||
defNode._blockHoist = node.params.length - i;
|
||||
body.push(defNode);
|
||||
}
|
||||
|
||||
// check if an index exceeds the functions arity
|
||||
function exceedsLastNonDefault(i) {
|
||||
return i + 1 > lastNonDefaultParam;
|
||||
}
|
||||
|
||||
//
|
||||
let lastNonDefaultParam = getFunctionArity(node);
|
||||
|
||||
//
|
||||
let params = path.get("params");
|
||||
for (let i = 0; i < params.length; i++) {
|
||||
let param = params[i];
|
||||
|
||||
if (!param.isAssignmentPattern()) {
|
||||
if (!param.isIdentifier()) {
|
||||
param.traverse(iifeVisitor, state);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
let left = param.get("left");
|
||||
let right = param.get("right");
|
||||
|
||||
//
|
||||
if (exceedsLastNonDefault(i) || left.isPattern()) {
|
||||
let placeholder = scope.generateUidIdentifier("x");
|
||||
placeholder._isDefaultPlaceholder = true;
|
||||
node.params[i] = placeholder;
|
||||
} else {
|
||||
node.params[i] = left.node;
|
||||
}
|
||||
|
||||
//
|
||||
if (!state.iife) {
|
||||
if (right.isIdentifier() && scope.hasOwnBinding(right.node.name) && scope.getOwnBinding(right.node.name).kind !== "param") {
|
||||
// the right hand side references a parameter
|
||||
state.iife = true;
|
||||
} else {
|
||||
right.traverse(iifeVisitor, state);
|
||||
}
|
||||
}
|
||||
|
||||
pushDefNode(left.node, right.node, i);
|
||||
}
|
||||
|
||||
// add declarations for trailing parameters
|
||||
for (let i = lastNonDefaultParam + 1; i < node.params.length; i++) {
|
||||
let param = node.params[i];
|
||||
if (param._isDefaultPlaceholder) continue;
|
||||
|
||||
let declar = buildCutOff(param, t.numberLiteral(i));
|
||||
declar._blockHoist = node.params.length - i;
|
||||
body.push(declar);
|
||||
}
|
||||
|
||||
// we need to cut off all trailing parameters
|
||||
node.params = node.params.slice(0, lastNonDefaultParam);
|
||||
|
||||
if (state.iife) {
|
||||
body.push(callDelegate(path, scope));
|
||||
path.set("body", t.blockStatement(body));
|
||||
} else {
|
||||
path.get("body").unshiftContainer("body", body);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import * as t from "babel-types";
|
||||
|
||||
export let visitor = {
|
||||
Function(path) {
|
||||
let params: Array = path.get("params");
|
||||
|
||||
for (let i = 0; i < params.length; i++) {
|
||||
let param = params[i];
|
||||
if (param.isArrayPattern() || param.isObjectPattern()) {
|
||||
let uid = path.scope.generateUidIdentifier("ref");
|
||||
|
||||
let declar = t.variableDeclaration("let", [
|
||||
t.variableDeclarator(param.node, uid)
|
||||
]);
|
||||
declar._blockHoist = params.length - i;
|
||||
|
||||
path.ensureBlock();
|
||||
path.get("body").unshiftContainer("body", declar);
|
||||
|
||||
param.replaceWith(uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,23 @@
|
||||
import type { NodePath } from "babel-traverse";
|
||||
import { visitors } from "babel-traverse";
|
||||
|
||||
import * as destructuring from "./destructuring";
|
||||
import * as def from "./default";
|
||||
import * as rest from "./rest";
|
||||
|
||||
export default function () {
|
||||
return {
|
||||
visitor: {
|
||||
// your visitor methods go here
|
||||
}
|
||||
visitor: visitors.merge([{
|
||||
ArrowFunctionExpression(path) {
|
||||
// default/rest visitors require access to `arguments`
|
||||
let params: Array<NodePath> = path.get("params");
|
||||
for (let param of params) {
|
||||
if (param.isRestElement() || param.isAssignmentPattern()) {
|
||||
path.arrowFunctionToShadowed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}, destructuring.visitor, rest.visitor, def.visitor])
|
||||
};
|
||||
}
|
||||
|
||||
230
packages/babel-plugin-transform-es2015-parameters/src/rest.js
Normal file
230
packages/babel-plugin-transform-es2015-parameters/src/rest.js
Normal file
@@ -0,0 +1,230 @@
|
||||
import template from "babel-template";
|
||||
import * as t from "babel-types";
|
||||
|
||||
let buildRest = template(`
|
||||
for (var LEN = ARGUMENTS.length,
|
||||
ARRAY = Array(ARRAY_LEN),
|
||||
KEY = START;
|
||||
KEY < LEN;
|
||||
KEY++) {
|
||||
ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
|
||||
}
|
||||
`);
|
||||
|
||||
let memberExpressionOptimisationVisitor = {
|
||||
Scope(path, state) {
|
||||
// check if this scope has a local binding that will shadow the rest parameter
|
||||
if (!path.scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
|
||||
path.skip();
|
||||
}
|
||||
},
|
||||
|
||||
Flow(path) {
|
||||
// don't touch reference in type annotations
|
||||
path.skip();
|
||||
},
|
||||
|
||||
Function(path, state) {
|
||||
// skip over functions as whatever `arguments` we reference inside will refer
|
||||
// to the wrong function
|
||||
let oldNoOptimise = state.noOptimise;
|
||||
state.noOptimise = true;
|
||||
path.traverse(memberExpressionOptimisationVisitor, state);
|
||||
state.noOptimise = oldNoOptimise;
|
||||
path.skip();
|
||||
},
|
||||
|
||||
ReferencedIdentifier(path, state) {
|
||||
let { node } = path;
|
||||
|
||||
// we can't guarantee the purity of arguments
|
||||
if (node.name === "arguments") {
|
||||
state.deopted = true;
|
||||
}
|
||||
|
||||
// is this a referenced identifier and is it referencing the rest parameter?
|
||||
if (node.name !== state.name) return;
|
||||
|
||||
if (state.noOptimise) {
|
||||
state.deopted = true;
|
||||
} else {
|
||||
if (path.parentPath.isMemberExpression({ computed: true, object: node })) {
|
||||
// if we know that this member expression is referencing a number then we can safely
|
||||
// optimise it
|
||||
let prop = path.parentPath.get("property");
|
||||
if (prop.isBaseType("number")) {
|
||||
state.candidates.push(path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// optimise single spread args in calls
|
||||
if (path.parentPath.isSpreadElement() && state.offset === 0) {
|
||||
let call = path.parentPath.parentPath;
|
||||
if (call.isCallExpression() && call.node.arguments.length === 1) {
|
||||
state.candidates.push(path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
state.references.push(path);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Deopt on use of a binding identifier with the same name as our rest param.
|
||||
*
|
||||
* See https://github.com/babel/babel/issues/2091
|
||||
*/
|
||||
|
||||
BindingIdentifier({ node }, state) {
|
||||
if (node.name === state.name) {
|
||||
state.deopted = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function optimiseMemberExpression(parent, offset) {
|
||||
if (offset === 0) return;
|
||||
|
||||
let newExpr;
|
||||
let prop = parent.property;
|
||||
|
||||
if (t.isLiteral(prop)) {
|
||||
prop.value += offset;
|
||||
prop.raw = String(prop.value);
|
||||
} else { // // UnaryExpression, BinaryExpression
|
||||
newExpr = t.binaryExpression("+", prop, t.numberLiteral(offset));
|
||||
parent.property = newExpr;
|
||||
}
|
||||
}
|
||||
|
||||
function hasRest(node) {
|
||||
return t.isRestElement(node.params[node.params.length - 1]);
|
||||
}
|
||||
|
||||
export let visitor = {
|
||||
Function(path) {
|
||||
let { node, scope } = path;
|
||||
if (!hasRest(node)) return;
|
||||
|
||||
let restParam = node.params.pop();
|
||||
let rest = restParam.argument;
|
||||
|
||||
let argsId = t.identifier("arguments");
|
||||
|
||||
// otherwise `arguments` will be remapped in arrow functions
|
||||
argsId._shadowedFunctionLiteral = path;
|
||||
|
||||
// support patterns
|
||||
if (t.isPattern(rest)) {
|
||||
let pattern = rest;
|
||||
rest = scope.generateUidIdentifier("ref");
|
||||
|
||||
let declar = t.variableDeclaration("let", pattern.elements.map(function (elem, index) {
|
||||
let accessExpr = t.memberExpression(rest, t.numberLiteral(index), true);
|
||||
return t.variableDeclarator(elem, accessExpr);
|
||||
}));
|
||||
node.body.body.unshift(declar);
|
||||
}
|
||||
|
||||
// check and optimise for extremely common cases
|
||||
let state = {
|
||||
references: [],
|
||||
offset: node.params.length,
|
||||
|
||||
argumentsNode: argsId,
|
||||
outerBinding: scope.getBindingIdentifier(rest.name),
|
||||
|
||||
// candidate member expressions we could optimise if there are no other references
|
||||
candidates: [],
|
||||
|
||||
// local rest binding name
|
||||
name: rest.name,
|
||||
|
||||
// whether any references to the rest parameter were made in a function
|
||||
deopted: false
|
||||
};
|
||||
|
||||
path.traverse(memberExpressionOptimisationVisitor, state);
|
||||
|
||||
if (!state.deopted && !state.references.length) {
|
||||
// we only have shorthands and there are no other references
|
||||
if (state.candidates.length) {
|
||||
for (let candidate of (state.candidates: Array)) {
|
||||
candidate.replaceWith(argsId);
|
||||
if (candidate.parentPath.isMemberExpression()) {
|
||||
optimiseMemberExpression(candidate.parent, state.offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
state.references = state.references.concat(state.candidates);
|
||||
}
|
||||
|
||||
// deopt shadowed functions as transforms like regenerator may try touch the allocation loop
|
||||
state.deopted = state.deopted || !!node.shadow;
|
||||
|
||||
//
|
||||
|
||||
let start = t.numberLiteral(node.params.length);
|
||||
let key = scope.generateUidIdentifier("key");
|
||||
let len = scope.generateUidIdentifier("len");
|
||||
|
||||
let arrKey = key;
|
||||
let arrLen = len;
|
||||
if (node.params.length) {
|
||||
// this method has additional params, so we need to subtract
|
||||
// the index of the current argument position from the
|
||||
// position in the array that we want to populate
|
||||
arrKey = t.binaryExpression("-", key, start);
|
||||
|
||||
// we need to work out the size of the array that we're
|
||||
// going to store all the rest parameters
|
||||
//
|
||||
// we need to add a check to avoid constructing the array
|
||||
// with <0 if there are less arguments than params as it'll
|
||||
// cause an error
|
||||
arrLen = t.conditionalExpression(
|
||||
t.binaryExpression(">", len, start),
|
||||
t.binaryExpression("-", len, start),
|
||||
t.numberLiteral(0)
|
||||
);
|
||||
}
|
||||
|
||||
let loop = buildRest({
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
ARRAY_LEN: arrLen,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key,
|
||||
LEN: len,
|
||||
});
|
||||
|
||||
if (state.deopted) {
|
||||
loop._blockHoist = node.params.length + 1;
|
||||
node.body.body.unshift(loop);
|
||||
} else {
|
||||
// perform allocation at the lowest common ancestor of all references
|
||||
loop._blockHoist = 1;
|
||||
|
||||
let target = path.getEarliestCommonAncestorFrom(state.references).getStatementParent();
|
||||
|
||||
// don't perform the allocation inside a loop
|
||||
let highestLoop;
|
||||
target.findParent(function (path) {
|
||||
if (path.isLoop()) {
|
||||
highestLoop = path;
|
||||
} else if (path.isFunction()) {
|
||||
// stop crawling up for functions
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (highestLoop) target = highestLoop;
|
||||
|
||||
target.insertBefore(loop);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user