From e66748fbb58f94a4df2fdeeb64d0d1a996e234e7 Mon Sep 17 00:00:00 2001 From: ntnyq Date: Mon, 13 Jan 2025 09:11:54 +0800 Subject: [PATCH 1/4] fix: report node when loc not found --- eslint-plugin-prettier.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 74cd8c04..f50d1935 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -13,6 +13,7 @@ * @typedef {import('prettier').FileInfoOptions} FileInfoOptions * @typedef {import('prettier').Options} PrettierOptions * @typedef {PrettierOptions & { onDiskFilepath: string, parserMeta?: ObjectMetaProperties['meta'], parserPath?: string, usePrettierrc?: boolean }} Options + * @typedef {(source: string, options: Options, fileInfoOptions: FileInfoOptions) => string} PrettierFormat */ 'use strict'; @@ -39,7 +40,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences; // Lazily-loaded Prettier. /** - * @type {(source: string, options: Options, fileInfoOptions: FileInfoOptions) => string} + * @type {PrettierFormat} */ let prettierFormat; @@ -160,11 +161,11 @@ const eslintPluginPrettier = { const source = sourceCode.text; return { - Program() { + Program(node) { if (!prettierFormat) { // Prettier is expensive to load, so only load it if needed. - prettierFormat = require('synckit').createSyncFn( - require.resolve('./worker'), + prettierFormat = /** @type {PrettierFormat} */ ( + require('synckit').createSyncFn(require.resolve('./worker')) ); } @@ -226,9 +227,13 @@ const eslintPluginPrettier = { } if (error.loc) { message = message.replace(/ \(\d+:\d+\)$/, ''); + + context.report({ message, loc: error.loc }); + + return; } - context.report({ message, loc: error.loc }); + context.report({ message, node }); return; } From 0fafed226f714f4fa009869e42c7d1e4b3c96bc3 Mon Sep 17 00:00:00 2001 From: ntnyq Date: Mon, 13 Jan 2025 12:25:50 +0800 Subject: [PATCH 2/4] chore: add changeset release --- .changeset/giant-bottles-sin.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/giant-bottles-sin.md diff --git a/.changeset/giant-bottles-sin.md b/.changeset/giant-bottles-sin.md new file mode 100644 index 00000000..942e4a9e --- /dev/null +++ b/.changeset/giant-bottles-sin.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-prettier": patch +--- + +fix: report node when loc not found From d6b90c15120d264305c90d00013c7bc15204b4be Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 15 Jan 2025 12:52:23 +0800 Subject: [PATCH 3/4] chore: add failing test case --- eslint-plugin-prettier.js | 8 ++------ test/fixtures/invalid-prettierrc/.prettierrc.cjs | 3 +++ test/fixtures/invalid-prettierrc/test.js | 1 + test/prettier.js | 2 ++ 4 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/invalid-prettierrc/.prettierrc.cjs create mode 100644 test/fixtures/invalid-prettierrc/test.js diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index f50d1935..7ddbbb7d 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -161,7 +161,7 @@ const eslintPluginPrettier = { const source = sourceCode.text; return { - Program(node) { + Program() { if (!prettierFormat) { // Prettier is expensive to load, so only load it if needed. prettierFormat = /** @type {PrettierFormat} */ ( @@ -227,13 +227,9 @@ const eslintPluginPrettier = { } if (error.loc) { message = message.replace(/ \(\d+:\d+\)$/, ''); - - context.report({ message, loc: error.loc }); - - return; } - context.report({ message, node }); + context.report({ message, loc: error.loc }); return; } diff --git a/test/fixtures/invalid-prettierrc/.prettierrc.cjs b/test/fixtures/invalid-prettierrc/.prettierrc.cjs new file mode 100644 index 00000000..71a847ac --- /dev/null +++ b/test/fixtures/invalid-prettierrc/.prettierrc.cjs @@ -0,0 +1,3 @@ +import 'node:path' + +module.exports = {} diff --git a/test/fixtures/invalid-prettierrc/test.js b/test/fixtures/invalid-prettierrc/test.js new file mode 100644 index 00000000..4ba52ba2 --- /dev/null +++ b/test/fixtures/invalid-prettierrc/test.js @@ -0,0 +1 @@ +module.exports = {} diff --git a/test/prettier.js b/test/prettier.js index 8e12ff3a..2a557710 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -389,6 +389,8 @@ runFixture('eslint-plugin-svelte3/*.svelte', [[], []], svelteUnsupported); */ runFixture('*.pug', [[]]); +runFixture('invalid-prettierrc/*', []); + // ------------------------------------------------------------------------------ // Helpers // ------------------------------------------------------------------------------ From 62005b2620ce49440bb3277d45ecf97f9328516f Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 15 Jan 2025 12:58:46 +0800 Subject: [PATCH 4/4] fix: report on program node if loc unavailable --- eslint-plugin-prettier.js | 9 +++++---- test/prettier.js | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 7ddbbb7d..37fd0056 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -161,7 +161,7 @@ const eslintPluginPrettier = { const source = sourceCode.text; return { - Program() { + Program(node) { if (!prettierFormat) { // Prettier is expensive to load, so only load it if needed. prettierFormat = /** @type {PrettierFormat} */ ( @@ -214,7 +214,7 @@ const eslintPluginPrettier = { let message = 'Parsing error: ' + err.message; const error = - /** @type {SyntaxError & {codeFrame: string; loc: SourceLocation}} */ ( + /** @type {SyntaxError & {codeFrame: string; loc?: SourceLocation}} */ ( err ); @@ -227,10 +227,11 @@ const eslintPluginPrettier = { } if (error.loc) { message = message.replace(/ \(\d+:\d+\)$/, ''); + context.report({ message, loc: error.loc }); + } else { + context.report({ message, node }); } - context.report({ message, loc: error.loc }); - return; } diff --git a/test/prettier.js b/test/prettier.js index 2a557710..1c428bcb 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -389,7 +389,20 @@ runFixture('eslint-plugin-svelte3/*.svelte', [[], []], svelteUnsupported); */ runFixture('*.pug', [[]]); -runFixture('invalid-prettierrc/*', []); +runFixture('invalid-prettierrc/*', [ + [ + { + column: 1, + endColumn: 20, + endLine: 1, + line: 1, + message: 'Parsing error: Cannot use import statement outside a module', + nodeType: 'Program', + ruleId: 'prettier/prettier', + severity: 2, + }, + ], +]); // ------------------------------------------------------------------------------ // Helpers