Skip to content

Commit

Permalink
infra(typescript-eslint): prefer-regexp-exec (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 17, 2023
1 parent c033044 commit 980f230
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
22 changes: 11 additions & 11 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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] === '-';
Expand All @@ -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 '-'
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/modules/airline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand Down

0 comments on commit 980f230

Please sign in to comment.