Skip to content

Commit 8e4ea24

Browse files
committed
Parse spacing
1 parent d9d7a27 commit 8e4ea24

File tree

4 files changed

+90
-23
lines changed

4 files changed

+90
-23
lines changed

src/bin/css_to_ts/spacing.ts

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,90 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
/*
1+
import memoize from "memoizee";
32
import { assert } from "tsafe/assert";
4-
import type { Equals } from "tsafe";
5-
import { objectKeys } from "tsafe/objectKeys";
63
import { exclude } from "tsafe/exclude";
7-
import { parseCss } from "./parseCss";
8-
*/
9-
import memoize from "memoizee";
4+
import { getRulesByBreakpoint } from "./breakpoints";
5+
import { objectKeys } from "tsafe/objectKeys";
106

117
export type SpacingTokenAndValue = { token: string; value: string };
128

139
export const parseSpacing = memoize((rawCssCode: string): SpacingTokenAndValue[] => {
14-
//const parsedCss = parseCss(rawCssCode);
15-
console.log(rawCssCode);
10+
const spacingTokenAndValues: SpacingTokenAndValue[] = [];
11+
12+
const rulesByBreakpoint = getRulesByBreakpoint(rawCssCode);
13+
14+
objectKeys(rulesByBreakpoint).forEach(breakpoint => {
15+
rulesByBreakpoint[breakpoint].forEach(({ selectors, declarations }) => {
16+
let value: string | undefined = undefined;
17+
18+
selectors
19+
.map((selector: string) => {
20+
const matchArr = selector.match(/^.fr-m-([0-9]+[a-zA-Z])+$/);
21+
return matchArr === null ? undefined : matchArr[1];
22+
})
23+
.filter(exclude(undefined))
24+
.forEach(token => {
25+
if (value === undefined) {
26+
const declaration = declarations.find(
27+
(declaration: any) =>
28+
declaration.type === "declaration" &&
29+
declaration.property === "margin"
30+
);
31+
assert(declaration !== undefined);
32+
value = (declaration as any).value.replace(
33+
/\s*!important\s*$/,
34+
""
35+
) as string;
36+
}
37+
38+
const preExistingSpacingTokenAndValues = spacingTokenAndValues.find(
39+
o => o.token === token
40+
);
41+
42+
if (breakpoint !== "root") {
43+
assert(
44+
preExistingSpacingTokenAndValues !== undefined &&
45+
preExistingSpacingTokenAndValues.value === value,
46+
"Need tu update the module so that we support spacing that varies depending on screen sizes, so far we assumed they didn't"
47+
);
48+
49+
return;
50+
}
51+
52+
assert(spacingTokenAndValues.find(o => o.token === token) === undefined);
53+
54+
spacingTokenAndValues.push({
55+
token,
56+
value
57+
});
58+
});
59+
});
60+
});
61+
62+
{
63+
const getValueWeight = (value: string) => {
64+
const match = value.match(/^([0-9.]+)/);
65+
assert(match !== null);
66+
const n = parseFloat(match[1]);
67+
assert(!isNaN(n));
68+
return n;
69+
};
70+
71+
spacingTokenAndValues.sort((a, b) => {
72+
const w1 = getValueWeight(a.value);
73+
const w2 = getValueWeight(b.value);
74+
75+
if (w1 === w2) {
76+
if (a.token < b.token) {
77+
return -1;
78+
}
79+
if (a.token > b.token) {
80+
return 1;
81+
}
82+
return 0;
83+
}
84+
85+
return w1 - w2;
86+
});
87+
}
1688

17-
return null as any;
89+
return spacingTokenAndValues;
1890
});

test/behavior/bin/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import testCases from "cases-of-test";
22
import _0 from "./colorDecisions";
33
import _1 from "./colorOptions";
4-
//import _2 from "./spacing";
4+
import _2 from "./spacing";
55
import _3 from "./typography";
66
import _4 from "./breakpoints.test";
77
import _5 from "./cssVariable.test";
88

99
export default () =>
1010
testCases({
11-
"tests": [
12-
[_0],
13-
[_1],
14-
//[_2],
15-
[_3],
16-
[_4],
17-
[_5]
18-
]
11+
"tests": [[_0], [_1], [_2], [_3], [_4], [_5]]
1912
});

test/behavior/bin/spacing/parseSpacing.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ export default () =>
2929
"value": "1.75rem"
3030
},
3131
{
32-
"token": "7v",
33-
"value": "1.75rem"
32+
"token": "12v",
33+
"value": "3rem"
34+
},
35+
{
36+
"token": "6w",
37+
"value": "3rem"
3438
}
3539
];
3640

test/behavior/bin/typography/parseTypographyVariants.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,5 +591,3 @@ sup {
591591

592592
expect(got).toStrictEqual(expected);
593593
});
594-
595-
console.log("PASS");

0 commit comments

Comments
 (0)