-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
fdd96cb
commit a5e5405
Showing
9 changed files
with
366 additions
and
464 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Improve regexes by making them shorter, consistent, and safer | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const regex = /[0-9]/; | ||
const regex = /[^0-9]/; | ||
const regex = /[a-zA-Z0-9_]/; | ||
const regex = /[a-z0-9_]/i; | ||
const regex = /[^a-zA-Z0-9_]/; | ||
const regex = /[^a-z0-9_]/i; | ||
const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const regex = /\d/; | ||
const regex = /\D/; | ||
const regex = /\w/; | ||
const regex = /\w/i; | ||
const regex = /\W/; | ||
const regex = /\W/i; | ||
const regex = /\d\.\w\-\D/i; | ||
``` | ||
|
||
## Options | ||
|
||
### sortCharacterClasses | ||
|
||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Disables optimizations that affect the sorting of character classes. For example, preserves the order of the characters in `[AaQqTt]` rather than sorting it to `[AQTaqt]`. |
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 |
---|---|---|
@@ -1,36 +1 @@ | ||
# Enforce the use of regex shorthands to improve readability | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const regex = /[0-9]/; | ||
const regex = /[^0-9]/; | ||
const regex = /[a-zA-Z0-9_]/; | ||
const regex = /[a-z0-9_]/i; | ||
const regex = /[^a-zA-Z0-9_]/; | ||
const regex = /[^a-z0-9_]/i; | ||
const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const regex = /\d/; | ||
const regex = /\D/; | ||
const regex = /\w/; | ||
const regex = /\w/i; | ||
const regex = /\W/; | ||
const regex = /\W/i; | ||
const regex = /\d\.\w\-\D/i; | ||
``` | ||
|
||
## Options | ||
|
||
### sortCharacterClasses | ||
|
||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Disables optimizations that affect the sorting of character classes. For example, preserves the order of the characters in `[AaQqTt]` rather than sorting it to `[AQTaqt]`. | ||
This rule was renamed to [`better-regex`](better-regex.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
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,106 @@ | ||
'use strict'; | ||
const cleanRegexp = require('clean-regexp'); | ||
const {optimize} = require('regexp-tree'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const quoteString = require('./utils/quote-string'); | ||
|
||
const message = '{{original}} can be optimized to {{optimized}}'; | ||
|
||
const create = context => { | ||
const {sortCharacterClasses} = context.options[0] || {}; | ||
|
||
const blacklist = []; | ||
|
||
if (sortCharacterClasses === false) { | ||
blacklist.push('charClassClassrangesMerge'); | ||
} | ||
|
||
return { | ||
'Literal[regex]': node => { | ||
const {raw: original, regex} = node; | ||
|
||
// Regular Expressions with `u` flag are not well handled by `regexp-tree` | ||
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162 | ||
if (regex.flags.includes('u')) { | ||
return; | ||
} | ||
|
||
let optimized = original; | ||
|
||
try { | ||
optimized = optimize(original, undefined, {blacklist}).toString(); | ||
} catch (_) {} | ||
|
||
if (original === optimized) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
message, | ||
data: { | ||
original, | ||
optimized | ||
}, | ||
fix: fixer => fixer.replaceText(node, optimized) | ||
}); | ||
}, | ||
'NewExpression[callee.name="RegExp"]': node => { | ||
const arguments_ = node.arguments; | ||
|
||
if (arguments_.length === 0 || arguments_[0].type !== 'Literal') { | ||
return; | ||
} | ||
|
||
const hasRegExp = arguments_[0].regex; | ||
|
||
if (hasRegExp) { | ||
return; | ||
} | ||
|
||
const oldPattern = arguments_[0].value; | ||
const flags = arguments_[1] && arguments_[1].type === 'Literal' ? arguments_[1].value : ''; | ||
|
||
const newPattern = cleanRegexp(oldPattern, flags); | ||
|
||
if (oldPattern !== newPattern) { | ||
// Escape backslash | ||
const fixed = quoteString((newPattern || '').replace(/\\/g, '\\\\')); | ||
|
||
context.report({ | ||
node, | ||
message, | ||
data: { | ||
original: oldPattern, | ||
optimized: newPattern | ||
}, | ||
fix: fixer => fixer.replaceText(arguments_[0], fixed) | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
sortCharacterClasses: { | ||
type: 'boolean', | ||
default: true | ||
} | ||
} | ||
} | ||
]; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
schema | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,102 +1,16 @@ | ||
'use strict'; | ||
const cleanRegexp = require('clean-regexp'); | ||
const {optimize} = require('regexp-tree'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const quoteString = require('./utils/quote-string'); | ||
|
||
const message = 'Use regex shorthands to improve readability.'; | ||
|
||
const create = context => { | ||
const {sortCharacterClasses} = context.options[0] || {}; | ||
|
||
const blacklist = []; | ||
|
||
if (sortCharacterClasses === false) { | ||
blacklist.push('charClassClassrangesMerge'); | ||
} | ||
|
||
return { | ||
'Literal[regex]': node => { | ||
const {raw: original, regex} = node; | ||
|
||
// Regular Expressions with `u` flag are not well handled by `regexp-tree` | ||
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162 | ||
if (regex.flags.includes('u')) { | ||
return; | ||
} | ||
|
||
let optimized = original; | ||
|
||
try { | ||
optimized = optimize(original, undefined, {blacklist}).toString(); | ||
} catch (_) {} | ||
|
||
if (original === optimized) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: '{{original}} can be optimized to {{optimized}}', | ||
data: { | ||
original, | ||
optimized | ||
}, | ||
fix: fixer => fixer.replaceText(node, optimized) | ||
}); | ||
}, | ||
'NewExpression[callee.name="RegExp"]': node => { | ||
const arguments_ = node.arguments; | ||
|
||
if (arguments_.length === 0 || arguments_[0].type !== 'Literal') { | ||
return; | ||
} | ||
|
||
const hasRegExp = arguments_[0].regex; | ||
|
||
if (hasRegExp) { | ||
return; | ||
} | ||
|
||
const oldPattern = arguments_[0].value; | ||
const flags = arguments_[1] && arguments_[1].type === 'Literal' ? arguments_[1].value : ''; | ||
|
||
const newPattern = cleanRegexp(oldPattern, flags); | ||
|
||
if (oldPattern !== newPattern) { | ||
// Escape backslash | ||
const fixed = quoteString((newPattern || '').replace(/\\/g, '\\\\')); | ||
|
||
context.report({ | ||
node, | ||
message, | ||
fix: fixer => fixer.replaceText(arguments_[0], fixed) | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
sortCharacterClasses: { | ||
type: 'boolean', | ||
default: true | ||
} | ||
} | ||
} | ||
]; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
schema | ||
} | ||
fixable: 'code' | ||
}, | ||
deprecated: true, | ||
replacedBy: [ | ||
'unicorn/better-regex' | ||
] | ||
}; |
Oops, something went wrong.