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:
parent
2b065350b5
commit
44da8201a5
69
packages/babel-plugin-transform-dotall-regex/README.md
Normal file
69
packages/babel-plugin-transform-dotall-regex/README.md
Normal 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 today’s environments.
|
||||
|
||||
## Example
|
||||
|
||||
**In**
|
||||
|
||||
```js
|
||||
/./s
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```js
|
||||
/[\0-\uFFFF]/
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```js
|
||||
/./su
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```js
|
||||
/[\0-\u{10FFFF}]/u
|
||||
```
|
||||
|
||||
[Here’s 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
|
||||
|
||||
| [](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
||||
|---|
|
||||
| [Mathias Bynens](https://mathiasbynens.be/) |
|
||||
34
packages/babel-plugin-transform-dotall-regex/package.json
Normal file
34
packages/babel-plugin-transform-dotall-regex/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
20
packages/babel-plugin-transform-dotall-regex/src/index.js
Normal file
20
packages/babel-plugin-transform-dotall-regex/src/index.js
Normal 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");
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
3
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/options.json
vendored
Normal file
3
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-dotall-regex"]
|
||||
}
|
||||
2
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/simple/actual.js
vendored
Normal file
2
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/simple/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var a = /./;
|
||||
var b = /./s;
|
||||
2
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/simple/expected.js
vendored
Normal file
2
packages/babel-plugin-transform-dotall-regex/test/fixtures/dotall-regex/simple/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var a = /./;
|
||||
var b = /[\0-\uFFFF]/;
|
||||
@ -0,0 +1,2 @@
|
||||
var a = /./u;
|
||||
var b = /./su;
|
||||
@ -0,0 +1,2 @@
|
||||
var a = /./u;
|
||||
var b = /[\0-\u{10FFFF}]/u;
|
||||
@ -0,0 +1,2 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
runner(__dirname);
|
||||
Loading…
x
Reference in New Issue
Block a user