Skip to content

Commit 5eecfce

Browse files
committed
fix: Fix "Invalid regular expression" introduced in the previous commit
1 parent 4a4cdbd commit 5eecfce

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/index.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ describe('getTerms', () => {
4646
expect(result).not.toContain('coffee');
4747
expect(result).toContain('mocha');
4848
expect(
49-
result.some((term) => Array.isArray(term) && term[1] === 'Node.js')
49+
result.some((term) => Array.isArray(term) && term[1] === 'Node.js'),
5050
).toBe(true);
5151
});
5252

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

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

7979
test('should not match a pattern in the middle of a word', () => {
8080
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
81-
'Foo superwebpacky bar'
81+
'Foo superwebpacky bar',
8282
);
8383
expect(result).toBeFalsy();
8484
});
8585

8686
test('should not match a pattern at the beginning of a string', () => {
8787
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
88-
'webpack bar'
88+
'webpack bar',
8989
);
9090
expect(result).toBeTruthy();
9191
expect(result?.[0]).toBe('webpack');
9292
});
9393

9494
test('should not match a pattern at the end of a string', () => {
9595
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
96-
'foo webpack'
96+
'foo webpack',
9797
);
9898
expect(result).toBeTruthy();
9999
expect(result?.[0]).toBe('webpack');
100100
});
101101

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

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

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

123123
test('should match a pattern at the end of a sentence', () => {
124124
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
125-
'My javascript.'
125+
'My javascript.',
126126
);
127127
expect(result).toBeTruthy();
128128
expect(result?.[0]).toBe('javascript');
129129
});
130130

131131
test('should match a pattern at the end of a sentence in the middle of a string', () => {
132132
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
133-
'My javascript. My webpack.'
133+
'My javascript. My webpack.',
134134
);
135135
expect(result).toBeTruthy();
136136
expect(result?.[0]).toBe('javascript');
@@ -151,21 +151,21 @@ describe('getMultipleWordRegExp', () => {
151151
['Bad Javascript?'],
152152
])('should match a pattern regardless of punctuation: %s', (string) => {
153153
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
154-
string
154+
string,
155155
);
156156
expect(result).toBeTruthy();
157157
});
158158

159159
test('should not match a pattern in as a part of a file name', () => {
160160
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
161-
'javascript.md'
161+
'javascript.md',
162162
);
163163
expect(result).toBeFalsy();
164164
});
165165

166166
test('should match a pattern regardless of its case', () => {
167167
const result = new RegExp(getMultipleWordRegExp(variants), 'igm').exec(
168-
'Javascript is good'
168+
'Javascript is good',
169169
);
170170
expect(result).toBeTruthy();
171171
expect(result?.[0]).toBe('Javascript');
@@ -219,23 +219,23 @@ describe('getAdvancedRegExp', () => {
219219
test('should return regexp as is if it has look behinds', () => {
220220
const regexp = new RegExp(
221221
getAdvancedRegExp(String.raw`(?<=(?:\w+[^.?!])? )base64\b`),
222-
'igm'
222+
'igm',
223223
);
224224
expect(regexp.test('use Base64')).toBeTruthy();
225225
});
226226

227227
test('should return regexp as is if it has positive look ahead', () => {
228228
const regexp = new RegExp(
229229
getAdvancedRegExp(String.raw`base64(?= \w)`),
230-
'igm'
230+
'igm',
231231
);
232232
expect(regexp.test('Base64 foo')).toBeTruthy();
233233
});
234234

235235
test('should return regexp as is if it has negative look ahead', () => {
236236
const regexp = new RegExp(
237237
getAdvancedRegExp(String.raw`base64(?! \w)`),
238-
'igm'
238+
'igm',
239239
);
240240
expect(regexp.test('Base64')).toBeTruthy();
241241
expect(regexp.test('Base64 foo')).toBeFalsy();
@@ -244,7 +244,7 @@ describe('getAdvancedRegExp', () => {
244244
test('should not match words inside filenames', () => {
245245
const regexp = new RegExp(
246246
getAdvancedRegExp(String.raw`(?<![\.-])css\b`),
247-
'igm'
247+
'igm',
248248
);
249249
expect(regexp.test('typings.for.css.modules.loader')).toBeFalsy();
250250
expect(regexp.test('typings-for-css-modules-loader')).toBeFalsy();
@@ -258,7 +258,7 @@ describe('getReplacement', () => {
258258
const result = getReplacement(
259259
'JavaScript',
260260
['npm', 'JavaScript', 'webpack'],
261-
'Javascript'
261+
'Javascript',
262262
);
263263
expect(result).toEqual('JavaScript');
264264
});
@@ -420,5 +420,5 @@ tester.run(
420420
],
421421
},
422422
],
423-
}
423+
},
424424
);

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const punctuation = String.raw`[\.,;:!?'"’”)]`;
3737

3838
function reporter(
3939
context: TextlintRuleContext,
40-
userOptions: Partial<Options> = {}
40+
userOptions: Partial<Options> = {},
4141
) {
4242
const options = { ...DEFAULT_OPTIONS, ...userOptions };
4343
const terms = getTerms(options.defaultTerms, options.terms, options.exclude);
@@ -48,7 +48,7 @@ function reporter(
4848

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

5454
const rules = [...exactWordRules, ...advancedRules];
@@ -62,7 +62,7 @@ function reporter(
6262
helper.isChildNode(
6363
// @ts-expect-error: Who the fuck knows what you want here ;-/
6464
node,
65-
options.skip.map((rule) => Syntax[rule])
65+
options.skip.map((rule) => Syntax[rule]),
6666
)
6767
) {
6868
return resolve();
@@ -73,7 +73,7 @@ function reporter(
7373
for (const [pattern, replacements] of rules) {
7474
const regExp = new RegExp(
7575
typeof pattern === 'string' ? getAdvancedRegExp(pattern) : pattern,
76-
'igm'
76+
'igm',
7777
);
7878

7979
let match;
@@ -102,7 +102,7 @@ function reporter(
102102

103103
const fix = fixer.replaceTextRange(
104104
[index, index + matched.length],
105-
replacement
105+
replacement,
106106
);
107107
const message = `Incorrect term: “${matched.trim()}”, use “${replacement.trim()}” instead`;
108108
report(node, new RuleError(message, { index, fix }));
@@ -118,7 +118,7 @@ function reporter(
118118
export function getTerms(
119119
defaultTerms: boolean,
120120
terms: string | Term[],
121-
exclude: string[]
121+
exclude: string[],
122122
) {
123123
const defaults = defaultTerms ? loadJson('../terms.jsonc') : [];
124124
const extras = typeof terms === 'string' ? loadJson(terms) : terms;
@@ -188,7 +188,7 @@ export function getAdvancedRegExp(pattern: string) {
188188
export function getReplacement(
189189
pattern: string,
190190
replacements: string | string[],
191-
matched: string
191+
matched: string,
192192
) {
193193
if (Array.isArray(replacements)) {
194194
return findWord(replacements, matched);

terms.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@
277277

278278
// Cases
279279
["kebab[_ ]?case", "kebab-case"],
280-
["camel[_- ]?case", "camelCase"],
281-
["pascal[_- ]?case", "PascalCase"],
280+
["camel[-_ ]?case", "camelCase"],
281+
["pascal[-_ ]?case", "PascalCase"],
282282
["snake[- ]?case", "snake_case"],
283283
["screaming[- ]?snake[- ]?case", "SCREAMING_SNAKE_CASE"],
284284

0 commit comments

Comments
 (0)