@@ -46,6 +46,13 @@ export function getCompatibleParser(base, parserFormat, options) {
4646 return parser
4747}
4848
49+ /**
50+ *
51+ * @param {* } base
52+ * @param {string } parserFormat
53+ * @param {import('prettier').Options } options
54+ * @returns {import('prettier').Parser<any> }
55+ */
4956function getFreshCompatibleParser ( base , parserFormat , options ) {
5057 if ( ! options . plugins ) {
5158 return base . parsers [ parserFormat ]
@@ -57,31 +64,19 @@ function getFreshCompatibleParser(base, parserFormat, options) {
5764
5865 // Now load parsers from plugins
5966 for ( const name of compatiblePlugins ) {
60- let path = null
67+ let plugin = findEnabledPlugin ( options , name )
6168
62- try {
63- path = require . resolve ( name )
64- } catch ( err ) {
65- continue
69+ if ( plugin ) {
70+ Object . assign ( parser , plugin . parsers [ parserFormat ] )
6671 }
67-
68- let plugin = options . plugins . find (
69- ( plugin ) => plugin . name === name || plugin . name === path ,
70- )
71-
72- // The plugin is not loaded
73- if ( ! plugin ) {
74- continue
75- }
76-
77- Object . assign ( parser , plugin . parsers [ parserFormat ] )
7872 }
7973
8074 return parser
8175}
8276
83- // We need to load this plugin dynamically because it's not available by default
84- // And we are not bundling it with the main Prettier plugin
77+ /**
78+ * @returns {Record<string, import('prettier').Parser<any>> }
79+ */
8580export function getAdditionalParsers ( ) {
8681 let parsers = { }
8782
@@ -92,6 +87,9 @@ export function getAdditionalParsers() {
9287 return parsers
9388}
9489
90+ /**
91+ * @returns {Record<string, import('prettier').Printer<any>> }
92+ */
9593export function getAdditionalPrinters ( ) {
9694 let printers = { }
9795
@@ -106,3 +104,36 @@ export function getAdditionalPrinters() {
106104
107105 return printers
108106}
107+
108+ /**
109+ *
110+ * @param {import('prettier').Options } options
111+ * @param {string } name
112+ * @returns {import('prettier').Plugin<any> | null }
113+ */
114+ function findEnabledPlugin ( options , name ) {
115+ let path = null
116+
117+ try {
118+ path = require . resolve ( name )
119+ } catch ( err ) {
120+ return null
121+ }
122+
123+ let plugin = options . plugins . find (
124+ ( plugin ) => plugin . name === name || plugin . name === path ,
125+ )
126+
127+ // The plugin was found by name or path
128+ if ( plugin ) {
129+ return plugin
130+ }
131+
132+ // The plugin was loaded with require so we use object equality to find it
133+ let mod = loadIfExists ( path )
134+ if ( mod && mod . parsers && options . plugins . includes ( mod ) ) {
135+ return mod
136+ }
137+
138+ return null
139+ }
0 commit comments