Skip to content

Commit

Permalink
fix(babel): support for dynamic keys in objects with css as values (#714
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Anber committed Dec 8, 2020
1 parent d1b70cc commit 17ee220
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/babel/__fixtures__/ts-data.ts
@@ -0,0 +1,4 @@
export enum TestEnum {
FirstValue,
SecondValue,
}
20 changes: 20 additions & 0 deletions packages/babel/__tests__/__snapshots__/babel.test.ts.snap
Expand Up @@ -187,6 +187,26 @@ Dependencies: NA
`;
exports[`handles objects with enums as keys 1`] = `
"import { css } from '@linaria/core';
import { TestEnum } from './ts-data.ts';
export const object = {
[TestEnum.FirstValue]: \\"t17gu1mi\\",
[TestEnum.SecondValue]: \\"tccybe9\\"
};"
`;
exports[`handles objects with enums as keys 2`] = `
CSS:
.t17gu1mi {}
.tccybe9 {}
Dependencies: NA
`;
exports[`handles objects with numeric keys 1`] = `
"import { css } from '@linaria/core';
export const object = {
Expand Down
17 changes: 17 additions & 0 deletions packages/babel/__tests__/babel.test.ts
Expand Up @@ -504,3 +504,20 @@ it('handles objects with numeric keys', async () => {
expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});

it('handles objects with enums as keys', async () => {
const { code, metadata } = await transpile(
dedent`
import { css } from '@linaria/core';
import { TestEnum } from './ts-data.ts';
export const object = {
[TestEnum.FirstValue]: css\`\`,
[TestEnum.SecondValue]: css\`\`,
}
`
);

expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});
7 changes: 3 additions & 4 deletions packages/babel/src/visitors/GenerateClassNames.ts
Expand Up @@ -6,7 +6,7 @@
*/

import { basename, dirname, relative } from 'path';
import type { TaggedTemplateExpression } from '@babel/types';
import type { ObjectProperty, TaggedTemplateExpression } from '@babel/types';
import type { NodePath } from '@babel/traverse';
import { debug } from '@linaria/logger';
import type { State, StrictOptions } from '../types';
Expand Down Expand Up @@ -54,9 +54,8 @@ export default function GenerateClassNames(
} else if ('value' in parentNode.key) {
displayName = parentNode.key.value.toString();
} else {
throw new Error(
`Unexpected object property key ${parentNode.key.type}`
);
const keyPath = (parent as NodePath<ObjectProperty>).get('key');
displayName = keyPath.getSource();
}
} else if (
t.isJSXOpeningElement(parentNode) &&
Expand Down

0 comments on commit 17ee220

Please sign in to comment.