Commit modules

This commit is contained in:
Federico Grandi
2019-12-14 23:17:51 +01:00
parent 8063b07a5a
commit a88246a48b
4243 changed files with 1074761 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
# babel-plugin-transform-regexp-constructors
This changes RegExp constructors into literals if the RegExp arguments are strings.
## Example
**In**
```javascript
const foo = 'ab+';
var a = new RegExp(foo+'c', 'i');
```
**Out**
```javascript
const foo = 'ab+';
var a = /ab+c/i;
```
## Installation
```sh
npm install babel-plugin-transform-regexp-constructors --save-dev
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-regexp-constructors"]
}
```
### Via CLI
```sh
babel --plugins transform-regexp-constructors script.js
```
### Via Node API
```javascript
require("@babel/core").transform("code", {
plugins: ["transform-regexp-constructors"]
});
```

View File

@@ -0,0 +1,53 @@
"use strict";
function createRegExpLiteral(args, prettify, t) {
const evaluatedArgs = args.map(a => a.evaluate());
if (!evaluatedArgs.every(a => a.confident && typeof a.value === "string")) {
return;
}
let pattern = evaluatedArgs.length >= 1 && evaluatedArgs[0].value !== "" ? evaluatedArgs[0].value : "(?:)";
const flags = evaluatedArgs.length >= 2 ? evaluatedArgs[1].value : "";
pattern = new RegExp(pattern).source;
if (prettify) {
pattern = pattern.replace(/\n/g, "\\n").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/[\b]/g, "[\\b]").replace(/\v/g, "\\v").replace(/\f/g, "\\f").replace(/\r/g, "\\r");
}
pattern = pattern.replace(/\0/g, "\\0");
return t.regExpLiteral(pattern, flags);
}
function maybeReplaceWithRegExpLiteral(path, t) {
if (!t.isIdentifier(path.node.callee, {
name: "RegExp"
})) {
return;
}
const regExpLiteral = createRegExpLiteral(path.get("arguments"), true, t);
if (regExpLiteral) {
path.replaceWith(regExpLiteral);
}
}
module.exports = function ({
types: t
}) {
return {
name: "transform-regexp-constructors",
visitor: {
NewExpression(path) {
maybeReplaceWithRegExpLiteral(path, t);
},
CallExpression(path) {
// equivalent to `new RegExp()` according to §21.2.3
maybeReplaceWithRegExpLiteral(path, t);
}
}
};
};

View File

@@ -0,0 +1,50 @@
{
"_args": [
[
"babel-plugin-transform-regexp-constructors@0.4.3",
"C:\\EndBug\\GitHub\\add-and-commit"
]
],
"_development": true,
"_from": "babel-plugin-transform-regexp-constructors@0.4.3",
"_id": "babel-plugin-transform-regexp-constructors@0.4.3",
"_inBundle": false,
"_integrity": "sha1-WLd3W2OvzzMyj66aX4j71PsLSWU=",
"_location": "/babel-plugin-transform-regexp-constructors",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "babel-plugin-transform-regexp-constructors@0.4.3",
"name": "babel-plugin-transform-regexp-constructors",
"escapedName": "babel-plugin-transform-regexp-constructors",
"rawSpec": "0.4.3",
"saveSpec": null,
"fetchSpec": "0.4.3"
},
"_requiredBy": [
"/babel-preset-minify"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-transform-regexp-constructors/-/babel-plugin-transform-regexp-constructors-0.4.3.tgz",
"_spec": "0.4.3",
"_where": "C:\\EndBug\\GitHub\\add-and-commit",
"author": {
"name": "shinew"
},
"bugs": {
"url": "https://github.com/babel/minify/issues"
},
"description": "This changes RegExp constructors into literals if the RegExp arguments are strings.",
"homepage": "https://github.com/babel/minify#readme",
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"name": "babel-plugin-transform-regexp-constructors",
"repository": {
"type": "git",
"url": "https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-regexp-constructors"
},
"version": "0.4.3"
}