Skip to content

Commit

Permalink
fix: report node when loc not found (#700)
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
ntnyq and JounQin authored Jan 15, 2025
1 parent 37d87ea commit aa5b59f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-bottles-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-prettier": patch
---

fix: report node when loc not found
16 changes: 9 additions & 7 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -39,7 +40,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;

// Lazily-loaded Prettier.
/**
* @type {(source: string, options: Options, fileInfoOptions: FileInfoOptions) => string}
* @type {PrettierFormat}
*/
let prettierFormat;

Expand Down Expand Up @@ -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'))
);
}

Expand Down Expand Up @@ -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
);

Expand All @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/invalid-prettierrc/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'node:path'

module.exports = {}
1 change: 1 addition & 0 deletions test/fixtures/invalid-prettierrc/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
15 changes: 15 additions & 0 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ------------------------------------------------------------------------------
Expand Down

0 comments on commit aa5b59f

Please sign in to comment.