Skip to content

Commit 1b46375

Browse files
committed
## 1.7.0 - February 2021
* Closes #6, Removes the requirement of `return` presence for replacement functions for invalid patterns.
1 parent 74cd129 commit 1b46375

File tree

7 files changed

+369
-23
lines changed

7 files changed

+369
-23
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.7.0 - February 2021
4+
5+
* Closes #6, Removes the requirement of `return` presence for replacement functions for invalid patterns.
6+
37
## 1.6.0 - February 2021
48

59
* Adds additional parameter `$` to replacement function for invalid patterns in order to allow smaller definitions.

README.md

Lines changed: 125 additions & 4 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.6.0",
26+
"eslint-plugin-regex": "1.7.0",
2727
..
2828
```
2929

@@ -251,6 +251,7 @@ Definition of the function must be done as a `string` in 1 line, and the followi
251251

252252
* It must return a `string` value, if not, return value will be ignored, i.e. it will silently fail.
253253
* Its definition must be **only the body of the function**.
254+
* For "simple" functions where the `return` is found at the beginning of the body of the function and the **exact** word *return* is not present, `return` can be omitted.
254255
* If the function has invalid Javascript code, the function will be ignored, i.e. it will silently fail.
255256

256257
Function will receive 3 parameters, to be used as desired:
@@ -263,7 +264,7 @@ Function will receive 3 parameters, to be used as desired:
263264
* `$[1]` = `captured[0]` and so on.
264265
* It allows smaller definitions.
265266

266-
e.g. Using parameter `text`
267+
**e.g. Using parameter `text`**
267268

268269
`"return text.trim()"` => only the body of the function + returns a `string` value based on `text`
269270

@@ -305,7 +306,31 @@ when linting with fix, the result will be:
305306
const exception = "error19"
306307
```
307308

308-
e.g. Using parameter `captured`
309+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
310+
311+
```json
312+
{
313+
"id": "regexIdN",
314+
"regex": "\\serror\\w*\\s",
315+
"replacement": {
316+
"function": "text.trim()"
317+
}
318+
}
319+
```
320+
321+
or
322+
323+
```json
324+
{
325+
"id": "regexIdN",
326+
"regex": "\\serror\\w*\\s",
327+
"replacement": {
328+
"function": "$[0].trim()"
329+
}
330+
}
331+
```
332+
333+
**e.g. Using parameter `captured`**
309334

310335
`"return captured[0]"` => only the body of the function + returns a `string` value based on `captured`
311336

@@ -347,7 +372,31 @@ when linting with fix, the result will be:
347372
const exception = "19"
348373
```
349374

350-
e.g. Using parameters `text` and `captured`
375+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
376+
377+
```json
378+
{
379+
"id": "regexIdN",
380+
"regex": "\\serror(\\w*)\\s",
381+
"replacement": {
382+
"function": "captured[0]"
383+
}
384+
}
385+
```
386+
387+
or
388+
389+
```json
390+
{
391+
"id": "regexIdN",
392+
"regex": "\\serror(\\w*)\\s",
393+
"replacement": {
394+
"function": "$[1]"
395+
}
396+
}
397+
```
398+
399+
**e.g. Using parameters `text` and `captured`**
351400

352401
`"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`
353402

