Skip to content

Commit 6c563ef

Browse files
authored
feat(fmt): resolve formatter overrides (#114)
1 parent c848b23 commit 6c563ef

5 files changed

Lines changed: 146 additions & 2 deletions

File tree

packages/rstack/THIRD_PARTY_NOTICES.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3030
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3131
SOFTWARE.
3232

33+
## micromatch
34+
35+
This package includes bundled code from [micromatch](https://github.com/micromatch/micromatch).
36+
37+
License: MIT
38+
39+
The MIT License (MIT)
40+
41+
Copyright (c) 2014-present, Jon Schlinkert.
42+
43+
Permission is hereby granted, free of charge, to any person obtaining a copy
44+
of this software and associated documentation files (the "Software"), to deal
45+
in the Software without restriction, including without limitation the rights
46+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
47+
copies of the Software, and to permit persons to whom the Software is
48+
furnished to do so, subject to the following conditions:
49+
50+
The above copyright notice and this permission notice shall be included in
51+
all copies or substantial portions of the Software.
52+
53+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
59+
THE SOFTWARE.
60+
3361
## Husky
3462

3563
Portions of the Git hook installer and runtime are derived from [Husky](https://github.com/typicode/husky).

packages/rstack/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@
6565
"@rstackjs/test-utils": "catalog:",
6666
"@rstest/adapter-rsbuild": "catalog:",
6767
"@rstest/adapter-rslib": "catalog:",
68+
"@types/micromatch": "catalog:",
6869
"@types/node": "catalog:",
6970
"lint-staged": "catalog:",
71+
"micromatch": "catalog:",
7072
"rslog": "catalog:",
7173
"typescript": "catalog:"
7274
},

packages/rstack/src/fmt/config.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { dirname } from 'node:path';
1+
import { dirname, relative } from 'node:path';
2+
import micromatch from 'micromatch';
3+
import type { Options as PrettierOptions } from 'prettier';
24
import type { FmtConfig, FmtConfigDefinition, ResolvedFmtConfig } from './types.ts';
35

46
type ResolveFmtConfigOptions = {
@@ -19,6 +21,47 @@ const normalizeFmtConfig = (config: FmtConfig | undefined, rootPath: string): Re
1921
};
2022
};
2123

24+
const pathMatchesGlobs = (
25+
filePath: string,
26+
patterns: string | string[],
27+
excludedPatterns?: string | string[],
28+
): boolean => {
29+
const patternList = Array.isArray(patterns) ? patterns : [patterns];
30+
const withSlashes = patternList.filter((pattern) => pattern.includes('/'));
31+
const withoutSlashes = patternList.filter((pattern) => !pattern.includes('/'));
32+
33+
return (
34+
micromatch.isMatch(filePath, withoutSlashes, {
35+
ignore: excludedPatterns,
36+
basename: true,
37+
dot: true,
38+
}) ||
39+
micromatch.isMatch(filePath, withSlashes, {
40+
ignore: excludedPatterns,
41+
basename: false,
42+
dot: true,
43+
})
44+
);
45+
};
46+
47+
/** Applies matching overrides to the shared formatter options. */
48+
const resolveFmtOptions = (filePath: string, config: ResolvedFmtConfig): PrettierOptions => {
49+
if (config.overrides.length === 0) {
50+
return config.baseOptions;
51+
}
52+
53+
const options = { ...config.baseOptions };
54+
const relativeFilePath = relative(config.rootPath, filePath);
55+
56+
for (const override of config.overrides) {
57+
if (pathMatchesGlobs(relativeFilePath, override.files, override.excludeFiles)) {
58+
Object.assign(options, override.options);
59+
}
60+
}
61+
62+
return options;
63+
};
64+
2265
/** Resolves a formatter config definition and its project root. */
2366
const resolveFmtConfig = async ({
2467
definition,
@@ -31,4 +74,4 @@ const resolveFmtConfig = async ({
3174
return normalizeFmtConfig(config, rootPath);
3275
};
3376

34-
export { normalizeFmtConfig, resolveFmtConfig };
77+
export { normalizeFmtConfig, resolveFmtConfig, resolveFmtOptions };

pnpm-lock.yaml

Lines changed: 69 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ catalog:
2828
'@testing-library/dom': '^10.4.1'
2929
'@testing-library/jest-dom': '^7.0.0'
3030
'@testing-library/react': '^16.3.2'
31+
'@types/micromatch': '^4.0.10'
3132
'@types/node': '^24.13.3'
3233
'@types/react': '^19.2.17'
3334
'@types/react-dom': '^19.2.3'
@@ -36,6 +37,7 @@ catalog:
3637
'happy-dom': '^20.11.1'
3738
'heading-case': '^1.1.4'
3839
'lint-staged': '^17.2.0'
40+
'micromatch': '4.0.8'
3941
prettier: '3.9.6'
4042
'prettier-plugin-packagejson': '^3.0.2'
4143
'react': '^19.2.8'

0 commit comments

Comments
 (0)