Skip to content

Commit 6c5692e

Browse files
committed
Align logic with styled-components
1 parent 83db62c commit 6c5692e

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

plugin/plugin.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,25 @@ function parseCss(cssText, substitutionMap) {
121121
.replace(/\/\/.*(?=\n|$)/g, '') // remove // comments
122122
)
123123
}
124+
const pattern = new RegExp(`^\\d+\\.${MAGIC_NUMBER}`)
125+
function startsWithSubstitution(line) {
126+
return pattern.test(line)
127+
}
124128
cssText = removeComments(cssText)
125-
const lines = cssText.split('\n')
129+
const SEPARATOR = ';'
130+
const lines = cssText.split('\n').reduce((acc, line) => acc.concat(line.split(SEPARATOR)), [])
131+
const processedLines = []
126132
let styles = []
127133
for (let i = 0; i < lines.length; i++) {
128134
let line = lines[i].trim()
129-
if (line.endsWith(';')) {
130-
line = line.substring(0, line.length - 1)
135+
if (startsWithSubstitution(line) && substitutionMap[line]) {
136+
line = `${MIXIN}:${line}`
131137
}
132-
if (substitutionMap[line]) { // mixin
133-
styles.push([MIXIN, line])
134-
lines[i] = ''
138+
if (line) {
139+
processedLines.push(line)
135140
}
136141
}
137-
cssText = lines.join('')
142+
cssText = processedLines.join(SEPARATOR)
138143
const { nodes } = postcss.parse(cssText)
139144
for (const node of nodes) {
140145
if (node.type === 'decl') {
@@ -180,7 +185,7 @@ function buildCssObject(identifier, t, substitutions) {
180185
const elements = []
181186
const expressions = []
182187
if (substitutions[value]) {
183-
return substitutions[value]
188+
return inject(substitutions[value])
184189
}
185190
const matches = splitSubstitution(value)
186191
for (const match of matches) {

plugin/transform.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ const stringKeys = [
3232
'textDecorationLine',
3333
]
3434

35-
const runtimeKeys = ['border', 'boxShadow']
35+
const mapper = {
36+
background: 'backgroundColor'
37+
}
3638

37-
const withoutTransform = (key, value) => ({ [key]: value })
39+
const runtimeKeys = ['border', 'boxShadow']
3840

3941
const isSurrounded = (value, char) => value[0] === char && value[value.length - 1] === char
4042
const stringTransform = (key, value) => {
@@ -45,7 +47,7 @@ const stringTransform = (key, value) => {
4547
}
4648
const runtimeTransform = (key, value) => ({ 'RUNTIME_': [key, value] })
4749

48-
const colorTransforms = colorKeys.map((key) => [key, withoutTransform])
50+
const colorTransforms = colorKeys.map((key) => [key, stringTransform])
4951
const stringTransforms = stringKeys.map((key) => [key, stringTransform])
5052
const runtimeTransforms = runtimeKeys.map((key) => [key, runtimeTransform])
5153

@@ -57,6 +59,7 @@ const CustomTransformers = Object.fromEntries(
5759
)
5860

5961
function transform(key, value) {
62+
key = mapper[key] ?? key
6063
let result
6164
try {
6265
const transformer = CustomTransformers[key] ?? getStylesForProperty

src/__tests__/buildDynamicStyles.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,50 @@ describe('buildDynamicStyles', () => {
121121
borderColor: 'black',
122122
})
123123
})
124+
125+
test('Should override styles', () => {
126+
const { css } = createStyled()
127+
const props = {
128+
theme: {},
129+
}
130+
let styles = buildDynamicStyles(props, [
131+
['height', 2],
132+
])
133+
134+
expect(styles).toStrictEqual({
135+
height: 2,
136+
})
137+
138+
const css1 = css([
139+
['height', 1],
140+
])
141+
styles = buildDynamicStyles(props, [
142+
['MIXIN_', css1],
143+
['height', 2],
144+
])
145+
expect(styles).toStrictEqual({
146+
height: 2,
147+
})
148+
149+
styles = buildDynamicStyles(props, [
150+
['height', 2],
151+
['MIXIN_', css1],
152+
])
153+
expect(styles).toStrictEqual({
154+
height: 1,
155+
})
156+
157+
const css2 = css([
158+
['height', 4],
159+
['MIXIN_', css1],
160+
])
161+
162+
styles = buildDynamicStyles(props, [
163+
['height', 20],
164+
['MIXIN_', css2],
165+
])
166+
expect(styles).toStrictEqual({
167+
height: 1,
168+
})
169+
})
124170
})

src/__tests__/buildPropsFromAttrs.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildPropsFromAttrs} from '../buildPropsFromAttrs'
1+
import { buildPropsFromAttrs } from '../buildPropsFromAttrs'
22

33
describe('buildPropsFromAttrs', () => {
44
test('Should combine passed props', () => {
@@ -25,7 +25,7 @@ describe('buildPropsFromAttrs', () => {
2525
props2: true,
2626
number: 2,
2727
out: true,
28-
overriddenNumber: 0,
28+
overriddenNumber: 2,
2929
theme: {},
3030
})
3131
})
@@ -42,8 +42,11 @@ describe('buildPropsFromAttrs', () => {
4242
const props2 = () => ({
4343
number: 2,
4444
})
45-
buildPropsFromAttrs(outProps, [prop1, props2])
45+
const props = buildPropsFromAttrs(outProps, [prop1, props2])
4646

47-
expect(outProps).toStrictEqual(excpectedProps)
47+
expect(props).toStrictEqual({
48+
number: 2,
49+
theme: {},
50+
})
4851
})
4952
})

src/__tests__/css.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('styled props', () => {
159159
[ 'textAlign', '1' ],
160160
[ 'textAlign', '1' ],
161161
[ 'color', '1' ],
162-
[ 'background', '1' ],
162+
[ 'backgroundColor', '1' ],
163163
[ 'backgroundColor', '1' ],
164164
[ 'backgroundColor', '1' ],
165165
[ 'borderColor', '1' ],
@@ -243,7 +243,9 @@ describe('styled props', () => {
243243
font-family: 'RobotoMono-Regular';
244244
${css`
245245
height: 1px;
246-
`}
246+
`};
247+
background: green;
248+
background: 'yellow';
247249
`
248250

249251
expect(styled.styles).toStrictEqual([
@@ -254,7 +256,9 @@ describe('styled props', () => {
254256
['fontFamily', 'Inter-Bold' ],
255257
['fontFamily', 'RobotoMono-Regular'],
256258
['fontFamily', 'RobotoMono-Regular'],
257-
['MIXIN_', new Css([['height', 1]])]
259+
['MIXIN_', new Css([['height', 1]])],
260+
['backgroundColor', 'green'],
261+
['backgroundColor', 'yellow'],
258262
])
259263
})
260264
})

src/buildPropsFromAttrs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ import { AnyTheme, BaseObject, InnerAttrs, Themed, UnknownProps } from './types'
1212
* { number: 1, overriddenNumber: 1}, // props as an object
1313
* (props) => { number: props.number + 1, overriddenNumber: props.overriddenNumber + 1 } // props a function
1414
* ]
15-
* result - { overriddenNumber: 0, number: 2 }
15+
* result - { overriddenNumber: 2, number: 2 }
1616
*/
1717
export function buildPropsFromAttrs<Theme extends BaseObject = AnyTheme>(props: Themed<UnknownProps, Theme>, attrs: InnerAttrs[]): Themed<UnknownProps, Theme> {
1818
if (attrs.length === 0) {
1919
return props
2020
}
21-
attrs = attrs.concat(props)
2221
return attrs.reduce<Themed<UnknownProps, Theme>>((acc, recordOrFn) => {
2322
return Object.assign(acc, getResult(acc, recordOrFn))
2423
}, { ...props })

0 commit comments

Comments
 (0)