Skip to content

Commit c60f74e

Browse files
gmathieuskovy
authored andcommitted
fix: resolve Prettier config based on generated file
1 parent 546dba6 commit c60f74e

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

__tests__/prettier/prettier.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import { join } from "path";
12
import prettier from "prettier";
23
import { attemptPrettier } from "../../lib/prettier";
34
import { classNamesToTypeDefinitions } from "../../lib/typescript";
45

6+
const file = join(__dirname, "test.d.ts");
57
const input =
68
"export type Styles = {'myClass': string;'yourClass': string;}; export type Classes = keyof Styles; declare const styles: Styles; export default styles;";
79

810
describe("attemptPrettier", () => {
911
it("should locate and apply prettier.format", async () => {
10-
const output = await attemptPrettier(input);
12+
const output = await attemptPrettier(file, input);
1113

1214
expect(prettier.format(input, { parser: "typescript" })).toMatch(output);
1315
});
@@ -16,14 +18,15 @@ describe("attemptPrettier", () => {
1618
const typeDefinition = await classNamesToTypeDefinitions({
1719
banner: "",
1820
classNames: ["nestedAnother", "nestedClass", "someStyles"],
21+
file,
1922
exportType: "default",
2023
});
2124

2225
if (!typeDefinition) {
2326
throw new Error("failed to collect typeDefinition");
2427
}
2528

26-
const output = await attemptPrettier(typeDefinition);
29+
const output = await attemptPrettier(file, typeDefinition);
2730

2831
expect(output).toMatchSnapshot();
2932
});
@@ -37,7 +40,7 @@ describe("attemptPrettier - mock prettier", () => {
3740
});
3841

3942
it("should fail to recognize prettier and return input", async () => {
40-
const output = await attemptPrettier(input);
43+
const output = await attemptPrettier(file, input);
4144

4245
expect(input).toMatch(output);
4346
});
@@ -49,7 +52,7 @@ describe("attemptPrettier - mock resolution check", () => {
4952
});
5053

5154
it("should fail to resolve prettier and return input", async () => {
52-
const output = await attemptPrettier(input);
55+
const output = await attemptPrettier(file, input);
5356

5457
expect(input).toMatch(output);
5558
});

__tests__/typescript/class-names-to-type-definitions.test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os from "os";
2-
import { classNamesToTypeDefinitions, ExportType } from "../../lib/typescript";
2+
import { join } from "path";
3+
import { classNamesToTypeDefinitions } from "../../lib/typescript";
34

45
jest.mock("../../lib/prettier/can-resolve", () => ({
56
canResolvePrettier: () => false,
67
}));
8+
const file = join(__dirname, "test.d.ts");
79

810
describe("classNamesToTypeDefinitions (without Prettier)", () => {
911
beforeEach(() => {
@@ -16,6 +18,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
1618
banner: "",
1719
classNames: ["myClass", "yourClass"],
1820
exportType: "named",
21+
file,
1922
});
2023

2124
expect(definition).toEqual(
@@ -28,6 +31,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
2831
banner: "",
2932
classNames: [],
3033
exportType: "named",
34+
file,
3135
});
3236

3337
expect(definition).toBeNull();
@@ -38,6 +42,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
3842
banner: "",
3943
classNames: ["myClass", "if"],
4044
exportType: "named",
45+
file,
4146
});
4247

4348
expect(definition).toEqual("export declare const myClass: string;\n");
@@ -51,6 +56,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
5156
banner: "",
5257
classNames: ["myClass", "invalid-variable"],
5358
exportType: "named",
59+
file,
5460
});
5561

5662
expect(definition).toEqual("export declare const myClass: string;\n");
@@ -66,6 +72,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
6672
banner: "",
6773
classNames: ["myClass", "yourClass"],
6874
exportType: "default",
75+
file,
6976
});
7077

