Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Commit ae347ba

Browse files
author
ismay
authored
Merge pull request #52 from taneba/master
Fixed multiple interpolations in a property
2 parents 55b94d9 + 217b008 commit ae347ba

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/utils/general.js

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ const isLastLineWhitespaceOnly = text => {
4848
return true
4949
}
5050

51+
/**
52+
* Checks if text is empty or space only
53+
*/
54+
const isEmptyOrSpaceOnly = text => {
55+
if (text === '') {
56+
return true
57+
}
58+
for (let i = text.length - 1; i >= 0; i -= 1) {
59+
const char = text.charAt(i)
60+
if (char !== '\t' && char !== ' ') {
61+
return false
62+
}
63+
}
64+
return true
65+
}
66+
5167
// eslint-disable-next-line no-return-assign
5268
const wrapSelector = content => `.selector${(count += 1)} {${content}}\n`
5369
const wrapKeyframes = content => `@keyframes {${content}}\n`
@@ -56,3 +72,4 @@ exports.wrapKeyframes = wrapKeyframes
5672
exports.wrapSelector = wrapSelector
5773
exports.fixIndentation = fixIndentation
5874
exports.isLastLineWhitespaceOnly = isLastLineWhitespaceOnly
75+
exports.isEmptyOrSpaceOnly = isEmptyOrSpaceOnly

src/utils/tagged-template-literal.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const isLastLineWhitespaceOnly = require('./general').isLastLineWhitespaceOnly
2+
const isEmptyOrSpaceOnly = require('./general').isEmptyOrSpaceOnly
23

34
/**
45
* Check if a node is a tagged template literal
@@ -21,7 +22,7 @@ const interleave = (quasis, expressions) => {
2122
const expression = expressions[i]
2223

2324
css += prevText
24-
if (isLastLineWhitespaceOnly(prevText)) {
25+
if (isLastLineWhitespaceOnly(prevText) && !isEmptyOrSpaceOnly(prevText)) {
2526
css += `-styled-mixin: ${expression.name}`
2627
if (nextText.charAt(0) !== ';') {
2728
css += ';'

test/fixtures/interpolations/valid.js

+8
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,11 @@ const Button6 = styled.button`
7171
`}
7272
background: blue;
7373
`
74+
75+
// multi interpolations within a property
76+
const borderWidth = '1px'
77+
const borderStyle = 'solid'
78+
const Button7 = styled.button`
79+
width: 20px;
80+
border: ${borderWidth} ${borderStyle} ${color};
81+
`

test/utils.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const interleave = require('../src/utils/tagged-template-literal').interleave
22
const isLastLineWhitespaceOnly = require('../src/utils/general').isLastLineWhitespaceOnly
3+
const isEmptyOrSpaceOnly = require('../src/utils/general').isEmptyOrSpaceOnly
34

45
describe('utils', () => {
56
describe('interleave', () => {
@@ -60,4 +61,22 @@ describe('utils', () => {
6061
expect(isLastLineWhitespaceOnly('not space\n ')).toEqual(true)
6162
})
6263
})
64+
65+
describe('isEmptyOrSpaceOnly', () => {
66+
it('should return true for empty string', () => {
67+
expect(isEmptyOrSpaceOnly('')).toEqual(true)
68+
})
69+
70+
it('should return true for consecutive empty string', () => {
71+
expect(isEmptyOrSpaceOnly(' ')).toEqual(true)
72+
})
73+
74+
it('should return true for string of spaces and tabs', () => {
75+
expect(isEmptyOrSpaceOnly(' \t ')).toEqual(true)
76+
})
77+
78+
it('should return false for string of newline', () => {
79+
expect(isEmptyOrSpaceOnly('\n')).toEqual(false)
80+
})
81+
})
6382
})

0 commit comments

Comments
 (0)