Skip to content

Commit 014417a

Browse files
committed
Implement auto keyword for flex including in shorthand for flex-basis
1 parent 1c1f113 commit 014417a

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (tokenStream) => {
1616
if (tokenStream.matches(NONE)) {
1717
tokenStream.expectEmpty();
1818
return { $merge: { flexGrow: 0, flexShrink: 0 } };
19-
} else if (tokenStream.matches(AUTO) && !tokenStream.lookAhead().hasTokens()) {
19+
} else if (tokenStream.test(AUTO) && !tokenStream.lookAhead().hasTokens()) {
2020
return { $merge: { flexGrow: 1, flexShrink: 1 } };
2121
}
2222

@@ -27,7 +27,7 @@ module.exports = (tokenStream) => {
2727
if (flexGrow === undefined && tokenStream.matches(NUMBER)) {
2828
flexGrow = tokenStream.lastValue;
2929

30-
if (tokenStream.lookahead().matches(NUMBER)) {
30+
if (tokenStream.lookAhead().matches(NUMBER)) {
3131
tokenStream.expect(SPACE);
3232
flexShrink = tokenStream.expect(NUMBER);
3333
}

0 commit comments

Comments
 (0)