Skip to content

Commit

Permalink
Merge pull request #265 from mizdra/token-has-only-one-original-location
Browse files Browse the repository at this point in the history
Token has only one `originalLocation`
  • Loading branch information
mizdra authored Jun 15, 2024
2 parents 54f5611 + b13166e commit 85ecbb5
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 173 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,19 @@ assert.deepEqual(result, {
tokens: [
{
name: 'b',
originalLocations: [
{
filePath: '/Users/mizdra/src/github.com/mizdra/packages/example/02-import/3.css',
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
],
originalLocation: {
filePath: '/Users/mizdra/src/github.com/mizdra/packages/example/02-import/3.css',
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: 'a',
originalLocations: [
{
filePath: '/Users/mizdra/src/github.com/mizdra/packages/example/02-import/2.css',
start: { line: 3, column: 1 },
end: { line: 3, column: 2 },
},
],
originalLocation: {
filePath: '/Users/mizdra/src/github.com/mizdra/packages/example/02-import/2.css',
start: { line: 3, column: 1 },
end: { line: 3, column: 2 },
},
},
],
});
Expand Down
65 changes: 32 additions & 33 deletions packages/happy-css-modules/src/emitter/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,40 @@ function generateTokenDeclarations(
// This is due to the sourcemap specification. Therefore, we output multiple type definitions
// with the same name and assign a separate original position to each.

for (let originalLocation of token.originalLocations) {
if (originalLocation.filePath === undefined) {
// If the original location is not specified, fallback to the source file.
originalLocation = {
filePath,
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
};
}
let originalLocation = token.originalLocation;
if (originalLocation.filePath === undefined) {
// If the original location is not specified, fallback to the source file.
originalLocation = {
filePath,
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
};
}

result.push(
originalLocation.filePath === filePath || isExternalFile(originalLocation.filePath)
? new SourceNode(null, null, null, [
'& Readonly<{ ',
new SourceNode(
originalLocation.start.line ?? null,
// The SourceNode's column is 0-based, but the originalLocation's column is 1-based.
originalLocation.start.column - 1 ?? null,
getRelativePath(sourceMapFilePath, originalLocation.filePath),
`"${token.name}"`,
token.name,
),
': string }>',
])
: // Imported tokens in non-external files are typed by dynamic import.
// See https://github.com/mizdra/happy-css-modules/issues/106.
new SourceNode(null, null, null, [
'& Readonly<Pick<(typeof import(',
`"${getRelativePath(filePath, originalLocation.filePath)}"`,
'))["default"], ',
result.push(
originalLocation.filePath === filePath || isExternalFile(originalLocation.filePath)
? new SourceNode(null, null, null, [
'& Readonly<{ ',
new SourceNode(
originalLocation.start.line ?? null,
// The SourceNode's column is 0-based, but the originalLocation's column is 1-based.
originalLocation.start.column - 1 ?? null,
getRelativePath(sourceMapFilePath, originalLocation.filePath),
`"${token.name}"`,
'>>',
]),
);
}
token.name,
),
': string }>',
])
: // Imported tokens in non-external files are typed by dynamic import.
// See https://github.com/mizdra/happy-css-modules/issues/106.
new SourceNode(null, null, null, [
'& Readonly<Pick<(typeof import(',
`"${getRelativePath(filePath, originalLocation.filePath)}"`,
'))["default"], ',
`"${token.name}"`,
'>>',
]),
);
}
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/happy-css-modules/src/emitter/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('isSubDirectoryFile', () => {
describe('emitGeneratedFiles', () => {
const defaultArgs = {
filePath: getFixturePath('/test/1.css'),
tokens: [fakeToken({ name: 'foo', originalLocations: [{ start: { line: 1, column: 1 } }] })],
tokens: [fakeToken({ name: 'foo', originalLocation: { start: { line: 1, column: 1 } } })],
emitDeclarationMap: true,
dtsFormatOptions: undefined,
cwd: getFixturePath('/test'),
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('emitGeneratedFiles', () => {
expect(await exists(getFixturePath('/test/1.css.d.ts.map'))).toBeFalsy();
});
test('skips writing to disk if the generated files are the same', async () => {
const tokens1 = [fakeToken({ name: 'foo', originalLocations: [{ start: { line: 1, column: 1 } }] })];
const tokens1 = [fakeToken({ name: 'foo', originalLocation: { start: { line: 1, column: 1 } } })];
await emitGeneratedFiles({ ...defaultArgs, tokens: tokens1 });
const mtimeForDts1 = (await stat(getFixturePath('/test/1.css.d.ts'))).mtime;
const mtimeForSourceMap1 = (await stat(getFixturePath('/test/1.css.d.ts.map'))).mtime;
Expand All @@ -68,7 +68,7 @@ describe('emitGeneratedFiles', () => {
expect(mtimeForSourceMap1).toEqual(mtimeForSourceMap2); // skipped

await waitForAsyncTask(1); // so that mtime changes.
const tokens2 = [fakeToken({ name: 'bar', originalLocations: [{ start: { line: 1, column: 1 } }] })];
const tokens2 = [fakeToken({ name: 'bar', originalLocation: { start: { line: 1, column: 1 } } })];
await emitGeneratedFiles({ ...defaultArgs, tokens: tokens2 });
const mtimeForDts3 = (await stat(getFixturePath('/test/1.css.d.ts'))).mtime;
const mtimeForSourceMap3 = (await stat(getFixturePath('/test/1.css.d.ts.map'))).mtime;
Expand Down
140 changes: 92 additions & 48 deletions packages/happy-css-modules/src/locator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ test('basic', async () => {
tokens: [
{
name: "a",
originalLocations: [
{ filePath: "<fixtures>/test/1.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: "b",
originalLocations: [
{ filePath: "<fixtures>/test/1.css", start: { line: 2, column: 1 }, end: { line: 2, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 2, column: 1 },
end: { line: 2, column: 2 },
},
},
],
}
Expand Down Expand Up @@ -74,27 +78,35 @@ test('tracks other files when `@import` is present', async () => {
tokens: [
{
name: "a",
originalLocations: [
{ filePath: "<fixtures>/test/2.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/2.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: "b",
originalLocations: [
{ filePath: "<fixtures>/test/3.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/3.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: "c",
originalLocations: [
{ filePath: "<fixtures>/test/4.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/4.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: "d",
originalLocations: [
{ filePath: "<fixtures>/test/5-recursive.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/5-recursive.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
],
}
Expand All @@ -120,16 +132,18 @@ test('does not track other files by `composes`', async () => {
tokens: [
{
name: "a",
originalLocations: [
{ filePath: "<fixtures>/test/1.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
],
}
`);
});

test('normalizes tokens', async () => {
test('unique tokens', async () => {
createFixtures({
'/test/1.css': dedent`
/* duplicate import */
Expand All @@ -142,9 +156,6 @@ test('normalizes tokens', async () => {
.a {} /* class selector that duplicates the import source */
.b {}
`,
'/test/3.css': dedent`
.c {}
`,
});
const result = await locator.load(getFixturePath('/test/1.css'));
expect(result).toMatchInlineSnapshot(`
Expand All @@ -153,17 +164,35 @@ test('normalizes tokens', async () => {
tokens: [
{
name: "a",
originalLocations: [
{ filePath: "<fixtures>/test/2.css", start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
{ filePath: "<fixtures>/test/1.css", start: { line: 4, column: 1 }, end: { line: 4, column: 2 } },
{ filePath: "<fixtures>/test/1.css", start: { line: 5, column: 1 }, end: { line: 5, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/2.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
},
{
name: "b",
originalLocations: [
{ filePath: "<fixtures>/test/2.css", start: { line: 2, column: 1 }, end: { line: 2, column: 2 } },
],
originalLocation: {
filePath: "<fixtures>/test/2.css",
start: { line: 2, column: 1 },
end: { line: 2, column: 2 },
},
},
{
name: "a",
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 4, column: 1 },
end: { line: 4, column: 2 },
},
},
{
name: "a",
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 5, column: 1 },
end: { line: 5, column: 2 },
},
},
],
}
Expand Down Expand Up @@ -233,16 +262,27 @@ describe('supports sourcemap', () => {
tokens: [
{
name: "nesting",
originalLocations: [
{ filePath: "<fixtures>/test/1.scss", start: { line: 1, column: 1 }, end: { line: 1, column: 8 } },
{ filePath: "<fixtures>/test/1.scss", start: { line: 3, column: 3 }, end: { line: 3, column: 10 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.scss",
start: { line: 1, column: 1 },
end: { line: 1, column: 8 },
},
},
{
name: "nesting",
originalLocation: {
filePath: "<fixtures>/test/1.scss",
start: { line: 3, column: 3 },
end: { line: 3, column: 10 },
},
},
{
name: "nesting_child",
originalLocations: [
{ filePath: "<fixtures>/test/1.scss", start: { line: 3, column: 3 }, end: { line: 3, column: 16 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.scss",
start: { line: 3, column: 3 },
end: { line: 3, column: 16 },
},
},
],
}
Expand Down Expand Up @@ -272,18 +312,22 @@ describe('supports sourcemap', () => {
tokens: [
{
name: "selector_list_a_1",
originalLocations: [
{ filePath: "<fixtures>/test/1.css", start: { line: 1, column: 1 }, end: { line: 1, column: 18 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 18 },
},
},
{
name: "selector_list_a_2",
originalLocations: [
{ filePath: "<fixtures>/test/1.css", start: { line: 1, column: 1 }, end: { line: 1, column: 18 } },
],
originalLocation: {
filePath: "<fixtures>/test/1.css",
start: { line: 1, column: 1 },
end: { line: 1, column: 18 },
},
},
{ name: "selector_list_b_1", originalLocations: [{}] },
{ name: "selector_list_b_2", originalLocations: [{}] },
{ name: "selector_list_b_1", originalLocation: {} },
{ name: "selector_list_b_2", originalLocation: {} },
],
}
`);
Expand Down
Loading

0 comments on commit 85ecbb5

Please sign in to comment.