Skip to content

Commit

Permalink
collect location
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Jun 15, 2024
1 parent cd4871c commit 0a8b19c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
45 changes: 45 additions & 0 deletions packages/happy-css-modules/src/locator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,51 @@ test('does not track other files by `composes`', async () => {
`);
});

test('tracks other files when `@value` is present', async () => {
createFixtures({
'/test/1.css': dedent`
@value a from './2.css';
@value b from '3.css';
@value c from '${getFixturePath('/test/4.css')}';
`,
'/test/2.css': dedent`
@value a: 1;
`,
'/test/3.css': dedent`
@value b: 2;
`,
'/test/4.css': dedent`
@value c: 3;
`,
});
const result = await locator.load(getFixturePath('/test/1.css'));
expect(result).toMatchInlineSnapshot(`
{
dependencies: ["<fixtures>/test/2.css", "<fixtures>/test/3.css", "<fixtures>/test/4.css"],
tokens: [
{
name: "a",
originalLocations: [
{ filePath: "<fixtures>/test/2.css", start: { line: 1, column: 8 }, end: { line: 1, column: 9 } },
],
},
{
name: "b",
originalLocations: [
{ filePath: "<fixtures>/test/3.css", start: { line: 1, column: 8 }, end: { line: 1, column: 9 } },
],
},
{
name: "c",
originalLocations: [
{ filePath: "<fixtures>/test/4.css", start: { line: 1, column: 8 }, end: { line: 1, column: 9 } },
],
},
],
}
`);
});

test('normalizes tokens', async () => {
createFixtures({
'/test/1.css': dedent`
Expand Down
36 changes: 28 additions & 8 deletions packages/happy-css-modules/src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { createDefaultTransformer, type Transformer } from '../transformer/index
import { unique, uniqueBy } from '../util.js';
import {
getOriginalLocationOfClassSelector,
getOriginalLocationOfAtValue,
generateLocalTokenNames,
parseAtImport,
type Location,
collectNodes,
parseAtValue,
} from './postcss.js';

export { collectNodes, type Location } from './postcss.js';
Expand All @@ -26,6 +28,8 @@ function isIgnoredSpecifier(specifier: string): boolean {
export type Token = {
/** The token name. */
name: string;
/** The name of the imported token. */
importedName?: string;
/** The original locations of the token in the source file. */
originalLocations: Location[];
};
Expand Down Expand Up @@ -195,14 +199,30 @@ export class Locator {
}

for (const atValue of atValues) {
const name = atValue.params.slice(0, atValue.params.indexOf(':'));
if (!localTokenNames.includes(name)) continue;

tokens.push({
name,
// TODO: `getOriginalLocation` expects a `ClassName`.
originalLocations: [],
});
const parsedAtValue = parseAtValue(atValue);

if (parsedAtValue.type === 'valueDeclaration') {
tokens.push({
name: parsedAtValue.tokenName,
originalLocations: [getOriginalLocationOfAtValue(atValue, parsedAtValue)],
});
} else if (parsedAtValue.type === 'valueImportDeclaration') {
if (isIgnoredSpecifier(parsedAtValue.from)) continue;
// eslint-disable-next-line no-await-in-loop
const from = await this.resolver(parsedAtValue.from, { request: filePath });
// eslint-disable-next-line no-await-in-loop
const result = await this._load(from);
dependencies.push(from, ...result.dependencies);
for (const token of result.tokens) {
const matchedImport = parsedAtValue.imports.find((i) => i.importedTokenName === token.name);
if (!matchedImport) continue;
tokens.push({
name: matchedImport.localTokenName,
importedName: matchedImport.importedTokenName,
originalLocations: token.originalLocations,
});
}
}
}

const result: LoadResult = {
Expand Down

0 comments on commit 0a8b19c

Please sign in to comment.