{"id":"62995d911d0cbd05","repo":"eslint/eslint","slug":"ast-is-missing-the-comments-array","errorCode":null,"errorMessage":"AST is missing the comments array.","messagePattern":"AST is missing the comments array\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"lib/languages/js/source-code/source-code.js","lineNumber":58,"sourceCode":"\n/**\n * Validates that the given AST has the required information.\n * @param {ASTNode} ast The Program node of the AST to check.\n * @throws {TypeError} If the AST doesn't contain the correct information.\n * @returns {void}\n * @private\n */\nfunction validate(ast) {\n\tif (!ast) {\n\t\tthrow new TypeError(`Unexpected empty AST. (${ast})`);\n\t}\n\n\tif (!ast.tokens) {\n\t\tthrow new TypeError(\"AST is missing the tokens array.\");\n\t}\n\n\tif (!ast.comments) {\n\t\tthrow new TypeError(\"AST is missing the comments array.\");\n\t}\n\n\tif (!ast.loc) {\n\t\tthrow new TypeError(\"AST is missing location information.\");\n\t}\n\n\tif (!ast.range) {\n\t\tthrow new TypeError(\"AST is missing range information\");\n\t}\n}\n\n/**\n * Retrieves globals for the given ecmaVersion.\n * @param {number} ecmaVersion The version to retrieve globals for.\n * @returns {Object} The globals for the given ecmaVersion.\n */\nfunction getGlobalsForEcmaVersion(ecmaVersion) {\n\tswitch (ecmaVersion) {","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/eslint/eslint/blob/f131c034ad91bbf06bcbb6b5e931447a8e419a46/lib/languages/js/source-code/source-code.js#L40-L76","documentation":"Thrown as a TypeError by validate(ast) (lib/languages/js/source-code/source-code.js:58) when the AST has no `comments` array. SourceCode attaches comments to nodes and exposes them via getAllComments; the parser must collect comments when `comment: true` (set by the JS language at index.js:253).","triggerScenarios":"A custom parser that omits `ast.comments`, or one where the comments option wasn't forwarded. Standard espree populates comments when `comment: true`.","commonSituations":"Wrapping @babel/parser or typescript-eslint parser without enabling comments, or stripping comments to reduce AST size before handing to SourceCode.","solutions":["Enable comments in the parser: forward `comment: true` (the JS language sets it; ensure your wrapper doesn't override).","Assign `ast.comments = collectedComments` if your parser exposes them under a different key.","Default to an empty array: `ast.comments ??= []` if your use case doesn't need comments."],"exampleFix":"// before\nconst ast = babelParse(text, { ranges: true, tokens: true });\nreturn { ast };\n\n// after\nconst ast = babelParse(text, { ranges: true, tokens: true, comment: true });\nreturn { ast };","handlingStrategy":"validation","validationCode":"if (!Array.isArray(ast.comments)) {\n  ast.comments = []; // or throw if comments are required\n}","typeGuard":"const hasComments = ast => Array.isArray(ast.comments);","tryCatchPattern":null,"preventionTips":["Forward comment:true to the underlying parser.","Default ast.comments to [] if your parser truly has none.","Do not strip comments during AST post-processing."],"tags":["parser","ast","comments","custom-parser"],"analyzedSha":"f131c034ad91bbf06bcbb6b5e931447a8e419a46","analyzedAt":"2026-08-03T19:53:24.025Z","schemaVersion":2}