Skip to content

Commit

Permalink
Merge pull request #252 from mizdra/remove-test-cases-for-composes
Browse files Browse the repository at this point in the history
Remove test cases for `composes`
  • Loading branch information
mizdra authored May 22, 2024
2 parents 6e41547 + 8d762c5 commit 258cd4e
Show file tree
Hide file tree
Showing 17 changed files with 13 additions and 134 deletions.
5 changes: 0 additions & 5 deletions packages/example/01-basic/1.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,3 @@
}
:local(.local_class_name_4) {
}
.composes_target {
}
.composes {
composes: composes_target;
}
2 changes: 0 additions & 2 deletions packages/example/01-basic/1.css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ declare const styles:
& Readonly<{ "local_class_name_2": string }>
& Readonly<{ "local_class_name_3": string }>
& Readonly<{ "local_class_name_4": string }>
& Readonly<{ "composes_target": string }>
& Readonly<{ "composes": string }>
;
export default styles;
//# sourceMappingURL=./1.css.d.ts.map
2 changes: 1 addition & 1 deletion packages/example/01-basic/1.css.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/example/03-composes/2.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.b {
composes: c from './3.css';
}
2 changes: 0 additions & 2 deletions packages/example/03-composes/3.css

This file was deleted.

5 changes: 0 additions & 5 deletions packages/example/03-composes/3.css.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/example/03-composes/3.css.d.ts.map

This file was deleted.

2 changes: 0 additions & 2 deletions packages/example/04-sass/1.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@
&_2 {
dummy: '';
}
composes: a_1; // css module feature test (composes)
composes: d from './4.scss'; // css module feature test (composes from other file)
}
2 changes: 0 additions & 2 deletions packages/example/05-less/1.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@
}
.b_1();
.b_2();
composes: a_1; // css module feature test (composes)
composes: c from './3.less'; // css module feature test (composes from other file)
}
2 changes: 0 additions & 2 deletions packages/example/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ console.log(styles1.local_class_name_1);
console.log(styles1.local_class_name_2);
console.log(styles1.local_class_name_3);
console.log(styles1.local_class_name_4);
console.log(styles1.composes_target);
console.log(styles1.composes);

console.log(styles2.a);
console.log(styles2.b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ test('basic', async () => {
.local_class_name_3 {}
}
:local(.local_class_name_4) {}
.composes_target {}
.composes {
composes: composes_target;
}
`,
});
await run({ ...defaultOptions });
Expand All @@ -65,8 +61,6 @@ test('basic', async () => {
'local_class_name_2',
'local_class_name_3',
'local_class_name_4',
'composes_target',
'composes',
]);
// FIXME: Fix an issue where the text at definition destination was incorrect.
expect(results).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -208,23 +202,6 @@ test('basic', async () => {
],
"identifier": "local_class_name_4",
},
{
"definitions": [
{
file: "<fixtures>/test/1.css",
text: ".composes_target ",
start: { line: 21, offset: 1 },
end: { line: 21, offset: 18 },
},
],
"identifier": "composes_target",
},
{
"definitions": [
{ file: "<fixtures>/test/1.css", text: ".composes ", start: { line: 22, offset: 1 }, end: { line: 22, offset: 11 } },
],
"identifier": "composes",
},
]
`);
const moduleDefinitions = await server.getModuleDefinitions(getFixturePath('/test/1.css'));
Expand Down Expand Up @@ -295,8 +272,6 @@ test('with transformer', async () => {
// sass feature test (nesting)
.nesting_1 { dummy: ''; }
&_2 { dummy: ''; }
composes: basic; // css module feature test (composes)
composes: d from './4.scss'; // css module feature test (composes from other file)
}
`,
'/test/2.scss': dedent`
Expand All @@ -306,9 +281,6 @@ test('with transformer', async () => {
'/test/3.scss': dedent`
.c { dummy: ''; }
`,
'/test/4.scss': dedent`
.d { dummy: ''; }
`,
});
await run({ ...defaultOptions });
const results = await server.getMultipleIdentifierDefinitions(getFixturePath(`/test/1.scss`), [
Expand All @@ -319,7 +291,6 @@ test('with transformer', async () => {
'b_1',
'b_2',
'c',
'd',
]);
expect(results).toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -364,10 +335,6 @@ test('with transformer', async () => {
],
"identifier": "c",
},
{
"definitions": [],
"identifier": "d",
},
]
`);
});
49 changes: 5 additions & 44 deletions packages/happy-css-modules/src/locator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,17 @@ test('tracks other files when `@import` is present', async () => {
`);
});

