Skip to content

Commit 05986a2

Browse files
committed
Make all test passes but typography
1 parent db250e0 commit 05986a2

File tree

11 files changed

+216
-42
lines changed

11 files changed

+216
-42
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ jspm_packages
4646
/dist_test
4747
/src/lib/generatedFromCss/
4848

49+
50+
# tmp!
51+
/typo.css
52+
/typography.ts
53+
/view.json
54+

src/bin/css_to_ts/colorOptions.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { multiReplace } from "../tools/multiReplace";
77
import { createGetCssVariable } from "./cssVariable";
88
import memoize from "memoizee";
99
import * as crypto from "crypto";
10+
import { cssColorRegexp } from "../tools/cssColorRegexp";
1011

1112
export type ColorScheme = "light" | "dark";
1213

@@ -344,26 +345,17 @@ export const parseColorOptions = memoize((rawCssCode: string): ColorOption[] =>
344345
return { declarations };
345346
})();
346347

347-
const htmlColorRegexp = /^#[0-9a-f]{3,6}$/;
348-
349348
return declarations
349+
.filter(({ value }: any) => cssColorRegexp.test(value))
350350
.map(({ property }: any) => {
351-
console.log({ property });
352-
353351
const cssVariableValue = getCssVariable(property);
354352

355-
console.log({ cssVariableValue });
356-
357353
const colorLight = cssVariableValue.root.light;
358354

359-
if (!htmlColorRegexp.test(colorLight)) {
360-
return undefined;
361-
}
362-
363355
const colorDark = cssVariableValue.root.dark;
364356

365357
assert(typeof colorDark === "string");
366-
assert(htmlColorRegexp.test(colorDark), `${colorDark} doesn't seem to be a color`);
358+
assert(cssColorRegexp.test(colorDark), `${colorDark} doesn't seem to be a color`);
367359

368360
const parsedName = parseColorOptionName(property);
369361

src/bin/css_to_ts/cssVariable.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { assert } from "tsafe/assert";
44
import type { Equals } from "tsafe";
55
import { exclude } from "tsafe/exclude";
66
import memoize from "memoizee";
7+
import type { ColorScheme } from "./colorOptions";
8+
9+
const orderedColorScheme = ["light", "dark"] as const;
10+
11+
assert<Equals<typeof orderedColorScheme[number], ColorScheme>>();
712

813
export type CssVariableValue = Record<
914
"root" | keyof MediaQueryByBreakpoint,
@@ -28,7 +33,7 @@ export const createGetCssVariable = memoize((rawCssCode: string) => {
2833
orderedBreakpoints.forEach(breakpoint => {
2934
const rules = rulesByBreakpoint[breakpoint];
3035

31-
(["light", "dark"] as const).forEach(colorScheme => {
36+
orderedColorScheme.forEach(colorScheme => {
3237
const [declaration] = rules
3338
.filter(
3439
(rule: any) =>
@@ -60,8 +65,6 @@ export const createGetCssVariable = memoize((rawCssCode: string) => {
6065
);
6166
}
6267

63-
//TODO: Fix! It's more complex than that!
64-
6568
const value = declaration?.value as string | undefined;
6669

6770
if (value === undefined) {
@@ -70,10 +73,26 @@ export const createGetCssVariable = memoize((rawCssCode: string) => {
7073

7174
const match = value.match(/^var\((--[^)]+)\)$/);
7275

73-
cssVariableValue[breakpoint][colorScheme] =
74-
match === null
75-
? value
76-
: getCssVariable(match[1] as any)[breakpoint][colorScheme];
76+
for (
77+
let i = orderedBreakpoints.indexOf(breakpoint);
78+
i < orderedBreakpoints.length;
79+
i++
80+
) {
81+
const breakpoint = orderedBreakpoints[i];
82+
83+
for (
84+
let j = orderedColorScheme.indexOf(colorScheme);
85+
j < orderedColorScheme.length;
86+
j++
87+
) {
88+
const colorScheme = orderedColorScheme[j];
89+
90+
(cssVariableValue[breakpoint] ??= {} as any)[colorScheme] =
91+
match === null
92+
? value
93+
: getCssVariable(match[1] as any)[breakpoint][colorScheme];
94+
}
95+
}
7796
});
7897
});
7998

src/bin/tools/cssColorRegexp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const cssColorRegexp =
2+
/(#(?:[0-9a-fA-F]{2}){2,4}$|(#[0-9a-fA-F]{3}$)|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\)$|black$|silver$|gray$|whitesmoke$|maroon$|red$|purple$|fuchsia$|green$|lime$|olivedrab$|yellow$|navy$|blue$|teal$|aquamarine$|orange$|aliceblue$|antiquewhite$|aqua$|azure$|beige$|bisque$|blanchedalmond$|blueviolet$|brown$|burlywood$|cadetblue$|chartreuse$|chocolate$|coral$|cornflowerblue$|cornsilk$|crimson$|darkblue$|darkcyan$|darkgoldenrod$|darkgray$|darkgreen$|darkgrey$|darkkhaki$|darkmagenta$|darkolivegreen$|darkorange$|darkorchid$|darkred$|darksalmon$|darkseagreen$|darkslateblue$|darkslategray$|darkslategrey$|darkturquoise$|darkviolet$|deeppink$|deepskyblue$|dimgray$|dimgrey$|dodgerblue$|firebrick$|floralwhite$|forestgreen$|gainsboro$|ghostwhite$|goldenrod$|gold$|greenyellow$|grey$|honeydew$|hotpink$|indianred$|indigo$|ivory$|khaki$|lavenderblush$|lavender$|lawngreen$|lemonchiffon$|lightblue$|lightcoral$|lightcyan$|lightgoldenrodyellow$|lightgray$|lightgreen$|lightgrey$|lightpink$|lightsalmon$|lightseagreen$|lightskyblue$|lightslategray$|lightslategrey$|lightsteelblue$|lightyellow$|limegreen$|linen$|mediumaquamarine$|mediumblue$|mediumorchid$|mediumpurple$|mediumseagreen$|mediumslateblue$|mediumspringgreen$|mediumturquoise$|mediumvioletred$|midnightblue$|mintcream$|mistyrose$|moccasin$|navajowhite$|oldlace$|olive$|orangered$|orchid$|palegoldenrod$|palegreen$|paleturquoise$|palevioletred$|papayawhip$|peachpuff$|peru$|pink$|plum$|powderblue$|rosybrown$|royalblue$|saddlebrown$|salmon$|sandybrown$|seagreen$|seashell$|sienna$|skyblue$|slateblue$|slategray$|slategrey$|snow$|springgreen$|steelblue$|tan$|thistle$|tomato$|transparent$|turquoise$|violet$|wheat$|white$|yellowgreen$|rebeccapurple$)/i;

src/test/bin/breakpoints/generateBreakpointsTsCode.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ assert<Extends<typeof breakpointValues, Record<BreakpointKeys, number>>>();
6565

6666
const got = generateBreakpointsTsCode(input);
6767

68-
console.log(JSON.stringify(got));
69-
console.log(JSON.stringify(expected));
70-
7168
assert(got === expected);
7269

7370
console.log("PASS");

src/test/bin/colorDecisions/generateGetColorDecisionsTsCode.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ console.log(`Running test ${__filename}`);
55

66
const input = `
77
:root {
8-
--grey-1000-50-hover: #FFFFFF;
9-
--grey-1000-50: #FFFFFF;
10-
--orange-terre-battue-850-200: #FFFFFF;
11-
--grey-975-100-hover: #FFFFFF;
12-
--grey-950-150: #FFFFFF;
8+
--grey-1000-50-hover: #ffffff;
9+
--grey-1000-50: #ffffff;
10+
--orange-terre-battue-850-200: #ffffff;
11+
--grey-975-100-hover: #ffffff;
12+
--grey-950-150: #ffffff;
1313
1414
--background-default-grey-hover: var(--grey-1000-50-hover);
1515
--background-default-grey: var(--grey-1000-50);
@@ -75,6 +75,6 @@ export function getColorDecisions(
7575

7676
const got = generateGetColorDecisionsTsCode(input);
7777

78-
assert(got === expected, "xxxx::");
78+
assert(got === expected);
7979

8080
console.log("PASS");

src/test/bin/colorDecisions/parseColorDecision.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ console.log(`Running test ${__filename}`);
77

88
const rawCssCode = `
99
:root {
10-
--grey-1000-50-hover: #FFFFFF;
11-
--grey-1000-50: #FFFFFF;
12-
--orange-terre-battue-850-200: #FFFFFF;
13-
--grey-975-100-hover: #FFFFFF;
14-
--grey-950-150: #FFFFFF;
10+
--grey-1000-50-hover: #ffffff;
11+
--grey-1000-50: #ffffff;
12+
--orange-terre-battue-850-200: #ffffff;
13+
--grey-975-100-hover: #ffffff;
14+
--grey-950-150: #ffffff;
1515
1616
--background-default-grey-hover: var(--grey-1000-50-hover);
1717
--background-default-grey: var(--grey-1000-50);

src/test/bin/colorDecisions/parseColorDecisionName.test.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ console.log(`Running test ${__filename}`);
77

88
const rawCssCode = `
99
:root {
10-
--grey-1000-50-hover: #FFFFFF;
11-
--grey-1000-50: #FFFFFF;
12-
--orange-terre-battue-850-200: #FFFFFF;
13-
--grey-975-100-hover: #FFFFFF;
14-
--grey-950-150: #FFFFFF;
15-
--blue-france-sun-113-625: #FFFFFF;
10+
--grey-1000-50-hover: #ffffff;
11+
--grey-1000-50: #ffffff;
12+
--orange-terre-battue-850-200: #ffffff;
13+
--grey-975-100-hover: #ffffff;
14+
--grey-950-150: #ffffff;
15+
--blue-france-sun-113-625: #ffffff;
1616
1717
--background-default-grey-hover: var(--grey-1000-50-hover);
1818
--background-default-grey: var(--grey-1000-50);
@@ -38,12 +38,8 @@ const rawCssCode = `
3838
@media (min-width: 62em) { }
3939
`;
4040

41-
console.log("before");
42-
4341
const { parseColorDecisionName } = createParseColorDecisionName(rawCssCode);
4442

45-
console.log("after");
46-
4743
{
4844
const expected: ParsedColorDecisionName = {
4945
"context": "background",

src/test/bin/colorOptions/generateGetColorOptionsTsCode.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ const input = `
4848
--name1-name2-sun-111-hover: #000003;
4949
--blue-france-main-525: #00000f;
5050
}
51+
52+
@media (min-width: 36em) { }
53+
@media (min-width: 48em) { }
54+
@media (min-width: 78em) { }
55+
@media (min-width: 62em) { }
5156
`;
5257

5358
const expected = `

src/test/bin/colorOptions/parseColorOptions.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const rawCssCode = `
5050
--name1-name2-sun-111-hover: #000003;
5151
--blue-france-main-525: #00000f;
5252
}
53+
54+
@media (min-width: 36em) { }
55+
@media (min-width: 48em) { }
56+
@media (min-width: 78em) { }
57+
@media (min-width: 62em) { }
5358
`;
5459

5560
const got = parseColorOptions(rawCssCode);

0 commit comments

Comments
 (0)