New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-regexp-constructors

Package Overview
Dependencies
Maintainers
4
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-regexp-constructors - npm Package Compare versions

Comparing version 0.0.1 to 0.0.3

48

lib/index.js
"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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc