-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathatRules.ts
More file actions
149 lines (123 loc) · 3.71 KB
/
atRules.ts
File metadata and controls
149 lines (123 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type {
DeclarationBlock,
ParsedComponent,
Rule,
TokenOrValue,
} from "lightningcss";
import type { StyleRuleMapping } from "./compiler.types";
import { toRNProperty } from "./selectors";
import { splitByDelimiter } from "./split-by-delimiter";
import type { StylesheetBuilder } from "./stylesheet";
export interface PropAtRule {
type: "unknown";
value: {
name: "prop";
prelude: Extract<TokenOrValue, { type: "token" }>[];
block: Extract<TokenOrValue, { type: "token" }>[] | null;
};
}
/***********************************************
* @react-native *
***********************************************/
export interface ReactNativeAtRule {
type: "custom";
value: {
name: "react-native";
prelude: null | Extract<ParsedComponent, { type: "repeated" }>;
body: {
type: "declaration-list";
value: Pick<DeclarationBlock, "declarations">;
};
};
}
export function maybeMutateReactNativeOptions(
rule: Rule | ReactNativeAtRule,
builder: StylesheetBuilder,
) {
if (rule.type !== "custom" || rule.value?.name !== "react-native") {
return;
}
const { declarations } = rule.value.body.value;
if (!declarations) return;
for (const declaration of declarations) {
if (declaration.property !== "custom") continue;
switch (declaration.value.name) {
case "preserve-variables": {
declaration.value.value.forEach((token) => {
if (token.type !== "dashed-ident") {
return;
}
if (token.value !== "true" && token.value !== "false") {
return;
}
builder.setOptions("preserveVariables", token.value === "true");
});
break;
}
default:
break;
}
}
}
/***********************************************
* @prop *
***********************************************/
function isPropAtRule(rule: Rule | PropAtRule): rule is PropAtRule {
return rule.type === "unknown" && rule.value.name === "prop";
}
export function parsePropAtRule(rules?: (Rule | PropAtRule)[]) {
const mapping: StyleRuleMapping = {};
if (!rules) return mapping;
for (const rule of rules) {
if (!isPropAtRule(rule)) continue;
if (rule.value.prelude.length > 0) {
const prelude = rule.value.prelude.filter((item) => {
return item.value.type !== "white-space";
});
propAtRuleBlock(prelude, mapping);
} else if (rule.value.block) {
// Remove all whitespace tokens
const blocks = rule.value.block.filter((item) => {
return item.value.type !== "white-space";
});
// Separate each rule, delimited by a semicolon
const blockRules = splitByDelimiter(blocks, (item) => {
return item.value.type === "semicolon";
});
for (const block of blockRules) {
propAtRuleBlock(block, mapping);
}
}
}
return mapping;
}
function propAtRuleBlock(
token: Extract<TokenOrValue, { type: "token" }>[],
mapping: StyleRuleMapping = {},
): StyleRuleMapping {
const [from, to] = splitByDelimiter(token, (item) => {
return item.value.type === "colon";
});
if (!from || from.length !== 1) {
return mapping;
}
const fromToken = from[0];
if (!fromToken || fromToken.value.type !== "ident") {
return mapping;
}
if (!to) {
mapping["*"] = toRNProperty(fromToken.value.value);
return mapping;
}
mapping[toRNProperty(fromToken.value.value)] = to.flatMap((item, index) => {
switch (item.value.type) {
case "delim":
return index === 0 && item.value.value === "*" ? ["*"] : [];
case "ident":
return [toRNProperty(item.value.value)];
default:
return [];
}
});
return mapping;
}