88 The above copyright notice and this permission notice shall be
99 included in all copies or substantial portions of this Source Code Form.
1010*/
11- import babelParsers from "prettier/plugins/babel" ;
12- import estreeParsers from "prettier/plugins/estree" ;
13- import prettier from "prettier" ;
11+ import babelParsers from 'prettier/plugins/babel' ;
1412
15- import { dependencies } from " ./rules/dependencies.js" ;
16- import { engines } from " ./rules/engines.js" ;
17- import { files } from " ./rules/files.js" ;
18- import { scripts } from " ./rules/scripts.js" ;
19- import { sort } from " ./rules/sort.js" ;
13+ import { dependencies } from ' ./rules/dependencies.js' ;
14+ import { engines } from ' ./rules/engines.js' ;
15+ import { files } from ' ./rules/files.js' ;
16+ import { scripts } from ' ./rules/scripts.js' ;
17+ import { sort } from ' ./rules/sort.js' ;
2018
2119const { parsers } = babelParsers ;
22- const { " json-stringify" : parser } = parsers ;
20+ const { ' json-stringify' : parser } = parsers ;
2321const { parse } = parser ;
2422const rePkg = / p a c k a g e \. j s o n $ / ;
2523
@@ -34,28 +32,26 @@ const format = (properties) => {
3432} ;
3533
3634const plugin = {
37- name : " prettier-plugin-package" ,
35+ name : ' prettier-plugin-package' ,
3836 parsers : {
39- " json-stringify" : {
37+ ' json-stringify' : {
4038 ...parser ,
4139 async parse ( text , options ) {
4240 const { filepath } = options ;
4341 const ast = parse ( text , options ) ;
4442
4543 if ( rePkg . test ( filepath ) ) {
4644 const { properties, node } = ast ;
47- ast . properties = format ( properties || node . properties ) ;
48- }
45+ const affectNode = ! properties && ! ! node ? .properties ;
46+ const formatResult = format ( properties || node . properties ) ;
4947
50- const formattedText = await prettier . format ( text , {
51- parser : "json-stringify" ,
52- plugins : [ babelParsers , estreeParsers ] ,
53- } ) ;
54- console . log ( "Formatted text:" , formattedText ) ;
48+ if ( affectNode ) ast . node . properties = formatResult ;
49+ else ast . properties = formatResult ;
50+ }
5551
5652 return ast ;
5753 } ,
58- astFormat : " estree-json" ,
54+ astFormat : ' estree-json' ,
5955 } ,
6056 } ,
6157} ;
0 commit comments