@@ -413,6 +462,78 @@ when linting with fix, the result will be:
413462
const sum = "4+5 = 4 + 5 = 9"
414463
```
415464

465+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
466+
467+
```json
468+
{
469+
"id": "regexIdN",
470+
"regex": "(\\d+)\\+(\\d+)",
471+
"replacement": {
472+
"function": "text + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
473+
}
474+
}
475+
```
476+
477+
or :
478+
479+
```json
480+
{
481+
"id": "regexIdN",
482+
"regex": "(\\d+)\\+(\\d+)",
483+
"replacement": {
484+
"function": "`${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`"
485+
}
486+
}
487+
```
488+
489+
**e.g. `return` required**
490+
491+
e.g. `const result = text === 'superb' ? 'Superb' : text; return result` => only the body of the function + returns a `string` value based on `text`.
492+
493+
Since the `return` is not found at the beginning of the body of the function, `return` cannot be omitted, then rule definition will be as usual:
494+
495+
```json
496+
{
497+
"id": "regexIdN",
498+
"regex": "\\w+",
499+
"replacement": {
500+
"function": "const result = text === 'superb' ? 'Superb' : text; return result"
501+
}
502+
}
503+
```
504+
505+
> Some cases may use Comma operator, e.g. `"function": "result = text === 'superb' ? 'Superb' : text, result"`
506+
507+
e.g. `return text === 'return' ? 'Return' : text` => only the body of the function + returns a `string` value based on `text`.
508+
509+
Since the *exact* word *return* is present, this will **required** `return`, then rule definition will be as usual:
510+
511+
```json
512+
{
513+
"id": "regexIdN",
514+
"regex": "\\w+",
515+
"replacement": {
516+
"function": "return text === 'return' ? 'Return' : text"
517+
}
518+
}
519+
```
520+
521+
Following case does not required `return`:
522+
523+
e.g. `return text === 'Return' ? 'RETURN' : text` => only the body of the function + returns a `string` value based on `text`.
524+
525+
Since the **exact** word *return* is not present, this will allow the following rule definition to be:
526+
527+
```json
528+
{
529+
"id": "regexIdN",
530+
"regex": "\\w+",
531+
"replacement": {
532+
"function": "text === 'Return' ? 'RETURN' : text"
533+
}
534+
}
535+
```
536+
416537
###### Debugging of the Replacement Function for *invalid* found pattern
417538

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

docs/rules/invalid-regex-rule.md

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Definition of the function must be done as a `string` in 1 line, and the followi
111111

112112
* It must return a `string` value, if not, return value will be ignored, i.e. it will silently fail.
113113
* Its definition must be **only the body of the function**.
114+
* For "simple" functions where the `return` is found at the beginning of the body of the function and the **exact** word *return* is not present, `return` can be omitted.
114115
* If the function has invalid Javascript code, the function will be ignored, i.e. it will silently fail.
115116

116117
Function will receive 3 parameters, to be used as desired:
@@ -123,7 +124,7 @@ Function will receive 3 parameters, to be used as desired:
123124
* `$[1]` = `captured[0]` and so on.
124125
* It allows smaller definitions.
125126

126-
e.g. Using parameter `text`
127+
**e.g. Using parameter `text`**
127128

128129
`"return text.trim()"` => only the body of the function + returns a `string` value based on `text`
129130

@@ -165,7 +166,31 @@ when linting with fix, the result will be:
165166
const exception = "error19"
166167
```
167168

168-
e.g. Using parameter `captured`
169+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
170+
171+
```json
172+
{
173+
"id": "regexIdN",
174+
"regex": "\\serror\\w*\\s",
175+
"replacement": {
176+
"function": "text.trim()"
177+
}
178+
}
179+
```
180+
181+
or
182+
183+
```json
184+
{
185+
"id": "regexIdN",
186+
"regex": "\\serror\\w*\\s",
187+
"replacement": {
188+
"function": "$[0].trim()"
189+
}
190+
}
191+
```
192+
193+
**e.g. Using parameter `captured`**
169194

170195
`"return captured[0]"` => only the body of the function + returns a `string` value based on `captured`
171196

@@ -207,7 +232,31 @@ when linting with fix, the result will be:
207232
const exception = "19"
208233
```
209234

210-
e.g. Using parameters `text` and `captured`
235+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
236+
237+
```json
238+
{
239+
"id": "regexIdN",
240+
"regex": "\\serror(\\w*)\\s",
241+
"replacement": {
242+
"function": "captured[0]"
243+
}
244+
}
245+
```
246+
247+
or
248+
249+
```json
250+
{
251+
"id": "regexIdN",
252+
"regex": "\\serror(\\w*)\\s",
253+
"replacement": {
254+
"function": "$[1]"
255+
}
256+
}
257+
```
258+
259+
**e.g. Using parameters `text` and `captured`**
211260

212261
`"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`
213262

