Skip to content

Commit

Permalink
fix: Fix "Invalid regular expression" introduced in the previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed Aug 2, 2024
1 parent 4a4cdbd commit 5eecfce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
40 changes: 20 additions & 20 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe('getTerms', () => {
expect(result).not.toContain('coffee');
expect(result).toContain('mocha');
expect(
result.some((term) => Array.isArray(term) && term[1] === 'Node.js')
result.some((term) => Array.isArray(term) && term[1] === 'Node.js'),
).toBe(true);
});

test('should remove the excluded terms (defined as Array)', () => {
const result = getTerms(true, [], ['Node[ .]?js']);
expect(result).toBeTruthy();
expect(
result.some((term) => Array.isArray(term) && term[1] === 'Node.js')
result.some((term) => Array.isArray(term) && term[1] === 'Node.js'),
).toBe(false);
});
});
Expand All @@ -71,66 +71,66 @@ describe('getMultipleWordRegExp', () => {

test('should match a pattern as a full word', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'My JavaScript is good'
'My JavaScript is good',
);
expect(result?.[0]).toBe('JavaScript');
});

test('should not match a pattern in the middle of a word', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'Foo superwebpacky bar'
'Foo superwebpacky bar',
);
expect(result).toBeFalsy();
});

test('should not match a pattern at the beginning of a string', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'webpack bar'
'webpack bar',
);
expect(result).toBeTruthy();
expect(result?.[0]).toBe('webpack');
});

test('should not match a pattern at the end of a string', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'foo webpack'
'foo webpack',
);
expect(result).toBeTruthy();
expect(result?.[0]).toBe('webpack');
});

test('should not match a pattern at the beginning of a word with a hyphen', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'Foo webpack-ish bar'
'Foo webpack-ish bar',
);
expect(result).toBeFalsy();
});

test('should not match a pattern in at the end of a word with a hyphen', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'Foo uber-webpack bar'
'Foo uber-webpack bar',
);
expect(result).toBeFalsy();
});

test('should not match a pattern in at the middle of a word with hyphens', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'Foo uber-webpack-ish bar'
'Foo uber-webpack-ish bar',
);
expect(result).toBeFalsy();
});

test('should match a pattern at the end of a sentence', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'My javascript.'
'My javascript.',
);
expect(result).toBeTruthy();
expect(result?.[0]).toBe('javascript');
});

test('should match a pattern at the end of a sentence in the middle of a string', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'My javascript. My webpack.'
'My javascript. My webpack.',
);
expect(result).toBeTruthy();
expect(result?.[0]).toBe('javascript');
Expand All @@ -151,21 +151,21 @@ describe('getMultipleWordRegExp', () => {
['Bad Javascript?'],
])('should match a pattern regardless of punctuation: %s', (string) => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
string
string,
);
expect(result).toBeTruthy();
});

test('should not match a pattern in as a part of a file name', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'javascript.md'
'javascript.md',
);
expect(result).toBeFalsy();
});

test('should match a pattern regardless of its case', () => {
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
'Javascript is good'
'Javascript is good',
);
expect(result).toBeTruthy();
expect(result?.[0]).toBe('Javascript');
Expand Down Expand Up @@ -219,23 +219,23 @@ describe('getAdvancedRegExp', () => {
test('should return regexp as is if it has look behinds', () => {
const regexp = new RegExp(
getAdvancedRegExp(String.raw`(?<=(?:\w+[^.?!])? )base64\b`),
'igm'
'igm',
);
expect(regexp.test('use Base64')).toBeTruthy();
});

test('should return regexp as is if it has positive look ahead', () => {
const regexp = new RegExp(
getAdvancedRegExp(String.raw`base64(?= \w)`),
'igm'
'igm',
);
expect(regexp.test('Base64 foo')).toBeTruthy();
});

test('should return regexp as is if it has negative look ahead', () => {
const regexp = new RegExp(
getAdvancedRegExp(String.raw`base64(?! \w)`),
'igm'
'igm',
);
expect(regexp.test('Base64')).toBeTruthy();
expect(regexp.test('Base64 foo')).toBeFalsy();
Expand All @@ -244,7 +244,7 @@ describe('getAdvancedRegExp', () => {
test('should not match words inside filenames', () => {
const regexp = new RegExp(
getAdvancedRegExp(String.raw`(?<![\.-])css\b`),
'igm'
'igm',
);
expect(regexp.test('typings.for.css.modules.loader')).toBeFalsy();
expect(regexp.test('typings-for-css-modules-loader')).toBeFalsy();
Expand All @@ -258,7 +258,7 @@ describe('getReplacement', () => {
const result = getReplacement(
'JavaScript',
['npm', 'JavaScript', 'webpack'],
'Javascript'
'Javascript',
);
expect(result).toEqual('JavaScript');
});
Expand Down Expand Up @@ -420,5 +420,5 @@ tester.run(
],
},
],
}
},
);
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const punctuation = String.raw`[\.,;:!?'"’”)]`;

function reporter(
context: TextlintRuleContext,
userOptions: Partial<Options> = {}
userOptions: Partial<Options> = {},
) {
const options = { ...DEFAULT_OPTIONS, ...userOptions };
const terms = getTerms(options.defaultTerms, options.terms, options.exclude);
Expand All @@ -48,7 +48,7 @@ function reporter(

// Create a separate regexp of each array rule ([pattern, replacement])
const advancedRules: Tuple[] = terms.filter(
(rule) => typeof rule !== 'string'
(rule) => typeof rule !== 'string',
);

const rules = [...exactWordRules, ...advancedRules];
Expand All @@ -62,7 +62,7 @@ function reporter(
helper.isChildNode(
// @ts-expect-error: Who the fuck knows what you want here ;-/
node,
options.skip.map((rule) => Syntax[rule])
options.skip.map((rule) => Syntax[rule]),
)
) {
return resolve();
Expand All @@ -73,7 +73,7 @@ function reporter(
for (const [pattern, replacements] of rules) {
const regExp = new RegExp(
typeof pattern === 'string' ? getAdvancedRegExp(pattern) : pattern,
'igm'
'igm',
);

let match;
Expand Down Expand Up @@ -102,7 +102,7 @@ function reporter(

const fix = fixer.replaceTextRange(
[index, index + matched.length],
replacement
replacement,
);
const message = `Incorrect term: “${matched.trim()}”, use “${replacement.trim()}” instead`;
report(node, new RuleError(message, { index, fix }));
Expand All @@ -118,7 +118,7 @@ function reporter(
export function getTerms(
defaultTerms: boolean,
terms: string | Term[],
exclude: string[]
exclude: string[],
) {
const defaults = defaultTerms ? loadJson('../terms.jsonc') : [];
const extras = typeof terms === 'string' ? loadJson(terms) : terms;
Expand Down Expand Up @@ -188,7 +188,7 @@ export function getAdvancedRegExp(pattern: string) {
export function getReplacement(
pattern: string,
replacements: string | string[],
matched: string
matched: string,
) {
if (Array.isArray(replacements)) {
return findWord(replacements, matched);
Expand Down
4 changes: 2 additions & 2 deletions terms.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@

// Cases
["kebab[_ ]?case", "kebab-case"],
["camel[_- ]?case", "camelCase"],
["pascal[_- ]?case", "PascalCase"],
["camel[-_ ]?case", "camelCase"],
["pascal[-_ ]?case", "PascalCase"],
["snake[- ]?case", "snake_case"],
["screaming[- ]?snake[- ]?case", "SCREAMING_SNAKE_CASE"],

Expand Down

0 comments on commit 5eecfce

Please sign in to comment.