Skip to content

feat: support non-js languages#743

Merged
JounQin merged 5 commits intoprettier:mainfrom
dotcarmen:non-js-lang-support
Jun 17, 2025
Merged

feat: support non-js languages#743
JounQin merged 5 commits intoprettier:mainfrom
dotcarmen:non-js-lang-support

Conversation

@dotcarmen
Copy link
Copy Markdown
Contributor

@dotcarmen dotcarmen commented Jun 14, 2025

close #312

This PR aims to support non-JS languages being linted with the prettier/prettier rule

Currently, this plugin only runs on Program nodes, which is typically the root node for languages that are processed into JS in ESLint. However, other eslint plugins may use a different root node type, such as @eslint/json's Document node type

This PR expands support for other languages by making 2 changes:

  1. instead of only matching Program nodes when instantiating the rule, the rule now matches the type of the root node that was parsed for the entire file.
  2. instead of relying on @eslint/js's getLocFromIndex, use a new local function getLocFromOffset that transforms a byte offset into an API-compatible location.

The latter change was necessary because eg @eslint/json doesn't include a getLocFromIndex method on its SourceCode class.


Important

This PR expands the prettier/prettier ESLint rule to support non-JS languages by matching the root node type and introduces getLocFromOffset to handle byte offsets.

  • Behavior:
    • Expands prettier/prettier rule to support non-JS languages by matching the root node type of the parsed file instead of only Program nodes.
    • Replaces getLocFromIndex with getLocFromOffset to convert byte offsets to locations, supporting languages without getLocFromIndex.
  • Functions:
    • Adds getLocFromOffset() to calculate line and column from byte offset in eslint-plugin-prettier.js.
    • Modifies reportDifference() to use getLocFromOffset() instead of getLocFromIndex.
  • Misc:
    • Updates rule instantiation to use [sourceCode.ast.type] instead of Program in eslint-plugin-prettier.js.

This description was created by Ellipsis for ee09d11. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • New Features
    • Added support for linting and formatting JSON files with the Prettier ESLint rule.
    • Enhanced compatibility with CSS files via the @eslint/css package.
  • Bug Fixes
    • Improved compatibility with different ESLint API versions for location reporting.
  • Tests
    • Introduced new test cases for JSON formatting, including valid, invalid, and empty JSON files.
  • Chores
    • Added development dependencies on the @eslint/json and @eslint/css packages.

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

Possible to process/lint CSS files?

2 participants