|
| 1 | +import esquery from 'esquery'; |
| 2 | +import visitorKeys from '../generated/keys.js'; |
| 3 | +import { FUNCTION_NODE_TYPE_IDS, NODE_TYPE_IDS_MAP } from '../generated/type_ids.js'; |
| 4 | +// @ts-expect-error we need to generate `.d.ts` file for this module |
| 5 | +import { ancestors } from '../generated/walk.js'; |
| 6 | + |
| 7 | +import type { ESQueryOptions, Selector as EsquerySelector } from 'esquery'; |
| 8 | +import type { Node as EsqueryNode } from 'estree'; |
| 9 | +import type { Node, VisitFn } from './types.ts'; |
| 10 | + |
| 11 | +const ObjectKeys = Object.keys; |
| 12 | + |
| 13 | +const { matches: esqueryMatches, parse: esqueryParse } = esquery; |
| 14 | + |
| 15 | +type NodeTypeId = number; |
| 16 | + |
| 17 | +// Options to call `esquery.matches` with. |
| 18 | +const ESQUERY_OPTIONS: ESQueryOptions = { |
| 19 | + nodeTypeKey: 'type', |
| 20 | + visitorKeys, |
| 21 | + fallback: (node: EsqueryNode) => ObjectKeys(node).filter(filterKey), |
| 22 | + matchClass: (_className: unknown, _node: EsqueryNode, _ancestors: EsqueryNode[]) => false, // TODO: Is this right? |
| 23 | +}; |
| 24 | +const filterKey = (key: string) => key !== 'parent' && key !== 'range' && key !== 'loc'; |
| 25 | + |
| 26 | +// Parsed selector. |
| 27 | +interface Selector { |
| 28 | + // Array of IDs of types this selector matches, or `null` if selector matches all types. |
| 29 | + typeIds: NodeTypeId[] | null; |
| 30 | + // `esquery` selector object for this selector. |
| 31 | + esquerySelector: EsquerySelector; |
| 32 | + // `true` if selector applies matching beyond just filtering on node type. |
| 33 | + // * `FunctionExpression > Identifier` is complex. |
| 34 | + // * `:matches(FunctionExpression, FunctionDeclaration)` is not complex. |
| 35 | + // Primarily this exists to make simple `:matches` faster. |
| 36 | + isComplex: boolean; |
| 37 | + // Number of attributes in selector. Used for calculating selector's specificity. |
| 38 | + attributeCount: number; |
| 39 | + // Number of identifiers in selector. Used for calculating selector's specificity. |
| 40 | + identifierCount: number; |
| 41 | +} |
| 42 | + |
| 43 | +// Cache of parsed `Selector`s. |
| 44 | +const cache: Map<string, Selector> = new Map([]); |
| 45 | + |
| 46 | +const EMPTY_TYPE_IDS_ARRAY: NodeTypeId[] = []; |
| 47 | + |
| 48 | +/** |
| 49 | + * Parse a selector string and return a `Selector` object which represents it. |
| 50 | + * |
| 51 | + * @param key - Selector string e.g. `Program > VariableDeclaration` |
| 52 | + * @returns `Selector` object |
| 53 | + */ |
| 54 | +export function parseSelector(key: string): Selector { |
| 55 | + // Used cached object if we've parsed this key before |
| 56 | + let selector = cache.get(key); |
| 57 | + if (selector !== void 0) return selector; |
| 58 | + |
| 59 | + // Parse with `esquery` and analyse |
| 60 | + const esquerySelector = esqueryParse(key); |
| 61 | + |
| 62 | + selector = { |
| 63 | + typeIds: null, |
| 64 | + esquerySelector, |
| 65 | + isComplex: false, |
| 66 | + attributeCount: 0, |
| 67 | + identifierCount: 0, |
| 68 | + }; |
| 69 | + selector.typeIds = analyzeSelector(esquerySelector, selector); |
| 70 | + |
| 71 | + // Store in cache for next time |
| 72 | + cache.set(key, selector); |
| 73 | + |
| 74 | + return selector; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Analyse an `EsquerySelector` to determine: |
| 79 | + * |
| 80 | + * 1. What node types it matches on. |
| 81 | + * 2. Whether it is "simple" or "complex" - "simple" matches a subset of node types without further conditions. |
| 82 | + * 3. It's specificity (number of identifiers and attributes). |
| 83 | + * |
| 84 | + * This function traverses the `EsquerySelector` and calls itself recursively. |
| 85 | + * It returns an array of node type IDs which the selector may match. |
| 86 | + * |
| 87 | + * @param esquerySelector - `EsquerySelector` to analyse. |
| 88 | + * @param selector - `Selector` which has its `isSimple`, `attributeCount`, and `identifierCount` updated. |
| 89 | + * @returns Array of node type IDs the selector matches, or `null` if it matches all nodes. |
| 90 | + */ |
| 91 | +function analyzeSelector(esquerySelector: EsquerySelector, selector: Selector): NodeTypeId[] | null { |
| 92 | + switch (esquerySelector.type) { |
| 93 | + case 'identifier': { |
| 94 | + selector.identifierCount++; |
| 95 | + |
| 96 | + const typeId = NODE_TYPE_IDS_MAP.get(esquerySelector.value); |
| 97 | + // If the type is invalid, just treat this selector as not matching any types. |
| 98 | + // But still increment `identifierCount`. |
| 99 | + // This matches ESLint's behavior. |
| 100 | + return typeId === void 0 ? EMPTY_TYPE_IDS_ARRAY : [typeId]; |
| 101 | + } |
| 102 | + |
| 103 | + case 'not': |
| 104 | + for (let i = 0, childSelectors = esquerySelector.selectors, len = childSelectors.length; i < len; i++) { |
| 105 | + analyzeSelector(childSelectors[i], selector); |
| 106 | + } |
| 107 | + selector.isComplex = true; |
| 108 | + return null; |
| 109 | + |
| 110 | + case 'matches': { |
| 111 | + // OR matcher. Matches a node if any of child selectors matches it. |
| 112 | + let nodeTypes: NodeTypeId[] | null = []; |
| 113 | + for (let i = 0, childSelectors = esquerySelector.selectors, len = childSelectors.length; i < len; i++) { |
| 114 | + const childNodeTypes = analyzeSelector(childSelectors[i], selector); |
| 115 | + if (childNodeTypes === null) { |
| 116 | + nodeTypes = null; |
| 117 | + } else if (nodeTypes !== null) { |
| 118 | + nodeTypes.push(...childNodeTypes); |
| 119 | + } |
| 120 | + } |
| 121 | + if (nodeTypes === null) return null; |
| 122 | + // De-duplicate |
| 123 | + // TODO: Faster way to do this? Sort and then dedupe manually? |
| 124 | + return [...new Set(nodeTypes)]; |
| 125 | + } |
| 126 | + |
| 127 | + case 'compound': { |
| 128 | + // AND matcher. Only matches a node if all child selectors match it. |
| 129 | + const childSelectors = esquerySelector.selectors, |
| 130 | + len = childSelectors.length; |
| 131 | + // TODO: Can `childSelectors` have 0 length? |
| 132 | + if (len === 0) return []; |
| 133 | + |
| 134 | + let nodeTypes: NodeTypeId[] | null = null; |
| 135 | + for (let i = 0; i < len; i++) { |
| 136 | + const childNodeTypes = analyzeSelector(childSelectors[i], selector); |
| 137 | + |
| 138 | + // If child selector matches all types, does not narrow the types the selector matches |
| 139 | + if (childNodeTypes === null) continue; |
| 140 | + |
| 141 | + if (nodeTypes === null) { |
| 142 | + // First child selector which matches specific types |
| 143 | + nodeTypes = childNodeTypes; |
| 144 | + } else { |
| 145 | + // Selector only matches intersection of all child selectors. |
| 146 | + // TODO: Could make this faster if `analyzeSelector` always returned an ordered array. |
| 147 | + nodeTypes = childNodeTypes.filter(nodeType => nodeTypes.includes(nodeType)); |
| 148 | + } |
| 149 | + } |
| 150 | + return nodeTypes; |
| 151 | + } |
| 152 | + |
| 153 | + case 'attribute': |
| 154 | + case 'field': |
| 155 | + case 'nth-child': |
| 156 | + case 'nth-last-child': |
| 157 | + selector.isComplex = true; |
| 158 | + selector.attributeCount++; |
| 159 | + return null; |
| 160 | + |
| 161 | + case 'child': |
| 162 | + case 'descendant': |
| 163 | + case 'sibling': |
| 164 | + case 'adjacent': |
| 165 | + selector.isComplex = true; |
| 166 | + analyzeSelector(esquerySelector.left, selector); |
| 167 | + return analyzeSelector(esquerySelector.right, selector); |
| 168 | + |
| 169 | + case 'class': |
| 170 | + // TODO: Should TS function types be included in `FUNCTION_NODE_TYPE_IDS`? |
| 171 | + // This TODO comment is from ESLint's implementation. Not sure what it means! |
| 172 | + // TODO: Abstract into JSLanguage somehow. |
| 173 | + if (esquerySelector.name === 'function') return FUNCTION_NODE_TYPE_IDS; |
| 174 | + selector.isComplex = true; |
| 175 | + return null; |
| 176 | + |
| 177 | + case 'wildcard': |
| 178 | + return null; |
| 179 | + |
| 180 | + default: |
| 181 | + selector.isComplex = true; |
| 182 | + return null; |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Wrap a visit function so it's only called if the provided `EsquerySelector` matches the AST node. |
| 188 | + * |
| 189 | + * IMPORTANT: Selector matching will only be correct if `ancestors` from `generated/walk.js` |
| 190 | + * contains the ancestors of the AST node passed to the returned visit function. |
| 191 | + * Therefore, the returned visit function can only be called during AST traversal. |
| 192 | + * |
| 193 | + * @params visitFn - Visit function to wrap |
| 194 | + * @params esquerySelector - `EsquerySelector` object |
| 195 | + * @returns Wrapped visit function |
| 196 | + */ |
| 197 | +export function wrapVisitFnWithSelectorMatch(visitFn: VisitFn, esquerySelector: EsquerySelector): VisitFn { |
| 198 | + return (node: Node) => { |
| 199 | + if (esqueryMatches(node as unknown as EsqueryNode, esquerySelector, ancestors, ESQUERY_OPTIONS)) { |
| 200 | + visitFn(node); |
| 201 | + } |
| 202 | + }; |
| 203 | +} |
0 commit comments