You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Closes#19, Allows to add additional RegEx flags.
* Fixes bug `RangeError: Maximum call stack size exceeded`.
* Replaces the recursive approach to a loop one.
* Improves code.
* Clones `$`.
* Simplifies code.
* Improves code performance.
* Improves code coverage.
* Adds JSDoc to improve Learnability and Maintainability.
* Improves project configuration.
* Renames `readme` folder to `.readme` in order to prevent it to be packed.
* Removes `.npmignore` (It doesn't work when using `files`: npm/npm#4479)
* Updates CI configuration.
* Updates Documentation.
#### The `string` representing the regular expression
208
+
#### The *string* representing the regular expression
209
209
210
210
Remember, Slashes (`/`) are not required in the string that defines the regex,
211
211
212
-
e.g. To get the following regex `/\bhttp:/`:
212
+
e.g. To get the following regex `/^(test|spec)$/`, define:
213
213
214
-
* when using `.eslintrc.js`, define the following string `"\bhttp:"`, or
215
-
* when using `.eslintrc.json`, define `"\\bhttp:"` (backslash needs to be double in a json file)
214
+
* **`"^(test|spec)$"`**, when using `.eslintrc.js` or `.eslintrc.json`.
216
215
217
-
e.g. To get the following regex `/.*test\.js/`:
216
+
e.g. To get the following regex `/\bhttp:/`, define:
218
217
219
-
* when using `.eslintrc.js`, define the following string `".*test\.js"`, or
220
-
* when using `.eslintrc.json`, define `".*test\\.js"` (backslash needs to be double in a json file)
218
+
* **`"\bhttp:"`**, when using `.eslintrc.js`, or
219
+
* **`"\\bhttp:"`**, when using `.eslintrc.json`. (backslash needs to be double in a json file)
220
+
221
+
e.g. To get the following regex `/.*test\.js/`, define:
222
+
223
+
* **`".*test\.js"`**, when using `.eslintrc.js`, or
224
+
* **`".*test\\.js"`**, when using `.eslintrc.json`. (backslash needs to be double in a json file)
221
225
222
226
#### Short pattern definition
223
227
@@ -247,6 +251,7 @@ Each pattern is specified by just a **`string`** representing the regular expres
247
251
It is specified by an `object`, with the following fields:
248
252
249
253
* `regex`: A **required** `string` for `regex/required` and `regex/invalid` representing the **Regular expression to look for**. [REQUIRED]
254
+
* `flags`: A combination of flags, `i`, `s` and/or `u`, to be used by the Regular Expression. [OPTIONAL]
250
255
* `replacement` for `regex/invalid` [1]: [OPTIONAL]
251
256
* An optional `string` used to replace the **invalid** found pattern, or
252
257
* An optional `object` that establish how the **invalid** found pattern will be replaced:
@@ -265,6 +270,7 @@ It is specified by an `object`, with the following fields:
265
270
{
266
271
"id": "regexId",
267
272
"regex": "regex",
273
+
"flags": "isu",
268
274
"replacement": "replacementString",
269
275
"message": "errorMessage",
270
276
"files": {
@@ -635,6 +641,114 @@ Since the **exact** word *return* is not present, this will allow the following
635
641
}
636
642
```
637
643
644
+
##### RegExp Flags
645
+
646
+
The following flags can be add to the regex:
647
+
648
+
*`i`: For case insensitive search.
649
+
*`s`: To allow `.` to match newline characters.
650
+
*`u`: To treat the regex as a sequence of unicode code points.
651
+
652
+
To define the flags to be used, employ the field `flags` in the detailed pattern:
653
+
654
+
* A combination of flags can be used, e.g. `"is"`.
655
+
* Order of flags is irrelevant, e.g. `"si"`.
656
+
* It's case insensitive, e.g. `"iS"`, `"Is"` and `"IS"` are the same.
657
+
* Invalid flags will be reported as an error by eslint.
658
+
659
+
> By default, `"gm"` is always added by the engine (since It's required).
660
+
661
+
e.g.
662
+
663
+
Having the following detailed pattern:
664
+
665
+
```json
666
+
{
667
+
"regex": "invalid",
668
+
"flags": "i"
669
+
}
670
+
```
671
+
672
+
`Invalid`, `inValid`, `INvalid` or `INVALID` will match.
673
+
674
+
### String to Regular expression conversion
675
+
676
+
Internally, each string from the array will be converted into a Regular Expression with `global` and `multiline` options, e.g.:
677
+
678
+
`"someRegex"` will be transformed into `/someRegex/gm`
679
+
680
+
> Remember that backslash needs to be double in strings of a json file, e.g. To get the following regex `/\bhttp:/` define the following string `"\\bhttp:"`.
681
+
682
+
### Empty Meta characters
683
+
684
+
For some special cases when using meta characters that may result in an empty match, e.g. `^`, eslint-plugin-regex will report only the first case found, and after that case is fixed, the following will be report, if present.
* Use eslint-plugin-regex for commit message linting
700
+
* Use eslint-plugin-regex for commit message linting
701
+
```
702
+
703
+
When linting, `eslint-plugin-regex` will only report the first case:
704
+
705
+
```bash
706
+
/path/to/some.js
707
+
3:1 error Invalid regular expression /^(?!(?:(feature|fix|docs|config|refactor|revert|test).*[\\.:]$)|(\\*\\s\\w.*\\.$)|$)/gm found regex/invalid
708
+
```
709
+
710
+
4:1 error will not be reported until 3:1 is fixed.
711
+
712
+
> The issue is that having an empty match does not allow the regex engine to move forward.
713
+
714
+
### Error report
715
+
716
+
The 'Short pattern definition' errors are reported with the following structure:
717
+
718
+
Given `someRegex`, the following message will be shown on error:
719
+
720
+
```
721
+
Invalid regular expression /someRegex/gm found
722
+
```
723
+
724
+
or
725
+
726
+
```
727
+
Required regular expression /someRegex/gm not found in file
728
+
```
729
+
730
+
The 'Detailed pattern definition' errors are reported with the following rules:
731
+
732
+
A . If `message` is present then that **exact message is reported**.
733
+
B . If `id` is present then:
734
+
735
+
Given `"id": "someRegexId"`, the following message will be shown on error:
736
+
737
+
```
738
+
Invalid regular expression 'someRegexId' found
739
+
```
740
+
741
+
or
742
+
743
+
```
744
+
Required regular expression 'someRegexId' not found in file
745
+
```
746
+
747
+
C . If neither `message` nor `id` is present then the 'Short pattern definition' error message is shown.
748
+
749
+
> *`message` takes precedence over `id`.
750
+
> * Although `id` is a quick solution (and useful when creating and testing a rule), using `message` will give more information to the team about the issue.
751
+
638
752
### Mixing
639
753
640
754
#### Mixing pattern types
@@ -800,7 +914,7 @@ A regex rule can be named with a custom name. The Rule name can be anything that
800
914
*`regex/*invalid*`, `regex/*disuse*` or `regex/*avoid*` for invalid patterns.
801
915
*`regex/*required*` or `regex/*use*` for required patterns.
802
916
803
-

917
+

804
918
805
919
> In the name `invalid`, `disuse` and `avoid` will take precedence over `required` and `use`, e.g. If custom regex rule name has both `avoid` and `use` in the name, then the respective regex patterns will be consider invalid patterns.
806
920
@@ -1149,52 +1263,6 @@ or using synonyms to mix error levels:
1149
1263
]
1150
1264
```
1151
1265
1152
-
#### String to Regular expression conversion
1153
-
1154
-
Internally, each string from the array will be converted into a Regular Expression with `global` and `multiline` options, e.g.:
1155
-
1156
-
`"someRegex"` will be transformed into `/someRegex/gm`
1157
-
1158
-
> Remember that backslash needs to be double in strings of a json file, e.g. To get the following regex `/\bhttp:/` define the following string `"\\bhttp:"`.
1159
-
1160
-
#### Error report
1161
-
1162
-
The 'Short pattern definition' errors are reported with the following structure:
1163
-
1164
-
Given `someRegex`, the following message will be shown on error:
1165
-
1166
-
```
1167
-
Invalid regular expression /someRegex/gm found
1168
-
```
1169
-
1170
-
or
1171
-
1172
-
```
1173
-
Required regular expression /someRegex/gm not found in file
1174
-
```
1175
-
1176
-
The 'Detailed pattern definition' errors are reported with the following rules:
1177
-
1178
-
A . If `message` is present then that **exact message is reported**.
1179
-
B . If `id` is present then:
1180
-
1181
-
Given `"id": "someRegexId"`, the following message will be shown on error:
1182
-
1183
-
```
1184
-
Invalid regular expression 'someRegexId' found
1185
-
```
1186
-
1187
-
or
1188
-
1189
-
```
1190
-
Required regular expression 'someRegexId' not found in file
1191
-
```
1192
-
1193
-
C . If neither `message` nor `id` is present then the 'Short pattern definition' error message is shown.
1194
-
1195
-
> * `message` takes precedence over `id`.
1196
-
> * Although `id` is a quick solution (and useful when creating and testing a rule), using `message` will give more information to the team about the issue.
*`id`: An optional `string` representing the **Pattern Id**.
69
-
*`message`: An optional `string` specifying the **Message to be shown when an invalid `regex` is found**.
70
-
*`files`: An optional `object` specifying which files to analyze:
68
+
* One must be defined, either the`string` or `function`.
69
+
*`id`: An optional `string` representing the **Pattern Id**.[OPTIONAL]
70
+
*`message`: An optional `string` specifying the **Message to be shown when an error happens** (invalid `regex` is found or required `regex` is not found). [OPTIONAL]
71
+
*`files`: An optional `object` specifying which files to analyze:[OPTIONAL]
71
72
*`ignore`: A `string` representing **Regular expression of the files to be ignored** when validating this specific pattern.
72
73
*`inspect`: A `string` representing **Regular expression of the files to be inspected** when validating this specific pattern.
73
74
75
+
```json
76
+
{
77
+
"id": "regexId",
78
+
"regex": "regex",
79
+
"flags": "isu",
80
+
"replacement": "replacementString",
81
+
"message": "errorMessage",
82
+
"files": {
83
+
"ignore": "ignoreFilesRegex",
84
+
"inspect": "inspectFilesRegex"
85
+
}
86
+
}
87
+
```
88
+
74
89
> *`regex` is the only Required field. Slashes (`/`) are not required in the string, e.g. To get the following regex `/\bhttp:/`:
75
90
> * when using `.eslintrc.js`, define the following string `"\bhttp:"`, or
76
91
> * when using `.eslintrc.json`, define `"\\bhttp:"` (backslash needs to de double in a json file).
0 commit comments