Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@rslib/core",
"@rslint/core",
"@rstest/core",
"@swc-next/parser",
"@swc-next/formatter-prettier-plugin",
"prettier"
],
"groupName": null,
Expand Down
30 changes: 0 additions & 30 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## Prettier SWC next parser adapter

The local SWC Next parser adapter includes portions derived from
[@prettier/plugin-yuku](https://github.com/prettier/prettier/tree/main/packages/plugin-yuku)
and Prettier's JavaScript parser postprocessing. The adapter reuses the public
ESTree printer, formatter options, and parser utilities from the installed
`prettier` package.

License: MIT

Copyright © James Long and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## fresh-import

This package includes bundled code from [fresh-import](https://github.com/sapphi-red/fresh-import).
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@rslib/core": "catalog:",
"@rslint/core": "catalog:",
"@rstest/core": "catalog:",
"@swc-next/parser": "catalog:",
"@swc-next/formatter-prettier-plugin": "catalog:",
"prettier": "catalog:",
"tinypool": "catalog:"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/configLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const resolveFmtConfigLayers = async (
): Promise<FmtConfig | undefined> => {
const configs = await resolveConfigLayers(layers, 'fmt');
return configs.length > 1
? (Object.assign({}, ...configs) as FmtConfig)
? Object.assign({}, ...configs) as FmtConfig
: configs[0];
};

Expand Down
68 changes: 62 additions & 6 deletions packages/rstack/src/fmt/prettierPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,72 @@ const fmtOptionsPlugin = {

const defaultFmtPlugins: PrettierPlugins = [swcNextPlugin, fmtOptionsPlugin];

/** Prepends bundled plugins so project plugins can override their parsers. */
/** Selects SWC for JS/TS files and Markdown fences unless a project plugin overrides it. */
const getPrettierPlugins = async (
options: ResolvedFmtOptions,
filePath: string,
): Promise<PrettierPlugins> => {
// An explicit native parser is also the escape hatch for bypassing SWC Next.
const projectPlugins = await Promise.all(
(options.plugins ?? []).map(async (plugin): Promise<Plugin> => {
if (typeof plugin === 'string' || plugin instanceof URL) {
const loaded = (await import(String(plugin))) as Plugin & {
default?: Plugin;
};
return loaded.default ?? loaded;
}
return plugin;
}),
);
// Parser wrappers such as Tailwind require Prettier's AST, not a prebuilt document.
const hasJavaScriptPlugin = projectPlugins.some((plugin) =>
['babel', 'typescript', 'swc', 'swc-ts', 'swc-tsx'].some(
(parser) => plugin.parsers?.[parser],
),
);
// Explicit SWC parsers retain the published plugin's behavior and validation.
const isSwcParser = /^(?:swc|swc-ts|swc-tsx|swc-next|swc-next-ts)$/.test(
typeof options.parser === 'string' ? options.parser : '',
);
const isSwcDefault =
!options.parser &&
/\.(?:[cm]?[jt]s|[jt]sx)$/i.test(filePath) &&
!hasJavaScriptPlugin;
// cspell:ignore mkdn mdwn mkdown
const isMarkdown = options.parser
? typeof options.parser === 'string' &&
/^(?:markdown|mdx|remark)$/.test(options.parser)
: /\.(?:md|mdx|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/i.test(filePath);
const isSwcEmbedded = isMarkdown && !hasJavaScriptPlugin;
// Validate before Prettier can skip empty files or files without a pragma.
// Unsupported SWC options must fail instead of silently selecting another parser.
if (isSwcParser || isSwcDefault) {
if (
options.requirePragma ||
options.insertPragma ||
options.checkIgnorePragma
) {
throw new Error('SWCFMT does not yet support pragma options');
}
if (
(options.rangeStart ?? 0) > 0 ||
(options.rangeEnd !== undefined && options.rangeEnd !== Infinity)
) {
throw new Error('SWCFMT currently supports whole-file formatting only');
}
if (
options.experimentalTernaries ||
options.experimentalOperatorPosition === 'start' ||
options.objectWrap === 'collapse'
) {
throw new Error(
'SWCFMT does not yet support the requested layout option',
);
}
}
const defaultPlugins =
options.parser === 'babel' || options.parser === 'typescript'
? [fmtOptionsPlugin]
: defaultFmtPlugins;
isSwcParser || isSwcDefault || isSwcEmbedded
? defaultFmtPlugins
: [fmtOptionsPlugin];
const plugins =
options.sortPackageJson === true && /(^|[/\\])package\.json$/.test(filePath)
? [
Expand All @@ -35,7 +91,7 @@ const getPrettierPlugins = async (
]
: defaultPlugins;

return options.plugins?.length ? [...plugins, ...options.plugins] : plugins;
return projectPlugins.length ? [...plugins, ...projectPlugins] : plugins;
};

export { getPrettierPlugins };
Loading
Loading