Skip to content

Commit

Permalink
linter fixes, reduce function complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
pes10k committed Aug 1, 2024
1 parent 7478b3c commit 4ce5ca6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
29 changes: 16 additions & 13 deletions out/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand All @@ -156,15 +156,15 @@ 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 [];
}
const allSiblingNodes = _allOtherSiblings(element);
return allSiblingNodes.filter(x => x.matches(subOperator));
}
else if (trimmedSelector.startsWith('>')) {
const subOperator = _stripOperator('>', trimmedSelector);
const subOperator = _stripCssOperator('>', trimmedSelector);
if (subOperator === null) {
return [];
}
Expand All @@ -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] : [];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down
42 changes: 24 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 []
}
Expand All @@ -179,15 +179,15 @@ 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 []
}
const allSiblingNodes = _allOtherSiblings(element)
return allSiblingNodes.filter(x => x.matches(subOperator))
}
else if (trimmedSelector.startsWith('>')) {
const subOperator = _stripOperator('>', trimmedSelector)
const subOperator = _stripCssOperator('>', trimmedSelector)
if (subOperator === null) {
return []
}
Expand All @@ -201,9 +201,7 @@ const operatorCssSelector = (selector: CSSSelector,
if (element.matches(selector)) {
return [element]
}
else {
return []
}
return []
}

const _hasPlainSelectorCase = (selector: CSSSelector,
Expand Down Expand Up @@ -346,7 +344,8 @@ const _upwardIntCase = (intNeedle: NeedlePosition,
}
if (currentElement === null) {
return []
} else {
}
else {
const htmlElement = _asHTMLElement(currentElement)
return (htmlElement === null) ? [] : [htmlElement]
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4ce5ca6

Please sign in to comment.