From 4ce5ca647e4e8e1ea60bcf36534b5216c912ed67 Mon Sep 17 00:00:00 2001 From: Peter Snyder Date: Thu, 1 Aug 2024 17:14:10 -0500 Subject: [PATCH] linter fixes, reduce function complexity --- eslint.config.js | 2 +- out/main.js | 29 ++++++++++++++++------------- src/main.ts | 42 ++++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index d92a42f..4ec79e4 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -11,7 +11,7 @@ export default tseslint.config( stylistic.configs['recommended-flat'], { rules: { - 'max-len': ['error', 80, { + 'max-len': ['error', 100, { 'ignoreStrings': true, }], '@typescript-eslint/no-explicit-any': 'off', diff --git a/out/main.js b/out/main.js index 516b7f3..3a10ec4 100644 --- a/out/main.js +++ b/out/main.js @@ -135,17 +135,17 @@ const _allChildrenRecursive = (element) => { .map(e => _asHTMLElement(e)) .filter(e => e !== null); }; +const _stripCssOperator = (operator, selector) => { + if (selector[0] !== operator) { + throw new Error(`Expected to find ${operator} in initial position of "${selector}`); + } + return selector.replace(operator, '').trimStart(); +}; // Implementation of ":css-selector" rule const operatorCssSelector = (selector, element) => { - const _stripOperator = (operator, selector) => { - if (selector[0] !== operator) { - throw new Error(`Expected to find ${operator} in initial position of "${selector}`); - } - return selector.replace(operator, '').trimStart(); - }; const trimmedSelector = selector.trimStart(); if (trimmedSelector.startsWith('+')) { - const subOperator = _stripOperator('+', trimmedSelector); + const subOperator = _stripCssOperator('+', trimmedSelector); if (subOperator === null) { return []; } @@ -156,7 +156,7 @@ const operatorCssSelector = (selector, element) => { return nextSibNode.matches(subOperator) ? [nextSibNode] : []; } else if (trimmedSelector.startsWith('~')) { - const subOperator = _stripOperator('~', trimmedSelector); + const subOperator = _stripCssOperator('~', trimmedSelector); if (subOperator === null) { return []; } @@ -164,7 +164,7 @@ const operatorCssSelector = (selector, element) => { return allSiblingNodes.filter(x => x.matches(subOperator)); } else if (trimmedSelector.startsWith('>')) { - const subOperator = _stripOperator('>', trimmedSelector); + const subOperator = _stripCssOperator('>', trimmedSelector); if (subOperator === null) { return []; } @@ -177,9 +177,7 @@ const operatorCssSelector = (selector, element) => { if (element.matches(selector)) { return [element]; } - else { - return []; - } + return []; }; const _hasPlainSelectorCase = (selector, element) => { return element.matches(selector) ? [element] : []; @@ -396,7 +394,7 @@ const fastPathOperatorTypes = [ 'matches-media', 'matches-path', ]; -const applyCompiledSelector = (selector, initNodes) => { +const _determineInitNodesAndIndex = (selector, initNodes) => { let nodesToConsider = []; let index = 0; // A couple of special cases to consider. @@ -429,6 +427,11 @@ const applyCompiledSelector = (selector, initNodes) => { const allNodes = W.Array.from(W.document.all); nodesToConsider = allNodes.filter(_asHTMLElement); } + return [index, nodesToConsider]; +}; +const applyCompiledSelector = (selector, initNodes) => { + const initState = _determineInitNodesAndIndex(selector, initNodes); + let [index, nodesToConsider] = initState; const numOperators = selector.length; for (index; nodesToConsider.length > 0 && index < numOperators; ++index) { const operator = selector[index]; diff --git a/src/main.ts b/src/main.ts index a54a53f..0cede5c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,7 +28,7 @@ const _compileRegEx = (regexText: string): RegExp => { // // If `exact` is true, then the string case it tested // for an exact match (the regex case is not affected). -const _testMatches = (test: string, value: string, exact: boolean = false): boolean => { +const _testMatches = (test: string, value: string, exact = false): boolean => { if (test[0] === '/') { return value.match(_compileRegEx(test)) !== null } @@ -155,20 +155,20 @@ const _allChildrenRecursive = (element: HTMLElement): HTMLElement[] => { .filter(e => e !== null) as HTMLElement[] } +const _stripCssOperator = (operator: string, selector: string) => { + if (selector[0] !== operator) { + throw new Error( + `Expected to find ${operator} in initial position of "${selector}`) + } + return selector.replace(operator, '').trimStart() +} + // Implementation of ":css-selector" rule const operatorCssSelector = (selector: CSSSelector, element: HTMLElement): OperatorResult => { - const _stripOperator = (operator: string, selector: string) => { - if (selector[0] !== operator) { - throw new Error( - `Expected to find ${operator} in initial position of "${selector}`) - } - return selector.replace(operator, '').trimStart() - } - const trimmedSelector = selector.trimStart() if (trimmedSelector.startsWith('+')) { - const subOperator = _stripOperator('+', trimmedSelector) + const subOperator = _stripCssOperator('+', trimmedSelector) if (subOperator === null) { return [] } @@ -179,7 +179,7 @@ const operatorCssSelector = (selector: CSSSelector, return nextSibNode.matches(subOperator) ? [nextSibNode] : [] } else if (trimmedSelector.startsWith('~')) { - const subOperator = _stripOperator('~', trimmedSelector) + const subOperator = _stripCssOperator('~', trimmedSelector) if (subOperator === null) { return [] } @@ -187,7 +187,7 @@ const operatorCssSelector = (selector: CSSSelector, return allSiblingNodes.filter(x => x.matches(subOperator)) } else if (trimmedSelector.startsWith('>')) { - const subOperator = _stripOperator('>', trimmedSelector) + const subOperator = _stripCssOperator('>', trimmedSelector) if (subOperator === null) { return [] } @@ -201,9 +201,7 @@ const operatorCssSelector = (selector: CSSSelector, if (element.matches(selector)) { return [element] } - else { - return [] - } + return [] } const _hasPlainSelectorCase = (selector: CSSSelector, @@ -346,7 +344,8 @@ const _upwardIntCase = (intNeedle: NeedlePosition, } if (currentElement === null) { return [] - } else { + } + else { const htmlElement = _asHTMLElement(currentElement) return (htmlElement === null) ? [] : [htmlElement] } @@ -463,8 +462,8 @@ const fastPathOperatorTypes: OperatorType[] = [ 'matches-path', ] -const applyCompiledSelector = (selector: CompiledProceduralSelector, - initNodes?: HTMLElement[]): HTMLElement[] => { +const _determineInitNodesAndIndex = (selector: CompiledProceduralSelector, + initNodes?: HTMLElement[]): [number, HTMLElement[]] => { let nodesToConsider: HTMLElement[] = [] let index = 0 @@ -499,6 +498,13 @@ const applyCompiledSelector = (selector: CompiledProceduralSelector, const allNodes = W.Array.from(W.document.all) nodesToConsider = allNodes.filter(_asHTMLElement) as HTMLElement[] } + return [index, nodesToConsider] +} + +const applyCompiledSelector = (selector: CompiledProceduralSelector, + initNodes?: HTMLElement[]): HTMLElement[] => { + const initState = _determineInitNodesAndIndex(selector, initNodes) + let [index, nodesToConsider] = initState const numOperators = selector.length for (index; nodesToConsider.length > 0 && index < numOperators; ++index) {