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

Commit 4785157

Browse files
committed
Fix most one-line css issues and put limitations in comments
1 parent 32d38a7 commit 4785157

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/utils/general.js

+16-27
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,25 @@ const fixIndentation = str => {
3232
}
3333
}
3434

35-
/**
36-
* Checks if last line of text has whitespaces only
37-
*/
38-
const isLastLineWhitespaceOnly = text => {
39-
for (let i = text.length - 1; i >= 0; i -= 1) {
40-
const char = text.charAt(i)
41-
if (char === '\n') {
42-
return true
43-
}
44-
if (char !== '\t' && char !== ' ') {
45-
return false
46-
}
35+
const nextNonWhitespaceChar = text => {
36+
const matches = text.match(/^\s*(.)/)
37+
if (matches) {
38+
return matches[1]
39+
} else {
40+
return null
4741
}
48-
return true
4942
}
5043

51-
/**
52-
* Checks if text is empty or space only
53-
*/
54-
const isEmptyOrSpaceOnly = text => {
55-
if (text === '') {
44+
const reverseString = str => str.split('').reverse().join('')
45+
46+
const isLastDeclarationCompleted = text => {
47+
const reversedText = reverseString(text)
48+
const lastNonWhitespaceChar = nextNonWhitespaceChar(reversedText)
49+
if (lastNonWhitespaceChar === ';' || lastNonWhitespaceChar === null) {
5650
return true
51+
} else {
52+
return false
5753
}
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
6554
}
6655

6756
// eslint-disable-next-line no-return-assign
@@ -71,5 +60,5 @@ const wrapKeyframes = content => `@keyframes {${content}}\n`
7160
exports.wrapKeyframes = wrapKeyframes
7261
exports.wrapSelector = wrapSelector
7362
exports.fixIndentation = fixIndentation
74-
exports.isLastLineWhitespaceOnly = isLastLineWhitespaceOnly
75-
exports.isEmptyOrSpaceOnly = isEmptyOrSpaceOnly
63+
exports.nextNonWhitespaceChar = nextNonWhitespaceChar
64+
exports.isLastDeclarationCompleted = isLastDeclarationCompleted

src/utils/tagged-template-literal.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const isLastLineWhitespaceOnly = require('./general').isLastLineWhitespaceOnly
2-
const isEmptyOrSpaceOnly = require('./general').isEmptyOrSpaceOnly
1+
const nextNonWhitespaceChar = require('./general').nextNonWhitespaceChar
2+
const isLastDeclarationCompleted = require('./general').isLastDeclarationCompleted
33

44
/**
55
* Check if a node is a tagged template literal
@@ -14,24 +14,36 @@ 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
1917
const interleave = (quasis, expressions) => {
18+
// Used for making sure our dummy mixins are all unique
19+
let count = 0
2020
let css = ''
2121
for (let i = 0, l = expressions.length; i < l; i += 1) {
2222
const prevText = quasis[i].value.raw
2323
const nextText = quasis[i + 1].value.raw
2424

2525
css += prevText
26-
if (isLastLineWhitespaceOnly(prevText) && !isEmptyOrSpaceOnly(prevText)) {
27-
css += `-styled-mixin${count}: dummyValue`
26+
let substitute
27+
if (isLastDeclarationCompleted(css)) {
28+
/** This block assumes that if you put an interpolation in the position
29+
* of the start of a declaration that the interpolation will
30+
* contain a full declaration and not later in the template literal
31+
* be completed by another interpolation / completed by following text
32+
* in the literal
33+
*/
34+
substitute = `-styled-mixin${count}: dummyValue`
2835
count += 1
29-
if (nextText.charAt(0) !== ';') {
30-
css += ';'
36+
if (nextNonWhitespaceChar(nextText) !== ';') {
37+
substitute += ';'
3138
}
3239
} else {
33-
css += '$dummyValue'
40+
/* This block assumes that we are in the middle of a declaration
41+
* and that the interpolation is providing a value, not a property
42+
* or part of a property
43+
*/
44+
substitute = '$dummyValue'
3445
}
46+
css += substitute
3547
}
3648
css += quasis[quasis.length - 1].value.raw
3749
return css

0 commit comments

Comments
 (0)