Skip to content

Commit c3b2972

Browse files
authored
Merge pull request #44 from styled-components/flex-shorthand-basis-auto
Update flex.js
2 parents 98eaf80 + 014417a commit c3b2972

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/TokenStream.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const SYMBOL_BASE_MATCH = 'SYMBOL_BASE_MATCH';
12
const SYMBOL_MATCH = 'SYMBOL_MATCH';
23

34
module.exports = class TokenStream {
@@ -16,30 +17,37 @@ module.exports = class TokenStream {
1617
return this.nodes.length > 0;
1718
}
1819

19-
lookahead() {
20+
lookAhead() {
2021
return new TokenStream(this.nodes.slice(1), this.parent);
2122
}
2223

23-
[SYMBOL_MATCH](...tokenDescriptors) {
24+
[SYMBOL_BASE_MATCH](...tokenDescriptors) {
2425
const node = this.node;
2526

2627
if (!node) return null;
2728

2829
for (let i = 0; i < tokenDescriptors.length; i += 1) {
2930
const tokenDescriptor = tokenDescriptors[i];
3031
const value = tokenDescriptor(node);
31-
32-
if (value !== null) {
33-
this.nodes = this.nodes.slice(1);
34-
this.lastFunction = null;
35-
this.lastValue = value;
36-
return value;
37-
}
32+
if (value !== null) return value;
3833
}
3934

4035
return null;
4136
}
4237

38+
[SYMBOL_MATCH](...tokenDescriptors) {
39+
const value = this[SYMBOL_BASE_MATCH](...tokenDescriptors);
40+
if (value === null) return null;
41+
this.nodes = this.nodes.slice(1);
42+
this.lastFunction = null;
43+
this.lastValue = value;
44+
return value;
45+
}
46+
47+
test(...tokenDescriptors) {
48+
return this[SYMBOL_BASE_MATCH](...tokenDescriptors) !== null;
49+
}
50+
4351
matches(...tokenDescriptors) {
4452
return this[SYMBOL_MATCH](...tokenDescriptors) !== null;
4553
}

src/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,22 @@ it('transforms flex shorthand with flex-grow/shrink taking priority over basis',
261261
['flex', '0 1 0'],
262262
], { flexGrow: 0, flexShrink: 1, flexBasis: 0 }));
263263

264+
it('transforms flex shorthand with flex-basis set to auto', () => runTest([
265+
['flex', '0 1 auto'],
266+
], { flexGrow: 0, flexShrink: 1 }));
267+
268+
it('transforms flex shorthand with flex-basis set to auto appearing first', () => runTest([
269+
['flex', 'auto 0 1'],
270+
], { flexGrow: 0, flexShrink: 1 }));
271+
272+
it('transforms flex auto keyword', () => runTest([
273+
['flex', 'auto'],
274+
], { flexGrow: 1, flexShrink: 1 }));
275+
276+
it('transforms flex none keyword', () => runTest([
277+
['flex', 'none'],
278+
], { flexGrow: 0, flexShrink: 0 }));
279+
264280
it('transforms flexFlow shorthand with two values', () => runTest([
265281
['flex-flow', 'column wrap'],
266282
], { flexDirection: 'column', flexWrap: 'wrap' }));

src/transforms/flex.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const defaultFlexGrow = 1;
66
const defaultFlexShrink = 1;
77
const defaultFlexBasis = 0;
88

9+
const FLEX_BASIS_AUTO = {}; // Used for reference equality
10+
911
module.exports = (tokenStream) => {
1012
let flexGrow;
1113
let flexShrink;
@@ -14,8 +16,7 @@ module.exports = (tokenStream) => {
1416
if (tokenStream.matches(NONE)) {
1517
tokenStream.expectEmpty();
1618
return { $merge: { flexGrow: 0, flexShrink: 0 } };
17-
} else if (tokenStream.matches(AUTO)) {
18-
tokenStream.expectEmpty();
19+
} else if (tokenStream.test(AUTO) && !tokenStream.lookAhead().hasTokens()) {
1920
return { $merge: { flexGrow: 1, flexShrink: 1 } };
2021
}
2122

@@ -26,12 +27,14 @@ module.exports = (tokenStream) => {
2627
if (flexGrow === undefined && tokenStream.matches(NUMBER)) {
2728
flexGrow = tokenStream.lastValue;
2829

29-
if (tokenStream.lookahead().matches(NUMBER)) {
30+
if (tokenStream.lookAhead().matches(NUMBER)) {
3031
tokenStream.expect(SPACE);
3132
flexShrink = tokenStream.expect(NUMBER);
3233
}
3334
} else if (flexBasis === undefined && tokenStream.matches(LENGTH)) {
3435
flexBasis = tokenStream.lastValue;
36+
} else if (flexBasis === undefined && tokenStream.matches(AUTO)) {
37+
flexBasis = FLEX_BASIS_AUTO;
3538
} else {
3639
tokenStream.throw();
3740
}
@@ -45,5 +48,7 @@ module.exports = (tokenStream) => {
4548
if (flexShrink === undefined) flexShrink = defaultFlexShrink;
4649
if (flexBasis === undefined) flexBasis = defaultFlexBasis;
4750

48-
return { $merge: { flexGrow, flexShrink, flexBasis } };
51+
return flexBasis !== FLEX_BASIS_AUTO
52+
? { $merge: { flexGrow, flexShrink, flexBasis } }
53+
: { $merge: { flexGrow, flexShrink } };
4954
};

0 commit comments

Comments
 (0)