99// Requirements
1010// ------------------------------------------------------------------------------
1111
12- const fs = require ( 'fs' ) ;
13- const path = require ( 'path' ) ;
14-
1512const {
1613 showInvisibles,
17- generateDifferences
14+ generateDifferences,
1815} = require ( 'prettier-linter-helpers' ) ;
1916
2017// ------------------------------------------------------------------------------
@@ -46,43 +43,21 @@ let prettier;
4643function reportDifference ( context , difference ) {
4744 const { operation, offset, deleteText = '' , insertText = '' } = difference ;
4845 const range = [ offset , offset + deleteText . length ] ;
49- const [ start , end ] = range . map ( index =>
46+ const [ start , end ] = range . map ( ( index ) =>
5047 context . getSourceCode ( ) . getLocFromIndex ( index )
5148 ) ;
5249
5350 context . report ( {
5451 messageId : operation ,
5552 data : {
5653 deleteText : showInvisibles ( deleteText ) ,
57- insertText : showInvisibles ( insertText )
54+ insertText : showInvisibles ( insertText ) ,
5855 } ,
5956 loc : { start, end } ,
60- fix : fixer => fixer . replaceTextRange ( range , insertText )
57+ fix : ( fixer ) => fixer . replaceTextRange ( range , insertText ) ,
6158 } ) ;
6259}
6360
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-
8661// ------------------------------------------------------------------------------
8762// Module Definition
8863// ------------------------------------------------------------------------------
@@ -95,15 +70,15 @@ module.exports = {
9570 rules : {
9671 'prettier/prettier' : 'error' ,
9772 'arrow-body-style' : 'off' ,
98- 'prefer-arrow-callback' : 'off'
99- }
100- }
73+ 'prefer-arrow-callback' : 'off' ,
74+ } ,
75+ } ,
10176 } ,
10277 rules : {
10378 prettier : {
10479 meta : {
10580 docs : {
106- url : 'https://github.com/prettier/eslint-plugin-prettier#options'
81+ url : 'https://github.com/prettier/eslint-plugin-prettier#options' ,
10782 } ,
10883 type : 'layout' ,
10984 fixable : 'code' ,
@@ -112,7 +87,7 @@ module.exports = {
11287 {
11388 type : 'object' ,
11489 properties : { } ,
115- additionalProperties : true
90+ additionalProperties : true ,
11691 } ,
11792 {
11893 type : 'object' ,
@@ -121,17 +96,17 @@ module.exports = {
12196 fileInfoOptions : {
12297 type : 'object' ,
12398 properties : { } ,
124- additionalProperties : true
125- }
99+ additionalProperties : true ,
100+ } ,
126101 } ,
127- additionalProperties : true
128- }
102+ additionalProperties : true ,
103+ } ,
129104 ] ,
130105 messages : {
131106 [ INSERT ] : 'Insert `{{ insertText }}`' ,
132107 [ DELETE ] : 'Delete `{{ deleteText }}`' ,
133- [ REPLACE ] : 'Replace `{{ deleteText }}` with `{{ insertText }}`'
134- }
108+ [ REPLACE ] : 'Replace `{{ deleteText }}` with `{{ insertText }}`' ,
109+ } ,
135110 } ,
136111 create ( context ) {
137112 const usePrettierrc =
@@ -145,9 +120,7 @@ module.exports = {
145120 // file paths. If this is the case then we need to resolve prettier
146121 // config and file info using the on-disk path instead of the virtual
147122 // 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 ( ) ;
151124 const source = sourceCode . text ;
152125
153126 return {
@@ -161,7 +134,7 @@ module.exports = {
161134
162135 const prettierRcOptions = usePrettierrc
163136 ? prettier . resolveConfig . sync ( onDiskFilepath , {
164- editorconfig : true
137+ editorconfig : true ,
165138 } )
166139 : null ;
167140
@@ -221,13 +194,7 @@ module.exports = {
221194 }
222195
223196 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' ;
231198 }
232199
233200 const prettierOptions = Object . assign (
@@ -279,9 +246,9 @@ module.exports = {
279246 reportDifference ( context , difference ) ;
280247 }
281248 }
282- }
249+ } ,
283250 } ;
284- }
285- }
286- }
251+ } ,
252+ } ,
253+ } ,
287254} ;
0 commit comments