babel-plugin-transform-regexp-constructors
Advanced tools
Comparing version 0.0.1 to 0.0.3
"use strict"; | ||
function createRegExpLiteral(args, prettify, t) { | ||
var evaluatedArgs = args.map(function (a) { | ||
return a.evaluate(); | ||
}); | ||
if (!evaluatedArgs.every(function (a) { | ||
return a.confident === true && typeof a.value === "string"; | ||
})) { | ||
return; | ||
} | ||
var pattern = evaluatedArgs.length >= 1 && evaluatedArgs[0].value !== "" ? evaluatedArgs[0].value : "(?:)"; | ||
var flags = evaluatedArgs.length >= 2 ? evaluatedArgs[1].value : ""; | ||
pattern = new RegExp(pattern).source; | ||
if (prettify) { | ||
pattern = pattern.replace(/\n/g, "\\n").replace(/[\b]/g, "[\\b]").replace(/\v/g, "\\v").replace(/\f/g, "\\f").replace(/\r/g, "\\r"); | ||
} | ||
return t.regExpLiteral(pattern, flags); | ||
} | ||
function maybeReplaceWithRegExpLiteral(path, t) { | ||
if (!t.isIdentifier(path.node.callee, { name: "RegExp" })) { | ||
return; | ||
} | ||
var regExpLiteral = createRegExpLiteral(path.get("arguments"), true, t); | ||
if (regExpLiteral) { | ||
path.replaceWith(regExpLiteral); | ||
} | ||
} | ||
module.exports = function (_ref) { | ||
@@ -10,16 +39,7 @@ var t = _ref.types; | ||
NewExpression: function NewExpression(path) { | ||
if (!t.isIdentifier(path.node.callee, { name: "RegExp" })) { | ||
return; | ||
} | ||
var evaluatedArgs = path.get("arguments").map(function (a) { | ||
return a.evaluate(); | ||
}); | ||
if (!evaluatedArgs.every(function (a) { | ||
return a.confident === true && typeof a.value === "string"; | ||
})) { | ||
return; | ||
} | ||
var pattern = evaluatedArgs.length >= 1 && evaluatedArgs[0].value !== "" ? evaluatedArgs[0].value : "(?:)"; | ||
var flags = evaluatedArgs.length >= 2 ? evaluatedArgs[1].value : ""; | ||
path.replaceWith(t.regExpLiteral(pattern, flags)); | ||
maybeReplaceWithRegExpLiteral(path, t); | ||
}, | ||
CallExpression: function CallExpression(path) { | ||
// equivalent to `new RegExp()` according to §21.2.3 | ||
maybeReplaceWithRegExpLiteral(path, t); | ||
} | ||
@@ -26,0 +46,0 @@ } |
{ | ||
"name": "babel-plugin-transform-regexp-constructors", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "This changes RegExp constructors into literals if the RegExp arguments are strings.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/babel/babili#readme", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
2689
41
1