Skip to content

Commit

Permalink
fix: support literal property names in object. fixes #170 (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 authored and zamotany committed Nov 13, 2017
1 parent 3a1ef42 commit 1ffce6b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
78 changes: 77 additions & 1 deletion src/babel/preval-extract/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe('preval-extract/index', () => {
expect(prevalStyles.mock.calls[0][1]).toBe('test');
});

it('should prevalStyles with object property', () => {
it('should prevalStyles with object property with identifier', () => {
const { visitor } = prevalExtractPlugin(babel);

const parent = {
Expand Down Expand Up @@ -269,6 +269,82 @@ describe('preval-extract/index', () => {
expect(prevalStyles.mock.calls[0][1]).toBe('foo');
});

it('should prevalStyles with object property with string literal', () => {
const { visitor } = prevalExtractPlugin(babel);

const parent = {
type: 'ObjectProperty',
node: {
type: 'ObjectProperty',
key: {
type: 'StringLiteral',
value: 'foo',
},
},
};

visitor.TaggedTemplateExpression(
{
node: {
type: 'TaggedTemplateExpression',
tag: {
type: 'Identifier',
name: 'css',
},
},
traverse: () => {},
findParent: check => (check(parent) ? parent : null),
replaceWith: () => {},
addComment: () => {},
},
{
skipFile: false,
foundLinariaTaggedLiterals: false,
}
);

expect(prevalStyles).toHaveBeenCalled();
expect(prevalStyles.mock.calls[0][1]).toBe('foo');
});

it('should prevalStyles with object property with numeric literal', () => {
const { visitor } = prevalExtractPlugin(babel);

const parent = {
type: 'ObjectProperty',
node: {
type: 'ObjectProperty',
key: {
type: 'NumericLiteral',
value: 42,
},
},
};

visitor.TaggedTemplateExpression(
{
node: {
type: 'TaggedTemplateExpression',
tag: {
type: 'Identifier',
name: 'css',
},
},
traverse: () => {},
findParent: check => (check(parent) ? parent : null),
replaceWith: () => {},
addComment: () => {},
},
{
skipFile: false,
foundLinariaTaggedLiterals: false,
}
);

expect(prevalStyles).toHaveBeenCalled();
expect(prevalStyles.mock.calls[0][1]).toBe(42);
});

it('should prevalStyles with JSX opening element', () => {
const { visitor } = prevalExtractPlugin(babel);

Expand Down
2 changes: 1 addition & 1 deletion src/babel/preval-extract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default (babel: BabelCore) => {

if (parent) {
if (types.isObjectProperty(parent)) {
title = parent.node.key.name;
title = parent.node.key.name || parent.node.key.value;
} else if (types.isJSXOpeningElement(parent)) {
title = parent.node.name.name;
} else if (types.isVariableDeclarator(parent)) {
Expand Down

0 comments on commit 1ffce6b

Please sign in to comment.