es6-spread
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "es6-spread", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/square/es6-spread", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -0,1 +1,5 @@ | ||
## v0.0.4 | ||
* Don't bother creating temporary variables for `this`. | ||
## v0.0.3 | ||
@@ -2,0 +6,0 @@ |
@@ -5,2 +5,3 @@ /* jshint node:true, undef:true, unused:true */ | ||
var through = require('through'); | ||
var esprima = require('esprima'); | ||
@@ -11,4 +12,5 @@ var recast = require('recast'); | ||
var b = types.builders; | ||
var NodePath = types.NodePath; | ||
var util = require('ast-util'); | ||
assert.ok( | ||
@@ -19,4 +21,2 @@ /harmony/.test(esprima.version), | ||
var ExpressionWithoutSideEffects = types.Type.or(n.Identifier, n.ThisExpression); | ||
/** | ||
@@ -28,3 +28,3 @@ * Visits a node of an AST looking for spread elements in arguments. This is | ||
* @param {Object} node | ||
* @this {ast-types.Path} | ||
* @this {ast-types.NodePath} | ||
*/ | ||
@@ -38,3 +38,3 @@ function visitNode(node) { | ||
if (n.MemberExpression.check(callee)) { | ||
if (ExpressionWithoutSideEffects.check(callee.object)) { | ||
if (n.ThisExpression.check(callee.object)) { | ||
// foo.bar(...a), safe to use `foo` as context | ||
@@ -44,15 +44,7 @@ context = callee.object; | ||
// foo().bar(...a), not safe to use `foo()` as context | ||
var scopeBody = this.scope.node.body; | ||
var scope = this.scope; | ||
if (n.BlockStatement.check(scopeBody)) { | ||
scopeBody = scopeBody.body; | ||
} | ||
// var $__0; | ||
context = uniqueIdentifierForScope(this.scope); | ||
scopeBody.unshift( | ||
b.variableDeclaration( | ||
'var', [b.variableDeclarator(context, null)] | ||
) | ||
); | ||
context = util.uniqueIdentifier(scope); | ||
util.injectVariable(scope, context); | ||
@@ -81,3 +73,3 @@ // ($__0 = foo()).bar(...a) | ||
), | ||
[context, buildConcatExpression(node.arguments)] | ||
[context, buildConcatExpression.call(this, node.arguments)] | ||
)); | ||
@@ -88,3 +80,3 @@ } | ||
// [1, ...a] -> [1].concat(a) | ||
this.replace(buildConcatExpression(node.elements)); | ||
this.replace(buildConcatExpression.call(this, node.elements)); | ||
} | ||
@@ -96,17 +88,7 @@ } else if (n.NewExpression.check(node)) { | ||
b.newExpression( | ||
b.callExpression( | ||
b.memberExpression( | ||
b.memberExpression( | ||
b.memberExpression( | ||
b.identifier('Function'), | ||
b.identifier('prototype'), | ||
false | ||
), | ||
b.identifier('bind'), | ||
false | ||
), | ||
b.identifier('apply'), | ||
false | ||
), | ||
[node.callee, buildConcatExpression([b.literal(null)].concat(node.arguments))] | ||
util.callFunctionBind( | ||
this.scope.getGlobalScope(), | ||
node.callee, | ||
b.literal(null), | ||
buildConcatExpression.call(this, node.arguments) | ||
), | ||
@@ -127,2 +109,3 @@ [] | ||
* @return {ast-types.Expression} | ||
* @this {ast-types.NodePath} | ||
*/ | ||
@@ -133,2 +116,3 @@ function buildConcatExpression(elements) { | ||
var remainder; | ||
var scope = this.scope; | ||
@@ -142,17 +126,5 @@ elements.forEach(function(element) { | ||
arrays.push( | ||
b.callExpression( | ||
b.memberExpression( | ||
b.memberExpression( | ||
b.memberExpression( | ||
b.identifier('Array'), | ||
b.identifier('prototype'), | ||
false | ||
), | ||
b.identifier('slice'), | ||
false | ||
), | ||
b.identifier('call'), | ||
false | ||
), | ||
[element.argument] | ||
util.callArraySlice( | ||
scope.getGlobalScope(), | ||
element.argument | ||
) | ||
@@ -187,29 +159,3 @@ ); | ||
var nextId = 0; | ||
/** | ||
* Generates a unique identifier for use as a variable. | ||
* | ||
* @private | ||
*/ | ||
function uniqueIdentifierForScope(scope) { | ||
var result; | ||
while (scope.declares(result = '$__' + nextId)) { | ||
nextId++; | ||
} | ||
var identifier = b.identifier(result); | ||
// Ensure this identifier counts as used in this scope. | ||
var bindings = scope.getBindings(); | ||
if (!Object.prototype.hasOwnProperty(bindings, result)) { | ||
bindings[result] = []; | ||
} | ||
bindings[result].push(new NodePath(identifier)); | ||
return identifier; | ||
} | ||
/** | ||
* Transform an Esprima AST generated from ES6 by replacing all spread elements | ||
@@ -216,0 +162,0 @@ * with an equivalent approach in ES5. |
{ | ||
"name": "es6-spread", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "ES6 spread compiled to ES5.", | ||
@@ -15,5 +15,6 @@ "main": "lib/index.js", | ||
"esprima": "git://github.com/ariya/esprima.git#harmony", | ||
"recast": "^0.5.13", | ||
"ast-types": "^0.3.23", | ||
"through": "~2.3.4" | ||
"recast": "^0.5.17", | ||
"ast-types": "^0.3.27", | ||
"through": "~2.3.4", | ||
"ast-util": "^0.0.5" | ||
}, | ||
@@ -20,0 +21,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12642
5
15
309
+ Addedast-util@^0.0.5
+ Addedast-util@0.0.5(transitive)
Updatedast-types@^0.3.27
Updatedrecast@^0.5.17