Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @babel/plugin-transform-named-capturing-groups-regex
- Loading branch information
1 parent
3581583
commit c0014fe
Showing
31 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-named-capturing-groups-regex/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-transform-named-capturing-groups-regex/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @babel/plugin-transform-named-capturing-groups-regex | ||
|
||
> Compile regular expressions using named groups to ES5. | ||
See our website [@babel/plugin-transform-named-capturing-groups-regex](https://babeljs.io/docs/en/next/babel-plugin-transform-named-capturing-groups-regex.html) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-transform-named-capturing-groups-regex | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-transform-named-capturing-groups-regex --dev | ||
``` |
26 changes: 26 additions & 0 deletions
26
packages/babel-plugin-transform-named-capturing-groups-regex/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@babel/plugin-transform-named-capturing-groups-regex", | ||
"version": "7.1.0", | ||
"description": "Compile regular expressions using named groups to ES5.", | ||
"homepage": "https://babeljs.io/", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin", | ||
"regex", | ||
"regexp", | ||
"regular expressions" | ||
], | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-named-capturing-groups-regex", | ||
"bugs": "https://github.com/babel/babel/issues", | ||
"dependencies": { | ||
"regexp-tree": "^0.0.78" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.0.0", | ||
"@babel/helper-plugin-test-runner": "^7.0.0" | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/babel-plugin-transform-named-capturing-groups-regex/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import regexpTree from "regexp-tree"; | ||
|
||
export default function({ types: t }, options) { | ||
const { runtime = true } = options; | ||
if (typeof runtime !== "boolean") { | ||
throw new Error(".runtime must be boolean"); | ||
} | ||
|
||
return { | ||
name: "transform-named-capturing-groups-regex", | ||
|
||
visitor: { | ||
RegExpLiteral(path) { | ||
const node = path.node; | ||
if (node.pattern.indexOf("(?<") === -1) { | ||
// Return early if there are no named groups. | ||
// The .indexOf check may have false positives (e.g. /\(?</); in | ||
// this case we parse the regex and regexp-tree won't transform it. | ||
return; | ||
} | ||
|
||
const result = regexpTree.compatTranspile(node.extra.raw, [ | ||
"namedCapturingGroups", | ||
]); | ||
const { namedCapturingGroups } = result.getExtra(); | ||
|
||
if ( | ||
namedCapturingGroups && | ||
Object.keys(namedCapturingGroups).length > 0 | ||
) { | ||
node.pattern = result.getSource(); | ||
|
||
if (runtime && !isRegExpTest(path)) { | ||
path.replaceWith( | ||
t.callExpression(this.addHelper("wrapRegExp"), [ | ||
node, | ||
t.valueToNode(namedCapturingGroups), | ||
]), | ||
); | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function isRegExpTest(path) { | ||
return ( | ||
path.parentPath.isMemberExpression({ | ||
object: path.node, | ||
computed: false, | ||
}) && path.parentPath.get("property").isIdentifier({ name: "test" }) | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; | ||
|
||
var result = re.exec("2017-12-23"); | ||
|
||
expect(result.groups).toEqual({ | ||
year: "2017", | ||
month: "12", | ||
day: "23", | ||
}); | ||
|
||
expect(result.groups).toEqual({ | ||
year: result[1], | ||
month: result[2], | ||
day: result[3], | ||
}); |
3 changes: 3 additions & 0 deletions
3
...-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/instanceof-regexp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var re = /(?<group>)/; | ||
|
||
expect(re instanceof RegExp).toBeTruthy(); |
15 changes: 15 additions & 0 deletions
15
packages/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/match.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; | ||
|
||
var result = "1999-09-29".match(re); | ||
|
||
expect(result.groups).toEqual({ | ||
year: "1999", | ||
month: "09", | ||
day: "29", | ||
}); | ||
|
||
expect(result.groups).toEqual({ | ||
year: result[1], | ||
month: result[2], | ||
day: result[3], | ||
}); |
4 changes: 4 additions & 0 deletions
4
...es/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/no-groups.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var re = /no-groups-\(?<looks-like-a-group>looks\)/; | ||
var result = re.exec("no-groups-(<looks-like-a-group>looks)") | ||
|
||
expect(result.groups).toBeUndefined(); |
3 changes: 3 additions & 0 deletions
3
...es/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-named-capturing-groups-regex"] | ||
} |
5 changes: 5 additions & 0 deletions
5
...bel-plugin-transform-named-capturing-groups-regex/test/fixtures/runtime/replace-string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; | ||
|
||
var result = "2015-10-31".replace(re, "$<day>/$<month>/$<year>") | ||
|
||
expect(result).toBe("31/10/2015"); |
1 change: 1 addition & 0 deletions
1
...s/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/syntax/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/(?<name>)\k<name>/; |
6 changes: 6 additions & 0 deletions
6
...bel-plugin-transform-named-capturing-groups-regex/test/fixtures/syntax/basic/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
"external-helpers", | ||
["transform-named-capturing-groups-regex", { "runtime": false }] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
.../babel-plugin-transform-named-capturing-groups-regex/test/fixtures/syntax/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/()\1/; |
1 change: 1 addition & 0 deletions
1
...ansform-named-capturing-groups-regex/test/fixtures/syntax/escape-no-unicode-flag/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/(?<\u{41}>)\k<\u{41}>/; |
3 changes: 3 additions & 0 deletions
3
...orm-named-capturing-groups-regex/test/fixtures/syntax/escape-no-unicode-flag/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"throws": "invalid group Unicode name \"\\u{41}\", use `u` flag." | ||
} |
1 change: 1 addition & 0 deletions
1
...-transform-named-capturing-groups-regex/test/fixtures/syntax/escape-unicode-flag/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/(?<\u{41}>)\k<\u{41}>/u; |
1 change: 1 addition & 0 deletions
1
...transform-named-capturing-groups-regex/test/fixtures/syntax/escape-unicode-flag/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/()\1/u; |
6 changes: 6 additions & 0 deletions
6
...ges/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/syntax/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
["external-helpers", { "helperVersion": "7.1000.0" }], | ||
["transform-named-capturing-groups-regex", { "runtime": false }] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
.../babel-plugin-transform-named-capturing-groups-regex/test/fixtures/wrapper/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"foo".match(/(?<double>.)\k<double>/); |
3 changes: 3 additions & 0 deletions
3
...babel-plugin-transform-named-capturing-groups-regex/test/fixtures/wrapper/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"foo".match(babelHelpers.wrapRegExp(/(.)\1/, { | ||
double: 1 | ||
})); |
1 change: 1 addition & 0 deletions
1
...-transform-named-capturing-groups-regex/test/fixtures/wrapper/looks-like-a-group/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/no-groups-\(?<looks-like-a-group>looks\)/; |
1 change: 1 addition & 0 deletions
1
...transform-named-capturing-groups-regex/test/fixtures/wrapper/looks-like-a-group/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/no-groups-\(?<looks-like-a-group>looks\)/; |
1 change: 1 addition & 0 deletions
1
...nsform-named-capturing-groups-regex/test/fixtures/wrapper/only-when-named-groups/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"foo".match(/(.)\1/); |
1 change: 1 addition & 0 deletions
1
...sform-named-capturing-groups-regex/test/fixtures/wrapper/only-when-named-groups/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"foo".match(/(.)\1/); |
6 changes: 6 additions & 0 deletions
6
...es/babel-plugin-transform-named-capturing-groups-regex/test/fixtures/wrapper/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
["external-helpers", { "helperVersion": "7.1000.0" }], | ||
"transform-named-capturing-groups-regex" | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
...ed-capturing-groups-regex/test/fixtures/wrapper/skips-anonymous-capturing-groups/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"abba".match(/(.)(?<n>.)\k<n>\1/); |
3 changes: 3 additions & 0 deletions
3
...d-capturing-groups-regex/test/fixtures/wrapper/skips-anonymous-capturing-groups/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"abba".match(babelHelpers.wrapRegExp(/(.)(.)\2\1/, { | ||
n: 2 | ||
})); |
1 change: 1 addition & 0 deletions
1
...-plugin-transform-named-capturing-groups-regex/test/fixtures/wrapper/test-method/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/^(?<x>.)\k<x>$/.test("aa"); |
1 change: 1 addition & 0 deletions
1
...plugin-transform-named-capturing-groups-regex/test/fixtures/wrapper/test-method/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/^(.)\1$/.test("aa"); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-named-capturing-groups-regex/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
|
||
runner(__dirname); |