babel-plugin-es5-array-autopolyfill
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -10,4 +10,26 @@ 'use strict'; | ||
var usedMethods = {}; | ||
return { | ||
visitor: { | ||
Program: { | ||
enter: function enter(path) { | ||
var usedMethods = {}; | ||
}, | ||
exit: function exit(path, state) { | ||
var libName = state.opts.polyfill; | ||
var methods = Object.keys(usedMethods).sort(); | ||
if (methods.length === 0) { | ||
return; | ||
} | ||
var specifiers = methods.map(function (el) { | ||
return t.importSpecifier(t.identifier(prefix + el), t.identifier(el)); | ||
}); | ||
var importDecl = t.importDeclaration(specifiers, t.stringLiteral(libName)); | ||
path.node.body = [importDecl].concat(path.node.body); | ||
} | ||
}, | ||
CallExpression: function CallExpression(path) { | ||
@@ -20,2 +42,4 @@ var callee = path.node.callee; | ||
if (t.isIdentifier(property) && replaceProperties.indexOf(property.name) >= 0) { | ||
usedMethods[property.name] = true; | ||
property.name = prefix + property.name; | ||
path.replaceWith(t.callExpression(property, [callee.object].concat(path.node.arguments))); | ||
@@ -28,2 +52,3 @@ } | ||
var replaceProperties = ['reduce', 'map', 'filter', 'forEach']; | ||
var replaceProperties = ['reduce', 'map', 'filter', 'forEach']; | ||
var prefix = "es5"; |
{ | ||
"name": "babel-plugin-es5-array-autopolyfill", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,11 +0,12 @@ | ||
map(arr, el => el + 1); | ||
map(arr, (el, i) => el + i); | ||
import { filter as es5filter, forEach as es5forEach, map as es5map, reduce as es5reduce } from "es5-collection-methods"; | ||
es5map(arr, el => el + 1); | ||
es5map(arr, (el, i) => el + i); | ||
filter(arr, el => !el); | ||
es5filter(arr, el => !el); | ||
reduce(arr, (acc, el) => acc + el, {}); | ||
es5reduce(arr, (acc, el) => acc + el, {}); | ||
forEach(arr, el => t(el)); | ||
forEach(arr, (el, i) => t(el, i)); | ||
es5forEach(arr, el => t(el)); | ||
es5forEach(arr, (el, i) => t(el, i)); | ||
obj.somMethid(1, 2, 3); |
Sorry, the diff of this file is not supported yet
4558
12
89