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 diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 74cd8c04..37fd0056 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')) ); } @@ -213,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 ); @@ -226,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/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..1c428bcb 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -389,6 +389,21 @@ runFixture('eslint-plugin-svelte3/*.svelte', [[], []], svelteUnsupported); */ runFixture('*.pug', [[]]); +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 // ------------------------------------------------------------------------------