Skip to content
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

fix: report node when loc not found #700

Merged
merged 4 commits into from
Jan 15, 2025
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/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} */ (
JounQin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading