Skip to content

Commit f824823

Browse files
committed
refactor: use regexp-groups to simplify ignores (POC)
1 parent 24eccf4 commit f824823

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

lib/rules/syntaxes/slot-attribute.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ module.exports = {
1616
/** @type {{ ignore: string[] }} */
1717
const options = context.options[0] || {}
1818
const { ignore = [] } = options
19-
/** @type {RegExp[]} */
20-
const ignorePatterns = ignore.map(regexp.toRegExp)
19+
const ignoreGroupMatcher = regexp.toRegExpGroupMatcher(ignore)
2120

2221
const sourceCode = context.getSourceCode()
2322
const tokenStore =
@@ -125,15 +124,12 @@ module.exports = {
125124
*/
126125
function reportSlot(slotAttr) {
127126
const componentName = slotAttr.parent.parent.rawName
128-
const componentNamePascalCase = casing.pascalCase(componentName)
129-
const componentNameKebabCase = casing.kebabCase(componentName)
130127

131128
if (
132-
ignorePatterns.some(
133-
(pattern) =>
134-
pattern.test(componentName) ||
135-
pattern.test(componentNamePascalCase) ||
136-
pattern.test(componentNameKebabCase)
129+
ignoreGroupMatcher(
130+
componentName,
131+
casing.pascalCase(componentName),
132+
casing.kebabCase(componentName)
137133
)
138134
) {
139135
return

lib/utils/regexp.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,32 @@ function isRegExp(string) {
4141
return RE_REGEXP_STR.test(string)
4242
}
4343

44+
/**
45+
* Converts an array of strings to a singular function to match any of them.
46+
* This function converts each string to a `RegExp` and returns a function that checks all of them.
47+
*
48+
* @param {string[]} [patterns] The strings or regular expression strings to match.
49+
* @returns {(...toCheck: string[]) => boolean} Returns a function that checks if any string matches any of the given patterns.
50+
*/
51+
function toRegExpGroupMatcher(patterns = []) {
52+
if (patterns.length === 0) {
53+
return () => false
54+
}
55+
56+
// In the future, we could optimize this by joining expressions with identical flags.
57+
const regexps = patterns.map(toRegExp)
58+
59+
if (regexps.length === 1) {
60+
return (...toCheck) => toCheck.some((str) => regexps[0].test(str))
61+
}
62+
63+
return (...toCheck) =>
64+
regexps.some((regexp) => toCheck.some((str) => regexp.test(str)))
65+
}
66+
4467
module.exports = {
4568
escape,
4669
toRegExp,
47-
isRegExp
70+
isRegExp,
71+
toRegExpGroupMatcher
4872
}

tests/lib/utils/regexp.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

3-
const { escape, toRegExp } = require('../../../lib/utils/regexp')
3+
const {
4+
escape,
5+
toRegExp,
6+
toRegExpGroupMatcher
7+
} = require('../../../lib/utils/regexp')
48
const assert = require('assert')
59

610
const ESCAPED = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\'
@@ -36,3 +40,57 @@ describe('toRegExp()', () => {
3640
assert.deepEqual(toRegExp(`${/[\sA-Z]+/u}`), /[\sA-Z]+/u)
3741
})
3842
})
43+
44+
describe('toRegExpCheckGroup()', () => {
45+
it('should return a function missing inout', () => {
46+
const groupMatcher = toRegExpGroupMatcher()
47+
assert.strictEqual(groupMatcher(''), false)
48+
assert.strictEqual(groupMatcher('foo'), false)
49+
assert.strictEqual(groupMatcher('bar'), false)
50+
})
51+
52+
it('should return a function for empty array', () => {
53+
const groupMatcher = toRegExpGroupMatcher([])
54+
assert.strictEqual(groupMatcher(''), false)
55+
assert.strictEqual(groupMatcher('foo'), false)
56+
assert.strictEqual(groupMatcher('bar'), false)
57+
})
58+
59+
it('should return a function for single simple pattern', () => {
60+
const groupMatcher = toRegExpGroupMatcher(['foo'])
61+
assert.strictEqual(groupMatcher(''), false)
62+
assert.strictEqual(groupMatcher('foo'), true)
63+
assert.strictEqual(groupMatcher('foo', 'early'), true)
64+
assert.strictEqual(groupMatcher('late', 'matches', 'foo'), true)
65+
assert.strictEqual(groupMatcher('foobar'), false)
66+
assert.strictEqual(groupMatcher('afoo', 'fooa', 'afooa', 'bar'), false)
67+
})
68+
69+
it('should return a function for multiple simple patterns', () => {
70+
const groupMatcher = toRegExpGroupMatcher(['foo', 'bar'])
71+
assert.strictEqual(groupMatcher('foo'), true)
72+
assert.strictEqual(groupMatcher('bar', 'early'), true)
73+
assert.strictEqual(groupMatcher('late', 'matches', 'foo'), true)
74+
assert.strictEqual(groupMatcher('foobar'), false)
75+
assert.strictEqual(groupMatcher('afoo', 'fooa', 'afooa'), false)
76+
})
77+
78+
it('should return a function for single regexp pattern', () => {
79+
const groupMatcher = toRegExpGroupMatcher(['/^foo/'])
80+
assert.strictEqual(groupMatcher(''), false)
81+
assert.strictEqual(groupMatcher('foo'), true)
82+
assert.strictEqual(groupMatcher('fooa', 'early'), true)
83+
assert.strictEqual(groupMatcher('late', 'matches', 'fooa'), true)
84+
assert.strictEqual(groupMatcher('barfoo'), false)
85+
assert.strictEqual(groupMatcher('afoo', 'afooa', 'bar'), false)
86+
})
87+
88+
it('should return a function for multiple regexp patterns', () => {
89+
const groupMatcher = toRegExpGroupMatcher(['/^foo/', '/bar$/'])
90+
assert.strictEqual(groupMatcher('foo'), true)
91+
assert.strictEqual(groupMatcher('bar', 'early'), true)
92+
assert.strictEqual(groupMatcher('late', 'matches', 'foo'), true)
93+
assert.strictEqual(groupMatcher('barfoo'), false)
94+
assert.strictEqual(groupMatcher('afoo', 'afooa', 'bara'), false)
95+
})
96+
})

0 commit comments

Comments
 (0)