From 980f230947ca250086aedf37160abe86c766f969 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Tue, 17 Oct 2023 22:18:04 +0200 Subject: [PATCH] infra(typescript-eslint): prefer-regexp-exec (#2466) --- .eslintrc.js | 1 + src/modules/helpers/index.ts | 22 +++++++++++----------- test/modules/airline.spec.ts | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index b678af7419c..ad17b31b7dd 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -112,6 +112,7 @@ module.exports = defineConfig({ 'error', { blankLine: 'always', prev: 'block-like', next: '*' }, ], + '@typescript-eslint/prefer-regexp-exec': 'error', '@typescript-eslint/restrict-template-expressions': [ 'error', { allowNumber: true, allowBoolean: true }, diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index ab0ec5b8b86..22d8491f0b6 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -407,7 +407,7 @@ export class SimpleHelpersModule { if (pattern instanceof RegExp) { isCaseInsensitive = pattern.flags.includes('i'); pattern = pattern.toString(); - pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp + pattern = /\/(.+?)\//.exec(pattern)?.[1] ?? ''; // Remove frontslash from front and back of RegExp } let min: number; @@ -417,7 +417,7 @@ export class SimpleHelpersModule { // Deal with single wildcards const SINGLE_CHAR_REG = /([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/; - let token = pattern.match(SINGLE_CHAR_REG); + let token = SINGLE_CHAR_REG.exec(pattern); while (token != null) { const quantifierMin: string = token[2]; const quantifierMax: string = token[3]; @@ -434,14 +434,14 @@ export class SimpleHelpersModule { pattern.slice(0, token.index) + token[1].repeat(repetitions) + pattern.slice(token.index + token[0].length); - token = pattern.match(SINGLE_CHAR_REG); + token = SINGLE_CHAR_REG.exec(pattern); } const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/; const RANGE_ALPHANUMEMRIC_REG = /\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+)|)/; // Deal with character classes with quantifiers `[a-z0-9]{min[, max]}` - token = pattern.match(RANGE_ALPHANUMEMRIC_REG); + token = RANGE_ALPHANUMEMRIC_REG.exec(pattern); while (token != null) { const isNegated = token[1] === '^'; const includesDash: boolean = token[2] === '-'; @@ -452,7 +452,7 @@ export class SimpleHelpersModule { const rangeCodes: number[] = []; let ranges = token[3]; - let range = ranges.match(SINGLE_RANGE_REG); + let range = SINGLE_RANGE_REG.exec(ranges); if (includesDash) { // 45 is the ascii code for '-' @@ -494,7 +494,7 @@ export class SimpleHelpersModule { } ranges = ranges.substring(range[0].length); - range = ranges.match(SINGLE_RANGE_REG); + range = SINGLE_RANGE_REG.exec(ranges); } repetitions = getRepetitionsBasedOnQuantifierParameters( @@ -549,12 +549,12 @@ export class SimpleHelpersModule { pattern.slice(0, token.index) + generatedString + pattern.slice(token.index + token[0].length); - token = pattern.match(RANGE_ALPHANUMEMRIC_REG); + token = RANGE_ALPHANUMEMRIC_REG.exec(pattern); } const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/; // Deal with quantifier ranges `{min,max}` - token = pattern.match(RANGE_REP_REG); + token = RANGE_REP_REG.exec(pattern); while (token != null) { min = parseInt(token[2]); max = parseInt(token[3]); @@ -568,19 +568,19 @@ export class SimpleHelpersModule { pattern.slice(0, token.index) + token[1].repeat(repetitions) + pattern.slice(token.index + token[0].length); - token = pattern.match(RANGE_REP_REG); + token = RANGE_REP_REG.exec(pattern); } const REP_REG = /(.)\{(\d+)\}/; // Deal with repeat `{num}` - token = pattern.match(REP_REG); + token = REP_REG.exec(pattern); while (token != null) { repetitions = parseInt(token[2]); pattern = pattern.slice(0, token.index) + token[1].repeat(repetitions) + pattern.slice(token.index + token[0].length); - token = pattern.match(REP_REG); + token = REP_REG.exec(pattern); } return pattern; diff --git a/test/modules/airline.spec.ts b/test/modules/airline.spec.ts index 2cbb47c3a14..45737f5abe1 100644 --- a/test/modules/airline.spec.ts +++ b/test/modules/airline.spec.ts @@ -95,7 +95,7 @@ describe('airline', () => { const seatRegex = /^(\d{1,2})([A-K])$/; it('should return a random narrowbody seat when not passing an argument', () => { const seat = faker.airline.seat(); - const matchResult = seat.match(seatRegex); + const matchResult = seatRegex.exec(seat); expect(matchResult).not.toBeNull(); const row = matchResult[1]; const seatLetter = matchResult[2]; @@ -106,7 +106,7 @@ describe('airline', () => { const seat = faker.airline.seat({ aircraftType: Aircraft.Narrowbody, }); - const matchResult = seat.match(seatRegex); + const matchResult = seatRegex.exec(seat); expect(matchResult).not.toBeNull(); const row = matchResult[1]; const seatLetter = matchResult[2]; @@ -115,7 +115,7 @@ describe('airline', () => { }); it('should return a random regional seat', () => { const seat = faker.airline.seat({ aircraftType: Aircraft.Regional }); - const matchResult = seat.match(seatRegex); + const matchResult = seatRegex.exec(seat); expect(matchResult).not.toBeNull(); const row = matchResult[1]; const seatLetter = matchResult[2]; @@ -124,7 +124,7 @@ describe('airline', () => { }); it('should return a random widebody seat', () => { const seat = faker.airline.seat({ aircraftType: Aircraft.Widebody }); - const matchResult = seat.match(seatRegex); + const matchResult = seatRegex.exec(seat); expect(matchResult).not.toBeNull(); const row = matchResult[1]; const seatLetter = matchResult[2];