Skip to content

Commit

Permalink
Merge pull request #78 from mizdra/handle-edge-cases
Browse files Browse the repository at this point in the history
Add test for edge cases
  • Loading branch information
mizdra authored Feb 3, 2025
2 parents 64b025f + 3e17cf9 commit 52855cb
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
56 changes: 56 additions & 0 deletions packages/codegen/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,59 @@ test('reports system error', async () => {
"
`);
});

test('generates .d.ts with circular import', async () => {
const iff = await createIFF({
'src/a.module.css': dedent`
@import './b.module.css';
.a1 { color: red; }
`,
'src/b.module.css': dedent`
@import './a.module.css';
.b1 { color: red; }
`,
'src/c.module.css': dedent`
@import './c.module.css';
.c1 { color: red; }
`,
'hcm.config.mjs': dedent`
export default {
pattern: 'src/**/*.module.css',
dtsOutDir: 'dist',
};
`,
});
const hcm = spawnSync('node', [binPath], {
cwd: iff.rootDir,
// MEMO: Suppress ExperimentalWarning output from `fs.promises.glob` and `path.matchesGlob`
// TODO: Remove `--no-warnings=ExperimentalWarning`
env: { ...process.env, NODE_OPTIONS: '--no-warnings=ExperimentalWarning' },
});
expect(hcm.error).toBeUndefined();
expect(hcm.stderr.toString()).toBe('');
expect(hcm.status).toBe(0);
expect(await readFile(iff.join('dist/src/a.module.css.d.ts'), 'utf-8')).toMatchInlineSnapshot(`
"declare const styles = {
a1: '' as readonly string,
...(await import('./b.module.css')).default,
};
export default styles;
"
`);
expect(await readFile(iff.join('dist/src/b.module.css.d.ts'), 'utf-8')).toMatchInlineSnapshot(`
"declare const styles = {
b1: '' as readonly string,
...(await import('./a.module.css')).default,
};
export default styles;
"
`);
expect(await readFile(iff.join('dist/src/c.module.css.d.ts'), 'utf-8')).toMatchInlineSnapshot(`
"declare const styles = {
c1: '' as readonly string,
...(await import('./c.module.css')).default,
};
export default styles;
"
`);
});
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {
type SyntacticDiagnostic,
type DiagnosticCategory,
type DiagnosticPosition,
} from './parser/diagnostic.js';
} from './diagnostic.js';
export { type CreateDtsOptions, createDts, STYLES_EXPORT_NAME } from './dts-creator.js';
export { createResolver, type Resolver } from './resolver.js';
export { type IsExternalFile, createIsExternalFile } from './external-file.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parser/at-value-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AtRule } from 'postcss';
import type { DiagnosticPosition, SyntacticDiagnostic } from './diagnostic.js';
import type { DiagnosticPosition, SyntacticDiagnostic } from '../diagnostic.js';
import type { Location } from './location.js';

interface ValueDeclaration {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/parser/css-module-parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AtRule, Node, Root, Rule } from 'postcss';
import { CssSyntaxError, parse } from 'postcss';
import safeParser from 'postcss-safe-parser';
import type { SyntacticDiagnostic } from '../diagnostic.js';
import { parseAtImport } from './at-import-parser.js';
import { parseAtValue } from './at-value-parser.js';
import type { SyntacticDiagnostic } from './diagnostic.js';
import { type Location } from './location.js';
import { parseRule } from './rule-parser.js';

Expand Down Expand Up @@ -76,8 +76,8 @@ export interface AtImportTokenImporter {
type: 'import';
/**
* The specifier of the file from which the token is imported.
* This is a string before being resolved and surrounded by quotes.
* @example `@import './a.module.css'` would have `from` as `"'./a.module.css'"`.
* This is a string before being resolved and unquoted.
* @example `@import './a.module.css'` would have `from` as `'./a.module.css'`.
*/
from: string;
/** The location of the `from` in *.module.css file. */
Expand All @@ -91,8 +91,8 @@ export interface AtValueTokenImporter {
values: AtValueTokenImporterValue[];
/**
* The specifier of the file from which the token is imported.
* This is a string before being resolved and surrounded by quotes.
* @example `@value a from './a.module.css'` would have `from` as `"'./a.module.css'"`.
* This is a string before being resolved and unquoted.
* @example `@value a from './a.module.css'` would have `from` as `'./a.module.css'`.
*/
from: string;
/** The location of the `from` in *.module.css file. */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parser/location.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dedent from 'dedent';
import selectorParser from 'postcss-selector-parser';
import { describe, expect, test } from 'vitest';
import type { DiagnosticPosition } from '../diagnostic.js';
import { createRoot, createRules } from '../test/ast.js';
import type { DiagnosticPosition } from './diagnostic.js';
import { calcDiagnosticsLocationForSelectorParserNode } from './location.js';

function calcLocations(source: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parser/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Rule } from 'postcss';
import type selectorParser from 'postcss-selector-parser';
import type { DiagnosticPosition } from './diagnostic.js';
import type { DiagnosticPosition } from '../diagnostic.js';

export interface Position {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parser/rule-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Rule } from 'postcss';
import selectorParser from 'postcss-selector-parser';
import type { SyntacticDiagnostic } from './diagnostic.js';
import type { SyntacticDiagnostic } from '../diagnostic.js';
import { calcDiagnosticsLocationForSelectorParserNode, type Location } from './location.js';

interface CollectResult {
Expand Down

0 comments on commit 52855cb

Please sign in to comment.