Skip to content

Commit 52f4410

Browse files
committed
refactor: fix tests
1 parent db35815 commit 52f4410

File tree

12 files changed

+181
-85
lines changed

12 files changed

+181
-85
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @see https://prettier.io/docs/en/configuration.html
3+
* @type {import("prettier").Config}
4+
*/
5+
const config = {};
6+
7+
export default config;

src/__tests__/babel/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../../tsconfig.json",
3+
"include": [
4+
"./*"
5+
]
6+
}

src/compiler/__tests__/@prop.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ test("@prop single", () => {
1818
d: [
1919
{
2020
color: "#f00",
21+
myBackgroundColor: "#00f",
2122
},
22-
["#00f", ["myBackgroundColor"]],
2323
],
2424
s: [1, 1],
2525
},
@@ -29,7 +29,7 @@ test("@prop single", () => {
2929
});
3030
});
3131

32-
test.only("@prop single, nested value", () => {
32+
test("@prop single, nested value", () => {
3333
const compiled = compile(`
3434
.test {
3535
color: red;
@@ -164,8 +164,10 @@ test("@prop multiple", () => {
164164
[
165165
{
166166
d: [
167-
["#f00", ["myColor"]],
168-
["#00f", ["myBackgroundColor"]],
167+
{
168+
myBackgroundColor: "#00f",
169+
myColor: "#f00",
170+
},
169171
],
170172
s: [1, 1],
171173
},

src/compiler/__tests__/compiler.test.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test("hello world", () => {
1414
{
1515
d: [
1616
{
17-
color: "rgb(100% 0% 0%)",
17+
color: "#f00",
1818
},
1919
],
2020
s: [1, 1],
@@ -105,6 +105,47 @@ test.skip("preserves unused CSS variables with preserve-variables", () => {
105105
});
106106
});
107107

108+
test("multiple rules with same selector", () => {
109+
const compiled = compile(`
110+
.redOrGreen:hover {
111+
color: green;
112+
}
113+
114+
.redOrGreen {
115+
color: red;
116+
}
117+
`);
118+
119+
expect(compiled).toStrictEqual({
120+
s: [
121+
[
122+
"redOrGreen",
123+
[
124+
{
125+
d: [
126+
{
127+
color: "#f00",
128+
},
129+
],
130+
s: [2, 1],
131+
},
132+
{
133+
d: [
134+
{
135+
color: "#008000",
136+
},
137+
],
138+
p: {
139+
h: 1,
140+
},
141+
s: [1, 2],
142+
},
143+
],
144+
],
145+
],
146+
});
147+
});
148+
108149
test.skip("transitions", () => {
109150
const compiled = compile(`
110151
.test {

src/compiler/attributes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test("multiple classes", () => {
1515
aq: [["a", "className", "*=", "my-class"]],
1616
d: [
1717
{
18-
color: "rgb(100% 0% 0%)",
18+
color: "#f00",
1919
},
2020
],
2121
s: [1, 2],

src/compiler/compiler.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type Visitor,
1212
} from "lightningcss";
1313

14+
import { maybeMutateReactNativeOptions, parsePropAtRule } from "./atRules";
1415
import type {
1516
CompilerOptions,
1617
ContainerQuery,
@@ -20,7 +21,7 @@ import { parseContainerCondition } from "./container-query";
2021
import { parseDeclaration } from "./declarations";
2122
import { extractKeyFrames } from "./keyframes";
2223
import { parseMediaQuery } from "./media-query";
23-
import { maybeMutateReactNativeOptions, parsePropAtRule } from "./atRules";
24+
import { getSelectors } from "./selectors";
2425
import { StylesheetBuilder } from "./stylesheet";
2526

2627
/**
@@ -37,6 +38,10 @@ export function compile(
3738
const { logger = debug("react-native-css") } = options;
3839
const features = Object.assign({}, options.features);
3940

41+
if (options.selectorPrefix && options.selectorPrefix.startsWith(".")) {
42+
options.selectorPrefix = options.selectorPrefix.slice(1);
43+
}
44+
4045
logger(`Features ${JSON.stringify(features)}`);
4146

4247
const builder = new StylesheetBuilder(options);
@@ -131,24 +136,31 @@ function extractRule(rule: Rule, builder: StylesheetBuilder) {
131136
case "style": {
132137
// If the rule is a style declaration, extract it with the `getExtractedStyle` function and store it in the `declarations` map
133138
builder = builder.fork("style");
134-
builder.newRule(parsePropAtRule(rule.value.rules));
135139

136140
const declarationBlock = rule.value.declarations;
141+
const mapping = parsePropAtRule(rule.value.rules);
142+
const selectors = getSelectors(
143+
rule.value.selectors,
144+
false,
145+
builder.getOptions(),
146+
);
137147

138148
if (declarationBlock) {
139149
if (declarationBlock.declarations) {
150+
builder.newRule(mapping);
140151
for (const declaration of declarationBlock.declarations) {
141152
parseDeclaration(declaration, builder);
142153
}
154+
builder.applyRuleToSelectors(selectors);
143155
}
144156

145157
if (declarationBlock.importantDeclarations) {
158+
builder.newRule(mapping, { important: true });
146159
for (const declaration of declarationBlock.importantDeclarations) {
147160
parseDeclaration(declaration, builder);
148161
}
162+
builder.applyRuleToSelectors(selectors);
149163
}
150-
151-
builder.applyRuleToSelectors(rule.value.selectors);
152164
}
153165
break;
154166
}

src/compiler/declarations.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ export function parseColor(cssColor: CssColor, builder: StylesheetBuilder) {
12911291

12921292
let color: Color | undefined;
12931293

1294-
const { hexColors = true, colorPrecision = 3 } = builder.getOptions();
1294+
const { hexColors = true, colorPrecision } = builder.getOptions();
12951295

12961296
switch (cssColor.type) {
12971297
case "currentcolor":
@@ -1411,8 +1411,8 @@ export function parseColor(cssColor: CssColor, builder: StylesheetBuilder) {
14111411
}
14121412
}
14131413

1414-
if (hexColors || colorPrecision) {
1415-
return color?.toString({ precision: colorPrecision });
1414+
if (!hexColors || colorPrecision) {
1415+
return color?.toString({ precision: colorPrecision ?? 3 });
14161416
} else {
14171417
return color?.toString({ format: "hex" });
14181418
}
@@ -1887,7 +1887,9 @@ export function parseFontSizeDeclaration(
18871887
declaration: DeclarationType<"font-size">,
18881888
builder: StylesheetBuilder,
18891889
) {
1890-
return parseFontSize(declaration.value, builder);
1890+
const fontSize = parseFontSize(declaration.value, builder);
1891+
builder.addDescriptor("fontSize", fontSize);
1892+
builder.addDescriptor("--__rn-css-em", fontSize);
18911893
}
18921894

18931895
export function parseFontSize(value: FontSize, builder: StylesheetBuilder) {

src/compiler/inheritance.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ test("nested classes", () => {
88

99
expect(compiled).toStrictEqual({
1010
s: [
11+
[
12+
"my-class",
13+
[
14+
{
15+
c: ["my-class"],
16+
s: [0],
17+
},
18+
],
19+
],
1120
[
1221
"test",
1322
[
1423
{
1524
cq: [{ n: "my-class" }],
16-
d: [{ color: "rgb(100% 0% 0%)" }],
25+
d: [{ color: "#f00" }],
1726
s: [1, 2],
1827
},
1928
],
@@ -53,7 +62,7 @@ test("multiple tiers classes", () => {
5362
[
5463
{
5564
cq: [{ n: "one" }, { n: "two" }],
56-
d: [{ color: "rgb(100% 0% 0%)" }],
65+
d: [{ color: "#f00" }],
5766
s: [1, 3],
5867
},
5968
],
@@ -83,7 +92,6 @@ test("tiers with multiple classes", () => {
8392
"three",
8493
[
8594
{
86-
aq: [["a", "className", "*=", "two"]],
8795
c: ["three"],
8896
s: [0],
8997
},
@@ -100,7 +108,7 @@ test("tiers with multiple classes", () => {
100108
n: "three",
101109
},
102110
],
103-
d: [{ color: "rgb(100% 0% 0%)" }],
111+
d: [{ color: "#f00" }],
104112
s: [1, 4],
105113
},
106114
],

src/compiler/media-query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import type {
1313
MediaFeatureComparison,
1414
StyleDescriptor,
1515
} from "./compiler.types";
16-
import type { StylesheetBuilder } from "./stylesheet";
1716
import { parseLength } from "./declarations";
17+
import type { StylesheetBuilder } from "./stylesheet";
1818

1919
export function parseMediaQuery(
2020
query: CSSMediaQuery,
2121
builder: StylesheetBuilder,
22-
): MediaCondition | undefined {
22+
) {
2323
let platformCondition: MediaCondition | undefined;
2424
let condition: MediaCondition | undefined;
2525

@@ -57,7 +57,7 @@ export function parseMediaQuery(
5757
mediaQuery = ["!", mediaQuery];
5858
}
5959

60-
return mediaQuery;
60+
builder.addMediaQuery(mediaQuery);
6161
}
6262

6363
function parseMediaQueryCondition(

src/compiler/selectors.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ function classNameSelector(
137137
let currentContainerQuery: ContainerQuery | undefined;
138138

139139
let isInClassBlock = false;
140+
let newBlock = false;
140141

141142
function getAttributeQuery() {
143+
if (newBlock) {
144+
currentContainerQuery ??= {};
145+
}
146+
142147
if (currentContainerQuery) {
143148
if (!currentContainerQuery.a) {
144149
currentContainerQuery.a = [];
@@ -151,6 +156,10 @@ function classNameSelector(
151156
}
152157

153158
function getPseudoClassesQuery() {
159+
if (newBlock) {
160+
currentContainerQuery ??= {};
161+
}
162+
154163
if (currentContainerQuery) {
155164
if (!currentContainerQuery.p) {
156165
currentContainerQuery.p = {};
@@ -180,23 +189,24 @@ function classNameSelector(
180189
} else if (isInClassBlock) {
181190
getAttributeQuery().unshift(["a", "className", "*=", component.name]);
182191
} else if (component.name !== options.selectorPrefix) {
183-
if (currentContainerQuery) {
192+
if (currentContainerQuery?.n) {
184193
getAttributeQuery().unshift([
185194
"a",
186195
"className",
187196
"*=",
188197
component.name,
189198
]);
190199
} else {
191-
currentContainerQuery = {
192-
n: component.name,
193-
};
200+
currentContainerQuery ??= {};
201+
currentContainerQuery.n = component.name;
202+
194203
containerQuery ??= [];
195204
containerQuery.unshift(currentContainerQuery);
196205
}
197206
}
198207

199208
isInClassBlock = true;
209+
newBlock = false;
200210

201211
specificity[Specificity.ClassName] =
202212
(specificity[Specificity.ClassName] ?? 0) + 1;
@@ -238,9 +248,12 @@ function classNameSelector(
238248
getAttributeQuery().push(["a", "children", "!"]);
239249
break;
240250
}
251+
default: {
252+
// We don't support other pseudo-classes
253+
return null;
254+
}
241255
}
242-
243-
return null;
256+
break;
244257
}
245258
case "attribute": {
246259
// We don't support attribute selectors as standalone selectors
@@ -322,6 +335,7 @@ function classNameSelector(
322335
// We only support the descendant combinator
323336
if (component.value === "descendant") {
324337
isInClassBlock = false;
338+
newBlock = true;
325339
currentContainerQuery = undefined;
326340
break;
327341
}

0 commit comments

Comments
 (0)