Skip to content

feat: support non-js languages #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

/**
* @typedef {{ line: number; column: number; offset: number }} Location
*
* @typedef {PrettierOptions & {
* onDiskFilepath: string;
* parserMeta?: ESLint.ObjectMetaProperties['meta'];
Expand All @@ -25,6 +27,11 @@
* options: Options,
* fileInfoOptions: FileInfoOptions,
* ) => string} PrettierFormat
*
*
* @typedef {Parameters<
* Exclude<ESLint.Plugin['rules'], undefined>[string]['create']
* >[0]} RuleContext
*/

'use strict';
Expand Down Expand Up @@ -57,10 +64,29 @@ let prettierFormat;
// Rule Definition
// ------------------------------------------------------------------------------

/**
* Converts a byte offset to a Location.
*
* See also `getLocFromIndex` in `@eslint/js`.
*
* @param {number[]} lineIndexes
* @param {number} offset
* @returns {Location}
*/
function getLocFromOffset(lineIndexes, offset) {
let line = 0;
while (line + 1 < lineIndexes.length && lineIndexes[line + 1] < offset) {
line += 1;
}

const column = offset - lineIndexes[line];
return { line: line + 1, column, offset };
}

/**
* Reports a difference.
*
* @param {Rule.RuleContext} context - The ESLint rule context.
* @param {RuleContext} context - The ESLint rule context.
* @param {Difference} difference - The difference object.
* @returns {void}
*/
Expand All @@ -71,8 +97,39 @@ function reportDifference(context, difference) {
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
// with the `sourceCode` property.
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
const [start, end] = range.map(index =>
(context.sourceCode ?? context.getSourceCode()).getLocFromIndex(index),
const sourceCode = context.sourceCode ?? context.getSourceCode();

const lazy = {
/**
* Lazily computes the line indices for `sourceCode`.
*
* @returns {number[]}
*/
get lineIndexes() {
// @ts-ignore
delete this.lineIndexes;

if (!('text' in sourceCode)) {
throw new Error(
'prettier/prettier: non-textual source code is unsupported',
);
}

// @ts-ignore
this.lineIndexes = [...sourceCode.text.matchAll(/\n/g)].map(
match => match.index,
);
// first line in the file starts at byte offset 0
this.lineIndexes.unshift(0);
return this.lineIndexes;
},
};

const [start, end] = range.map(
index =>
// @ts-ignore
sourceCode.getLocFromIndex?.(index) ??
getLocFromOffset(lazy.lineIndexes, index),
);

context.report({
Expand All @@ -90,7 +147,7 @@ function reportDifference(context, difference) {
// Module Definition
// ------------------------------------------------------------------------------

/** @type {ESLint.Plugin} */
/** @satisfies {ESLint.Plugin} */
const eslintPluginPrettier = {
meta: { name, version },
configs: {
Expand Down Expand Up @@ -168,7 +225,7 @@ const eslintPluginPrettier = {
const source = sourceCode.text;

return {
Program(node) {
[sourceCode.ast.type](node) {
if (!prettierFormat) {
// Prettier is expensive to load, so only load it if needed.
prettierFormat = /** @type {PrettierFormat} */ (
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@commitlint/config-conventional": "^19.8.0",
"@eslint-community/eslint-plugin-eslint-comments": "^4.4.1",
"@eslint/js": "^9.23.0",
"@eslint/json": "^0.12.0",
"@graphql-eslint/eslint-plugin": "^4.3.0",
"@html-eslint/parser": "^0.41.0",
"@prettier/plugin-pug": "^3.2.1",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/invalid/json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CODE:
{
"a": [
"b",
{"c":
"d"}
] }

OUTPUT:
{
"a": ["b", { "c": "d" }]
}

OPTIONS:
[]

ERRORS:
[
{
message: 'Replace `"a":·[⏎"b",⏎{"c":⏎"d"}⏎]·` with `··"a":·["b",·{·"c":·"d"·}]⏎`',
},
]
26 changes: 26 additions & 0 deletions test/prettier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import eslintPluginPug from 'eslint-plugin-pug';
import vueEslintParser from 'vue-eslint-parser';
import * as eslintPluginGraphql from '@graphql-eslint/eslint-plugin';
import eslintMdx from 'eslint-mdx';
import eslintPluginJson from '@eslint/json';

const rule = eslintPluginPrettier.rules.prettier;
const RuleTester =
Expand Down Expand Up @@ -380,6 +381,31 @@ runFixture('invalid-prettierrc/*', [
],
]);

const jsonRuleTester = new RuleTester({
plugins: {
json: eslintPluginJson,
},
language: 'json/json',
});

jsonRuleTester.run('@eslint/json', rule, {
valid: [
{
code: '{}\n',
filename: 'empty.json',
},
{
code: '{ "foo": 1 }\n',
filename: 'simple.json',
},
],
invalid: [
Object.assign(loadInvalidFixture('json'), {
filename: 'invalid.json',
}),
],
});

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
Expand Down