Skip to content

Commit

Permalink
Run bunx biome check --fix --unsafe .
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Feb 8, 2025
1 parent 15504ca commit 167e0a3
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 54 deletions.
4 changes: 2 additions & 2 deletions examples/rollup/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/sveltekit/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/vite/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/vite@svelte-5/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/webpack/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/webpack/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "node:path";
import carbonPreprocess from "carbon-preprocess-svelte";
// @ts-check
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import path from "node:path";
import carbonPreprocess from "carbon-preprocess-svelte";

const { optimizeImports, OptimizeCssPlugin } = carbonPreprocess;

Expand Down
4 changes: 2 additions & 2 deletions examples/webpack@svelte-5/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
import "carbon-components-svelte/css/g100.css";
import { DataTable } from "carbon-components-svelte";
</script>

<DataTable
Expand Down
4 changes: 2 additions & 2 deletions examples/webpack@svelte-5/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "node:path";
import carbonPreprocess from "carbon-preprocess-svelte";
// @ts-check
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import path from "node:path";
import carbonPreprocess from "carbon-preprocess-svelte";

const { optimizeImports, OptimizeCssPlugin } = carbonPreprocess;

Expand Down
21 changes: 9 additions & 12 deletions scripts/extract-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ExtractSelectorsProps = {
export function extractSelectors(props: ExtractSelectorsProps) {
const { code, filename } = props;
const ast = parse(code, { filename });
const selectors: Map<string, any> = new Map();
const selectors: Map<string, { type: string; name?: string }> = new Map();
const components: Set<string> = new Set();

walk(ast, {
Expand All @@ -31,12 +31,9 @@ export function extractSelectors(props: ExtractSelectorsProps) {
// class="{c} c1 c2 c3"
node.value?.map((value: ANode) => {
if (value.type === "Text") {
value.data
.split(/\s+/)
.filter(Boolean)
.forEach((selector: string) =>
selectors.set(selector, { type: node.type }),
);
for (const selector of value.data.split(/\s+/).filter(Boolean)) {
selectors.set(selector, { type: node.type });
}
}
});
}
Expand Down Expand Up @@ -72,23 +69,23 @@ export function extractSelectors(props: ExtractSelectorsProps) {
const classes: string[] = [];

// Iterate through all class attribute identifiers
Array.from(selectors).forEach((selector) => {
const [v = ""] = selector;
for (const [v = "", { type, name }] of selectors) {
const value = v.trim();

if (typeof v === "string") {
const value = v.trim();

if (value.startsWith("bx--") && !value.startsWith(".")) {
classes.push("." + value);
classes.push(`.${value}`);
} else {
if (value.startsWith(".")) {
classes.push(value);
} else {
classes.push("." + value);
classes.push(`.${value}`);
}
}
}
});
}

return {
/** Unique classes in the current component. */
Expand Down
9 changes: 5 additions & 4 deletions scripts/index-components.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import { Glob } from "bun";
import { walk } from "estree-walker";
import path from "node:path";
import { parse } from "svelte/compiler";
import { CarbonSvelte } from "../src/constants";
import { isSvelteFile } from "../src/utils";
Expand Down Expand Up @@ -81,9 +81,10 @@ for (const [parent, components] of sub_components.entries()) {
if (parent_map) {
const sub_classes = components.flatMap((component) => {
if (exports_map.has(component)) {
return exports_map.get(component)!.classes;
} else if (internal_components.has(component)) {
return internal_components.get(component)!.classes;
return exports_map.get(component)?.classes;
}
if (internal_components.has(component)) {
return internal_components.get(component)?.classes;
}

// Components that fall through here are icon components,
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum CarbonSvelte {
export enum CarbonSvelte {
Components = "carbon-components-svelte",
Icons = "carbon-icons-svelte",
Pictograms = "carbon-pictograms-svelte",
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/print-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const formatter = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 });

function toHumanReadableSize(size_in_kb: number) {
if (size_in_kb >= BITS_DENOM) {
return formatter.format(size_in_kb / BITS_DENOM) + " MB";
return `${formatter.format(size_in_kb / BITS_DENOM)} MB`;
}

return formatter.format(size_in_kb) + " kB";
return `${formatter.format(size_in_kb)} kB`;
}

function percentageDiff(a: number, b: number) {
return formatter.format(((a - b) / a) * 100) + "%";
return `${formatter.format(((a - b) / a) * 100)}%`;
}

function stringSizeInKB(str: string) {
Expand Down
28 changes: 20 additions & 8 deletions tests/OptimizeCssPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { Compiler } from "webpack";
import { CarbonSvelte } from "../src/constants";
import OptimizeCssPlugin from "../src/plugins/OptimizeCssPlugin";

// Mock webpack compiler and related types
const createMockCompiler = (options: any = {}) => {
const createMockCompiler = (
options: {
assets?: Record<string, unknown>;
fileDependencies?: string[];
} = {},
) => {
const { assets = {}, fileDependencies = [] } = options;

const compilation = {
Expand Down Expand Up @@ -46,21 +52,27 @@ const createMockCompiler = (options: any = {}) => {
};
};

const asCompiler = (mock: ReturnType<typeof createMockCompiler>): Compiler => {
return mock as unknown as Compiler;
};

describe("OptimizeCssPlugin", () => {
test("constructor sets default options correctly", () => {
const plugin = new OptimizeCssPlugin();
expect((plugin as any).options).toEqual({
// @ts-expect-error – options is private
expect(plugin.options).toEqual({
verbose: true,
preserveAllIBMFonts: false,
});
} as const);
});

test("constructor respects provided options", () => {
const plugin = new OptimizeCssPlugin({
verbose: false,
preserveAllIBMFonts: true,
});
expect((plugin as any).options).toEqual({
// @ts-expect-error – options is private
expect(plugin.options).toEqual({
verbose: false,
preserveAllIBMFonts: true,
});
Expand All @@ -73,7 +85,7 @@ describe("OptimizeCssPlugin", () => {
fileDependencies: ["regular-component.svelte"],
});

plugin.apply(mockCompiler as any);
plugin.apply(asCompiler(mockCompiler));

expect(mockCompiler.compilation.updateAsset).not.toHaveBeenCalled();
});
Expand All @@ -90,7 +102,7 @@ describe("OptimizeCssPlugin", () => {
fileDependencies: [carbonComponent],
});

plugin.apply(mockCompiler as any);
plugin.apply(asCompiler(mockCompiler));

expect(mockCompiler.compilation.updateAsset).toHaveBeenCalledWith(
"styles.css",
Expand All @@ -110,7 +122,7 @@ describe("OptimizeCssPlugin", () => {
fileDependencies: [carbonComponent],
});

plugin.apply(mockCompiler as any);
plugin.apply(asCompiler(mockCompiler));

expect(mockCompiler.compilation.updateAsset).toHaveBeenCalledWith(
"styles.css",
Expand All @@ -130,7 +142,7 @@ describe("OptimizeCssPlugin", () => {
fileDependencies: [carbonComponent],
});

plugin.apply(mockCompiler as any);
plugin.apply(asCompiler(mockCompiler));

expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
Expand Down
12 changes: 6 additions & 6 deletions tests/create-optimized-css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ a { color: blue }

test("preserves .bx--body class", () => {
const result = createOptimizedCss({
source: `.bx--body { margin: 0 } .bx--unused-class { color: red }`,
source: ".bx--body { margin: 0 } .bx--unused-class { color: red }",
ids: [],
});
expect(result).toEqual(`.bx--body { margin: 0 }`);
expect(result).toEqual(".bx--body { margin: 0 }");
});

test("handles complex selectors with Carbon classes", () => {
Expand All @@ -161,19 +161,19 @@ button.bx--btn.bx--btn--primary { color: white }`);

test("avoids false positives", () => {
const result = createOptimizedCss({
source: `.bx--btn, .bx--btn--primary, .bx--unused { color: white }`,
source: ".bx--btn, .bx--btn--primary, .bx--unused { color: white }",
ids: ["Button"],
});
expect(result).toEqual(
`.bx--btn, .bx--btn--primary, .bx--unused { color: white }`,
".bx--btn, .bx--btn--primary, .bx--unused { color: white }",
);
});

test("ignores non-Carbon prefixed rules", () => {
const result = createOptimizedCss({
source: `.custom-class { color: red }`,
source: ".custom-class { color: red }",
ids: ["Button"],
});
expect(result).toEqual(`.custom-class { color: red }`);
expect(result).toEqual(".custom-class { color: red }");
});
});
6 changes: 2 additions & 4 deletions tests/optimize-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import { Airplane } from "carbon-pictograms-svelte";
import { Airplane as Airplane2 } from "carbon-pictograms-svelte";
import Airplane3 from "carbon-pictograms-svelte/lib/Airplane.svelte";`,
}),
)
.toEqual(`import Accordion from "carbon-components-svelte/src/Accordion/Accordion.svelte";
).toEqual(`import Accordion from "carbon-components-svelte/src/Accordion/Accordion.svelte";
import AccordionItem from "carbon-components-svelte/src/Accordion/AccordionItem.svelte";
import Accordion2 from "carbon-components-svelte/src/Accordion/Accordion.svelte";
import breakpoints from "carbon-components-svelte/src/Breakpoint/breakpoints.js";
Expand Down Expand Up @@ -69,8 +68,7 @@ import Airplane3 from "carbon-pictograms-svelte/lib/Airplane.svelte";`);
import { Add, Download } from "carbon-icons-svelte";
import { Airplane, Analytics } from "carbon-pictograms-svelte";`,
}),
)
.toEqual(`import Accordion from "carbon-components-svelte/src/Accordion/Accordion.svelte";
).toEqual(`import Accordion from "carbon-components-svelte/src/Accordion/Accordion.svelte";
import AccordionItem from "carbon-components-svelte/src/Accordion/AccordionItem.svelte";
import bp from "carbon-components-svelte/src/Breakpoint/breakpoints.js";
import Add from "carbon-icons-svelte/lib/Add.svelte";
Expand Down

0 comments on commit 167e0a3

Please sign in to comment.