test('tracks other files when `composes` is present', async () => {
test('does not track other files by `composes`', async () => {
createFixtures({
'/test/1.css': dedent`
.a {
composes: b from './2.css';
composes: c d from './3.css';
composes: e from '${getFixturePath('/test/4.css')}';
composes: c from './3.css'; /* non-existent file */
}
`,
'/test/2.css': dedent`
.b {}
`,
'/test/3.css': dedent`
.c {}
.d {}
`,
'/test/4.css': dedent`
.e {}
`,
});
const result = await locator.load(getFixturePath('/test/1.css'));
expect(result).toMatchInlineSnapshot(`
Expand All @@ -159,14 +151,7 @@ test('normalizes tokens', async () => {
/* duplicate import */
@import './2.css';
@import '2.css';
.a {
/* duplicate composes */
composes: c from './3.css';
composes: c from '3.css';
composes: c c from './3.css';
/* duplicate import and composes */
composes: b from './2.css';
}
.a {}
.a {} /* duplicate class selector */
`,
'/test/2.css': dedent`
Expand All @@ -187,7 +172,7 @@ test('normalizes tokens', async () => {
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: 12, column: 1 }, end: { line: 12, column: 2 } },
{ filePath: "<fixtures>/test/1.css", start: { line: 5, column: 1 }, end: { line: 5, column: 2 } },
],
},
{
Expand All @@ -205,12 +190,7 @@ test.failing('returns the result from the cache when the file has not been modif
createFixtures({
'/test/1.css': dedent`
@import './2.css';
@import './2.css';
.a {
composes: b from './2.css';
composes: c from './3.css';
composes: d from './3.css';
}
@import './3.css';
`,
'/test/2.css': dedent`
.b {}
Expand Down Expand Up @@ -246,25 +226,6 @@ test.failing('returns the result from the cache when the file has not been modif
expect(readFileSpy).toHaveBeenCalledTimes(2);
});

test('ignores the composition of non-existent tokens', async () => {
// In css-loader and postcss-modules, compositions of non-existent tokens are simply ignored.
// Therefore, happy-css-modules follows suit.
// It may be preferable to warn rather than ignore, but for now, we will focus on compatibility.
// ref: https://github.com/css-modules/css-modules/issues/356
createFixtures({
'/test/1.css': dedent`
.a {
composes: b c from './2.css';
}
`,
'/test/2.css': dedent`
.b {}
`,
});
const result = await locator.load(getFixturePath('/test/1.css'));
expect(result.tokens.map((t) => t.name)).toStrictEqual(['a']);
});

describe('supports sourcemap', () => {
test('restores original locations from sourcemap', async () => {
const transformer = createDefaultTransformer();
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-css-modules/src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type CacheEntry = {

/** The result of `Locator#load`. */
export type LoadResult = {
/** The path of the file imported from the source file with `@import` or `composes`. */
/** The path of the file imported from the source file with `@import`. */
dependencies: string[];
/** The tokens exported by the source file. */
tokens: Token[];
Expand Down
31 changes: 4 additions & 27 deletions packages/happy-css-modules/src/locator/postcss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ describe('generateLocalTokenNames', () => {
.local_class_name_3 {}
}
:local(.local_class_name_4) {}
.composes_target {}
.composes {
composes: composes_target;
}
`),
),
).toStrictEqual([
Expand All @@ -50,8 +46,6 @@ describe('generateLocalTokenNames', () => {
'local_class_name_2',
'local_class_name_3',
'local_class_name_4',
'composes_target',
'composes',
]);
});
test('does not track styles imported by @import in other file because it is not a local token', async () => {
Expand Down Expand Up @@ -224,23 +218,6 @@ describe('getOriginalLocation', () => {
`{ filePath: "/test/test.css", start: { line: 6, column: 8 }, end: { line: 6, column: 26 } }`,
);
});
test('composes', () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const [composes_target, composes] = createClassSelectors(
createRoot(dedent`
.composes_target {}
.composes {
composes: composes_target;
}
`),
);
expect(getOriginalLocation(composes_target!.rule, composes_target!.classSelector)).toMatchInlineSnapshot(
`{ filePath: "/test/test.css", start: { line: 1, column: 1 }, end: { line: 1, column: 16 } }`,
);
expect(getOriginalLocation(composes!.rule, composes!.classSelector)).toMatchInlineSnapshot(
`{ filePath: "/test/test.css", start: { line: 2, column: 1 }, end: { line: 2, column: 9 } }`,
);
});
test('with_newline', () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const [with_newline_1, with_newline_2, with_newline_3] = createClassSelectors(
Expand Down Expand Up @@ -268,8 +245,8 @@ test('collectNodes', () => {
@import;
@import "test.css";
@ignored;
.a { ignored: "ignored"; composes: a; }
.b { ignored: "ignored"; composes: b; }
.a { ignored: "ignored"; }
.b { ignored: "ignored"; }
`);

const { atImports, classSelectors } = collectNodes(ast);
Expand All @@ -278,9 +255,9 @@ test('collectNodes', () => {
expect(atImports[0]!.toString()).toEqual('@import');
expect(atImports[1]!.toString()).toEqual('@import "test.css"');
expect(classSelectors).toHaveLength(2);
expect(classSelectors[0]!.rule.toString()).toEqual('.a { ignored: "ignored"; composes: a; }');
expect(classSelectors[0]!.rule.toString()).toEqual('.a { ignored: "ignored"; }');
expect(classSelectors[0]!.classSelector.toString()).toEqual('.a');
expect(classSelectors[1]!.rule.toString()).toEqual('.b { ignored: "ignored"; composes: b; }');
expect(classSelectors[1]!.rule.toString()).toEqual('.b { ignored: "ignored"; }');
expect(classSelectors[1]!.classSelector.toString()).toEqual('.b');
});

Expand Down
4 changes: 2 additions & 2 deletions packages/happy-css-modules/src/locator/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export async function generateLocalTokenNames(ast: Root): Promise<string[]> {
postcss
.default()
// postcss-modules collects tokens (i.e., includes external tokens) by following
// the dependencies specified in the @import and composes properties.
// the dependencies specified in the @import.
// However, we do not want `generateLocalTokenNames` to return external tokens.
// So we remove the @import and composes properties beforehand.
// So we remove the @import beforehand.
.use(removeDependenciesPlugin())
.use(
modules({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ test('handles less features', async () => {
&_2 { dummy: ''; }
.b_1();
.b_2();
composes: a_1; // css module feature test (composes)
composes: c from './3.less'; // css module feature test (composes from other file)
}
`,
'/test/2.less': dedent`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ test('handles sass features', async () => {
// sass feature test (nesting)
.a_2_1 { dummy: ''; }
&_2 { dummy: ''; }
composes: a_1; // css module feature test (composes)
composes: d from './4.scss'; // css module feature test (composes from other file)
}
`,
'/test/2.scss': dedent`
Expand Down

0 comments on commit 258cd4e

Please sign in to comment.