Skip to content

Commit 74cd129

Browse files
committed
## 1.6.0 - February 2021
* Adds additional parameter `$` to replacement function for invalid patterns in order to allow smaller definitions.
1 parent f71baf2 commit 74cd129

File tree

7 files changed

+249
-15
lines changed

7 files changed

+249
-15
lines changed

CHANGELOG.md

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

3+
## 1.6.0 - February 2021
4+
5+
* Adds additional parameter `$` to replacement function for invalid patterns in order to allow smaller definitions.
6+
37
## 1.5.0 - February 2021
48

59
* Adds capturing groups to replacement functions for invalid patterns.

README.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __________________
2323
..
2424
"devDependencies": {
2525
"eslint": ">=4.0.0",
26-
"eslint-plugin-regex": "1.5.0",
26+
"eslint-plugin-regex": "1.6.0",
2727
..
2828
```
2929

@@ -215,7 +215,7 @@ It is specified by an `object`, with the following fields:
215215
* An optional `string` used to replace the **invalid** found pattern, or
216216
* An optional `object` that establish how the **invalid** found pattern will be replaced:
217217
* `function`: used to replace the **invalid** found pattern.
218-
* It will receive 2 parameters: `text` and `captured`.
218+
* It will receive 3 parameters: `text`, `captured` and `$`, that can be used as desired.
219219
* It must return a `string` value, if not, return value will be ignored.
220220
* Its definition must be only the body of the function.
221221
* [More Information](#definition-of-the-function-used-to-replace-invalid-found-pattern).
@@ -253,10 +253,15 @@ Definition of the function must be done as a `string` in 1 line, and the followi
253253
* Its definition must be **only the body of the function**.
254254
* If the function has invalid Javascript code, the function will be ignored, i.e. it will silently fail.
255255

256-
Function will receive 2 parameters:
256+
Function will receive 3 parameters, to be used as desired:
257257

258258
* `text`: a `string` with the value of the invalid text found.
259259
* `captured`: an `array` of strings with the values of the captured groups for the regex.
260+
* `$`: an `array` of strings, with the value of the invalid text found plus the values of the captured groups for the regex.
261+
* `$[0]` = `text`: a `string` with the value of the invalid text found.
262+
* `$[1..]` = `captured`: an `array` of strings with the values of the captured groups for the regex.
263+
* `$[1]` = `captured[0]` and so on.
264+
* It allows smaller definitions.
260265

261266
e.g. Using parameter `text`
262267

@@ -274,6 +279,18 @@ Having the following rule in `.eslintrc.json`:
274279
}
275280
```
276281

282+
or using `$`:
283+
284+
```json
285+
{
286+
"id": "regexIdN",
287+
"regex": "\\serror\\w*\\s",
288+
"replacement": {
289+
"function": "return $[0].trim()"
290+
}
291+
}
292+
```
293+
277294
then, given:
278295

279296
`example.js`
@@ -304,6 +321,18 @@ Having the following rule in `.eslintrc.json`:
304321
}
305322
```
306323

324+
or using `$`:
325+
326+
```json
327+
{
328+
"id": "regexIdN",
329+
"regex": "\\serror(\\w*)\\s",
330+
"replacement": {
331+
"function": "return $[1]"
332+
}
333+
}
334+
```
335+
307336
then, given:
308337

309338
`example.js`
@@ -318,6 +347,72 @@ when linting with fix, the result will be:
318347
const exception = "19"
319348
```
320349

