Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra(unicorn): better-regex #2487

Closed
wants to merge 10 commits into from
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ module.exports = defineConfig({

// TODO @Shinigami92 2023-09-23: The following rules currently conflict with our code.
// Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently.
'unicorn/better-regex': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/filename-case': 'off',
'unicorn/import-style': 'off',
Expand Down
2 changes: 1 addition & 1 deletion scripts/apidoc/parameterDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function cleanParameterDefault(value?: string): string | undefined {
}

// Strip type casts: "'foobar' as unknown as T" => "'foobar'"
return value.replace(/ as unknown as [A-Za-z<>]+/, '');
return value.replace(/ as unknown as [<>A-Za-z]+/, '');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ function extractDefaultFromComment(comment?: Comment): string | undefined {
return;
}

const result = /^(.*)[ \n]Defaults to `([^`]+)`\.(.*)$/s.exec(text);
const result = /^(.*)[\n ]Defaults to `([^`]+)`\.(.*)$/s.exec(text);
if (!result) {
return;
}
Expand All @@ -344,6 +344,6 @@ function extractDefaultFromComment(comment?: Comment): string | undefined {

summary.splice(summary.length - 2, 2);
const lastSummaryPart = summary[summary.length - 1];
lastSummaryPart.text = lastSummaryPart.text.replace(/[ \n]Defaults to $/, '');
lastSummaryPart.text = lastSummaryPart.text.replace(/[\n ]Defaults to $/, '');
return result[2];
}
4 changes: 2 additions & 2 deletions scripts/apidoc/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export function extractSourceBaseUrl(
reflection: DeclarationReflection | SignatureReflection
): string {
return extractSourceUrl(reflection).replace(
/^(.*\/blob\/[0-9a-f]+\/)(.*)$/,
/^(.*\/blob\/[\da-f]+\/)(.*)$/,
'$1'
);
}
Expand All @@ -202,7 +202,7 @@ export function extractSourcePath(
reflection: DeclarationReflection | SignatureReflection
): string {
return extractSourceUrl(reflection).replace(
/^(.*\/blob\/[0-9a-f]+\/)(.*)$/,
/^(.*\/blob\/[\da-f]+\/)(.*)$/,
'$2'
);
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async function updateLocaleFile(filePath: string): Promise<void> {
if (lstatSync(filePath).isFile()) {
const [locale, moduleKey, entryKey] = filePath
.substring(pathLocales.length + 1, filePath.length - 3)
.split(/[\\/]/);
.split(/[/\\]/);
await updateLocaleFileHook(filePath, locale, moduleKey, entryKey);
}
}
Expand Down Expand Up @@ -405,7 +405,7 @@ async function main(): Promise<void> {
console.error(error);
}

const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
const localizedFaker = `faker${locale.replace(/^[a-z]+/, (part) =>
part.toUpperCase()
)}`;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/finance/iban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ const iban: Iban = {
pattern10: ['01', '02', '03', '04', '05', '06', '07', '08', '09'],
pattern100: ['001', '002', '003', '004', '005', '006', '007', '008', '009'],
toDigitString: (str) =>
str.replace(/[A-Z]/gi, (match) =>
str.replace(/[a-z]/gi, (match) =>
String(match.toUpperCase().charCodeAt(0) - 55)
),
};
Expand Down
2 changes: 1 addition & 1 deletion src/modules/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class GitModule {
const email = this.faker.internet.email({ firstName, lastName });

// Normalize user according to https://github.com/libgit2/libgit2/issues/5342
user = user.replace(/^[.,:;"\\']|[<>\n]|[.,:;"\\']$/g, '');
user = user.replace(/^["',.:;\\]|[\n<>]|["',.:;\\]$/g, '');

lines.push(
`Author: ${user} <${email}>`,
Expand Down
16 changes: 8 additions & 8 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ function legacyRegexpStringParse(
string: string = ''
): string {
// Deal with range repeat `{min,max}`
const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
const REP_REG = /(.)\{(\d+)\}/;
const RANGE_REG = /\[(\d+)-(\d+)\]/;
const RANGE_REP_REG = /(.){(\d+),(\d+)}/;
const REP_REG = /(.){(\d+)}/;
const RANGE_REG = /\[(\d+)-(\d+)]/;
let min: number;
let max: number;
let tmp: number;
Expand Down Expand Up @@ -416,7 +416,7 @@ export class SimpleHelpersModule {

// Deal with single wildcards
const SINGLE_CHAR_REG =
/([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
/([\d.a-z])(?:{(\d+)(?:,(\d+)|)}|([*+?]))(?![^[]*]|[^{]*})/i;
let token = SINGLE_CHAR_REG.exec(pattern);
while (token != null) {
const quantifierMin: string = token[2];
Expand All @@ -437,9 +437,9 @@ export class SimpleHelpersModule {
token = SINGLE_CHAR_REG.exec(pattern);
}

const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/;
const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[!"#$&()+,./@`-])/;
const RANGE_ALPHANUMEMRIC_REG =
/\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+)|)/;
/\[(\^|)(-|)(.+?)](?:{(\d+)(?:,(\d+)|)}|([*+?])|)/;
// Deal with character classes with quantifiers `[a-z0-9]{min[, max]}`
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
while (token != null) {
Expand Down Expand Up @@ -552,7 +552,7 @@ export class SimpleHelpersModule {
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
}

const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
const RANGE_REP_REG = /(.){(\d+),(\d+)}/;
// Deal with quantifier ranges `{min,max}`
token = RANGE_REP_REG.exec(pattern);
while (token != null) {
Expand All @@ -571,7 +571,7 @@ export class SimpleHelpersModule {
token = RANGE_REP_REG.exec(pattern);
}

const REP_REG = /(.)\{(\d+)\}/;
const REP_REG = /(.){(\d+)}/;
// Deal with repeat `{num}`
token = REP_REG.exec(pattern);
while (token != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/image/providers/unsplash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class Unsplash {
url += `/${width}x${height}`;

if (keyword != null) {
const keywordFormat = /^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$/;
// One or more keywords separated by commas
const keywordFormat = /^[\da-z]+(,[\da-z]+)*$/i;
if (keywordFormat.test(keyword)) {
url += `?${keyword}`;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class InternetModule {
let localPart: string = this.userName({ firstName, lastName });
// Strip any special characters from the local part of the email address
// This could happen if invalid chars are passed in manually in the firstName/lastName
localPart = localPart.replace(/[^A-Za-z0-9._+-]+/g, '');
localPart = localPart.replace(/[^\w+.-]+/g, '');

// The local part of an email address is limited to 64 chars per RFC 3696
// We limit to 50 chars to be more realistic
Expand Down Expand Up @@ -1462,8 +1462,8 @@ export class InternetModule {
* Copyright(c) 2011-2013 Bermi Ferrer <bermi@bermilabs.com>
* MIT Licensed
*/
const vowel = /[aeiouAEIOU]$/;
const consonant = /[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/;
const vowel = /[aeiou]$/i;
const consonant = /[b-df-hj-np-tv-z]$/i;
const _password = (
length: number,
memorable: boolean,
Expand Down
20 changes: 10 additions & 10 deletions test/modules/airline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ describe('airline', () => {
describe(`recordLocator()`, () => {
it('should use the default values when not passing arguments', () => {
const recordLocator = faker.airline.recordLocator();
expect(recordLocator).toMatch(/^[A-HJ-KM-NP-Z]{6}$/);
expect(recordLocator).toMatch(/^[A-HJKMNP-Z]{6}$/);
});
it('should allow numeric characters', () => {
const recordLocator = faker.airline.recordLocator({
allowNumerics: true,
});
expect(recordLocator).toMatch(/^[2-9A-HJ-KM-NP-Z]{6}$/);
expect(recordLocator).toMatch(/^[2-9A-HJKMNP-Z]{6}$/);
});
it('should allow visually similar characters', () => {
const recordLocator = faker.airline.recordLocator({
Expand All @@ -87,7 +87,7 @@ describe('airline', () => {
allowNumerics: true,
allowVisuallySimilarCharacters: true,
});
expect(recordLocator).toMatch(/^[0-9A-Z]{6}$/);
expect(recordLocator).toMatch(/^[\dA-Z]{6}$/);
});
});

Expand Down Expand Up @@ -129,7 +129,7 @@ describe('airline', () => {
const row = matchResult[1];
const seatLetter = matchResult[2];
expect(row).toSatisfy((row: number) => row >= 1 && row <= 60);
expect(seatLetter).toMatch(/^[A-HJ-K]$/);
expect(seatLetter).toMatch(/^[A-HJK]$/);
});
});

Expand All @@ -143,37 +143,37 @@ describe('airline', () => {
describe(`flightNumber()`, () => {
it('should return a random flight number', () => {
const flightNumber = faker.airline.flightNumber();
expect(flightNumber).toMatch(/^[1-9][0-9]{0,3}$/);
expect(flightNumber).toMatch(/^[1-9]\d{0,3}$/);
});
it('should return a random flight number with 3 digits', () => {
const flightNumber = faker.airline.flightNumber({ length: 3 });
expect(flightNumber).toMatch(/^[1-9][0-9]{2}$/);
expect(flightNumber).toMatch(/^[1-9]\d{2}$/);
});
it('should return a random flight number with 2 to 4 digits', () => {
const flightNumber = faker.airline.flightNumber({
length: { min: 2, max: 4 },
});
expect(flightNumber).toMatch(/^[1-9][0-9]{1,3}$/);
expect(flightNumber).toMatch(/^[1-9]\d{1,3}$/);
});
it('should return a random flight number with leading zeros', () => {
const flightNumber = faker.airline.flightNumber({
addLeadingZeros: true,
});
expect(flightNumber).toMatch(/^[0-9]{4}$/);
expect(flightNumber).toMatch(/^\d{4}$/);
});
it('should return a random flight number with 3 digits and leading zeros', () => {
const flightNumber = faker.airline.flightNumber({
length: 3,
addLeadingZeros: true,
});
expect(flightNumber).toMatch(/^[0-9][1-9][0-9]{2}$/);
expect(flightNumber).toMatch(/^\d[1-9]\d{2}$/);
});
it('should return a random flight number with 2 to 4 digits and leading zeros', () => {
const flightNumber = faker.airline.flightNumber({
length: { min: 2, max: 4 },
addLeadingZeros: true,
});
expect(flightNumber).toMatch(/^[0-9]{1,4}$/);
expect(flightNumber).toMatch(/^\d{1,4}$/);
});
});
}
Expand Down