9
9
// Requirements
10
10
// ------------------------------------------------------------------------------
11
11
12
+ const fs = require ( 'fs' ) ;
13
+ const path = require ( 'path' ) ;
14
+
12
15
const {
13
16
showInvisibles,
14
17
generateDifferences
@@ -25,6 +28,9 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
25
28
// ------------------------------------------------------------------------------
26
29
27
30
// Lazily-loaded Prettier.
31
+ /**
32
+ * @type {import('prettier') }
33
+ */
28
34
let prettier ;
29
35
30
36
// ------------------------------------------------------------------------------
@@ -55,6 +61,26 @@ function reportDifference(context, difference) {
55
61
} ) ;
56
62
}
57
63
64
+ /**
65
+ * get normalized filepath in case of virtual filename
66
+ * @param {string } filepath
67
+ * @returns {string }
68
+ */
69
+ function normalizeFilepath ( filepath ) {
70
+ try {
71
+ if ( fs . statSync ( filepath ) . isFile ( ) ) {
72
+ return filepath ;
73
+ }
74
+ } catch ( err ) {
75
+ // https://github.com/eslint/eslint/issues/11989
76
+ if ( err . code === 'ENOTDIR' ) {
77
+ return normalizeFilepath ( path . dirname ( filepath ) ) ;
78
+ }
79
+ }
80
+
81
+ return filepath ;
82
+ }
83
+
58
84
// ------------------------------------------------------------------------------
59
85
// Module Definition
60
86
// ------------------------------------------------------------------------------
@@ -112,6 +138,7 @@ module.exports = {
112
138
( context . options [ 1 ] && context . options [ 1 ] . fileInfoOptions ) || { } ;
113
139
const sourceCode = context . getSourceCode ( ) ;
114
140
const filepath = context . getFilename ( ) ;
141
+ const normalizedFilepath = normalizeFilepath ( filepath ) ;
115
142
const source = sourceCode . text ;
116
143
117
144
return {
@@ -124,13 +151,13 @@ module.exports = {
124
151
const eslintPrettierOptions = context . options [ 0 ] || { } ;
125
152
126
153
const prettierRcOptions = usePrettierrc
127
- ? prettier . resolveConfig . sync ( filepath , {
154
+ ? prettier . resolveConfig . sync ( normalizedFilepath , {
128
155
editorconfig : true
129
156
} )
130
157
: null ;
131
158
132
159
const prettierFileInfo = prettier . getFileInfo . sync (
133
- filepath ,
160
+ normalizedFilepath ,
134
161
Object . assign (
135
162
{ } ,
136
163
{ resolveConfig : true , ignorePath : '.prettierignore' } ,
@@ -145,7 +172,8 @@ module.exports = {
145
172
146
173
const initialOptions = { } ;
147
174
148
- // ESLint suppports processors that let you extract and lint JS
175
+ // for ESLint < 6.0
176
+ // it supports processors that let you extract and lint JS
149
177
// fragments within a non-JS language. In the cases where prettier
150
178
// supports the same language as a processor, we want to process
151
179
// the provided source code as javascript (as ESLint provides the
@@ -165,8 +193,13 @@ module.exports = {
165
193
// * Prettier supports parsing the file type
166
194
// * There is an ESLint processor that extracts JavaScript snippets
167
195
// from the file type.
196
+ //
197
+ // for ESLint >= 6.0
198
+ // it supports virtual filename, if filepath is not same as normalizedFilepath,
199
+ // it means filepath is virtual name, and we can guess the file type by prettier automatically
168
200
const parserBlocklist = [ null , 'graphql' , 'markdown' , 'html' ] ;
169
201
if (
202
+ filepath === normalizedFilepath &&
170
203
parserBlocklist . indexOf ( prettierFileInfo . inferredParser ) !== - 1
171
204
) {
172
205
// Prettier v1.16.0 renamed the `babylon` parser to `babel`
@@ -187,7 +220,7 @@ module.exports = {
187
220
) ;
188
221
189
222
// prettier.format() may throw a SyntaxError if it cannot parse the
190
- // source code it is given. Ususally for JS files this isn't a
223
+ // source code it is given. Usually for JS files this isn't a
191
224
// problem as ESLint will report invalid syntax before trying to
192
225
// pass it to the prettier plugin. However this might be a problem
193
226
// for non-JS languages that are handled by a plugin. Notably Vue
@@ -205,7 +238,7 @@ module.exports = {
205
238
let message = 'Parsing error: ' + err . message ;
206
239
207
240
// Prettier's message contains a codeframe style preview of the
208
- // invalid code and the line/column at which the error occured .
241
+ // invalid code and the line/column at which the error occurred .
209
242
// ESLint shows those pieces of information elsewhere already so
210
243
// remove them from the message
211
244
if ( err . codeFrame ) {
0 commit comments