Skip to content

Commit 593fe17

Browse files
authored
fix: conditionally register ember and oxc parsers when dependencies available (#234)
Prevents errors when prettier-plugin-ember-template-tag or @prettier/plugin-oxc are not installed. The previous getter-based lazy loading caused errors during parser enumeration by other plugins (like prettier-plugin-toml), breaking compatibility. Now parsers are only registered when dependencies exist. resolves #233 ## Testing I tested by removing the spread operators and confirmed relevant tests fail. I also tested with my [repro](https://github.com/jahands/prettier-sort-imports-plugin-repro#) posted in #233 and confirmed it no longer errors
1 parent ec4ea16 commit 593fe17

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/index.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ const getEmberPlugin = () => {
6767
}
6868
};
6969

70+
const isEmberPluginAvailable = () => {
71+
try {
72+
getEmberPlugin();
73+
return true;
74+
} catch {
75+
return false;
76+
}
77+
};
78+
7079
const getOxcPlugin = () => {
7180
try {
7281
const oxcPlugin = require('@prettier/plugin-oxc');
@@ -79,6 +88,15 @@ const getOxcPlugin = () => {
7988
}
8089
};
8190

91+
const isOxcPluginAvailable = () => {
92+
try {
93+
getOxcPlugin();
94+
return true;
95+
} catch {
96+
return false;
97+
}
98+
};
99+
82100
export const parsers = {
83101
babel: {
84102
...babelParsers.babel,
@@ -88,34 +106,26 @@ export const parsers = {
88106
...babelParsers['babel-ts'],
89107
preprocess: defaultPreprocessor,
90108
},
91-
get 'ember-template-tag'() {
92-
const emberPlugin = getEmberPlugin();
93-
94-
return {
95-
...emberPlugin.parsers['ember-template-tag'],
109+
...(isEmberPluginAvailable() && {
110+
'ember-template-tag': {
111+
...getEmberPlugin().parsers['ember-template-tag'],
96112
preprocess: emberPreprocessor,
97-
};
98-
},
113+
},
114+
}),
99115
flow: {
100116
...flowParsers.flow,
101117
preprocess: defaultPreprocessor,
102118
},
103-
get oxc() {
104-
const oxcPlugin = getOxcPlugin();
105-
106-
return {
107-
...oxcPlugin.parsers.oxc,
119+
...(isOxcPluginAvailable() && {
120+
oxc: {
121+
...getOxcPlugin().parsers.oxc,
108122
preprocess: defaultPreprocessor,
109-
};
110-
},
111-
get 'oxc-ts'() {
112-
const oxcPlugin = getOxcPlugin();
113-
114-
return {
115-
...oxcPlugin.parsers['oxc-ts'],
123+
},
124+
'oxc-ts': {
125+
...getOxcPlugin().parsers['oxc-ts'],
116126
preprocess: defaultPreprocessor,
117-
};
118-
},
127+
},
128+
}),
119129
typescript: {
120130
...typescriptParsers.typescript,
121131
preprocess: defaultPreprocessor,
@@ -127,9 +137,7 @@ export const parsers = {
127137
};
128138

129139
export const printers = {
130-
get 'estree-oxc'() {
131-
const oxcPlugin = getOxcPlugin();
132-
133-
return oxcPlugin.printers['estree-oxc'];
134-
},
140+
...(isOxcPluginAvailable() && {
141+
'estree-oxc': getOxcPlugin().printers['estree-oxc'],
142+
}),
135143
};

0 commit comments

Comments
 (0)