Skip to content

Commit 959da81

Browse files
authored
Merge pull request #16 from brave-experiments/reduce-function-complexity
linter fixes, reduce function complexity
2 parents 7478b3c + 4ce5ca6 commit 959da81

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

Diff for: eslint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default tseslint.config(
1111
stylistic.configs['recommended-flat'],
1212
{
1313
rules: {
14-
'max-len': ['error', 80, {
14+
'max-len': ['error', 100, {
1515
'ignoreStrings': true,
1616
}],
1717
'@typescript-eslint/no-explicit-any': 'off',

Diff for: out/main.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,17 @@ const _allChildrenRecursive = (element) => {
135135
.map(e => _asHTMLElement(e))
136136
.filter(e => e !== null);
137137
};
138+
const _stripCssOperator = (operator, selector) => {
139+
if (selector[0] !== operator) {
140+
throw new Error(`Expected to find ${operator} in initial position of "${selector}`);
141+
}
142+
return selector.replace(operator, '').trimStart();
143+
};
138144
// Implementation of ":css-selector" rule
139145
const operatorCssSelector = (selector, element) => {
140-
const _stripOperator = (operator, selector) => {
141-
if (selector[0] !== operator) {
142-
throw new Error(`Expected to find ${operator} in initial position of "${selector}`);
143-
}
144-
return selector.replace(operator, '').trimStart();
145-
};
146146
const trimmedSelector = selector.trimStart();
147147
if (trimmedSelector.startsWith('+')) {
148-
const subOperator = _stripOperator('+', trimmedSelector);
148+
const subOperator = _stripCssOperator('+', trimmedSelector);
149149
if (subOperator === null) {
150150
return [];
151151
}
@@ -156,15 +156,15 @@ const operatorCssSelector = (selector, element) => {
156156
return nextSibNode.matches(subOperator) ? [nextSibNode] : [];
157157
}
158158
else if (trimmedSelector.startsWith('~')) {
159-
const subOperator = _stripOperator('~', trimmedSelector);
159+
const subOperator = _stripCssOperator('~', trimmedSelector);
160160
if (subOperator === null) {
161161
return [];
162162
}
163163
const allSiblingNodes = _allOtherSiblings(element);
164164
return allSiblingNodes.filter(x => x.matches(subOperator));
165165
}
166166
else if (trimmedSelector.startsWith('>')) {
167-
const subOperator = _stripOperator('>', trimmedSelector);
167+
const subOperator = _stripCssOperator('>', trimmedSelector);
168168
if (subOperator === null) {
169169
return [];
170170
}
@@ -177,9 +177,7 @@ const operatorCssSelector = (selector, element) => {
177177
if (element.matches(selector)) {
178178
return [element];
179179
}
180-
else {
181-
return [];
182-
}
180+
return [];
183181
};
184182
const _hasPlainSelectorCase = (selector, element) => {
185183
return element.matches(selector) ? [element] : [];
@@ -396,7 +394,7 @@ const fastPathOperatorTypes = [
396394
'matches-media',
397395
'matches-path',
398396
];
399-
const applyCompiledSelector = (selector, initNodes) => {
397+
const _determineInitNodesAndIndex = (selector, initNodes) => {
400398
let nodesToConsider = [];
401399
let index = 0;
402400
// A couple of special cases to consider.
@@ -429,6 +427,11 @@ const applyCompiledSelector = (selector, initNodes) => {
429427
const allNodes = W.Array.from(W.document.all);
430428
nodesToConsider = allNodes.filter(_asHTMLElement);
431429
}
430+
return [index, nodesToConsider];
431+
};
432+
const applyCompiledSelector = (selector, initNodes) => {
433+
const initState = _determineInitNodesAndIndex(selector, initNodes);
434+
let [index, nodesToConsider] = initState;
432435
const numOperators = selector.length;
433436
for (index; nodesToConsider.length > 0 && index < numOperators; ++index) {
434437
const operator = selector[index];

Diff for: src/main.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const _compileRegEx = (regexText: string): RegExp => {
2828
//
2929
// If `exact` is true, then the string case it tested
3030
// for an exact match (the regex case is not affected).
31-
const _testMatches = (test: string, value: string, exact: boolean = false): boolean => {
31+
const _testMatches = (test: string, value: string, exact = false): boolean => {
3232
if (test[0] === '/') {
3333
return value.match(_compileRegEx(test)) !== null
3434
}
@@ -155,20 +155,20 @@ const _allChildrenRecursive = (element: HTMLElement): HTMLElement[] => {
155155
.filter(e => e !== null) as HTMLElement[]
156156
}
157157

158+
const _stripCssOperator = (operator: string, selector: string) => {
159+
if (selector[0] !== operator) {
160+
throw new Error(
161+
`Expected to find ${operator} in initial position of "${selector}`)
162+
}
163+
return selector.replace(operator, '').trimStart()
164+
}
165+
158166
// Implementation of ":css-selector" rule
159167
const operatorCssSelector = (selector: CSSSelector,
160168
element: HTMLElement): OperatorResult => {
161-
const _stripOperator = (operator: string, selector: string) => {
162-
if (selector[0] !== operator) {
163-
throw new Error(
164-
`Expected to find ${operator} in initial position of "${selector}`)
165-
}
166-
return selector.replace(operator, '').trimStart()
167-
}
168-
169169
const trimmedSelector = selector.trimStart()
170170
if (trimmedSelector.startsWith('+')) {
171-
const subOperator = _stripOperator('+', trimmedSelector)
171+
const subOperator = _stripCssOperator('+', trimmedSelector)
172172
if (subOperator === null) {
173173
return []
174174
}
@@ -179,15 +179,15 @@ const operatorCssSelector = (selector: CSSSelector,
179179
return nextSibNode.matches(subOperator) ? [nextSibNode] : []
180180
}
181181
else if (trimmedSelector.startsWith('~')) {
182-
const subOperator = _stripOperator('~', trimmedSelector)
182+
const subOperator = _stripCssOperator('~', trimmedSelector)
183183
if (subOperator === null) {
184184
return []
185185
}
186186
const allSiblingNodes = _allOtherSiblings(element)
187187
return allSiblingNodes.filter(x => x.matches(subOperator))
188188
}
189189
else if (trimmedSelector.startsWith('>')) {
190-
const subOperator = _stripOperator('>', trimmedSelector)
190+
const subOperator = _stripCssOperator('>', trimmedSelector)
191191
if (subOperator === null) {
192192
return []
193193
}
@@ -201,9 +201,7 @@ const operatorCssSelector = (selector: CSSSelector,
201201
if (element.matches(selector)) {
202202
return [element]
203203
}
204-
else {
205-
return []
206-
}
204+
return []
207205
}
208206

209207
const _hasPlainSelectorCase = (selector: CSSSelector,
@@ -346,7 +344,8 @@ const _upwardIntCase = (intNeedle: NeedlePosition,
346344
}
347345
if (currentElement === null) {
348346
return []
349-
} else {
347+
}
348+
else {
350349
const htmlElement = _asHTMLElement(currentElement)
351350
return (htmlElement === null) ? [] : [htmlElement]
352351
}
@@ -463,8 +462,8 @@ const fastPathOperatorTypes: OperatorType[] = [
463462
'matches-path',
464463
]
465464

466-
const applyCompiledSelector = (selector: CompiledProceduralSelector,
467-
initNodes?: HTMLElement[]): HTMLElement[] => {
465+
const _determineInitNodesAndIndex = (selector: CompiledProceduralSelector,
466+
initNodes?: HTMLElement[]): [number, HTMLElement[]] => {
468467
let nodesToConsider: HTMLElement[] = []
469468
let index = 0
470469

@@ -499,6 +498,13 @@ const applyCompiledSelector = (selector: CompiledProceduralSelector,
499498
const allNodes = W.Array.from(W.document.all)
500499
nodesToConsider = allNodes.filter(_asHTMLElement) as HTMLElement[]
501500
}
501+
return [index, nodesToConsider]
502+
}
503+
504+
const applyCompiledSelector = (selector: CompiledProceduralSelector,
505+
initNodes?: HTMLElement[]): HTMLElement[] => {
506+
const initState = _determineInitNodesAndIndex(selector, initNodes)
507+
let [index, nodesToConsider] = initState
502508

503509
const numOperators = selector.length
504510
for (index; nodesToConsider.length > 0 && index < numOperators; ++index) {

0 commit comments

Comments
 (0)