-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-regexp-lookbehind): add no-regexp-lookbehind
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# no-regexp-lookbehind | ||
|
||
This prevents the use of the RegExp lookbehind feature | ||
|
||
```js | ||
/(?<=a>)b/ | ||
|
||
new RegExp("/(?<=a)b") | ||
``` | ||
|
||
These will not be allowed because they are not supported in the following browsers: | ||
|
||
- Edge < 79 | ||
- Safari (any version at the time of writing) | ||
- Firefox (any version at the time of writing) | ||
- Chrome < 62 | ||
|
||
## What is the Fix? | ||
|
||
You may be able to rewrite your experession using (Negative) Lookaheads, but if not then there is no solution for this, aside from pulling in a custom RegExp library. |
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
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,28 @@ | ||
const hasLookbehind = s => s.includes('(?<=') || s.includes('(?<!)') | ||
|
||
module.exports = (context, badBrowser) => ({ | ||
'Literal[regex]'(node) { | ||
if (hasLookbehind(node.regex.pattern)) { | ||
context.report(node, `RegExp lookbehinds are not supported in ${badBrowser}`) | ||
} | ||
}, | ||
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) { | ||
const [source] = node.arguments; | ||
if ( | ||
source && | ||
( | ||
( | ||
source.type === 'Literal' && | ||
typeof source.value === 'string' && | ||
hasLookbehind(source.value) | ||
) || | ||
( | ||
source.type === 'TemplateLiteral' && | ||
source.quasis.some(({value: {raw}}) => hasLookbehind(raw)) | ||
) | ||
) | ||
) { | ||
context.report(node, `RegExp lookbehinds are not supported in ${badBrowser}`) | ||
} | ||
} | ||
}) |
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,49 @@ | ||
var rule = require('../lib/rules/no-regexp-lookbehind') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var ruleTester = new RuleTester({parserOptions: {ecmaVersion: 2020}}) | ||
|
||
ruleTester.run('no-regexp-lookbehind', rule, { | ||
valid: [ | ||
{code: '/(?:a)b/'}, | ||
{code: '/(?:a)b/g'}, | ||
{code: '/(?<named_group_not_checked_here>)/'}, | ||
{code: 'RegExp("(?:a)b", "g")'}, | ||
{code: 'RegExp("(?:a)b", "g")'}, | ||
{code: 'RegExp("(?<named-group-not-checked-here>)")'}, | ||
], | ||
invalid: [ | ||
{ | ||
code: '/(?<=a)b/', | ||
errors: [ | ||
{ | ||
message: 'RegExp lookbehinds are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'new RegExp("/(?<=a)b")', | ||
errors: [ | ||
{ | ||
message: 'RegExp lookbehinds are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '/(?<=a)b/g', | ||
errors: [ | ||
{ | ||
message: 'RegExp lookbehinds are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'new RegExp("/(?<=a)b", "g")', | ||
errors: [ | ||
{ | ||
message: 'RegExp lookbehinds are not supported in undefined' | ||
} | ||
] | ||
}, | ||
] | ||
}) |