Skip to content

Commit 62005b2

Browse files
committed
fix: report on program node if loc unavailable
1 parent d6b90c1 commit 62005b2

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

eslint-plugin-prettier.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const eslintPluginPrettier = {
161161
const source = sourceCode.text;
162162

163163
return {
164-
Program() {
164+
Program(node) {
165165
if (!prettierFormat) {
166166
// Prettier is expensive to load, so only load it if needed.
167167
prettierFormat = /** @type {PrettierFormat} */ (
@@ -214,7 +214,7 @@ const eslintPluginPrettier = {
214214
let message = 'Parsing error: ' + err.message;
215215

216216
const error =
217-
/** @type {SyntaxError & {codeFrame: string; loc: SourceLocation}} */ (
217+
/** @type {SyntaxError & {codeFrame: string; loc?: SourceLocation}} */ (
218218
err
219219
);
220220

@@ -227,10 +227,11 @@ const eslintPluginPrettier = {
227227
}
228228
if (error.loc) {
229229
message = message.replace(/ \(\d+:\d+\)$/, '');
230+
context.report({ message, loc: error.loc });
231+
} else {
232+
context.report({ message, node });
230233
}
231234

232-
context.report({ message, loc: error.loc });
233-
234235
return;
235236
}
236237

test/prettier.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,20 @@ runFixture('eslint-plugin-svelte3/*.svelte', [[], []], svelteUnsupported);
389389
*/
390390
runFixture('*.pug', [[]]);
391391

392-
runFixture('invalid-prettierrc/*', []);
392+
runFixture('invalid-prettierrc/*', [
393+
[
394+
{
395+
column: 1,
396+
endColumn: 20,
397+
endLine: 1,
398+
line: 1,
399+
message: 'Parsing error: Cannot use import statement outside a module',
400+
nodeType: 'Program',
401+
ruleId: 'prettier/prettier',
402+
severity: 2,
403+
},
404+
],
405+
]);
393406

394407
// ------------------------------------------------------------------------------
395408
// Helpers

0 commit comments

Comments
 (0)