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: enable no-useless-escape eslint rule #2434

Merged
merged 7 commits into from
Oct 7, 2023
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ module.exports = defineConfig({
},
plugins: ['@typescript-eslint', 'prettier', 'deprecation', 'jsdoc'],
rules: {
// We may want to use this in the future
'no-useless-escape': 'off',
eqeqeq: ['error', 'always', { null: 'ignore' }],
'no-else-return': 'error',
'prefer-template': 'error',
Expand Down
2 changes: 1 addition & 1 deletion scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function updateLocaleFile(filePath: string): void {
if (lstatSync(filePath).isFile()) {
const pathParts = filePath
.substring(pathLocales.length + 1, filePath.length - 3)
.split(/[\\\/]/);
.split(/[\\/]/);
const locale = pathParts[0];
pathParts.splice(0, 1);
updateLocaleFileHook(filePath, locale, pathParts);
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 @@ -97,7 +97,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
12 changes: 6 additions & 6 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 RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
const REP_REG = /(.)\{(\d+)\}/;
const RANGE_REG = /\[(\d+)\-(\d+)\]/;
const RANGE_REG = /\[(\d+)-(\d+)\]/;
let min: number;
let max: number;
let tmp: number;
Expand Down Expand Up @@ -192,7 +192,7 @@ export class SimpleHelpersModule {
.normalize('NFKD') //for example è decomposes to as e + ̀
.replace(/[\u0300-\u036f]/g, '') // removes combining marks
.replace(/ /g, '-') // replaces spaces with hyphens
.replace(/[^\w\.\-]+/g, ''); // removes all non-word characters except for dots and hyphens
.replace(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens
}

/**
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+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
/([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
let token = pattern.match(SINGLE_CHAR_REG);
while (token != null) {
const quantifierMin: string = token[2];
Expand All @@ -439,7 +439,7 @@ export class SimpleHelpersModule {

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 = pattern.match(RANGE_ALPHANUMEMRIC_REG);
while (token != null) {
Expand Down Expand Up @@ -548,7 +548,7 @@ export class SimpleHelpersModule {
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
}

const RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/;
const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
// Deal with quantifier ranges `{min,max}`
token = pattern.match(RANGE_REP_REG);
while (token != null) {
Expand Down
2 changes: 1 addition & 1 deletion 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(/[^A-Za-z0-9._+-]+/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
2 changes: 1 addition & 1 deletion test/modules/commerce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('commerce', () => {
expect(
amount,
'The expected match should not include a currency symbol'
).toMatch(/^[0-9\.]+$/);
).toMatch(/^[0-9.]+$/);
});

it('should handle negative amounts, but return 0', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/modules/finance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ describe('finance', () => {
expect(
amount,
'The expected match should not include a currency symbol'
).toMatch(/^[0-9\.]+$/);
).toMatch(/^[0-9.]+$/);
});

it('should handle negative amounts', () => {
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('finance', () => {
it('should return a correct credit card number when issuer provided', () => {
//TODO: implement checks for each format with regexp
const visa = faker.finance.creditCardNumber('visa');
expect(visa).toMatch(/^4(([0-9]){12}|([0-9]){3}(\-([0-9]){4}){3})$/);
expect(visa).toMatch(/^4(([0-9]){12}|([0-9]){3}(-([0-9]){4}){3})$/);
expect(visa).toSatisfy(luhnCheck);

const mastercard = faker.finance.creditCardNumber('mastercard');
Expand Down Expand Up @@ -490,7 +490,7 @@ describe('finance', () => {

it('should return custom formatted strings', () => {
let number = faker.finance.creditCardNumber('###-###-##L');
expect(number).toMatch(/^\d{3}\-\d{3}\-\d{3}$/);
expect(number).toMatch(/^\d{3}-\d{3}-\d{3}$/);
expect(number).toSatisfy(luhnCheck);

number = faker.finance.creditCardNumber('234[5-9]#{999}L');
Expand Down
22 changes: 10 additions & 12 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe('helpers', () => {
'6453-####-####-####-###L'
);
expect(number).toMatch(
/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/
/^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/
);
expect(number).toSatisfy(luhnCheck);
});
Expand All @@ -574,7 +574,7 @@ describe('helpers', () => {
'*'
);
expect(number).toMatch(
/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/
/^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/
);
expect(number).toSatisfy(luhnCheck);
});
Expand All @@ -585,14 +585,14 @@ describe('helpers', () => {
'*'
);
expect(number).toMatch(
/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/
/^6453-([0-9]){4}-([0-9]){4}-([0-9]){4}-([0-9]){4}$/
);
expect(number).toSatisfy(luhnCheck);
number = faker.helpers.replaceCreditCardSymbols(
'645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L'
);
expect(number).toMatch(
/^645[5-9]\-([0-9]){4,6}\-([0-9]){1,2}\-([0-9]){4,6}\-([0-9]){4}$/
/^645[5-9]-([0-9]){4,6}-([0-9]){1,2}-([0-9]){4,6}-([0-9]){4}$/
);
expect(number).toSatisfy(luhnCheck);
});
Expand All @@ -607,14 +607,14 @@ describe('helpers', () => {
const string = faker.helpers.regexpStyleStringParse('#{5,10}');
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^\#{5,10}$/);
expect(string).toMatch(/^#{5,10}$/);
});

it('flips the range when min > max', () => {
const string = faker.helpers.regexpStyleStringParse('#{10,5}');
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^\#{5,10}$/);
expect(string).toMatch(/^#{5,10}$/);
});

it('repeats string {n} number of times', () => {
Expand All @@ -638,9 +638,7 @@ describe('helpers', () => {
const string = faker.helpers.regexpStyleStringParse(
'Test#{5}%{2,5}Testing**[1-5]**{10}END'
);
expect(string).toMatch(
/^Test\#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/
);
expect(string).toMatch(/^Test#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/);
});
});

Expand All @@ -649,7 +647,7 @@ describe('helpers', () => {
const string = faker.helpers.fromRegExp(/#{5,10}/);
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^\#{5,10}$/);
expect(string).toMatch(/^#{5,10}$/);
});

it('repeats string {n} number of times', () => {
Expand All @@ -667,7 +665,7 @@ describe('helpers', () => {
const string = faker.helpers.fromRegExp(
'Test#{5}%{2,5}Testing*[1-5]{10}END'
);
expect(string).toMatch(/^Test\#{5}%{2,5}Testing*[1-5]{10}END$/);
expect(string).toMatch(/^Test#{5}%{2,5}Testing*[1-5]{10}END$/);
});

it('throws error when min > max outside set', () => {
Expand Down Expand Up @@ -1176,7 +1174,7 @@ describe('helpers', () => {
'lName',
'domain',
]); // third argument is provider, or domain for email
expect(result).toMatch(/\@domain/);
expect(result).toMatch(/@domain/);
});

it('should be possible to limit unique call by maxTime in ms', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/modules/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ describe('image', () => {

expect(avatarUrl).toBeTypeOf('string');
expect(avatarUrl).toMatch(
/^https:\/\/cloudflare\-ipfs\.com\/ipfs\/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye\/avatar\/\d{1,4}\.jpg$/
/^https:\/\/cloudflare-ipfs\.com\/ipfs\/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye\/avatar\/\d{1,4}\.jpg$/
);
});
});
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('image', () => {

expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/loremflickr\.com\/\d+\/\d+\?lock=\d+$/
/^https:\/\/loremflickr\.com\/\d+\/\d+\?lock=\d+$/
);
});
});
Expand All @@ -474,7 +474,7 @@ describe('image', () => {

expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/picsum\.photos\/seed\/[0-9a-zA-Z]+\/\d+\/\d+$/
/^https:\/\/picsum\.photos\/seed\/[0-9a-zA-Z]+\/\d+\/\d+$/
);
});
});
Expand All @@ -485,7 +485,7 @@ describe('image', () => {

expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/via\.placeholder\.com\/\d+x\d+\/[0-9a-fA-F]{6}\/[0-9a-fA-F]{6}\.[a-z]{3,4}\?text=.+$/
/^https:\/\/via\.placeholder\.com\/\d+x\d+\/[0-9a-fA-F]{6}\/[0-9a-fA-F]{6}\.[a-z]{3,4}\?text=.+$/
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/modules/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ describe('internet', () => {
expect(ua).toBeTypeOf('string');
expect(ua.length).toBeGreaterThanOrEqual(1);
expect(ua).toMatch(
/^(([^\d]+\/[\dA-Za-z\.]+(\s\(.*\)))|([^\d]+\/[\dA-Za-z\.]+(\s\(.*\)*))(\s[^\d]+\/[\dA-Za-z\.]+(\s\(.*\)*))*)$/
/^(([^\d]+\/[\dA-Za-z.]+(\s\(.*\)))|([^\d]+\/[\dA-Za-z.]+(\s\(.*\)*))(\s[^\d]+\/[\dA-Za-z.]+(\s\(.*\)*))*)$/
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/apidoc/verify-jsdoc-tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('verify JSDoc tags', () => {

const path = resolvePathToMethodFile(moduleName, methodName);
const imports = [
...new Set(examples.match(/(?<!\.)faker[^\.]*(?=\.)/g)),
...new Set(examples.match(/(?<!\.)faker[^.]*(?=\.)/g)),
];
writeFileSync(
path,
Expand Down