Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve Prettier config based on generated file #225

Merged
merged 1 commit into from
Mar 23, 2024
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
11 changes: 7 additions & 4 deletions __tests__/prettier/prettier.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { join } from "path";
import prettier from "prettier";
import { attemptPrettier } from "../../lib/prettier";
import { classNamesToTypeDefinitions } from "../../lib/typescript";

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

describe("attemptPrettier", () => {
it("should locate and apply prettier.format", async () => {
const output = await attemptPrettier(input);
const output = await attemptPrettier(file, input);

expect(prettier.format(input, { parser: "typescript" })).toMatch(output);
});
Expand All @@ -16,14 +18,15 @@ describe("attemptPrettier", () => {
const typeDefinition = await classNamesToTypeDefinitions({
banner: "",
classNames: ["nestedAnother", "nestedClass", "someStyles"],
file,
exportType: "default",
});

if (!typeDefinition) {
throw new Error("failed to collect typeDefinition");
}

const output = await attemptPrettier(typeDefinition);
const output = await attemptPrettier(file, typeDefinition);

expect(output).toMatchSnapshot();
});
Expand All @@ -37,7 +40,7 @@ describe("attemptPrettier - mock prettier", () => {
});

it("should fail to recognize prettier and return input", async () => {
const output = await attemptPrettier(input);
const output = await attemptPrettier(file, input);

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

it("should fail to resolve prettier and return input", async () => {
const output = await attemptPrettier(input);
const output = await attemptPrettier(file, input);

expect(input).toMatch(output);
});
Expand Down
20 changes: 18 additions & 2 deletions __tests__/typescript/class-names-to-type-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os from "os";
import { classNamesToTypeDefinitions, ExportType } from "../../lib/typescript";
import { join } from "path";
import { classNamesToTypeDefinitions } from "../../lib/typescript";

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

describe("classNamesToTypeDefinitions (without Prettier)", () => {
beforeEach(() => {
Expand All @@ -16,6 +18,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "named",
file,
});

expect(definition).toEqual(
Expand All @@ -28,6 +31,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
banner: "",
classNames: [],
exportType: "named",
file,
});

expect(definition).toBeNull();
Expand All @@ -38,6 +42,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
banner: "",
classNames: ["myClass", "if"],
exportType: "named",
file,
});

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

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

expect(definition).toEqual(
Expand All @@ -78,6 +85,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
banner: "",
classNames: [],
exportType: "default",
file,
});

expect(definition).toBeNull();
Expand All @@ -89,7 +97,9 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
const definition = await classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass"],
exportType: "invalid" as ExportType,
// @ts-expect-error -- invalid export type
exportType: "invalid",
file,
});

expect(definition).toBeNull();
Expand All @@ -103,6 +113,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
classNames: ["myClass", "yourClass"],
exportType: "default",
quoteType: "double",
file,
});

expect(definition).toEqual(
Expand All @@ -116,6 +127,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
classNames: ["myClass", "yourClass"],
exportType: "named",
quoteType: "double",
file,
});

expect(definition).toEqual(
Expand All @@ -131,6 +143,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
classNames: ["myClass", "yourClass"],
exportType: "default",
exportTypeName: "Classes",
file,
});

expect(definition).toEqual(
Expand All @@ -144,6 +157,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
classNames: ["myClass", "yourClass"],
exportType: "default",
exportTypeInterface: "IStyles",
file,
});

expect(definition).toEqual(
Expand All @@ -161,6 +175,7 @@ describe("classNamesToTypeDefinitions (without Prettier)", () => {
banner,
classNames: ["myClass", "yourClass"],
exportType: "default",
file,
});

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

expect(firstLine(definition!)).toBe(banner);
Expand Down
1 change: 1 addition & 0 deletions lib/core/list-different.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const checkFile = async (
const classNames = await fileToClassNames(file, options);
const typeDefinition = await classNamesToTypeDefinitions({
classNames: classNames,
file,
...options,
});

Expand Down
1 change: 1 addition & 0 deletions lib/core/write-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const writeFile = async (
const classNames = await fileToClassNames(file, options);
const typeDefinition = await classNamesToTypeDefinitions({
classNames,
file,
...options,
});

Expand Down
5 changes: 3 additions & 2 deletions lib/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ const isPrettier = (t: unknown): t is Prettier =>
* Try to load prettier and config from project to format input,
* fall back to input if prettier is not found or failed
*
* @param {file} file
* @param {string} input
*/
export const attemptPrettier = async (input: string) => {
export const attemptPrettier = async (file: string, input: string) => {
if (!canResolvePrettier()) {
return input;
}
Expand All @@ -35,7 +36,7 @@ export const attemptPrettier = async (input: string) => {
}

try {
const config = await prettier.resolveConfig(process.cwd(), {
const config = await prettier.resolveConfig(file, {
editorconfig: true,
});
// try to return formatted output
Expand Down
3 changes: 2 additions & 1 deletion lib/typescript/class-names-to-type-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const QUOTE_TYPES: QuoteType[] = ["single", "double"];
export interface TypeDefinitionOptions {
banner: string;
classNames: ClassName[];
file: string;
exportType: ExportType;
exportTypeName?: string;
exportTypeInterface?: string;
Expand Down Expand Up @@ -95,7 +96,7 @@ export const classNamesToTypeDefinitions = async (

if (lines.length) {
const typeDefinition = lines.join(`${os.EOL}`) + `${os.EOL}`;
return await attemptPrettier(typeDefinition);
return await attemptPrettier(options.file, typeDefinition);
} else {
return null;
}
Expand Down
Loading