Skip to content

Enhancement: Allow use of optimize options and add recommended config #60

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

Merged
merged 3 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ indent_size = 2
[Makefile]
indent_style = tab
indent_size = 4

[*.md]
indent_style = space
indent_size = 4
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
!.eslintrc.js
20 changes: 20 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'
module.exports = {
env: {
node: true,
es6: true
},
extends: [
'eslint:recommended'
],
rules: {
semi: [2, 'never'],
quotes: [2, 'single', {
avoidEscape: true
}],
'prefer-const': [2],
'no-var': [2],
'prefer-destructuring': [2],
'object-shorthand': [2]
}
}
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: node_js

node_js:
- "7"

- 8
- 10
- 12

cache:
directories:
- "node_modules"

30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ Then configure the rules you want to use under the rules section.
}
```

If you wish to add a `whitelist` or `blacklist` array against `regexp-tree`'s
[transforms](https://github.com/DmitrySoshnikov/regexp-tree/blob/master/src/optimizer/README.md#transforms), you can add them on an objects object:

```json
{
"rules": {
"optimize-regex/optimize-regex": ["warn", {
"blacklist": ["charClassClassrangesMerge"]
}]
}
}
```

If you want the latter particular settings, you can avoid setting `plugins` and
`rules` and just use:

```json
{
"extends": ["optimize-regex/recommended"]
}
```

Or without the blacklist:

```json
{
"extends": ["optimize-regex/all"]
}
```

## Rules

* [optimize-regex](./docs/rules/optimize-regex.md)
Expand Down
23 changes: 21 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@

'use strict'

module.exports.rules = {
'optimize-regex': require('./rules/optimize-regex'),
module.exports = {
rules: {
'optimize-regex': require('./rules/optimize-regex'),
},

configs: {
recommended: {
plugins: ['optimize-regex'],
rules: {
'optimize-regex/optimize-regex': ['warn', {
'blacklist': ['charClassClassrangesMerge']
}]
}
},
all: {
plugins: ['optimize-regex'],
rules: {
'optimize-regex/optimize-regex': ['warn']
}
}
}
}
55 changes: 42 additions & 13 deletions lib/rules/optimize-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

const { parse, generate, optimize } = require('regexp-tree')

const optimizerTransforms = require('regexp-tree/dist/optimizer/transforms/index.js')

const transforms = [...optimizerTransforms.keys()]

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -19,11 +23,32 @@ module.exports = {
recommended: true,
},
fixable: 'code',
schema: [],
schema: [
{
additionalProperties: false,
type: 'object',
properties: {
whitelist: {
type: 'array',
items: {
type: 'string',
enum: transforms
}
},
blacklist: {
type: 'array',
items: {
type: 'string',
enum: transforms
}
}
}
}
],
},

create: function(context) {
const sourceCode = context.getSourceCode()
create (context) {
const options = context.options[0] || {}

/**
* Optimize regular expression literals
Expand All @@ -34,30 +59,34 @@ module.exports = {
*/

function optimizeRegexLiteral(node) {
const { type, value, start } = sourceCode.getFirstToken(node)

if (type !== 'RegularExpression') {
return
}
const { raw /* , start */ } = node

let parsedSource
try {
parsedSource = parse(value)
parsedSource = parse(raw)
} catch (e) {
// istanbul ignore next
context.report({
node,
message: "{{original}} can't be parsed: {{message}}",
data: {
original: value,
original: raw,
message: e.message,
},
})

// istanbul ignore next
return
}

const originalRegex = generate(parsedSource).toString()
const optimizedRegex = optimize(value).toString()
const optimizedRegex = optimize(
raw,
options.whitelist,
{
blacklist: options.blacklist
}
).toString()

if (originalRegex === optimizedRegex) {
return
Expand All @@ -67,7 +96,7 @@ module.exports = {
node,
message: '{{original}} can be optimized to {{optimized}}',
data: {
original: value,
original: raw,
optimized: optimizedRegex,
},
fix(fixer) {
Expand All @@ -77,7 +106,7 @@ module.exports = {
}

return {
Literal: optimizeRegexLiteral,
'Literal[regex]': optimizeRegexLiteral,
}
},
}
Loading