2 * @fileoverview The event generator for AST nodes.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const esquery = require("esquery");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * An object describing an AST selector
20 * @typedef {Object} ASTSelector
21 * @property {string} rawSelector The string that was parsed into this selector
22 * @property {boolean} isExit `true` if this should be emitted when exiting the node rather than when entering
23 * @property {Object} parsedSelector An object (from esquery) describing the matching behavior of the selector
24 * @property {string[]|null} listenerTypes A list of node types that could possibly cause the selector to match,
25 * or `null` if all node types could cause a match
26 * @property {number} attributeCount The total number of classes, pseudo-classes, and attribute queries in this selector
27 * @property {number} identifierCount The total number of identifier queries in this selector
30 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
35 * Computes the union of one or more arrays
36 * @param {...any[]} arrays One or more arrays to union
37 * @returns {any[]} The union of the input arrays
39 function union(...arrays) {
41 // TODO(stephenwade): Replace this with arrays.flat() when we drop support for Node v10
42 return [...new Set([].concat(...arrays))];
46 * Computes the intersection of one or more arrays
47 * @param {...any[]} arrays One or more arrays to intersect
48 * @returns {any[]} The intersection of the input arrays
50 function intersection(...arrays) {
51 if (arrays.length === 0) {
55 let result = [...new Set(arrays[0])];
57 for (const array of arrays.slice(1)) {
58 result = result.filter(x => array.includes(x));
64 * Gets the possible types of a selector
65 * @param {Object} parsedSelector An object (from esquery) describing the matching behavior of the selector
66 * @returns {string[]|null} The node types that could possibly trigger this selector, or `null` if all node types could trigger it
68 function getPossibleTypes(parsedSelector) {
69 switch (parsedSelector.type) {
71 return [parsedSelector.value];
74 const typesForComponents = parsedSelector.selectors.map(getPossibleTypes);
76 if (typesForComponents.every(Boolean)) {
77 return union(...typesForComponents);
83 const typesForComponents = parsedSelector.selectors.map(getPossibleTypes).filter(typesForComponent => typesForComponent);
85 // If all of the components could match any type, then the compound could also match any type.
86 if (!typesForComponents.length) {
91 * If at least one of the components could only match a particular type, the compound could only match
92 * the intersection of those types.
94 return intersection(...typesForComponents);
101 return getPossibleTypes(parsedSelector.right);
110 * Counts the number of class, pseudo-class, and attribute queries in this selector
111 * @param {Object} parsedSelector An object (from esquery) describing the selector's matching behavior
112 * @returns {number} The number of class, pseudo-class, and attribute queries in this selector
114 function countClassAttributes(parsedSelector) {
115 switch (parsedSelector.type) {
120 return countClassAttributes(parsedSelector.left) + countClassAttributes(parsedSelector.right);
125 return parsedSelector.selectors.reduce((sum, childSelector) => sum + countClassAttributes(childSelector), 0);
130 case "nth-last-child":
139 * Counts the number of identifier queries in this selector
140 * @param {Object} parsedSelector An object (from esquery) describing the selector's matching behavior
141 * @returns {number} The number of identifier queries
143 function countIdentifiers(parsedSelector) {
144 switch (parsedSelector.type) {
149 return countIdentifiers(parsedSelector.left) + countIdentifiers(parsedSelector.right);
154 return parsedSelector.selectors.reduce((sum, childSelector) => sum + countIdentifiers(childSelector), 0);
165 * Compares the specificity of two selector objects, with CSS-like rules.
166 * @param {ASTSelector} selectorA An AST selector descriptor
167 * @param {ASTSelector} selectorB Another AST selector descriptor
169 * a value less than 0 if selectorA is less specific than selectorB
170 * a value greater than 0 if selectorA is more specific than selectorB
171 * a value less than 0 if selectorA and selectorB have the same specificity, and selectorA <= selectorB alphabetically
172 * a value greater than 0 if selectorA and selectorB have the same specificity, and selectorA > selectorB alphabetically
174 function compareSpecificity(selectorA, selectorB) {
175 return selectorA.attributeCount - selectorB.attributeCount ||
176 selectorA.identifierCount - selectorB.identifierCount ||
177 (selectorA.rawSelector <= selectorB.rawSelector ? -1 : 1);
181 * Parses a raw selector string, and throws a useful error if parsing fails.
182 * @param {string} rawSelector A raw AST selector
183 * @returns {Object} An object (from esquery) describing the matching behavior of this selector
184 * @throws {Error} An error if the selector is invalid
186 function tryParseSelector(rawSelector) {
188 return esquery.parse(rawSelector.replace(/:exit$/u, ""));
190 if (err.location && err.location.start && typeof err.location.start.offset === "number") {
191 throw new SyntaxError(`Syntax error in selector "${rawSelector}" at position ${err.location.start.offset}: ${err.message}`);
197 const selectorCache = new Map();
200 * Parses a raw selector string, and returns the parsed selector along with specificity and type information.
201 * @param {string} rawSelector A raw AST selector
202 * @returns {ASTSelector} A selector descriptor
204 function parseSelector(rawSelector) {
205 if (selectorCache.has(rawSelector)) {
206 return selectorCache.get(rawSelector);
209 const parsedSelector = tryParseSelector(rawSelector);
213 isExit: rawSelector.endsWith(":exit"),
215 listenerTypes: getPossibleTypes(parsedSelector),
216 attributeCount: countClassAttributes(parsedSelector),
217 identifierCount: countIdentifiers(parsedSelector)
220 selectorCache.set(rawSelector, result);
224 //------------------------------------------------------------------------------
226 //------------------------------------------------------------------------------
229 * The event generator for AST nodes.
230 * This implements below interface.
233 * interface EventGenerator {
234 * emitter: SafeEmitter;
235 * enterNode(node: ASTNode): void;
236 * leaveNode(node: ASTNode): void;
240 class NodeEventGenerator {
242 // eslint-disable-next-line jsdoc/require-description
244 * @param {SafeEmitter} emitter
245 * An SafeEmitter which is the destination of events. This emitter must already
246 * have registered listeners for all of the events that it needs to listen for.
247 * (See lib/linter/safe-emitter.js for more details on `SafeEmitter`.)
248 * @param {ESQueryOptions} esqueryOptions `esquery` options for traversing custom nodes.
249 * @returns {NodeEventGenerator} new instance
251 constructor(emitter, esqueryOptions) {
252 this.emitter = emitter;
253 this.esqueryOptions = esqueryOptions;
254 this.currentAncestry = [];
255 this.enterSelectorsByNodeType = new Map();
256 this.exitSelectorsByNodeType = new Map();
257 this.anyTypeEnterSelectors = [];
258 this.anyTypeExitSelectors = [];
260 emitter.eventNames().forEach(rawSelector => {
261 const selector = parseSelector(rawSelector);
263 if (selector.listenerTypes) {
264 const typeMap = selector.isExit ? this.exitSelectorsByNodeType : this.enterSelectorsByNodeType;
266 selector.listenerTypes.forEach(nodeType => {
267 if (!typeMap.has(nodeType)) {
268 typeMap.set(nodeType, []);
270 typeMap.get(nodeType).push(selector);
274 const selectors = selector.isExit ? this.anyTypeExitSelectors : this.anyTypeEnterSelectors;
276 selectors.push(selector);
279 this.anyTypeEnterSelectors.sort(compareSpecificity);
280 this.anyTypeExitSelectors.sort(compareSpecificity);
281 this.enterSelectorsByNodeType.forEach(selectorList => selectorList.sort(compareSpecificity));
282 this.exitSelectorsByNodeType.forEach(selectorList => selectorList.sort(compareSpecificity));
286 * Checks a selector against a node, and emits it if it matches
287 * @param {ASTNode} node The node to check
288 * @param {ASTSelector} selector An AST selector descriptor
291 applySelector(node, selector) {
292 if (esquery.matches(node, selector.parsedSelector, this.currentAncestry, this.esqueryOptions)) {
293 this.emitter.emit(selector.rawSelector, node);
298 * Applies all appropriate selectors to a node, in specificity order
299 * @param {ASTNode} node The node to check
300 * @param {boolean} isExit `false` if the node is currently being entered, `true` if it's currently being exited
303 applySelectors(node, isExit) {
304 const selectorsByNodeType = (isExit ? this.exitSelectorsByNodeType : this.enterSelectorsByNodeType).get(node.type) || [];
305 const anyTypeSelectors = isExit ? this.anyTypeExitSelectors : this.anyTypeEnterSelectors;
308 * selectorsByNodeType and anyTypeSelectors were already sorted by specificity in the constructor.
309 * Iterate through each of them, applying selectors in the right order.
311 let selectorsByTypeIndex = 0;
312 let anyTypeSelectorsIndex = 0;
314 while (selectorsByTypeIndex < selectorsByNodeType.length || anyTypeSelectorsIndex < anyTypeSelectors.length) {
316 selectorsByTypeIndex >= selectorsByNodeType.length ||
317 anyTypeSelectorsIndex < anyTypeSelectors.length &&
318 compareSpecificity(anyTypeSelectors[anyTypeSelectorsIndex], selectorsByNodeType[selectorsByTypeIndex]) < 0
320 this.applySelector(node, anyTypeSelectors[anyTypeSelectorsIndex++]);
322 this.applySelector(node, selectorsByNodeType[selectorsByTypeIndex++]);
328 * Emits an event of entering AST node.
329 * @param {ASTNode} node A node which was entered.
334 this.currentAncestry.unshift(node.parent);
336 this.applySelectors(node, false);
340 * Emits an event of leaving AST node.
341 * @param {ASTNode} node A node which was left.
345 this.applySelectors(node, true);
346 this.currentAncestry.shift();
350 module.exports = NodeEventGenerator;