Skip to content

fix(eslint-plugin-pnpm): support @eslint/json's json/json language for json rules - #39

Merged
antfu merged 1 commit into
made-in-common:mainfrom
antfubot:fix/json-eslint-language-compat
Aug 13, 2026
Merged

antfu merged 1 commit into
made-in-common:mainfrom
antfubot:fix/json-eslint-language-compat

Conversation

@antfubot

Copy link
Copy Markdown
Contributor

Problem

Linting package.json with @eslint/json's json/json language alongside eslint-plugin-pnpm's json-* rules (as recommended when you also want plain JSON linting) crashes:

TypeError: Cannot read properties of undefined (reading 'expression')
    at getPackageJsonRootNode ...

This happens because the json-* rules assumed context.sourceCode.ast always has the jsonc-eslint-parser shape (Program -> body[0]: JSONExpressionStatement -> expression). When the file is linted through @eslint/json's json/json language instead, the AST comes from the Momoa parser and has a completely different shape (Document -> body: Object -> members), so blindly indexing into it throws.

Reproduced locally with the exact config from the issue (@eslint/json + eslint-plugin-pnpm both targeting package.json), confirming the reported stack trace.

Fix

Wrap rule create functions with eslint-json-compat-utils's toCompatCreate. It detects when context.sourceCode.ast is a Momoa/@eslint/json AST and transparently exposes a jsonc-eslint-parser-compatible sourceCode/AST view (and converts fixes/reports back), so the existing rule implementations work unmodified. For files parsed the normal way (jsonc-eslint-parser, or non-JSON files), it's a no-op passthrough — confirmed no regressions in the existing test suite.

Added a regression test (json-language-compat.test.ts) that runs the json-* rules through a real Linter configured with @eslint/json's json/json language, verifying no crash, correct reporting/autofix, and that @eslint/json's own rules (e.g. json/no-duplicate-keys) keep working side by side.

Fixes #38


This PR was created with the help of an agent.

…e for json rules

The json-* rules assumed the sourceCode AST always came from
jsonc-eslint-parser, indexing into it directly. When package.json is
linted with @eslint/json's json/json language (as recommended when
combining JSON linting with other json rules), the AST is produced by
the Momoa parser instead, which has a different shape. This caused
a crash: "Cannot read properties of undefined (reading 'expression')".

Wrap rule create functions with eslint-json-compat-utils's
toCompatCreate, which transparently adapts the jsonc-eslint-parser-
shaped rules to also understand the json/json language's AST, so both
setups work without crashing.

Fixes made-in-common#38

This PR was created with the help of an agent.
@antfu
antfu merged commit a8dad79 into made-in-common:main Aug 13, 2026
7 checks passed
@antfu antfu mentioned this pull request Aug 13, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compatibility with regular linting of json and yaml

2 participants