eslint/eslint · critical

AST is missing the comments array.

Error message

AST is missing the comments array.

What it means

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).

Source

Thrown at lib/languages/js/source-code/source-code.js:58

/**
 * Validates that the given AST has the required information.
 * @param {ASTNode} ast The Program node of the AST to check.
 * @throws {TypeError} If the AST doesn't contain the correct information.
 * @returns {void}
 * @private
 */
function validate(ast) {
	if (!ast) {
		throw new TypeError(`Unexpected empty AST. (${ast})`);
	}

	if (!ast.tokens) {
		throw new TypeError("AST is missing the tokens array.");
	}

	if (!ast.comments) {
		throw new TypeError("AST is missing the comments array.");
	}

	if (!ast.loc) {
		throw new TypeError("AST is missing location information.");
	}

	if (!ast.range) {
		throw new TypeError("AST is missing range information");
	}
}

/**
 * Retrieves globals for the given ecmaVersion.
 * @param {number} ecmaVersion The version to retrieve globals for.
 * @returns {Object} The globals for the given ecmaVersion.
 */
function getGlobalsForEcmaVersion(ecmaVersion) {
	switch (ecmaVersion) {

View on GitHub (pinned to f131c034ad)

Solutions

  1. Enable comments in the parser: forward `comment: true` (the JS language sets it; ensure your wrapper doesn't override).
  2. Assign `ast.comments = collectedComments` if your parser exposes them under a different key.
  3. Default to an empty array: `ast.comments ??= []` if your use case doesn't need comments.

Example fix

// before
const ast = babelParse(text, { ranges: true, tokens: true });
return { ast };

// after
const ast = babelParse(text, { ranges: true, tokens: true, comment: true });
return { ast };
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(ast.comments)) {
  ast.comments = []; // or throw if comments are required
}

Type guard

const hasComments = ast => Array.isArray(ast.comments);

Prevention

When it happens

Trigger: A custom parser that omits `ast.comments`, or one where the comments option wasn't forwarded. Standard espree populates comments when `comment: true`.

Common situations: Wrapping @babel/parser or typescript-eslint parser without enabling comments, or stripping comments to reduce AST size before handing to SourceCode.

Related errors


AI-assisted analysis of eslint/eslint@f131c034ad (2026-08-03). Data as JSON: /data/errors/62995d911d0cbd05.json. Report an issue: GitHub.