350+
e.g. Using parameters `text` and `captured`
351+
352+
`"return text + ' = ' + captured[0] + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))"` => only the body of the function + returns a `string` value based on `text` and `captured`
353+
354+
Having the following rule in `.eslintrc.json`:
355+
356+
```json
357+
{
358+
"id": "regexIdN",
359+
"regex": "(\\d+)\\+(\\d+)",
360+
"replacement": {
361+
"function": "return text + ' = ' + captured[0] + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))"
362+
}
363+
}
364+
```
365+
366+
or using `$`:
367+
368+
```json
369+
{
370+
"id": "regexIdN",
371+
"regex": "(\\d+)\\+(\\d+)",
372+
"replacement": {
373+
"function": "return $[0] + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
374+
}
375+
}
376+
```
377+
378+
or :
379+
380+
```json
381+
{
382+
"id": "regexIdN",
383+
"regex": "(\\d+)\\+(\\d+)",
384+
"replacement": {
385+
"function": "return text + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
386+
}
387+
}
388+
```
389+
390+
or :
391+
392+
```json
393+
{
394+
"id": "regexIdN",
395+
"regex": "(\\d+)\\+(\\d+)",
396+
"replacement": {
397+
"function": "return `${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`"
398+
}
399+
}
400+
```
401+
402+
then, given:
403+
404+
`example.js`
405+
406+
```js
407+
const sum = "4+5"
408+
```
409+
410+
when linting with fix, the result will be:
411+
412+
```js
413+
const sum = "4+5 = 4 + 5 = 9"
414+
```
415+
321416
###### Debugging of the Replacement Function for *invalid* found pattern
322417

323418
* It is possible to add `console` statements to print some information in the Replacement Function.

docs/rules/invalid-regex-rule.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ It is specified by an `object`, with the following fields:
6161
* An optional `string` used to replace the invalid found pattern, or
6262
* An optional `object` that establish how the invalid found pattern will be replaced:
6363
* `function`: used to replace the invalid found pattern.
64-
* It will receive 2 parameters: `text` and `captured`.
64+
* It will receive 3 parameters: `text`, `captured` and `$`, that can be used as desired.
6565
* It must return a `string` value, if not, return value will be ignored.
6666
* Its definition must be only the body of the function.
6767
* [More Information](#definition-of-the-function-used-to-replace-invalid-found-pattern).
@@ -113,10 +113,15 @@ Definition of the function must be done as a `string` in 1 line, and the followi
113113
* Its definition must be **only the body of the function**.
114114
* If the function has invalid Javascript code, the function will be ignored, i.e. it will silently fail.
115115

116-
Function will receive 2 parameters:
116+
Function will receive 3 parameters, to be used as desired:
117117

118118
* `text`: a `string` with the value of the invalid text found.
119119
* `captured`: an `array` of strings with the values of the captured groups for the regex.
120+
* `$`: an `array` of strings, with the value of the invalid text found plus the values of the captured groups for the regex.
121+
* `$[0]` = `text`: a `string` with the value of the invalid text found.
122+
* `$[1..]` = `captured`: an `array` of strings with the values of the captured groups for the regex.
123+
* `$[1]` = `captured[0]` and so on.
124+
* It allows smaller definitions.
120125

121126
e.g. Using parameter `text`
122127

@@ -134,6 +139,18 @@ Having the following rule in `.eslintrc.json`:
134139
}
135140
```
136141

142+
or using `$`:
143+
144+
```json
145+
{
146+
"id": "regexIdN",
147+
"regex": "\\serror\\w*\\s",
148+
"replacement": {
149+
"function": "return $[0].trim()"
150+
}
151+
}
152+
```
153+
137154
then, given:
138155

139156
`example.js`
@@ -164,6 +181,18 @@ Having the following rule in `.eslintrc.json`:
164181
}
165182
```
166183

184+
or using `$`:
185+
186+
```json
187+
{
188+
"id": "regexIdN",
189+
"regex": "\\serror(\\w*)\\s",
190+
"replacement": {
191+
"function": "return $[1]"
192+
}
193+
}
194+
```
195+
167196
then, given:
168197

169198
`example.js`
@@ -178,6 +207,72 @@ when linting with fix, the result will be:
178207
const exception = "19"
179208
```
180209

210+
e.g. Using parameters `text` and `captured`
211+
212+
`"return text + ' = ' + captured[0] + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))"` => only the body of the function + returns a `string` value based on `text` and `captured`
213+
214+
Having the following rule in `.eslintrc.json`:
215+
216+
```json
217+
{
218+
"id": "regexIdN",
219+
"regex": "(\\d+)\\+(\\d+)",
220+
"replacement": {
221+
"function": "return text + ' = ' + captured[0] + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))"
222+
}
223+
}
224+
```
225+
226+
or using `$`:
227+
228+
```json
229+
{
230+
"id": "regexIdN",
231+
"regex": "(\\d+)\\+(\\d+)",
232+
"replacement": {
233+
"function": "return $[0] + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
234+
}
235+
}
236+
```
237+
238+
or :
239+
240+
```json
241+
{
242+
"id": "regexIdN",
243+
"regex": "(\\d+)\\+(\\d+)",
244+
"replacement": {
245+
"function": "return text + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
246+
}
247+
}
248+
```
249+
250+
or :
251+
252+
```json
253+
{
254+
"id": "regexIdN",
255+
"regex": "(\\d+)\\+(\\d+)",
256+
"replacement": {
257+
"function": "return `${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`"
258+
}
259+
}
260+
```
261+
262+
then, given:
263+
264+
`example.js`
265+
266+
```js
267+
const sum = "4+5"
268+
```
269+
270+
when linting with fix, the result will be:
271+
272+
```js
273+
const sum = "4+5 = 4 + 5 = 9"
274+
```
275+
181276
##### Debugging of the Replacement Function for invalid found pattern
182277

183278
* It is possible to add `console` statements to print some information in the Replacement Function.

lib/rules/invalid-regex-rule.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ function checkRegexInSource(source, regex) {
3636
const foundDetail = regex.exec(source)
3737
return !!foundDetail
3838
? {
39-
pattern: foundDetail.shift(),
40-
captured: foundDetail,
39+
$: foundDetail,
4140
patternIndex: foundDetail.index,
4241
nextChar: regex.lastIndex
4342
}
@@ -97,16 +96,16 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
9796
function buildReplacementFunction(replacement) {
9897
if (/\breturn\b/.test(replacement)) {
9998
try {
100-
const replacementFunction = new Function('text', 'captured', replacement) // eslint-disable-line no-new-func
101-
return (text, captured) => {
99+
const replacementFunction = new Function('text', 'captured', '$', replacement) // eslint-disable-line no-new-func
100+
return $ => {
102101
try {
103-
const replacement = replacementFunction(text, captured)
102+
const replacement = replacementFunction($[0], $.slice(1), $)
104103
if (typeof replacement === 'string') {
105104
return replacement
106105
}
107106
}
108107
catch(e) {}
109-
return text
108+
return $[0]
110109
}
111110
}
112111
catch(e) {}
@@ -125,7 +124,7 @@ function createReplacement(replacement) {
125124
return (from, found) =>
126125
fixer => fixer.replaceTextRange(
127126
[from + found.patternIndex, from + found.nextChar],
128-
replacementFunction(found.pattern, found.captured)
127+
replacementFunction(found.$)
129128
)
130129
}
131130
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "eslint-plugin-regex",
33
"description": "ESLint rules using Regular Expression",
4-
"version": "1.5.0",
4+
"version": "1.6.0",
55
"license": "MIT",
66
"author": "Gonzalo Müller Bravo",
77
"main": "lib/index.js",

0 commit comments

Comments
 (0)