Skip to content

Commit 4a0e57d

Browse files
authored
Catch and format SyntaxErrors as eslint violations (#141)
* Bump development prettier version to 1.15.3 * Catch and format SyntaxErrors as eslint violations If Prettier can't format a file due to not being able to parse the given content it throws a SyntaxError. If that happens then announce the error as an ESLint rule violation instead of crashing.
1 parent d34daed commit 4a0e57d

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

eslint-plugin-prettier.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,40 @@ module.exports = {
209209
{ filepath }
210210
);
211211

212-
const prettierSource = prettier.format(source, prettierOptions);
212+
// prettier.format() may throw a SyntaxError if it cannot parse the
213+
// source code it is given. Ususally for JS files this isn't a
214+
// problem as ESLint will report invalid syntax before trying to
215+
// pass it to the prettier plugin. However this might be a problem
216+
// for non-JS languages that are handled by a plugin. Notably Vue
217+
// files throw an error if they contain unclosed elements, such as
218+
// `<template><div></template>. In this case report an error at the
219+
// point at which parsing failed.
220+
let prettierSource;
221+
try {
222+
prettierSource = prettier.format(source, prettierOptions);
223+
} catch (err) {
224+
if (!(err instanceof SyntaxError)) {
225+
throw err;
226+
}
227+
228+
let message = 'Parsing error: ' + err.message;
229+
230+
// Prettier's message contains a codeframe style preview of the
231+
// invalid code and the line/column at which the error occured.
232+
// ESLint shows those pieces of information elsewhere already so
233+
// remove them from the message
234+
if (err.codeFrame) {
235+
message = message.replace(`\n${err.codeFrame}`, '');
236+
}
237+
if (err.loc) {
238+
message = message.replace(/ \(\d+:\d+\)$/, '');
239+
}
240+
241+
context.report({ message, loc: err.loc });
242+
243+
return;
244+
}
245+
213246
if (source !== prettierSource) {
214247
const differences = generateDifferences(source, prettierSource);
215248

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"eslint-plugin-node": "^8.0.0",
4444
"eslint-plugin-self": "^1.1.0",
4545
"mocha": "^5.2.0",
46-
"prettier": "^1.13.0",
46+
"prettier": "^1.15.3",
4747
"vue-eslint-parser": "^4.0.2"
4848
},
4949
"engines": {

test/invalid/vue-syntax-error.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CODE:
2+
<template>
3+
<div>
4+
</template>
5+
<script>
6+
a();
7+
</script>
8+
9+
OUTPUT:
10+
<template>
11+
<div>
12+
</template>
13+
<script>
14+
a();
15+
</script>
16+
17+
OPTIONS:
18+
[]
19+
20+
ERRORS:
21+
[
22+
{
23+
message: 'Parsing error: Unexpected closing tag "template". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags',
24+
line: 3, column: 2
25+
},
26+
]

test/prettier.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ const vueRuleTester = new RuleTester({
9292
vueRuleTester.run('prettier', rule, {
9393
valid: [
9494
{
95-
code: `<template>foo</template>\n<script>\n"";\n</script>\n`,
95+
code: `<template>\n <div>HI</div>\n</template>\n<script>\n3;\n</script>\n`,
9696
filename: 'valid.vue'
9797
}
9898
],
9999
invalid: [
100100
Object.assign(loadInvalidFixture('vue'), {
101101
filename: 'invalid.vue'
102+
}),
103+
Object.assign(loadInvalidFixture('vue-syntax-error'), {
104+
filename: 'syntax-error.vue'
102105
})
103106
]
104107
});

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,9 @@ prettier-linter-helpers@^1.0.0:
679679
dependencies:
680680
fast-diff "^1.1.2"
681681

682-
prettier@^1.13.0:
683-
version "1.14.3"
684-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.3.tgz#90238dd4c0684b7edce5f83b0fb7328e48bd0895"
682+
prettier@^1.15.3:
683+
version "1.15.3"
684+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.15.3.tgz#1feaac5bdd181237b54dbe65d874e02a1472786a"
685685

686686
progress@^2.0.0:
687687
version "2.0.0"

0 commit comments

Comments
 (0)