Skip to content

Commit 9b7ff77

Browse files
authored
feat(fmt): add project-root plugin resolver (#134)
1 parent 8bdfe75 commit 9b7ff77

6 files changed

Lines changed: 248 additions & 0 deletions

File tree

‎packages/rstack/THIRD_PARTY_NOTICES.md‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,78 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
142142
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
143143
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
144144

145+
## import-meta-resolve
146+
147+
This package includes bundled code from
148+
[import-meta-resolve](https://github.com/wooorm/import-meta-resolve).
149+
150+
License: MIT
151+
152+
Copyright (c) Titus Wormer <mailto:tituswormer@gmail.com>
153+
154+
Permission is hereby granted, free of charge, to any person obtaining
155+
a copy of this software and associated documentation files (the
156+
'Software'), to deal in the Software without restriction, including
157+
without limitation the rights to use, copy, modify, merge, publish,
158+
distribute, sublicense, and/or sell copies of the Software, and to
159+
permit persons to whom the Software is furnished to do so, subject to
160+
the following conditions:
161+
162+
The above copyright notice and this permission notice shall be
163+
included in all copies or substantial portions of the Software.
164+
165+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
166+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
167+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
168+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
169+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
170+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
171+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
172+
173+
This software includes derivative work based on Node.js.
174+
175+
Copyright Node.js contributors. All rights reserved.
176+
177+
Permission is hereby granted, free of charge, to any person obtaining a copy
178+
of this software and associated documentation files (the "Software"), to
179+
deal in the Software without restriction, including without limitation the
180+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
181+
sell copies of the Software, and to permit persons to whom the Software is
182+
furnished to do so, subject to the following conditions:
183+
184+
The above copyright notice and this permission notice shall be included in
185+
all copies or substantial portions of the Software.
186+
187+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
188+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
189+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
190+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
192+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
193+
THE SOFTWARE.
194+
195+
Parts of Node.js originate from the Joyent Node repository.
196+
197+
Copyright Joyent, Inc. and other Node contributors. All rights reserved.
198+
199+
Permission is hereby granted, free of charge, to any person obtaining a copy
200+
of this software and associated documentation files (the "Software"), to
201+
deal in the Software without restriction, including without limitation the
202+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
203+
sell copies of the Software, and to permit persons to whom the Software is
204+
furnished to do so, subject to the following conditions:
205+
206+
The above copyright notice and this permission notice shall be included in
207+
all copies or substantial portions of the Software.
208+
209+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
210+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
211+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
212+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
213+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
214+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
215+
THE SOFTWARE.
216+
145217
## is-binary-path
146218

147219
This package includes bundled code from [is-binary-path](https://github.com/sindresorhus/is-binary-path).

‎packages/rstack/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"atomically": "catalog:",
7171
"fast-ignore": "catalog:",
7272
"ignore": "catalog:",
73+
"import-meta-resolve": "catalog:",
7374
"is-binary-path": "catalog:",
7475
"lint-staged": "catalog:",
7576
"micromatch": "catalog:",

‎packages/rstack/src/fmt/plugins.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { isAbsolute, join, resolve as resolvePath } from 'node:path';
2+
import { pathToFileURL } from 'node:url';
3+
import { moduleResolve } from 'import-meta-resolve';
4+
import type { Options as PrettierOptions } from 'prettier';
5+
import type { ResolvedFmtConfig } from './types.ts';
6+
7+
type FmtPlugin = NonNullable<PrettierOptions['plugins']>[number];
8+
9+
const resolveModuleUrl = (specifier: string, parentUrl: URL): string =>
10+
moduleResolve(specifier, parentUrl).href;
11+
12+
const resolvePlugin = (plugin: FmtPlugin, rootPath: string, parentUrl: URL): FmtPlugin => {
13+
if (plugin instanceof URL) {
14+
return resolveModuleUrl(plugin.href, parentUrl);
15+
}
16+
if (typeof plugin !== 'string') {
17+
return plugin;
18+
}
19+
if (isAbsolute(plugin)) {
20+
return resolveModuleUrl(pathToFileURL(plugin).href, parentUrl);
21+
}
22+
if (URL.canParse(plugin)) {
23+
return resolveModuleUrl(plugin, parentUrl);
24+
}
25+
26+
try {
27+
return resolveModuleUrl(pathToFileURL(resolvePath(rootPath, plugin)).href, parentUrl);
28+
} catch {
29+
return resolveModuleUrl(plugin, parentUrl);
30+
}
31+
};
32+
33+
const resolveOptionsPlugins = (
34+
options: PrettierOptions,
35+
rootPath: string,
36+
parentUrl: URL,
37+
): PrettierOptions => {
38+
const { plugins } = options;
39+
if (!plugins?.some((plugin) => typeof plugin === 'string' || plugin instanceof URL)) {
40+
return options;
41+
}
42+
43+
const resolvedPlugins = plugins.map((plugin) => resolvePlugin(plugin, rootPath, parentUrl));
44+
45+
return resolvedPlugins.every((plugin, index) => plugin === plugins[index])
46+
? options
47+
: { ...options, plugins: resolvedPlugins };
48+
};
49+
50+
/** Resolves plugin specifiers from the Rstack config root. */
51+
const resolveFmtConfigPlugins = (config: ResolvedFmtConfig): ResolvedFmtConfig => {
52+
const { rootPath } = config;
53+
const parentUrl = pathToFileURL(join(rootPath, 'index.js'));
54+
const baseOptions = resolveOptionsPlugins(config.baseOptions, rootPath, parentUrl);
55+
let overrides = config.overrides;
56+
57+
for (let index = 0; index < overrides.length; index++) {
58+
const override = overrides[index];
59+
if (!override.options) {
60+
continue;
61+
}
62+
63+
const options = resolveOptionsPlugins(override.options, rootPath, parentUrl);
64+
if (options !== override.options) {
65+
if (overrides === config.overrides) {
66+
overrides = [...overrides];
67+
}
68+
overrides[index] = { ...override, options };
69+
}
70+
}
71+
72+
return baseOptions === config.baseOptions && overrides === config.overrides
73+
? config
74+
: { ...config, baseOptions, overrides };
75+
};
76+
77+
export { resolveFmtConfigPlugins };
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { pathToFileURL } from 'node:url';
2+
import { expect, test } from 'rstack/test';
3+
import { normalizeFmtConfig } from '../../src/fmt/config.ts';
4+
import { resolveFmtConfigPlugins } from '../../src/fmt/plugins.ts';
5+
import { withTempProject, writeProjectFile } from './helpers.ts';
6+
7+
test('resolves plugin specifiers from the config root', async () => {
8+
await withTempProject(async (rootPath) => {
9+
const packageEntry = writeProjectFile(
10+
rootPath,
11+
'node_modules/prettier-plugin-packagejson/import.mjs',
12+
'export default {};',
13+
);
14+
writeProjectFile(
15+
rootPath,
16+
'node_modules/prettier-plugin-packagejson/package.json',
17+
JSON.stringify({
18+
name: 'prettier-plugin-packagejson',
19+
exports: {
20+
import: './import.mjs',
21+
require: './require.cjs',
22+
},
23+
}),
24+
);
25+
writeProjectFile(
26+
rootPath,
27+
'node_modules/prettier-plugin-packagejson/require.cjs',
28+
'module.exports = {};',
29+
);
30+
31+
const relativePlugin = writeProjectFile(rootPath, 'plugins/relative.mjs');
32+
const absolutePlugin = writeProjectFile(rootPath, 'plugins/absolute.mjs');
33+
const urlPlugin = writeProjectFile(rootPath, 'plugins/url.mjs');
34+
const overridePlugin = writeProjectFile(rootPath, 'plugins/override.mjs');
35+
const pluginObject = { languages: [] };
36+
const config = normalizeFmtConfig(
37+
{
38+
plugins: [
39+
'prettier-plugin-packagejson',
40+
'./plugins/relative.mjs',
41+
absolutePlugin,
42+
pathToFileURL(urlPlugin),
43+
'data:text/javascript,export default {}',
44+
pluginObject,
45+
],
46+
overrides: [
47+
{
48+
files: '*.json',
49+
options: {
50+
plugins: ['plugins/override.mjs'],
51+
},
52+
},
53+
],
54+
},
55+
rootPath,
56+
);
57+
58+
const resolved = resolveFmtConfigPlugins(config);
59+
60+
expect(resolved.baseOptions.plugins).toEqual([
61+
pathToFileURL(packageEntry).href,
62+
pathToFileURL(relativePlugin).href,
63+
pathToFileURL(absolutePlugin).href,
64+
pathToFileURL(urlPlugin).href,
65+
'data:text/javascript,export default {}',
66+
pluginObject,
67+
]);
68+
expect(resolved.baseOptions.plugins?.at(-1)).toBe(pluginObject);
69+
expect(resolved.overrides[0].options?.plugins).toEqual([pathToFileURL(overridePlugin).href]);
70+
expect(config.baseOptions.plugins?.[0]).toBe('prettier-plugin-packagejson');
71+
expect(config.overrides[0].options?.plugins?.[0]).toBe('plugins/override.mjs');
72+
});
73+
});
74+
75+
test('does not copy config containing only plugin objects', () => {
76+
const pluginObject = { languages: [] };
77+
const config = normalizeFmtConfig(
78+
{
79+
plugins: [pluginObject],
80+
overrides: [{ files: '*.json', options: { plugins: [pluginObject] } }],
81+
},
82+
import.meta.dirname,
83+
);
84+
85+
expect(resolveFmtConfigPlugins(config)).toBe(config);
86+
});

‎pnpm-lock.yaml‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ catalog:
3939
'happy-dom': '^20.11.1'
4040
'heading-case': '^1.1.4'
4141
ignore: 7.0.6
42+
'import-meta-resolve': '4.2.0'
4243
is-binary-path: 3.0.0
4344
'lint-staged': '^17.2.0'
4445
'micromatch': '4.0.8'

0 commit comments

Comments
 (0)