Import babel-plugin-transform-dotall-regex (#7059)

Original: https://github.com/mathiasbynens/babel-plugin-transform-dotall-regex

Moving it into the official Babel repository makes it easier to maintain the transform.
This commit is contained in:
Mathias Bynens 2017-12-19 17:01:59 +01:00 committed by Henry Zhu
parent 2b065350b5
commit 44da8201a5
9 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,69 @@
# @babel/plugin-transform-dotall-regex
> Compile regular expressions using [the `s` (`dotAll`) flag](https://github.com/tc39/proposal-regexp-dotall-flag) to ES5 that works in todays environments.
## Example
**In**
```js
/./s
```
**Out**
```js
/[\0-\uFFFF]/
```
**In**
```js
/./su
```
**Out**
```js
/[\0-\u{10FFFF}]/u
```
[Heres an online demo.](https://mothereff.in/regexpu#input=const+regex+%3D+/foo.bar/s%3B%0Aconsole.log%28%0A++regex.test%28%27foo%5Cnbar%27%29%0A%29%3B%0A//+%E2%86%92+true&dotAllFlag=1)
## Installation
```sh
npm install --save-dev @babel/plugin-transform-dotall-regex
```
## Usage
### Via `.babelrc` (recommended)
`.babelrc`
```json
{
"plugins": ["@babel/plugin-transform-dotall-regex"]
}
```
### Via CLI
```sh
$ babel --plugins @babel/plugin-transform-dotall-regex script.js
```
### Via Node.js API
```js
require('@babel/core').transform(code, {
'plugins': ['@babel/plugin-transform-dotall-regex']
});
```
## Author
| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|---|
| [Mathias Bynens](https://mathiasbynens.be/) |

View File

@ -0,0 +1,34 @@
{
"name": "@babel/plugin-transform-dotall-regex",
"version": "7.0.0-beta.35",
"description": "Compile regular expressions using the `s` (`dotAll`) flag to ES5.",
"homepage": "https://babeljs.io/",
"license": "MIT",
"main": "lib/index.js",
"engines": {
"node": ">=4"
},
"keywords": [
"babel-plugin",
"regex",
"regexp",
"regular expressions",
"dotall"
],
"repository": {
"type": "git",
"url": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-dotall-regex.git"
},
"bugs": "https://github.com/babel/babel/issues",
"dependencies": {
"@babel/helper-regex": "7.0.0-beta.35",
"regexpu-core": "^4.1.3"
},
"peerDependencies": {
"@babel/core": "7.0.0-beta.35"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.35",
"@babel/helper-plugin-test-runner": "7.0.0-beta.35"
}
}

View File

@ -0,0 +1,20 @@
import rewritePattern from "regexpu-core";
import * as regex from "@babel/helper-regex";
export default function() {
return {
visitor: {
RegExpLiteral(path) {
const node = path.node;
if (!regex.is(node, "s")) {
return;
}
node.pattern = rewritePattern(node.pattern, node.flags, {
dotAllFlag: true,
useUnicodeFlag: regex.is(node, "u"),
});
regex.pullFlag(node, "s");
},
},
};
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-dotall-regex"]
}

View File

@ -0,0 +1,2 @@
var a = /./;
var b = /./s;

View File

@ -0,0 +1,2 @@
var a = /./;
var b = /[\0-\uFFFF]/;

View File

@ -0,0 +1,2 @@
var a = /./u;
var b = /./su;

View File

@ -0,0 +1,2 @@
var a = /./u;
var b = /[\0-\u{10FFFF}]/u;

View File

@ -0,0 +1,2 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);