File tree Expand file tree Collapse file tree 3 files changed +74
-1
lines changed
Expand file tree Collapse file tree 3 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -42,3 +42,46 @@ exports[`format should be correct 1`] = `
4242}
4343"
4444` ;
45+
46+ exports [` format should follow custom order 1` ] = `
47+ "{
48+ " version" : " " ,
49+ " name" : " " ,
50+ " description" : " " ,
51+ " keywords" : [
52+ " C" ,
53+ " B" ,
54+ " A"
55+ ],
56+ " license" : " " ,
57+ " author" : " " ,
58+ " scripts" : {
59+ " prebuild" : " " ,
60+ " build" : " " ,
61+ " postbuild" : " " ,
62+ " dev" : " " ,
63+ " lint" : " " ,
64+ " start" : " " ,
65+ " pretest" : " " ,
66+ " test" : " " ,
67+ " posttest" : " "
68+ },
69+ " dependencies" : {
70+ " A" : " *" ,
71+ " B" : " *"
72+ },
73+ " devDependencies" : {
74+ " A" : " *" ,
75+ " B" : " *"
76+ },
77+ " peerDependencies" : {
78+ " A" : " *" ,
79+ " B" : " *"
80+ },
81+ " optionalDependencies" : {
82+ " A" : " *" ,
83+ " B" : " *"
84+ }
85+ }
86+ "
87+ ` ;
Original file line number Diff line number Diff line change @@ -22,8 +22,27 @@ exports.parsers = {
2222 if ( parser . preprocess ) {
2323 text = parser . preprocess ( text , options )
2424 }
25+ const sortPackageJsonOptions =
26+ options . packageSortOrder . length !== 0
27+ ? {
28+ sortOrder : options . packageSortOrder ,
29+ }
30+ : undefined
2531
26- return testPath ( options . filepath ) ? sortPackageJson . default ( text ) : text
32+ return testPath ( options . filepath )
33+ ? sortPackageJson . default ( text , sortPackageJsonOptions )
34+ : text
2735 } ,
2836 } ,
2937}
38+
39+ /** @type {import('prettier').Plugin['options'] } */
40+ exports . options = {
41+ packageSortOrder : {
42+ category : 'Global' ,
43+ type : 'string' ,
44+ array : true ,
45+ default : [ { value : [ ] } ] ,
46+ description : 'Custom ordering array' ,
47+ } ,
48+ }
Original file line number Diff line number Diff line change @@ -71,4 +71,15 @@ describe('format', () => {
7171
7272 delete parsers [ 'json-stringify' ] . preprocess
7373 } )
74+
75+ it ( 'should follow custom order' , ( ) =>
76+ expect (
77+ Promise . resolve (
78+ prettier . format ( uglyJson , {
79+ filepath : 'package.json' ,
80+ plugins : [ './lib/index.cjs' ] ,
81+ packageSortOrder : [ 'version' , 'name' ] ,
82+ } ) ,
83+ ) ,
84+ ) . resolves . toMatchSnapshot ( ) )
7485} )
You can’t perform that action at this time.
0 commit comments