@@ -46,6 +46,13 @@ export function getCompatibleParser(base, parserFormat, options) {
46
46
return parser
47
47
}
48
48
49
+ /**
50
+ *
51
+ * @param {* } base
52
+ * @param {string } parserFormat
53
+ * @param {import('prettier').Options } options
54
+ * @returns {import('prettier').Parser<any> }
55
+ */
49
56
function getFreshCompatibleParser ( base , parserFormat , options ) {
50
57
if ( ! options . plugins ) {
51
58
return base . parsers [ parserFormat ]
@@ -57,31 +64,19 @@ function getFreshCompatibleParser(base, parserFormat, options) {
57
64
58
65
// Now load parsers from plugins
59
66
for ( const name of compatiblePlugins ) {
60
- let path = null
67
+ let plugin = findEnabledPlugin ( options , name )
61
68
62
- try {
63
- path = require . resolve ( name )
64
- } catch ( err ) {
65
- continue
69
+ if ( plugin ) {
70
+ Object . assign ( parser , plugin . parsers [ parserFormat ] )
66
71
}
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 ] )
78
72
}
79
73
80
74
return parser
81
75
}
82
76
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
+ */
85
80
export function getAdditionalParsers ( ) {
86
81
let parsers = { }
87
82
@@ -92,6 +87,9 @@ export function getAdditionalParsers() {
92
87
return parsers
93
88
}
94
89
90
+ /**
91
+ * @returns {Record<string, import('prettier').Printer<any>> }
92
+ */
95
93
export function getAdditionalPrinters ( ) {
96
94
let printers = { }
97
95
@@ -106,3 +104,36 @@ export function getAdditionalPrinters() {
106
104
107
105
return printers
108
106
}
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