Skip to content

Commit 4706de2

Browse files
authored
Merge pull request styled-components#59 from emilgoldsmith/fix/styled-components#55/several-interpolation-strings
Make -styled-mixins unique and clarify interpolation linting in readme
2 parents b0ab9da + 2012bce commit 4706de2

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ const Wrapper = styled.div`
8383
`;
8484
```
8585

86+
#### Interpolation linting
87+
We do not currently support linting interpolations as it could be a big performance hit though we aspire to have at least partial support in the future. You can of course lint your own mixins in their separate files, but it won't be linted in context, the implementation currently just inserts relevant dummy values. This, we are afraid, means you won't be able to lint cases such as `declaration-block-no-duplicate-properties` etc. and won't be able to lint outside mixins such as [polished](https://github.com/styled-components/polished).
8688

8789
## License
8890

src/utils/tagged-template-literal.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ const hasInterpolations = node => !node.quasi.quasis[0].tail
1414
/**
1515
* Merges the interpolations in a parsed tagged template literals with the strings
1616
*/
17+
// Used for making sure our dummy mixins are all unique
18+
let count = 0
1719
const interleave = (quasis, expressions) => {
1820
let css = ''
1921
for (let i = 0, l = expressions.length; i < l; i += 1) {
2022
const prevText = quasis[i].value.raw
2123
const nextText = quasis[i + 1].value.raw
22-
const expression = expressions[i]
2324

2425
css += prevText
2526
if (isLastLineWhitespaceOnly(prevText) && !isEmptyOrSpaceOnly(prevText)) {
26-
css += `-styled-mixin: ${expression.name}`
27+
css += `-styled-mixin${count}: dummyValue`
28+
count += 1
2729
if (nextText.charAt(0) !== ';') {
2830
css += ';'
2931
}
3032
} else {
31-
css += `$${expression.name}`
33+
css += '$dummyValue'
3234
}
3335
}
3436
css += quasis[quasis.length - 1].value.raw

test/fixtures/interpolations/valid.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const Box3 = styled(Box2)`
4242
`
4343

4444
// Multiline
45+
// prettier-ignore
4546
const Button4 = styled.button`
4647
display: block;
4748
${`
@@ -52,6 +53,7 @@ const Button4 = styled.button`
5253

5354
// Conditional
5455
const cond = true
56+
// prettier-ignore
5557
const Button5 = styled.button`
5658
display: block;
5759
${cond &&
@@ -63,10 +65,11 @@ const Button5 = styled.button`
6365

6466
// Conditional
6567
const cond2 = false
68+
// prettier-ignore
6669
const Button6 = styled.button`
6770
display: block;
6871
${cond2 &&
69-
`
72+
`
7073
color: ${cond2};
7174
`}
7275
background: blue;
@@ -79,3 +82,11 @@ const Button7 = styled.button`
7982
width: 20px;
8083
border: ${borderWidth} ${borderStyle} ${color};
8184
`
85+
86+
// Several interpolation statements in a block
87+
// prettier-ignore
88+
const Button8 = styled.button`
89+
${`display: block;`}
90+
${`color: ${color};`}
91+
${`background: blue;`}
92+
`

test/interpolations.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path')
44
const processor = path.join(__dirname, '../src/index.js')
55
const rules = {
66
'block-no-empty': true,
7+
'declaration-block-no-duplicate-properties': true,
78
indentation: 2
89
}
910

test/utils.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('utils', () => {
3535
}
3636
]
3737
expect(interleave(quasis, expressions)).toEqual(
38-
'\n display: block;\n color: $color;\n background: blue;\n'
38+
'\n display: block;\n color: $dummyValue;\n background: blue;\n'
3939
)
4040
})
4141
})

0 commit comments

Comments
 (0)