babel-plugin-es5-array-autopolyfill
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -41,3 +41,2 @@ 'use strict'; | ||
} | ||
var identifierPath = path.get("callee.object", true); | ||
@@ -48,2 +47,8 @@ if (!identifierPath.isNodeType("Identifier") && !identifierPath.isNodeType("TypeCastExpression")) { | ||
var callee = path.node.callee; | ||
if (isObjectKeysExpression(identifierPath, property)) { | ||
return replaceObjectKeys(t, path, property, usedMethods); | ||
} | ||
annotation = identifierPath.getTypeAnnotation(); | ||
@@ -54,9 +59,20 @@ | ||
} | ||
if (isArrayReturn(property.name) && !isReplacable(property.name) && !t.isTypeCastExpression(path.parentPath.node)) { | ||
path.replaceWith(t.typeCastExpression(t.callExpression(callee, path.node.arguments), t.arrayTypeAnnotation(t.identifier('Any')))); | ||
return; | ||
} | ||
if (!isReplacable(property.name)) { | ||
return; | ||
} | ||
var oldName = property.name; | ||
property.name = prefix + property.name; | ||
var callee = path.node.callee; | ||
var expression = t.callExpression(property, [callee.object].concat(path.node.arguments)); | ||
usedMethods[oldName] = true; | ||
if (arrayReturn.indexOf(oldName) >= 0) { | ||
path.replaceWith(t.typeCastExpression(expression, t.arrayTypeAnnotation('Any'))); | ||
path.replaceWith(t.typeCastExpression(expression, t.arrayTypeAnnotation(t.identifier('Any')))); | ||
} else { | ||
@@ -72,3 +88,3 @@ path.replaceWith(expression); | ||
var replaceProperties = ['reduce', 'map', 'filter', 'forEach']; | ||
var arrayReturn = ['map', 'filter']; | ||
var arrayReturn = ['map', 'filter', 'concat', 'slice', 'sort', 'splice']; | ||
var prefix = "es5"; | ||
@@ -89,7 +105,21 @@ | ||
function isObjectCaller(t, property) { | ||
return t.isIdentifier(property) && property && replaceProperties.indexOf(property.name) >= 0; | ||
return t.isIdentifier(property) && property; | ||
} | ||
function isFunctionTypeCastCaller(t, path, prop) { | ||
return t.isTypeCastExpression(path.parentPath.node) && !prop; | ||
function isReplacable(name) { | ||
return replaceProperties.indexOf(name) >= 0; | ||
} | ||
function isArrayReturn(name) { | ||
return arrayReturn.indexOf(name) >= 0; | ||
} | ||
function isObjectKeysExpression(path, prop) { | ||
return path.node.name === 'Object' && prop.name === 'keys' && !path.scope.getBinding('Object'); | ||
} | ||
function replaceObjectKeys(t, path, property, usedMethods) { | ||
usedMethods[property.name] = true; | ||
property.name = prefix + property.name; | ||
path.replaceWith(t.typeCastExpression(t.callExpression(property, path.node.arguments), t.arrayTypeAnnotation(t.identifier('String')))); | ||
} |
{ | ||
"name": "babel-plugin-es5-array-autopolyfill", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
9470
27
173