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

Fixed multiple interpolations in a property #52

Merged
merged 2 commits into from Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/utils/general.js
Expand Up @@ -48,6 +48,22 @@ const isLastLineWhitespaceOnly = text => {
return true
}

/**
* Checks if text is empty or space only
*/
const isEmptyOrSpaceOnly = text => {
if (text === '') {
return true
}
for (let i = text.length - 1; i >= 0; i -= 1) {
const char = text.charAt(i)
if (char !== '\t' && char !== ' ') {
return false
}
}
return true
}

// eslint-disable-next-line no-return-assign
const wrapSelector = content => `.selector${(count += 1)} {${content}}\n`
const wrapKeyframes = content => `@keyframes {${content}}\n`
Expand All @@ -56,3 +72,4 @@ exports.wrapKeyframes = wrapKeyframes
exports.wrapSelector = wrapSelector
exports.fixIndentation = fixIndentation
exports.isLastLineWhitespaceOnly = isLastLineWhitespaceOnly
exports.isEmptyOrSpaceOnly = isEmptyOrSpaceOnly
3 changes: 2 additions & 1 deletion src/utils/tagged-template-literal.js
@@ -1,4 +1,5 @@
const isLastLineWhitespaceOnly = require('./general').isLastLineWhitespaceOnly
const isEmptyOrSpaceOnly = require('./general').isEmptyOrSpaceOnly

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

css += prevText
if (isLastLineWhitespaceOnly(prevText)) {
if (isLastLineWhitespaceOnly(prevText) && !isEmptyOrSpaceOnly(prevText)) {
css += `-styled-mixin: ${expression.name}`
if (nextText.charAt(0) !== ';') {
css += ';'
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/interpolations/valid.js
Expand Up @@ -71,3 +71,11 @@ const Button6 = styled.button`
`}
background: blue;
`

// multi interpolations within a property
const borderWidth = '1px'
const borderStyle = 'solid'
const Button7 = styled.button`
width: 20px;
border: ${borderWidth} ${borderStyle} ${color};
`
19 changes: 19 additions & 0 deletions test/utils.test.js
@@ -1,5 +1,6 @@
const interleave = require('../src/utils/tagged-template-literal').interleave
const isLastLineWhitespaceOnly = require('../src/utils/general').isLastLineWhitespaceOnly
const isEmptyOrSpaceOnly = require('../src/utils/general').isEmptyOrSpaceOnly

describe('utils', () => {
describe('interleave', () => {
Expand Down Expand Up @@ -60,4 +61,22 @@ describe('utils', () => {
expect(isLastLineWhitespaceOnly('not space\n ')).toEqual(true)
})
})

describe('isEmptyOrSpaceOnly', () => {
it('should return true for empty string', () => {
expect(isEmptyOrSpaceOnly('')).toEqual(true)
})

it('should return true for consecutive empty string', () => {
expect(isEmptyOrSpaceOnly(' ')).toEqual(true)
})

it('should return true for string of spaces and tabs', () => {
expect(isEmptyOrSpaceOnly(' \t ')).toEqual(true)
})

it('should return false for string of newline', () => {
expect(isEmptyOrSpaceOnly('\n')).toEqual(false)
})
})
})