Skip to content

Commit 97475db

Browse files
Added support for importing relative plugins implicitly, without starting with "./"
1 parent 08c7aef commit 97475db

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolea
111111
}
112112

113113
async function getModule<T = unknown>(modulePath: string): Promise<T> {
114-
const moduleExports = await import(url.pathToFileURL(modulePath).href);
115-
const module = moduleExports.default || moduleExports.exports || moduleExports;
116-
return module;
114+
const moduleExports = await import(url.pathToFileURL(modulePath).href);
115+
const module = moduleExports.default || moduleExports.exports || moduleExports;
116+
return module;
117117
}
118118

119119
function getModulePath(name: string, rootPath: string): string {
@@ -143,8 +143,11 @@ async function getPluginOrExit(name: string): Promise<PrettierPlugin> {
143143

144144
function getPluginPath(name: string): string {
145145
const rootPath = path.join(process.cwd(), "index.js");
146-
const pluginPath = getModulePath(name, rootPath);
147-
return pluginPath;
146+
try {
147+
return getModulePath(`./${name}`, rootPath);
148+
} catch {
149+
return getModulePath(name, rootPath);
150+
}
148151
}
149152

150153
function getPluginVersion(name: string): string | null {
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
import createPlugin from "../../../config/utils/create-plugin.cjs";
2-
3-
const plugin = createPlugin({
4-
name: "baz",
5-
print: (text) => `content from \`prettier-plugin-baz.js\` file + ${text}`,
6-
finalNewLine: false,
7-
});
8-
9-
export default plugin;
1+
export default {
2+
languages: [
3+
{
4+
name: "baz",
5+
parsers: ["baz"],
6+
extensions: [`.${"baz"}`],
7+
},
8+
],
9+
parsers: {
10+
baz: {
11+
parse: (text) => ({ value: text }),
12+
astFormat: "baz",
13+
},
14+
},
15+
printers: {
16+
baz: {
17+
print(path, options) {
18+
return (
19+
`content from \`prettier-plugin-baz.js\` file + ${path.getValue().value}`
20+
);
21+
},
22+
},
23+
},
24+
};

test/__tests__/plugin-resolution.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ describe("loads --plugin by its relative path", () => {
1313
});
1414
});
1515

16-
// TODO (43081j): we don't currently support loading relative paths without
17-
// the leading `./`
18-
describe.skip("loads --plugin by its relative path without leading ./", () => {
16+
describe("loads --plugin by its relative path without leading ./", () => {
1917
runCli("plugins", [
2018
"automatic/file.txt",
2119
"--parser=bar",
@@ -41,9 +39,7 @@ describe("loads --plugin by package name", () => {
4139
});
4240
});
4341

44-
// TODO (43081j): we don't currently support loading relative paths without
45-
// the leading `./`
46-
describe.skip("loads --plugin by filename without leading ./, should resolve to file, not package", () => {
42+
describe("loads --plugin by filename without leading ./, should resolve to file, not package", () => {
4743
runCli("plugins/automatic", [
4844
"file.txt",
4945
"--parser=baz",

0 commit comments

Comments
 (0)