Skip to content

Commit e9ebcdd

Browse files
committedJan 24, 2023
fix: pass full file path to prettier::resolveConfig, not just the dirname
1 parent 6606dc2 commit e9ebcdd

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed
 

‎prettier.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
trailingComma: 'none',
1111
overrides: [
1212
{
13-
files: '**/*.@(ts|?(@(c|m))js)?(x)',
13+
files: '**/*.?(@(c|m))@(ts|js)?(x)',
1414
options: {
1515
parser: 'babel-ts',
1616
printWidth: 90

‎src/formatters/prettier.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ const debug = debugFactory('babel-plugin-tester:formatter');
1414
type MaybePrettierOptions = PrettierOptions | null;
1515
const configDirectoryCache: Record<string, MaybePrettierOptions> = Object.create(null);
1616

17-
const getCachedConfig = (directory: string) => {
18-
if (!(directory in configDirectoryCache)) {
17+
const getCachedConfig = (filepath: string) => {
18+
if (!(filepath in configDirectoryCache)) {
19+
configDirectoryCache[filepath] = resolvePrettierConfig.sync(filepath);
1920
debug(
20-
`caching prettier configuration resolved from ${directory}: %O`,
21-
configDirectoryCache[directory]
21+
`caching prettier configuration resolved from ${filepath}: %O`,
22+
configDirectoryCache[filepath]
2223
);
23-
configDirectoryCache[directory] = resolvePrettierConfig.sync(directory);
2424
} else {
25-
debug(`using cached prettier configuration resolved from ${directory}`);
25+
debug(`using cached prettier configuration resolved from ${filepath}`);
2626
}
2727

28-
return configDirectoryCache[directory];
28+
return configDirectoryCache[filepath];
2929
};
3030

3131
export type { PrettierOptions };
@@ -58,7 +58,7 @@ export const prettierFormatter: ResultFormatter<{
5858
filename,
5959
filepath = filename || path.join(cwd, 'dummy.js'),
6060
config,
61-
prettierOptions = config || getCachedConfig(cwd)
61+
prettierOptions = config || getCachedConfig(filepath)
6262
} = {}
6363
) => {
6464
const finalPrettierOptions = {

‎test/fixtures/prettier-configured/fixture/.prettierrc.js

+9
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ module.exports = {
2323
endOfLine: "lf",
2424
embeddedLanguageFormatting: "auto",
2525
singleAttributePerLine: false,
26+
overrides: [
27+
{
28+
files: "**/*.mts",
29+
options: {
30+
parser: "babel-ts",
31+
singleQuote: true
32+
},
33+
},
34+
],
2635
};

‎test/unit-plugin-tester.test.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -3633,11 +3633,11 @@ describe('tests targeting both FixtureOptions and TestObject interfaces', () =>
36333633
tests: {
36343634
'formatted-1': {
36353635
code: `
3636-
console.log( "hey" )
3637-
`,
3636+
console.log( "hey" )
3637+
`,
36383638
output: `
3639-
console.log("hey");
3640-
`
3639+
console.log("hey");
3640+
`
36413641
},
36423642
'formatted-2': {
36433643
codeFixture: codeFileWithPrettierConfig,
@@ -3650,6 +3650,23 @@ describe('tests targeting both FixtureOptions and TestObject interfaces', () =>
36503650
})
36513651
);
36523652

3653+
await runPluginTester(
3654+
getDummyPresetOptions({
3655+
filepath: getFixturePath('prettier-configured/fixture/code.mts'),
3656+
formatResult: prettierFormatter,
3657+
tests: {
3658+
formatted: {
3659+
code: `
3660+
console.log( "hey" )
3661+
`,
3662+
output: `
3663+
console.log('hey');
3664+
`
3665+
}
3666+
}
3667+
})
3668+
);
3669+
36533670
expect(logSpy.mock.calls).toMatchObject([['hey'], ['hey']]);
36543671
});
36553672
});

0 commit comments

Comments
 (0)
Please sign in to comment.