Skip to content

Commit 9373b88

Browse files
committed
## 1.10.0 - August 2022
* 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.
1 parent edd9713 commit 9373b88

23 files changed

+725
-261
lines changed

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"root": true,
1313
"rules": {
1414
"global-require": "error",
15+
"init-declarations": "off",
1516
"max-params": "off",
1617
"no-console": "error",
1718
"no-invalid-this": "error",
@@ -22,14 +23,14 @@
2223
"consistent": true
2324
}
2425
],
25-
"array-bracket-spacing": "off",
2626
"quotes": [
2727
"error",
2828
"single"
2929
],
3030
"semi": [
3131
"error",
3232
"never"
33-
]
33+
],
34+
"valid-jsdoc": "off"
3435
}
3536
}

.gitlab-ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ test:
4444
untracked: true
4545
paths:
4646
- node_modules/
47+
coverage: '/Branches.*?(\d+(?:\.\d+)?)%/'
4748
artifacts:
4849
when: always
4950
paths:
5051
- build/coverage/lcov-report/
52+
reports:
53+
coverage_report:
54+
coverage_format: cobertura
55+
path: build/coverage/cobertura-coverage.xml
5156

5257
pages:
5358
stage: assemble

.npmignore

Lines changed: 0 additions & 23 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# ESLint Plugin Regex Change Log
22

3+
## 1.10.0 - August 2022
4+
5+
* Closes #19, Allows to add additional RegEx flags.
6+
* Fixes bug `RangeError: Maximum call stack size exceeded`.
7+
* Replaces the recursive approach to a loop one.
8+
* Improves code.
9+
* Clones `$`.
10+
* Simplifies code.
11+
* Improves code performance.
12+
* Improves code coverage.
13+
* Adds JSDoc to improve Learnability and Maintainability.
14+
* Improves project configuration.
15+
* Renames `readme` folder to `.readme` in order to prevent it to be packed.
16+
* Removes `.npmignore` (It doesn't work when using `files`: https://github.com/npm/npm/issues/4479)
17+
* Updates CI configuration.
18+
* Updates Documentation.
19+
320
## 1.9.1 - July 2022
421

522
* Fixes duplicate entries bug that happens when adding custom regex rule with same name.

README.md

Lines changed: 125 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ __________________
2828
},
2929
"devDependencies": {
3030
"eslint": ">=4.0.0",
31-
"eslint-plugin-regex": "1.9.1",
31+
"eslint-plugin-regex": "1.10.0",
3232
```
3333

3434
2 . Configure eslint:
@@ -129,7 +129,7 @@ Name | Fixable | Description
129129

130130
Each rule defines a set of patterns:
131131

132-
![Rules](readme/rules.svg)
132+
![Rules](.readme/rules.svg)
133133

134134
### 📏 `regex/invalid`
135135

@@ -205,19 +205,23 @@ Both rule has two options:
205205
]
206206
```
207207

208-
#### The `string` representing the regular expression
208+
#### The *string* representing the regular expression
209209

210210
Remember, Slashes (`/`) are not required in the string that defines the regex,
211211

212-
e.g. To get the following regex `/\bhttp:/`:
212+
e.g. To get the following regex `/^(test|spec)$/`, define:
213213

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`.
216215

217-
e.g. To get the following regex `/.*test\.js/`:
216+
e.g. To get the following regex `/\bhttp:/`, define:
218217

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)
221225

222226
#### Short pattern definition
223227

@@ -247,6 +251,7 @@ Each pattern is specified by just a **`string`** representing the regular expres
247251
It is specified by an `object`, with the following fields:
248252

249253
* `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]
250255
* `replacement` for `regex/invalid` [1]: [OPTIONAL]
251256
* An optional `string` used to replace the **invalid** found pattern, or
252257
* 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:
265270
{
266271
"id": "regexId",
267272
"regex": "regex",
273+
"flags": "isu",
268274
"replacement": "replacementString",
269275
"message": "errorMessage",
270276
"files": {
@@ -635,6 +641,114 @@ Since the **exact** word *return* is not present, this will allow the following
635641
}
636642
```
637643

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.
685+
686+
e.g.
687+
688+
```json
689+
{
690+
"regex": "^(?!(?:(feature|fix|docs|config|refactor|revert|test).*[\\.:]$)|(\\*\\s\\w.*\\.$)|$)"
691+
}
692+
```
693+
694+
`/path/to/some.js`:
695+
696+
```text
697+
config(ALL):
698+
699+
* 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+
638752
### Mixing
639753

640754
#### Mixing pattern types
@@ -800,7 +914,7 @@ A regex rule can be named with a custom name. The Rule name can be anything that
800914
* `regex/*invalid*`, `regex/*disuse*` or `regex/*avoid*` for invalid patterns.
801915
* `regex/*required*` or `regex/*use*` for required patterns.
802916

803-
![Mixed Rules](readme/mixed-rules.svg)
917+
![Mixed Rules](.readme/mixed-rules.svg)
804918

805919
> 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.
806920
@@ -1149,52 +1263,6 @@ or using synonyms to mix error levels:
11491263
]
11501264
```
11511265

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.
1197-
11981266
### `regex/invalid` vs `regex/required`
11991267

12001268
Both rule were design with *binary* approach:
@@ -1231,7 +1299,7 @@ __________________
12311299

12321300
## Extending/Developing
12331301

1234-
[Developing](js/readme/developing.md)
1302+
[Developing](js/.readme/developing.md)
12351303

12361304
## Contributing
12371305

docs/rules/invalid-regex-rule.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,36 @@ It is specified by just a regular expression `string`, i.e. `"regex"`
5656

5757
It is specified by an `object`, with the following fields:
5858

59-
* `regex`: A **required** `string` representing the **Regular expression to look for**.
60-
* `replacement` [1]:
61-
* An optional `string` used to replace the invalid found pattern, or
62-
* An optional `object` that establish how the invalid found pattern will be replaced:
63-
* `function`: used to replace the invalid found pattern.
59+
* `regex`: A **required** `string` for `regex/required` and `regex/invalid` representing the **Regular expression to look for**. [REQUIRED]
60+
* `flags`: A combination of flags, `i`, `s` and/or `u`, to be used by the Regular Expression. [OPTIONAL]
61+
* `replacement` for `regex/invalid` [1]: [OPTIONAL]
62+
* An optional `string` used to replace the **invalid** found pattern, or
63+
* An optional `object` that establish how the **invalid** found pattern will be replaced:
64+
* `function`: used to replace the **invalid** found pattern.
6465
* It will receive 3 parameters: `text`, `captured` and `$`, that can be used as desired.
6566
* It must return a `string` value, if not, return value will be ignored.
6667
* Its definition must be only the body of the function.
67-
* [More Information](#definition-of-the-function-used-to-replace-invalid-found-pattern).
68-
* `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]
7172
* `ignore`: A `string` representing **Regular expression of the files to be ignored** when validating this specific pattern.
7273
* `inspect`: A `string` representing **Regular expression of the files to be inspected** when validating this specific pattern.
7374

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+
7489
> * `regex` is the only Required field. Slashes (`/`) are not required in the string, e.g. To get the following regex `/\bhttp:/`:
7590
> * when using `.eslintrc.js`, define the following string `"\bhttp:"`, or
7691
> * when using `.eslintrc.json`, define `"\\bhttp:"` (backslash needs to de double in a json file).

0 commit comments

Comments
 (0)