{"id":"ce1db1c0c18ac80f","repo":"eslint/eslint","slug":"syntax-error-in-selector-selector-at-position","errorCode":null,"errorMessage":"Syntax error in selector \"${selector}\" at position ${err.location.start.offset}: ${err.message}","messagePattern":"Syntax error in selector \"(.+?)\" at position (.+?): (.+?)","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"lib/linter/esquery.js","lineNumber":272,"sourceCode":"\treturn null;\n}\n\n/**\n * Parses a raw selector string, and throws a useful error if parsing fails.\n * @param {string} selector The selector string to parse.\n * @returns {Object} An object (from esquery) describing the matching behavior of this selector\n * @throws {Error} An error if the selector is invalid\n */\nfunction tryParseSelector(selector) {\n\ttry {\n\t\treturn esquery.parse(selector);\n\t} catch (err) {\n\t\tif (\n\t\t\terr.location &&\n\t\t\terr.location.start &&\n\t\t\ttypeof err.location.start.offset === \"number\"\n\t\t) {\n\t\t\tthrow new SyntaxError(\n\t\t\t\t`Syntax error in selector \"${selector}\" at position ${err.location.start.offset}: ${err.message}`,\n\t\t\t\t{\n\t\t\t\t\tcause: err,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t\tthrow err;\n\t}\n}\n\n/**\n * Parses a raw selector string, and returns the parsed selector along with specificity and type information.\n * @param {string} source A raw AST selector\n * @returns {ESQueryParsedSelector} A selector descriptor\n */\nfunction parse(source) {\n\tif (selectorCache.has(source)) {\n\t\treturn selectorCache.get(source);","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/eslint/eslint/blob/f131c034ad91bbf06bcbb6b5e931447a8e419a46/lib/linter/esquery.js#L254-L290","documentation":"Thrown by tryParseSelector when esquery fails to parse an AST selector string and the resulting error carries a location object (err.location.start.offset). The wrapper re-throws as a SyntaxError with the original selector, the 0-based offset of the failure, and esquery's message, chaining the original error as cause. Selectors that fail without a location are re-thrown unchanged.","triggerScenarios":"Writing an invalid esquery selector in a rule's visitor: 'FunctionDeclaration >', ':not(', 'Identifier[', or any malformed CSS-like selector. The error surfaces when ESLint parses rule selectors at load/config time.","commonSituations":"Authoring a custom ESLint rule with a typo'd selector; using esquery syntax not supported by the bundled version; copy-paste from CSS docs introducing unsupported pseudo-classes; a dynamic selector built from user input that produced malformed output.","solutions":["Read the reported offset and fix the selector at that character position.","Validate selectors in isolation with require('esquery').parse(sel) during rule authoring.","Cross-check the selector against the esquery syntax reference supported by your ESLint version.","If building selectors dynamically, sanitize and unit-test the generated string."],"exampleFix":"// before (malformed selector)\ncontext.report({ node, message: 'x' });\n// rule defined with selector: 'FunctionDeclaration >'\n\n// after\n// selector: 'FunctionDeclaration > BlockStatement'","handlingStrategy":"try-catch","validationCode":"function isValidSelector(selector) {\n  try { require('esquery').parse(selector.replace(/:exit$/u, '')); return true; } catch { return false; }\n}","typeGuard":"function isParsableSelector(selector) { return typeof selector === 'string' && isValidSelector(selector); }","tryCatchPattern":"try { const parsed = require('esquery').parse(selector); } catch (err) {\n  if (err.location && typeof err.location.start?.offset === 'number') {\n    throw new SyntaxError(`Bad selector at offset ${err.location.start.offset}: ${err.message}`, { cause: err });\n  }\n  throw err;\n}","preventionTips":["Unit-test custom rule selectors with esquery.parse during authoring.","Cross-check selector syntax against the esquery version bundled with your ESLint.","When building selectors dynamically, sanitize and validate the generated string before use."],"tags":["esquery","selector","syntax-error","rule-authoring","eslint"],"analyzedSha":"f131c034ad91bbf06bcbb6b5e931447a8e419a46","analyzedAt":"2026-08-03T19:53:24.025Z","schemaVersion":2}