Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/weak-lamps-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-prettier": minor
---

feat: support non-js languages like `css` for `@eslint/css` and `json` for `@eslint/json`
46 changes: 42 additions & 4 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @import {AST, ESLint, Linter, Rule, SourceCode} from 'eslint'
* @import {Position} from 'estree'
* @import {FileInfoOptions, Options as PrettierOptions} from 'prettier'
* @import {Difference} from 'prettier-linter-helpers'
*/
Expand Down Expand Up @@ -57,6 +58,42 @@ let prettierFormat;
// Rule Definition
// ------------------------------------------------------------------------------

/** @type {WeakMap<SourceCode, number[]>} */
const lineIndexesCache = new WeakMap();

/**
* Ponyfill `sourceCode.getLocFromIndex` when it's unavailable.
*
* See also `getLocFromIndex` in `@eslint/js`.
*
* @param {SourceCode} sourceCode
* @param {number} index
* @returns {Position}
*/
function getLocFromIndex(sourceCode, index) {
if (typeof sourceCode.getLocFromIndex === 'function') {
return sourceCode.getLocFromIndex(index);
}

let lineIndexes = lineIndexesCache.get(sourceCode);
if (!lineIndexes) {
lineIndexes = [...sourceCode.text.matchAll(/\r?\n/g)].map(
match => match.index,
);
// first line in the file starts at byte offset 0
lineIndexes.unshift(0);
lineIndexesCache.set(sourceCode, lineIndexes);
}

let line = 0;
while (line + 1 < lineIndexes.length && lineIndexes[line + 1] < index) {
line += 1;
}
const column = index - lineIndexes[line];

return { line: line + 1, column };
}

/**
* Reports a difference.
*
Expand All @@ -71,9 +108,9 @@ 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 [start, end] = range.map(index => getLocFromIndex(sourceCode, index));

context.report({
messageId: operation,
Expand Down Expand Up @@ -168,7 +205,8 @@ const eslintPluginPrettier = {
const source = sourceCode.text;

return {
Program(node) {
/** @param {unknown} 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.

1 change: 1 addition & 0 deletions test/fixtures/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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"·}]⏎`',
},
]
59 changes: 57 additions & 2 deletions test/prettier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ 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 =
eslintUnsupportedApi.FlatRuleTester ?? eslintPackage.RuleTester;
const ESLint = eslintUnsupportedApi.FlatESLint ?? eslintPackage.ESLint;

const isESLint9 = !eslintUnsupportedApi.FlatRuleTester;

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -380,6 +383,53 @@ runFixture('invalid-prettierrc/*', [
],
]);

runFixture('*.json', [
[
{
column: 1,
endColumn: 1,
endLine: 2,
fix: {
range: [0, 1],
text: '',
},
line: 1,
message: 'Delete `⏎`',
messageId: 'delete',
nodeType: null,
ruleId: 'prettier/prettier',
severity: 2,
},
],
]);

if (isESLint9) {
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 Expand Up @@ -436,7 +486,7 @@ function getPrettierRcJsFilename(dir, file = 'dummy.js') {
* @type {ESLint}
* @import {ESLint} from 'eslint'
*/
let eslint;
var eslint; // bad mocha: `ReferenceError: Cannot access 'eslint' before initialization`

/**
* @param {string} pattern
Expand Down Expand Up @@ -504,11 +554,16 @@ async function runFixture(pattern, asserts, skip) {
pug: eslintPluginPug,
},
},
{
files: ['**/*.json'],
plugins: {
json: eslintPluginJson,
},
},
],
ignore: false,
});
}

if (skip) {
return;
}
Expand Down