Skip to content

Commit 7b8a1e6

Browse files
authored
Enhancement: Allow use of optimize options and add recommended config (#60)
* - Breaking change: To support nyc testing on Travis, dropping looking at Node 6 - Enhancement: Allow use of `optimize` options - Enhancement: Add `recommended` and `all` configs which avoid need for `plugins` and `rules` together - Linting: Apply eslint:recommended, with a few additional rules and an ignore file - Travis: Update from 7 to check 8, 10, 12 - npm: Add recommended fields (bugs, homepage, contributors); use https URL for auto-opening within IDEs and linting against package.json validator - npm: Add `eslint` script - npm: Update devDeps - Maintenance: `.editorconfig` to enforce spacing on Markdown - Testing: Add nyc coverage
1 parent 69cab7c commit 7b8a1e6

10 files changed

+1954
-563
lines changed

Diff for: .editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ indent_size = 2
1111
[Makefile]
1212
indent_style = tab
1313
indent_size = 4
14+
15+
[*.md]
16+
indent_style = space
17+
indent_size = 4

Diff for: .eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
!.eslintrc.js

Diff for: .eslintrc.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
module.exports = {
3+
env: {
4+
node: true,
5+
es6: true
6+
},
7+
extends: [
8+
'eslint:recommended'
9+
],
10+
rules: {
11+
semi: [2, 'never'],
12+
quotes: [2, 'single', {
13+
avoidEscape: true
14+
}],
15+
'prefer-const': [2],
16+
'no-var': [2],
17+
'prefer-destructuring': [2],
18+
'object-shorthand': [2]
19+
}
20+
}

Diff for: .travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: node_js
22

33
node_js:
4-
- "7"
5-
4+
- 8
5+
- 10
6+
- 12
7+
68
cache:
79
directories:
810
- "node_modules"
9-

Diff for: README.md

+30
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@ Then configure the rules you want to use under the rules section.
4343
}
4444
```
4545

46+
If you wish to add a `whitelist` or `blacklist` array against `regexp-tree`'s
47+
[transforms](https://github.com/DmitrySoshnikov/regexp-tree/blob/master/src/optimizer/README.md#transforms), you can add them on an objects object:
48+
49+
```json
50+
{
51+
"rules": {
52+
"optimize-regex/optimize-regex": ["warn", {
53+
"blacklist": ["charClassClassrangesMerge"]
54+
}]
55+
}
56+
}
57+
```
58+
59+
If you want the latter particular settings, you can avoid setting `plugins` and
60+
`rules` and just use:
61+
62+
```json
63+
{
64+
"extends": ["optimize-regex/recommended"]
65+
}
66+
```
67+
68+
Or without the blacklist:
69+
70+
```json
71+
{
72+
"extends": ["optimize-regex/all"]
73+
}
74+
```
75+
4676
## Rules
4777

4878
* [optimize-regex](./docs/rules/optimize-regex.md)

Diff for: lib/index.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55

66
'use strict'
77

8-
module.exports.rules = {
9-
'optimize-regex': require('./rules/optimize-regex'),
8+
module.exports = {
9+
rules: {
10+
'optimize-regex': require('./rules/optimize-regex'),
11+
},
12+
13+
configs: {
14+
recommended: {
15+
plugins: ['optimize-regex'],
16+
rules: {
17+
'optimize-regex/optimize-regex': ['warn', {
18+
'blacklist': ['charClassClassrangesMerge']
19+
}]
20+
}
21+
},
22+
all: {
23+
plugins: ['optimize-regex'],
24+
rules: {
25+
'optimize-regex/optimize-regex': ['warn']
26+
}
27+
}
28+
}
1029
}

Diff for: lib/rules/optimize-regex.js

+42-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

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

10+
const optimizerTransforms = require('regexp-tree/dist/optimizer/transforms/index.js')
11+
12+
const transforms = [...optimizerTransforms.keys()]
13+
1014
//------------------------------------------------------------------------------
1115
// Rule Definition
1216
//------------------------------------------------------------------------------
@@ -19,11 +23,32 @@ module.exports = {
1923
recommended: true,
2024
},
2125
fixable: 'code',
22-
schema: [],
26+
schema: [
27+
{
28+
additionalProperties: false,
29+
type: 'object',
30+
properties: {
31+
whitelist: {
32+
type: 'array',
33+
items: {
34+
type: 'string',
35+
enum: transforms
36+
}
37+
},
38+
blacklist: {
39+
type: 'array',
40+
items: {
41+
type: 'string',
42+
enum: transforms
43+
}
44+
}
45+
}
46+
}
47+
],
2348
},
2449

25-
create: function(context) {
26-
const sourceCode = context.getSourceCode()
50+
create (context) {
51+
const options = context.options[0] || {}
2752

2853
/**
2954
* Optimize regular expression literals
@@ -34,30 +59,34 @@ module.exports = {
3459
*/
3560

3661
function optimizeRegexLiteral(node) {
37-
const { type, value, start } = sourceCode.getFirstToken(node)
38-
39-
if (type !== 'RegularExpression') {
40-
return
41-
}
62+
const { raw /* , start */ } = node
4263

4364
let parsedSource
4465
try {
45-
parsedSource = parse(value)
66+
parsedSource = parse(raw)
4667
} catch (e) {
68+
// istanbul ignore next
4769
context.report({
4870
node,
4971
message: "{{original}} can't be parsed: {{message}}",
5072
data: {
51-
original: value,
73+
original: raw,
5274
message: e.message,
5375
},
5476
})
5577

78+
// istanbul ignore next
5679
return
5780
}
5881

5982
const originalRegex = generate(parsedSource).toString()
60-
const optimizedRegex = optimize(value).toString()
83+
const optimizedRegex = optimize(
84+
raw,
85+
options.whitelist,
86+
{
87+
blacklist: options.blacklist
88+
}
89+
).toString()
6190

6291
if (originalRegex === optimizedRegex) {
6392
return
@@ -67,7 +96,7 @@ module.exports = {
6796
node,
6897
message: '{{original}} can be optimized to {{optimized}}',
6998
data: {
70-
original: value,
99+
original: raw,
71100
optimized: optimizedRegex,
72101
},
73102
fix(fixer) {
@@ -77,7 +106,7 @@ module.exports = {
77106
}
78107

79108
return {
80-
Literal: optimizeRegexLiteral,
109+
'Literal[regex]': optimizeRegexLiteral,
81110
}
82111
},
83112
}

0 commit comments

Comments
 (0)