Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/salty-hornets-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@css-modules-kit/core': minor
'@css-modules-kit/ts-plugin': patch
---

refactor: consolidate `checkCSSModule` arguments into `CheckerArgs`
8 changes: 7 additions & 1 deletion packages/codegen/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ export function createProject(args: ProjectArgs): Project {
for (const cssModule of cssModuleMap.values()) {
let diagnostics = semanticDiagnosticsMap.get(cssModule.fileName);
if (!diagnostics) {
diagnostics = checkCSSModule(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
diagnostics = checkCSSModule(cssModule, {
config,
getExportRecord: (m) => exportBuilder.build(m),
matchesPattern,
resolver,
getCSSModule,
});
semanticDiagnosticsMap.set(cssModule.fileName, diagnostics);
}
allDiagnostics.push(...diagnostics);
Expand Down
37 changes: 16 additions & 21 deletions packages/core/src/checker.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dedent from 'dedent';
import { describe, expect, test } from 'vitest';
import type { CheckerArgs } from './checker.js';
import { checkCSSModule } from './checker.js';
import { createExportBuilder } from './export-builder.js';
import { createResolver } from './resolver.js';
Expand All @@ -14,29 +15,23 @@ const matchesPattern = (path: string) => path.endsWith('.module.css');

type Checker = (cssModule: CSSModule) => ReturnType<typeof checkCSSModule>;

interface PrepareCheckerOptions {
config?: ReturnType<typeof fakeConfig>;
resolver?: typeof resolver;
matchesPattern?: typeof matchesPattern;
}

function prepareChecker(options?: PrepareCheckerOptions): Checker {
const config = options?.config ?? fakeConfig();
const resolverFn = options?.resolver ?? resolver;
const matchesPatternFn = options?.matchesPattern ?? matchesPattern;
function prepareChecker(args?: Partial<CheckerArgs>): Checker {
const config = args?.config ?? fakeConfig();
const resolverFn = args?.resolver ?? resolver;
const matchesPatternFn = args?.matchesPattern ?? matchesPattern;
const exportBuilder = createExportBuilder({
getCSSModule: readAndParseCSSModule,
matchesPattern: matchesPatternFn,
resolver: resolverFn,
});
return (cssModule: CSSModule) => {
return checkCSSModule(
cssModule,
return checkCSSModule(cssModule, {
config,
createExportBuilder({
getCSSModule: readAndParseCSSModule,
matchesPattern: matchesPatternFn,
resolver: resolverFn,
}),
matchesPatternFn,
resolverFn,
readAndParseCSSModule,
);
getExportRecord: (m) => exportBuilder.build(m),
matchesPattern: matchesPatternFn,
resolver: resolverFn,
getCSSModule: readAndParseCSSModule,
});
};
}

Expand Down
30 changes: 16 additions & 14 deletions packages/core/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import type {
AtValueTokenImporterValue,
CSSModule,
Diagnostic,
ExportBuilder,
ExportRecord,
Location,
MatchesPattern,
Resolver,
TokenImporter,
} from './type.js';
import { isValidAsJSIdentifier } from './util.js';

// eslint-disable-next-line max-params, complexity
export function checkCSSModule(
cssModule: CSSModule,
config: CMKConfig,
exportBuilder: ExportBuilder,
matchesPattern: MatchesPattern,
resolver: Resolver,
getCSSModule: (path: string) => CSSModule | undefined,
): Diagnostic[] {
export interface CheckerArgs {
config: CMKConfig;
getExportRecord: (cssModule: CSSModule) => ExportRecord;
matchesPattern: MatchesPattern;
resolver: Resolver;
getCSSModule: (path: string) => CSSModule | undefined;
}

// eslint-disable-next-line complexity
export function checkCSSModule(cssModule: CSSModule, args: CheckerArgs): Diagnostic[] {
const { config } = args;
const diagnostics: Diagnostic[] = [];

for (const token of cssModule.localTokens) {
Expand All @@ -37,16 +39,16 @@ export function checkCSSModule(
}

for (const tokenImporter of cssModule.tokenImporters) {
const from = resolver(tokenImporter.from, { request: cssModule.fileName });
if (!from || !matchesPattern(from)) continue;
const imported = getCSSModule(from);
const from = args.resolver(tokenImporter.from, { request: cssModule.fileName });
if (!from || !args.matchesPattern(from)) continue;
const imported = args.getCSSModule(from);
if (!imported) {
diagnostics.push(createCannotImportModuleDiagnostic(cssModule, tokenImporter));
continue;
}

if (tokenImporter.type === 'value') {
const exportRecord = exportBuilder.build(imported);
const exportRecord = args.getExportRecord(imported);
for (const value of tokenImporter.values) {
if (!exportRecord.allTokens.includes(value.name)) {
diagnostics.push(createModuleHasNoExportedTokenDiagnostic(cssModule, tokenImporter, value));
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
type Resolver,
type MatchesPattern,
type ExportBuilder,
type ExportRecord,
type DiagnosticSourceFile,
type Diagnostic,
type DiagnosticWithLocation,
Expand All @@ -33,7 +34,7 @@ export {
createMatchesPattern,
getFileNamesByPattern,
} from './file.js';
export { checkCSSModule } from './checker.js';
export { checkCSSModule, type CheckerArgs } from './checker.js';
export { createExportBuilder } from './export-builder.js';
export { join, resolve, relative, dirname, basename, parse, isAbsolute } from './path.js';
export { findUsedTokenNames } from './util.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export function getSemanticDiagnostics(
// Clear cache to update export records for all files
exportBuilder.clearCache();

const diagnostics = checkCSSModule(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
const diagnostics = checkCSSModule(cssModule, {
config,
getExportRecord: (m) => exportBuilder.build(m),
matchesPattern,
resolver,
getCSSModule,
});
const sourceFile = languageService.getProgram()!.getSourceFile(fileName)!;
const tsDiagnostics = diagnostics.map((diagnostic) => convertDiagnostic(diagnostic, () => sourceFile));
prior.push(...tsDiagnostics);
Expand Down