babel-plugin-es5-array-autopolyfill
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -34,23 +34,31 @@ 'use strict'; | ||
CallExpression: function CallExpression(path) { | ||
var property = path.get('callee.property').node; | ||
if (!t.isIdentifier(property) || replaceProperties.indexOf(property.name) < 0) { | ||
return; | ||
} | ||
CallExpression: { | ||
exit: function exit(path) { | ||
var annotation; | ||
var property = path.get('callee.property').node; | ||
if (!isObjectCaller(t, property)) { | ||
return; | ||
} | ||
var identifierPath = path.get("callee.object", true); | ||
var identifierPath = path.get("callee.object", true); | ||
if (!identifierPath.isNodeType("Identifier") && !identifierPath.isNodeType("TypeCastExpression")) { | ||
return; | ||
} | ||
if (!identifierPath.isNodeType("Identifier") && !identifierPath.isNodeType("TypeCastExpression")) { | ||
return; | ||
} | ||
annotation = identifierPath.getTypeAnnotation(); | ||
var annotation = identifierPath.getTypeAnnotation(); | ||
if (!isArrayType(t, annotation)) { | ||
return; | ||
if (!isArrayType(t, annotation)) { | ||
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'))); | ||
} else { | ||
path.replaceWith(expression); | ||
} | ||
} | ||
var callee = path.node.callee; | ||
usedMethods[property.name] = true; | ||
property.name = prefix + property.name; | ||
path.replaceWith(t.callExpression(property, [callee.object].concat(path.node.arguments))); | ||
} | ||
@@ -62,2 +70,3 @@ } | ||
var replaceProperties = ['reduce', 'map', 'filter', 'forEach']; | ||
var arrayReturn = ['map', 'filter']; | ||
var prefix = "es5"; | ||
@@ -75,2 +84,10 @@ | ||
return false; | ||
} | ||
function isObjectCaller(t, property) { | ||
return t.isIdentifier(property) && property && replaceProperties.indexOf(property.name) >= 0; | ||
} | ||
function isFunctionTypeCastCaller(t, path, prop) { | ||
return t.isTypeCastExpression(path.parentPath.node) && !prop; | ||
} |
{ | ||
"name": "babel-plugin-es5-array-autopolyfill", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -14,3 +14,2 @@ import path from 'path'; | ||
fs.readdirSync(fixturesDir).map((caseName) => { | ||
it(`should ${caseName.split('-').join(' ')}`, () => { | ||
@@ -17,0 +16,0 @@ const fixtureDir = path.join(fixturesDir, caseName); |
7562
21
137