Skip to content

Commit

Permalink
fix: handle single nameFormat without array literal
Browse files Browse the repository at this point in the history
  • Loading branch information
skovy committed Sep 30, 2022
1 parent d97049e commit 46f82a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 16 additions & 0 deletions __tests__/sass/file-to-class-names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ describeAllImplementations((implementation) => {
"some-styles",
]);
});

test("it handles only a string", async () => {
const result = await fileToClassNames(
`${__dirname}/../dummy-styles/complex.scss`,
{
nameFormat: "snake",
implementation,
}
);

expect(result).toEqual([
"nested_another",
"nested_class",
"some_styles",
]);
});
});

describe("aliases", () => {
Expand Down
1 change: 1 addition & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const { _: patterns, ...rest } = yargs
.option("nameFormat", {
alias: "n",
array: true,
string: true,
choices: NAME_FORMATS,
describe: "The name format that should be used to transform class names.",
})
Expand Down
26 changes: 17 additions & 9 deletions lib/sass/file-to-class-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,28 @@ const transformersMap = {
snake: (className: ClassName) => snakeCase(className),
} as const;

type NameFormatsWithTransformer = keyof typeof transformersMap;
type NameFormatWithTransformer = keyof typeof transformersMap;
const NAME_FORMATS_WITH_TRANSFORMER = Object.keys(
transformersMap
) as NameFormatWithTransformer[];

export const NAME_FORMATS = Object.keys(transformersMap).concat(["all"]);
export const NAME_FORMATS = [...NAME_FORMATS_WITH_TRANSFORMER, "all"] as const;
export type NameFormat = typeof NAME_FORMATS[number];

export interface SASSOptions extends SASSImporterOptions {
additionalData?: string;
includePaths?: string[];
nameFormat?: (string | number)[];
nameFormat?: string | string[];
implementation: Implementations;
}
export const nameFormatDefault: NameFormatsWithTransformer = "camel";
export const nameFormatDefault: NameFormatWithTransformer = "camel";

export const fileToClassNames = async (
file: string,
{
additionalData,
includePaths = [],
nameFormat,
nameFormat: rawNameFormat,
implementation,
aliases,
aliasPrefixes,
Expand All @@ -47,10 +51,14 @@ export const fileToClassNames = async (
) => {
const { renderSync } = getImplementation(implementation);

let nameFormats: NameFormatsWithTransformer[] = nameFormat
? ((nameFormat.includes("all")
? NAME_FORMATS.filter((item) => item !== "all")
: nameFormat) as NameFormatsWithTransformer[])
const nameFormat = (
typeof rawNameFormat === "string" ? [rawNameFormat] : rawNameFormat
) as NameFormat[];

let nameFormats: NameFormatWithTransformer[] = nameFormat
? nameFormat.includes("all")
? NAME_FORMATS_WITH_TRANSFORMER
: (nameFormat as NameFormatWithTransformer[])
: [nameFormatDefault];

const data = fs.readFileSync(file).toString();
Expand Down

0 comments on commit 46f82a9

Please sign in to comment.