Skip to content
Merged
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
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ Rstack CLI brings the Rstack toolchain together for JavaScript development, with

It also covers local development needs outside Rstack's scope, with Prettier formatting and lint-staged commands.

| Command | Description | Powered by |
| ------------ | -------------------------------- | --------------------------------------------------------- |
| `rs dev` | Run the app dev server | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs build` | Build the app for production | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs preview` | Preview the app production build | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs test` | Run tests | [Rstest](https://github.com/web-infra-dev/rstest) |
| `rs lint` | Lint code | [Rslint](https://github.com/web-infra-dev/rslint) |
| `rs lib` | Build library | [Rslib](https://github.com/web-infra-dev/rslib) |
| `rs doc` | Serve or build docs | [Rspress](https://github.com/web-infra-dev/rspress) |
| `rs fmt` | Format code | [Prettier](https://github.com/prettier/prettier) |
| `rs setup` | Install Git hooks | - |
| `rs staged` | Run tasks on staged Git files | [lint-staged](https://github.com/lint-staged/lint-staged) |
| Command | Description | Powered by |
| ------------ | -------------------------------- | ------------------------------------------------------------------------------------------------- |
| `rs dev` | Run the app dev server | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs build` | Build the app for production | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs preview` | Preview the app production build | [Rsbuild](https://github.com/web-infra-dev/rsbuild) |
| `rs test` | Run tests | [Rstest](https://github.com/web-infra-dev/rstest) |
| `rs lint` | Lint code | [Rslint](https://github.com/web-infra-dev/rslint) |
| `rs lib` | Build library | [Rslib](https://github.com/web-infra-dev/rslib) |
| `rs doc` | Serve or build docs | [Rspress](https://github.com/web-infra-dev/rspress) |
| `rs fmt` | Format code | [Prettier](https://github.com/prettier/prettier) + [Yuku](https://github.com/yuku-toolchain/yuku) |
| `rs setup` | Install Git hooks | - |
| `rs staged` | Run tasks on staged Git files | [lint-staged](https://github.com/lint-staged/lint-staged) |

Rstack CLI fits into your existing project workflow. It does not replace your runtime, package manager, or task runner, such as [pnpm](https://github.com/pnpm/pnpm), [Bun](https://github.com/oven-sh/bun), [Turborepo](https://github.com/vercel/turborepo), [Nx](https://github.com/nrwl/nx), and [Nub](https://github.com/nubjs/nub).

Expand Down
59 changes: 59 additions & 0 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,65 @@ 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/plugin-yuku

This package includes bundled code from
[@prettier/plugin-yuku](https://github.com/prettier/prettier/tree/main/packages/plugin-yuku).

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.

The bundled plugin also contains MIT-licensed code from:

- emoji-regex 10.6.0, copyright Mathias Bynens
- escape-string-regexp 5.0.0, copyright Sindre Sorhus
- get-east-asian-width 1.6.0, copyright Sindre Sorhus
- index-to-position 1.2.0, copyright Sindre Sorhus
- is-es5-identifier-name 1.0.1, copyright fisker Cheung
- jest-docblock 30.4.0, copyright Meta Platforms, Inc. and Jest contributors
- narrow-emojis 0.0.3, copyright fisker Cheung
- Prettier 3.10.0-dev, copyright James Long and contributors
- to-fast-properties 4.0.0, copyright Petka Antonov, Benjamin Gruenbaum,
John-David Dalton, and Sindre Sorhus
- trim-newlines 5.0.0, copyright Sindre Sorhus

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 notices 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.

## atomically

This package includes bundled code from
Expand Down
4 changes: 3 additions & 1 deletion packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@
"@rslib/core": "catalog:",
"@rslint/core": "catalog:",
"@rstest/core": "catalog:",
"prettier": "catalog:"
"prettier": "catalog:",
"yuku-parser": "catalog:"
},
"devDependencies": {
"@prettier/plugin-yuku": "catalog:",
"@rspress/core": "catalog:",
"@rstackjs/load-config": "catalog:",
"@rstackjs/test-utils": "catalog:",
Expand Down
2 changes: 2 additions & 0 deletions packages/rstack/src/fmt/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { format, formatWithCursor } from 'prettier';
import { resolveFmtOptions } from './config.ts';
import { resolveFmtParser } from './parser.ts';
import { createFmtPluginResolver } from './plugins.ts';
import { getPrettierPlugins } from './prettierPlugins.ts';
import type { FormatTextOptions, FormatTextResult } from './types.ts';

/** Formats source text without reading formatter config or ignore files. */
Expand All @@ -23,6 +24,7 @@ const formatText = async (
...options,
filepath: filePath,
parser,
plugins: getPrettierPlugins(options.plugins),
};

if (cursorOffset === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion packages/rstack/src/fmt/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getFileInfo, type FileInfoOptions, type Options as PrettierOptions } from 'prettier';
import { getPrettierPlugins } from './prettierPlugins.ts';

const fileInfoOptions = {
ignorePath: [],
Expand All @@ -15,7 +16,7 @@ const resolveFmtParser = async (
(
await getFileInfo(filePath, {
...fileInfoOptions,
plugins: options.plugins,
plugins: getPrettierPlugins(options.plugins),
})
).inferredParser;

Expand Down
12 changes: 12 additions & 0 deletions packages/rstack/src/fmt/prettierPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as yukuPlugin from '@prettier/plugin-yuku';
Comment thread
chenjiahan marked this conversation as resolved.
import type { Options as PrettierOptions } from 'prettier';

type PrettierPlugins = NonNullable<PrettierOptions['plugins']>;

const defaultFmtPlugins: PrettierPlugins = [yukuPlugin];

/** Prepends Yuku so project plugins can override the default parser. */
const getPrettierPlugins = (plugins: PrettierOptions['plugins']): PrettierPlugins =>
plugins?.length ? [...defaultFmtPlugins, ...plugins] : defaultFmtPlugins;

export { getPrettierPlugins };
6 changes: 5 additions & 1 deletion packages/rstack/src/fmt/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import { readFile, writeFile } from 'atomically';
import { format } from 'prettier';
import { getPrettierPlugins } from './prettierPlugins.ts';
import type { FmtFileRequest } from './types.ts';

const formatFileSerial = async (
{ path, options }: FmtFileRequest,
shouldWrite: boolean,
): Promise<boolean> => {
const source = await readFile(path, 'utf8');
const formatted = await format(source, options);
const formatted = await format(source, {
...options,
plugins: getPrettierPlugins(options.plugins),
});

if (source === formatted) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/tests/cli/fmt/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ test('returns exit code 2 for formatting errors', () => {

expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain('error index.ts: SyntaxError: Expression expected.');
expect(result.stderr).toContain('error index.ts: SyntaxError:');
});

test('succeeds when no files can be formatted', () => {
Expand Down
22 changes: 18 additions & 4 deletions packages/rstack/tests/fmt/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ test('applies config ignore patterns outside the config root', async () => {
});
});

test('resolves parsers and accepts unknown extensions with an explicit parser', async () => {
test('uses Yuku parsers by default and accepts an explicit parser', async () => {
await withTempProject(async (rootPath) => {
writeProjectFile(rootPath, 'index.js');
writeProjectFile(rootPath, 'index.ts');
writeProjectFile(rootPath, 'source.custom');
writeProjectFile(rootPath, 'unknown.extension');

const inferredFiles = await discover(rootPath);
const configuredFiles = await discover(rootPath, ['source.custom'], { parser: 'babel' });

expect(relativePaths(rootPath, inferredFiles)).toEqual(['index.ts']);
expect(inferredFiles[0].options.parser).toBe('typescript');
expect(relativePaths(rootPath, inferredFiles)).toEqual(['index.js', 'index.ts']);
expect(inferredFiles.map((file) => file.options.parser)).toEqual(['yuku', 'yuku-ts']);
expect(configuredFiles[0].options).toMatchObject({
filepath: path.join(rootPath, 'source.custom'),
parser: 'babel',
Expand All @@ -71,7 +72,10 @@ test('resolves plugins after applying matching overrides', async () => {
rootPath,
'node_modules/prettier-plugin-fixture/index.mjs',
`export default {
languages: [{ name: 'Fixture JSON', parsers: ['json'], extensions: ['.fixture'] }],
languages: [
{ name: 'Fixture JSON', parsers: ['json'], extensions: ['.fixture'] },
{ name: 'Fixture TypeScript', parsers: ['babel'], extensions: ['.ts'] },
],
};
`,
);
Expand All @@ -88,6 +92,10 @@ test('resolves plugins after applying matching overrides', async () => {
files: '*.fixture',
options: { plugins: ['prettier-plugin-fixture'] },
},
{
files: '*.ts',
options: { plugins: ['prettier-plugin-fixture'] },
},
{
files: '*.md',
options: { plugins: ['missing-plugin'] },
Expand All @@ -104,5 +112,11 @@ test('resolves plugins after applying matching overrides', async () => {
plugins: [pathToFileURL(pluginEntry).href],
},
});
expect(files[1]).toMatchObject({
options: {
parser: 'babel',
plugins: [pathToFileURL(pluginEntry).href],
},
});
});
});
Loading