9
9
// Requirements
10
10
// ------------------------------------------------------------------------------
11
11
12
- const fs = require ( 'fs' ) ;
13
- const path = require ( 'path' ) ;
14
-
15
12
const {
16
13
showInvisibles,
17
- generateDifferences
14
+ generateDifferences,
18
15
} = require ( 'prettier-linter-helpers' ) ;
19
16
20
17
// ------------------------------------------------------------------------------
@@ -46,43 +43,21 @@ let prettier;
46
43
function reportDifference ( context , difference ) {
47
44
const { operation, offset, deleteText = '' , insertText = '' } = difference ;
48
45
const range = [ offset , offset + deleteText . length ] ;
49
- const [ start , end ] = range . map ( index =>
46
+ const [ start , end ] = range . map ( ( index ) =>
50
47
context . getSourceCode ( ) . getLocFromIndex ( index )
51
48
) ;
52
49
53
50
context . report ( {
54
51
messageId : operation ,
55
52
data : {
56
53
deleteText : showInvisibles ( deleteText ) ,
57
- insertText : showInvisibles ( insertText )
54
+ insertText : showInvisibles ( insertText ) ,
58
55
} ,
59
56
loc : { start, end } ,
60
- fix : fixer => fixer . replaceTextRange ( range , insertText )
57
+ fix : ( fixer ) => fixer . replaceTextRange ( range , insertText ) ,
61
58
} ) ;
62
59
}
63
60
64
- /**
65
- * Given a filepath, get the nearest path that is a regular file.
66
- * The filepath provided by eslint may be a virtual filepath rather than a file
67
- * on disk. This attempts to transform a virtual path into an on-disk path
68
- * @param {string } filepath
69
- * @returns {string }
70
- */
71
- function getOnDiskFilepath ( filepath ) {
72
- try {
73
- if ( fs . statSync ( filepath ) . isFile ( ) ) {
74
- return filepath ;
75
- }
76
- } catch ( err ) {
77
- // https://github.com/eslint/eslint/issues/11989
78
- if ( err . code === 'ENOTDIR' ) {
79
- return getOnDiskFilepath ( path . dirname ( filepath ) ) ;
80
- }
81
- }
82
-
83
- return filepath ;
84
- }
85
-
86
61
// ------------------------------------------------------------------------------
87
62
// Module Definition
88
63
// ------------------------------------------------------------------------------
@@ -95,15 +70,15 @@ module.exports = {
95
70
rules : {
96
71
'prettier/prettier' : 'error' ,
97
72
'arrow-body-style' : 'off' ,
98
- 'prefer-arrow-callback' : 'off'
99
- }
100
- }
73
+ 'prefer-arrow-callback' : 'off' ,
74
+ } ,
75
+ } ,
101
76
} ,
102
77
rules : {
103
78
prettier : {
104
79
meta : {
105
80
docs : {
106
- url : 'https://github.com/prettier/eslint-plugin-prettier#options'
81
+ url : 'https://github.com/prettier/eslint-plugin-prettier#options' ,
107
82
} ,
108
83
type : 'layout' ,
109
84
fixable : 'code' ,
@@ -112,7 +87,7 @@ module.exports = {
112
87
{
113
88
type : 'object' ,
114
89
properties : { } ,
115
- additionalProperties : true
90
+ additionalProperties : true ,
116
91
} ,
117
92
{
118
93
type : 'object' ,
@@ -121,17 +96,17 @@ module.exports = {
121
96
fileInfoOptions : {
122
97
type : 'object' ,
123
98
properties : { } ,
124
- additionalProperties : true
125
- }
99
+ additionalProperties : true ,
100
+ } ,
126
101
} ,
127
- additionalProperties : true
128
- }
102
+ additionalProperties : true ,
103
+ } ,
129
104
] ,
130
105
messages : {
131
106
[ INSERT ] : 'Insert `{{ insertText }}`' ,
132
107
[ DELETE ] : 'Delete `{{ deleteText }}`' ,
133
- [ REPLACE ] : 'Replace `{{ deleteText }}` with `{{ insertText }}`'
134
- }
108
+ [ REPLACE ] : 'Replace `{{ deleteText }}` with `{{ insertText }}`' ,
109
+ } ,
135
110
} ,
136
111
create ( context ) {
137
112
const usePrettierrc =
@@ -145,9 +120,7 @@ module.exports = {
145
120
// file paths. If this is the case then we need to resolve prettier
146
121
// config and file info using the on-disk path instead of the virtual
147
122
// path.
148
- // See https://github.com/eslint/eslint/issues/11989 for ideas around
149
- // being able to get this value directly from eslint in the future.
150
- const onDiskFilepath = getOnDiskFilepath ( filepath ) ;
123
+ const onDiskFilepath = context . getPhysicalFilename ( ) ;
151
124
const source = sourceCode . text ;
152
125
153
126
return {
@@ -161,7 +134,7 @@ module.exports = {
161
134
162
135
const prettierRcOptions = usePrettierrc
163
136
? prettier . resolveConfig . sync ( onDiskFilepath , {
164
- editorconfig : true
137
+ editorconfig : true ,
165
138
} )
166
139
: null ;
167
140
@@ -221,13 +194,7 @@ module.exports = {
221
194
}
222
195
223
196
if ( filepath === onDiskFilepath && inferParserToBabel ) {
224
- // Prettier v1.16.0 renamed the `babylon` parser to `babel`
225
- // Use the modern name if available
226
- const supportBabelParser = prettier
227
- . getSupportInfo ( )
228
- . languages . some ( language => language . parsers . includes ( 'babel' ) ) ;
229
-
230
- initialOptions . parser = supportBabelParser ? 'babel' : 'babylon' ;
197
+ initialOptions . parser = 'babel' ;
231
198
}
232
199
233
200
const prettierOptions = Object . assign (
@@ -279,9 +246,9 @@ module.exports = {
279
246
reportDifference ( context , difference ) ;
280
247
}
281
248
}
282
- }
249
+ } ,
283
250
} ;
284
- }
285
- }
286
- }
251
+ } ,
252
+ } ,
253
+ } ,
287
254
} ;
0 commit comments