7178
expect(definition).toEqual(
@@ -78,6 +85,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
7885
banner: "",
7986
classNames: [],
8087
exportType: "default",
88+
file,
8189
});
8290

8391
expect(definition).toBeNull();
@@ -89,7 +97,9 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
8997
const definition = await classNamesToTypeDefinitions({
9098
banner: "",
9199
classNames: ["myClass"],
92-
exportType: "invalid" as ExportType,
100+
// @ts-expect-error -- invalid export type
101+
exportType: "invalid",
102+
file,
93103
});
94104

95105
expect(definition).toBeNull();
@@ -103,6 +113,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
103113
classNames: ["myClass", "yourClass"],
104114
exportType: "default",
105115
quoteType: "double",
116+
file,
106117
});
107118

108119
expect(definition).toEqual(
@@ -116,6 +127,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
116127
classNames: ["myClass", "yourClass"],
117128
exportType: "named",
118129
quoteType: "double",
130+
file,
119131
});
120132

121133
expect(definition).toEqual(
@@ -131,6 +143,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
131143
classNames: ["myClass", "yourClass"],
132144
exportType: "default",
133145
exportTypeName: "Classes",
146+
file,
134147
});
135148

136149
expect(definition).toEqual(
@@ -144,6 +157,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
144157
classNames: ["myClass", "yourClass"],
145158
exportType: "default",
146159
exportTypeInterface: "IStyles",
160+
file,
147161
});
148162

149163
expect(definition).toEqual(
@@ -161,6 +175,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
161175
banner,
162176
classNames: ["myClass", "yourClass"],
163177
exportType: "default",
178+
file,
164179
});
165180

166181
expect(firstLine(definition!)).toBe(banner);
@@ -172,6 +187,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
172187
banner,
173188
classNames: ["myClass", "yourClass"],
174189
exportType: "named",
190+
file,
175191
});
176192

177193
expect(firstLine(definition!)).toBe(banner);

lib/core/list-different.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const checkFile = async (
3131
const classNames = await fileToClassNames(file, options);
3232
const typeDefinition = await classNamesToTypeDefinitions({
3333
classNames: classNames,
34+
file,
3435
...options,
3536
});
3637

lib/core/write-file.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const writeFile = async (
2424
const classNames = await fileToClassNames(file, options);
2525
const typeDefinition = await classNamesToTypeDefinitions({
2626
classNames,
27+
file,
2728
...options,
2829
});
2930

lib/prettier/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const isPrettier = (t: unknown): t is Prettier =>
2020
* Try to load prettier and config from project to format input,
2121
* fall back to input if prettier is not found or failed
2222
*
23+
* @param {file} file
2324
* @param {string} input
2425
*/
25-
export const attemptPrettier = async (input: string) => {
26+
export const attemptPrettier = async (file: string, input: string) => {
2627
if (!canResolvePrettier()) {
2728
return input;
2829
}
@@ -35,7 +36,7 @@ export const attemptPrettier = async (input: string) => {
3536
}
3637

3738
try {
38-
const config = await prettier.resolveConfig(process.cwd(), {
39+
const config = await prettier.resolveConfig(file, {
3940
editorconfig: true,
4041
});
4142
// try to return formatted output

lib/typescript/class-names-to-type-definition.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const QUOTE_TYPES: QuoteType[] = ["single", "double"];
1313
export interface TypeDefinitionOptions {
1414
banner: string;
1515
classNames: ClassName[];
16+
file: string;
1617
exportType: ExportType;
1718
exportTypeName?: string;
1819
exportTypeInterface?: string;
@@ -95,7 +96,7 @@ export const classNamesToTypeDefinitions = async (
9596

9697
if (lines.length) {
9798
const typeDefinition = lines.join(`${os.EOL}`) + `${os.EOL}`;
98-
return await attemptPrettier(typeDefinition);
99+
return await attemptPrettier(options.file, typeDefinition);
99100
} else {
100101
return null;
101102
}

0 commit comments

Comments
 (0)