New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import babel-plugin-transform-unicode-property-regex #6499
Merged
jridgewell
merged 5 commits into
babel:master
from
mathiasbynens:unicode-property-escapes
Oct 18, 2017
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/babel-plugin-transform-unicode-property-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,55 @@ | ||
# @babel/plugin-transform-unicode-property-regex | ||
|
||
Compile [Unicode property escapes](https://github.com/mathiasbynens/regexpu-core/blob/master/property-escapes.md) (`\p{…}` and `\P{…}`) in Unicode regular expressions to ES5 or ES6 that works in today’s environments. | ||
|
||
**Note:** the Unicode property escape syntax is non-standard and may or may not reflect what eventually gets specified. | ||
|
||
[Here’s an online demo.](https://mothereff.in/regexpu#input=var+regex+%3D+/%5Cp%7BScript_Extensions%3DGreek%7D/u%3B&unicodePropertyEscape=1) | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install @babel/plugin-transform-unicode-property-regex | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (recommended) | ||
|
||
`.babelrc` | ||
|
||
```json | ||
{ | ||
"plugins": ["transform-unicode-property-regex"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins transform-unicode-property-regex script.js | ||
``` | ||
|
||
### Via Node.js API | ||
|
||
```js | ||
require('@babel/core').transform(code, { | ||
'plugins': ['transform-unicode-property-regex'] | ||
}); | ||
``` | ||
|
||
To transpile to ES6/ES2015: | ||
|
||
```js | ||
require('@babel/core').transform(code, { | ||
'plugins': [ | ||
['transform-unicode-property-regex', { 'useUnicodeFlag': true }] | ||
] | ||
}); | ||
``` | ||
|
||
## Author | ||
|
||
| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") | | ||
|---| | ||
| [Mathias Bynens](https://mathiasbynens.be/) | |
30 changes: 30 additions & 0 deletions
30
packages/babel-plugin-transform-unicode-property-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,30 @@ | ||
{ | ||
"name": "@babel/plugin-transform-unicode-property-regex", | ||
"version": "7.0.0-beta.3", | ||
"description": "Compile Unicode property escapes in Unicode regular expressions to ES5.", | ||
"homepage": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-unicode-property-regex", | ||
"main": "lib/index.js", | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"keywords": [ | ||
"babel-plugin", | ||
"regex", | ||
"regexp", | ||
"regular expressions", | ||
"unicode properties", | ||
"unicode" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-unicode-property-regex.git" | ||
}, | ||
"bugs": "https://github.com/babel/babel/issues", | ||
"dependencies": { | ||
"@babel/helper-regex": "7.0.0-beta.3", | ||
"regexpu-core": "^4.1.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.3" | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/babel-plugin-transform-unicode-property-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,23 @@ | ||
import rewritePattern from "regexpu-core"; | ||
import * as regex from "babel-helper-regex"; | ||
|
||
export default function() { | ||
return { | ||
visitor: { | ||
RegExpLiteral(path, state) { | ||
const node = path.node; | ||
if (!regex.is(node, "u")) { | ||
return; | ||
} | ||
const useUnicodeFlag = state.opts.useUnicodeFlag || false; | ||
node.pattern = rewritePattern(node.pattern, node.flags, { | ||
unicodePropertyEscape: true, | ||
useUnicodeFlag: useUnicodeFlag, | ||
}); | ||
if (!useUnicodeFlag) { | ||
regex.pullFlag(node, "u"); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
...l-plugin-transform-unicode-property-regex/test/fixtures/with-unicode-flag/emoji/actual.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,2 @@ | ||
var regexEmojiModifier = /\p{Emoji_Modifier}/u; | ||
var regexEmojiComponent = /\p{Emoji_Component}/u; |
2 changes: 2 additions & 0 deletions
2
...plugin-transform-unicode-property-regex/test/fixtures/with-unicode-flag/emoji/expected.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,2 @@ | ||
var regexEmojiModifier = /[\u{1F3FB}-\u{1F3FF}]/u; | ||
var regexEmojiComponent = /[#\*0-9\u200D\u20E3\uFE0F\u{1F1E6}-\u{1F1FF}\u{1F3FB}-\u{1F3FF}\u{E0020}-\u{E007F}]/u; |
7 changes: 7 additions & 0 deletions
7
...abel-plugin-transform-unicode-property-regex/test/fixtures/with-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,7 @@ | ||
{ | ||
"plugins": [ | ||
["transform-unicode-property-regex", { | ||
"useUnicodeFlag": true | ||
}] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
...nsform-unicode-property-regex/test/fixtures/with-unicode-flag/script-extensions/actual.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 @@ | ||
var regex = /\p{Script_Extensions=Anatolian_Hieroglyphs}/u; |
1 change: 1 addition & 0 deletions
1
...form-unicode-property-regex/test/fixtures/with-unicode-flag/script-extensions/expected.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 @@ | ||
var regex = /[\u{14400}-\u{14646}]/u; |
1 change: 1 addition & 0 deletions
1
...-plugin-transform-unicode-property-regex/test/fixtures/with-unicode-flag/simple/actual.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 @@ | ||
var regex = /\p{ASCII_Hex_Digit}/u; |
1 change: 1 addition & 0 deletions
1
...lugin-transform-unicode-property-regex/test/fixtures/with-unicode-flag/simple/expected.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 @@ | ||
var regex = /[0-9A-Fa-f]/u; |
1 change: 1 addition & 0 deletions
1
...gin-transform-unicode-property-regex/test/fixtures/with-unicode-flag/unicode-10/actual.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 @@ | ||
var regex = /\p{Regional_Indicator}/u; |
1 change: 1 addition & 0 deletions
1
...n-transform-unicode-property-regex/test/fixtures/with-unicode-flag/unicode-10/expected.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 @@ | ||
var regex = /[\u{1F1E6}-\u{1F1FF}]/u; |
2 changes: 2 additions & 0 deletions
2
...lugin-transform-unicode-property-regex/test/fixtures/without-unicode-flag/emoji/actual.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,2 @@ | ||
var regexEmojiModifier = /\p{Emoji_Modifier}/u; | ||
var regexEmojiComponent = /\p{Emoji_Component}/u; |
2 changes: 2 additions & 0 deletions
2
...gin-transform-unicode-property-regex/test/fixtures/without-unicode-flag/emoji/expected.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,2 @@ | ||
var regexEmojiModifier = /(?:\uD83C[\uDFFB-\uDFFF])/; | ||
var regexEmojiComponent = /(?:[#\*0-9\u200D\u20E3\uFE0F]|\uD83C[\uDDE6-\uDDFF\uDFFB-\uDFFF]|\uDB40[\uDC20-\uDC7F])/; |
5 changes: 5 additions & 0 deletions
5
...l-plugin-transform-unicode-property-regex/test/fixtures/without-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,5 @@ | ||
{ | ||
"plugins": [ | ||
["transform-unicode-property-regex"] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
...orm-unicode-property-regex/test/fixtures/without-unicode-flag/script-extensions/actual.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 @@ | ||
var regex = /\p{Script_Extensions=Anatolian_Hieroglyphs}/u; |
1 change: 1 addition & 0 deletions
1
...m-unicode-property-regex/test/fixtures/without-unicode-flag/script-extensions/expected.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 @@ | ||
var regex = /(?:\uD811[\uDC00-\uDE46])/; |
1 change: 1 addition & 0 deletions
1
...ugin-transform-unicode-property-regex/test/fixtures/without-unicode-flag/simple/actual.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 @@ | ||
var regex = /\p{ASCII_Hex_Digit}/u; |
1 change: 1 addition & 0 deletions
1
...in-transform-unicode-property-regex/test/fixtures/without-unicode-flag/simple/expected.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 @@ | ||
var regex = /[0-9A-Fa-f]/; |
1 change: 1 addition & 0 deletions
1
...-transform-unicode-property-regex/test/fixtures/without-unicode-flag/unicode-10/actual.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 @@ | ||
var regex = /\p{Regional_Indicator}/u; |
1 change: 1 addition & 0 deletions
1
...ransform-unicode-property-regex/test/fixtures/without-unicode-flag/unicode-10/expected.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 @@ | ||
var regex = /(?:\uD83C[\uDDE6-\uDDFF])/; |
2 changes: 2 additions & 0 deletions
2
packages/babel-plugin-transform-unicode-property-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,2 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
runner(__dirname); |
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 |
---|---|---|
|
@@ -13,6 +13,6 @@ | |
"@babel/plugin-transform-class-properties": "7.0.0-beta.3", | ||
"@babel/plugin-transform-object-rest-spread": "7.0.0-beta.3", | ||
"@babel/plugin-transform-optional-catch-binding": "7.0.0-beta.3", | ||
"babel-plugin-transform-unicode-property-regex": "^2.0.5" | ||
"@babel/plugin-transform-unicode-property-regex": "7.0.0-beta.3" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
.git
should be removed, since that url doesn't point to the git repo itself but to a subdirectory.