@@ -273,6 +322,78 @@ when linting with fix, the result will be:
273322
const sum = "4+5 = 4 + 5 = 9"
274323
```
275324

325+
As the body of the function is "simple", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:
326+
327+
```json
328+
{
329+
"id": "regexIdN",
330+
"regex": "(\\d+)\\+(\\d+)",
331+
"replacement": {
332+
"function": "text + ' = ' + $[1] + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))"
333+
}
334+
}
335+
```
336+
337+
or :
338+
339+
```json
340+
{
341+
"id": "regexIdN",
342+
"regex": "(\\d+)\\+(\\d+)",
343+
"replacement": {
344+
"function": "`${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`"
345+
}
346+
}
347+
```
348+
349+
**e.g. `return` required**
350+
351+
e.g. `const result = text === 'superb' ? 'Superb' : text; return result` => only the body of the function + returns a `string` value based on `text`.
352+
353+
Since the `return` is not found at the beginning of the body of the function, `return` cannot be omitted, then rule definition will be as usual:
354+
355+
```json
356+
{
357+
"id": "regexIdN",
358+
"regex": "\\w+",
359+
"replacement": {
360+
"function": "const result = text === 'superb' ? 'Superb' : text; return result"
361+
}
362+
}
363+
```
364+
365+
> Some cases may use Comma operator, e.g. `"function": "result = text === 'superb' ? 'Superb' : text, result"`
366+
367+
e.g. `return text === 'return' ? 'Return' : text` => only the body of the function + returns a `string` value based on `text`.
368+
369+
Since the *exact* word *return* is present, this will **required** `return`, then rule definition will be as usual:
370+
371+
```json
372+
{
373+
"id": "regexIdN",
374+
"regex": "\\w+",
375+
"replacement": {
376+
"function": "return text === 'return' ? 'Return' : text"
377+
}
378+
}
379+
```
380+
381+
Following case does not required `return`:
382+
383+
e.g. `return text === 'Return' ? 'RETURN' : text` => only the body of the function + returns a `string` value based on `text`.
384+
385+
Since the **exact** word *return* is not present, this will allow the following rule definition to be:
386+
387+
```json
388+
{
389+
"id": "regexIdN",
390+
"regex": "\\w+",
391+
"replacement": {
392+
"function": "text === 'Return' ? 'RETURN' : text"
393+
}
394+
}
395+
```
396+
276397
##### Debugging of the Replacement Function for invalid found pattern
277398

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

lib/rules/invalid-regex-rule.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,22 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
9494
}
9595

9696
function buildReplacementFunction(replacement) {
97-
if (/\breturn\b/.test(replacement)) {
98-
try {
99-
const replacementFunction = new Function('text', 'captured', '$', replacement) // eslint-disable-line no-new-func
100-
return $ => {
101-
try {
102-
const replacement = replacementFunction($[0], $.slice(1), $)
103-
if (typeof replacement === 'string') {
104-
return replacement
105-
}
97+
try {
98+
const replacementFunction = new Function('text', 'captured', '$', /\breturn\b/.test(replacement) ? replacement : `return ${replacement}`) // eslint-disable-line no-new-func
99+
return $ => {
100+
try {
101+
const replacement = replacementFunction($[0], $.slice(1), $)
102+
if (typeof replacement === 'string') {
103+
return replacement
106104
}
107-
catch(e) {}
108-
return $[0]
109105
}
106+
catch(e) {}
107+
return $[0]
110108
}
111-
catch(e) {}
112109
}
113-
return null
110+
catch(e) {
111+
return null
112+
}
114113
}
115114

116115
function createReplacement(replacement) {

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.6.0",
4+
"version": "1.7.0",
55
"license": "MIT",
66
"author": "Gonzalo Müller Bravo",
77
"main": "lib/index.js",

0 commit comments

Comments
 (0)