Skip to content

Commit a5bb785

Browse files
committed
feat: 增加权重逻辑
1 parent fca94e6 commit a5bb785

File tree

9 files changed

+181
-149
lines changed

9 files changed

+181
-149
lines changed

__test__/fixure/pesudo.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { View, Text } from '@tarojs/components'
22
import './pesudo.scss'
33

4-
function Pesudo() {
4+
export default function Pesudo() {
55

66
return <View className='pesudo'>
77
<Text className='text'>asdasdasdasdasd</Text>

__test__/fixure/pesudo.scss

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
color: red;
99
}
1010

11-
.a, .a .b {
12-
font-size: 20px;
13-
text-overflow: ellipsis;
14-
}
11+
12+
.aaa {
13+
font-size: 20px;
14+
-webkit-line-clamp: 2;
15+
text-overflow: ellipsis;
16+
color: #00f;
17+
}

__test__/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ process.env.platform = 'arm64'
44
const { parse } = require('../index.js')
55
const component = fs.readFileSync(path.resolve(__dirname, 'fixure/pesudo.jsx'), 'utf8')
66
const css1 = fs.readFileSync(path.resolve(__dirname, 'fixure/pesudo.scss'), 'utf8')
7-
const code = parse(component, [css1], "Harmony")
7+
const code = parse(component, [css1], {
8+
platformString: 'Harmony',
9+
isEnableNesting: true
10+
})
811

912
console.log(code)

__test__/index.spec.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ test('test parse Harmony', (t) => {
1414
const css1 = fs.readFileSync(path.resolve(__dirname, 'fixure/Mod.scss'), 'utf8')
1515
const css2 = fs.readFileSync(path.resolve(__dirname, 'fixure/test.scss'), 'utf8')
1616

17-
const code = parse(jsx, [css1, css2], "Harmony")
17+
const code = parse(jsx, [css1, css2], {
18+
platformString: 'Harmony',
19+
isEnableNesting: true
20+
})
1821
t.snapshot(code)
1922
})
2023

@@ -23,6 +26,9 @@ test('test parse ReactNative', (t) => {
2326
const css1 = fs.readFileSync(path.resolve(__dirname, 'fixure/Mod.scss'), 'utf8')
2427
const css2 = fs.readFileSync(path.resolve(__dirname, 'fixure/test.scss'), 'utf8')
2528

26-
const code = parse(jsx, [css1, css2], "ReactNative")
29+
const code = parse(jsx, [css1, css2], {
30+
platformString: 'ReactNative',
31+
isEnableNesting: true
32+
})
2733
t.snapshot(code)
2834
})

__test__/index.spec.mjs.md

Lines changed: 133 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Generated by [AVA](https://avajs.dev).
1010
1111
`import React from 'react';␊
1212
import './Mod.scss';␊
13+
import { calcDynamicStyle, convertNumber2VP, __combine_nesting_style__ } from "@tarojs/runtime";␊
1314
let __inner_style_data__;␊
1415
function __inner_style__() {␊
1516
if (__inner_style_data__) return __inner_style_data__;␊
@@ -326,7 +327,6 @@ Generated by [AVA](https://avajs.dev).
326327
};␊
327328
return __inner_style_data__;␊
328329
}␊
329-
import { calcDynamicStyle, convertNumber2VP } from "@tarojs/runtime";␊
330330
function Bb() {␊
331331
return /*#__PURE__*/ React.createElement("div", {␊
332332
className: boxClass.join(' '),␊
@@ -935,6 +935,138 @@ Generated by [AVA](https://avajs.dev).
935935
import { StyleSheet } from "react-native";␊
936936
import { scalePx2dp, scaleVu2dp } from "@tarojs/runtime-rn";␊
937937
function ignoreStyleFileCache() {}␊
938+
function Bb() {␊
939+
return /*#__PURE__*/ React.createElement("div", {␊
940+
className: boxClass.join(' '),␊
941+
style: calcDynamicStyle(__inner_style__(), boxClass.join(' '), bannerStyle)␊
942+
}, /*#__PURE__*/ React.createElement("div", {␊
943+
className: "cnt_row1",␊
944+
style: {␊
945+
alignItems: "center",␊
946+
display: "flex",␊
947+
gap: scalePx2dp(10),␊
948+
height: scalePx2dp(50),␊
949+
justifyContent: "flex-start",␊
950+
position: "relative",␊
951+
width: scalePx2dp(311)␊
952+
}␊
953+
}, /*#__PURE__*/ React.createElement("img", {␊
954+
className: "img",␊
955+
src: "//img12.360buyimg.com/img/jfs/t1/100881/15/44805/18567/64f58062F1b45e0cb/caf065a7410087ce.png",␊
956+
style: {␊
957+
width: scalePx2dp(50),␊
958+
height: scalePx2dp(50)␊
959+
}␊
960+
}), /*#__PURE__*/ React.createElement("div", {␊
961+
className: "cnt_col",␊
962+
style: {␊
963+
display: "flex",␊
964+
flexDirection: "column",␊
965+
alignItems: "flex-start",␊
966+
justifyContent: "center",␊
967+
gap: scalePx2dp(8),␊
968+
width: scalePx2dp(145),␊
969+
height: scalePx2dp(50)␊
970+
}␊
971+
}, /*#__PURE__*/ React.createElement("span", {␊
972+
className: "line1 instruction1",␊
973+
style: {␊
974+
whiteSpace: "nowrap",␊
975+
overflow: "hidden",␊
976+
flex: \`2 2 10%\`,␊
977+
borderRadius: "50%",␊
978+
alignItems: "flex-end",␊
979+
color: "#000",␊
980+
fontSize: scalePx2dp(16),␊
981+
flexShrink: 0,␊
982+
width: scalePx2dp(116),␊
983+
display: "flex"␊
984+
}␊
985+
}, "巴拉巴拉小魔仙"), /*#__PURE__*/ React.createElement("span", {␊
986+
className: "line1 txt",␊
987+
style: {␊
988+
whiteSpace: "nowrap",␊
989+
overflow: "hidden",␊
990+
flex: \`2 2 10%\`,␊
991+
borderRadius: "50%",␊
992+
alignItems: "flex-end",␊
993+
color: "#999",␊
994+
fontSize: scalePx2dp(12),␊
995+
flexShrink: 0,␊
996+
display: "flex"␊
997+
}␊
998+
}, "成员: 4000+")), /*#__PURE__*/ React.createElement("div", {␊
999+
className: "cnt_row2",␊
1000+
style: {␊
1001+
alignItems: "flex-start",␊
1002+
borderColor: "#999",␊
1003+
borderRadius: scalePx2dp(12),␊
1004+
borderStyle: "solid",␊
1005+
borderTopLeftRadius: scalePx2dp(1000),␊
1006+
borderWidth: scalePx2dp(1),␊
1007+
display: "flex",␊
1008+
flexShrink: 0,␊
1009+
gap: scalePx2dp(-94),␊
1010+
height: scalePx2dp(23),␊
1011+
justifyContent: "space-between",␊
1012+
left: scalePx2dp(201),␊
1013+
paddingBottom: scalePx2dp(4),␊
1014+
paddingLeft: scalePx2dp(8),␊
1015+
paddingRight: scalePx2dp(8),␊
1016+
paddingTop: scalePx2dp(4),␊
1017+
position: "absolute",␊
1018+
top: scalePx2dp(3),␊
1019+
width: scalePx2dp(110),␊
1020+
zIndex: 12␊
1021+
}␊
1022+
}, this.getDom(), /*#__PURE__*/ React.createElement("img", {␊
1023+
className: "icon1",␊
1024+
src: "//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png",␊
1025+
style: {␊
1026+
width: scalePx2dp(15),␊
1027+
height: scalePx2dp(15)␊
1028+
}␊
1029+
}), /*#__PURE__*/ React.createElement("span", {␊
1030+
className: "instruction2",␊
1031+
style: {␊
1032+
color: "#999",␊
1033+
fontSize: scalePx2dp(11),␊
1034+
textAlign: "center",␊
1035+
flexShrink: 0,␊
1036+
overflow: "hidden"␊
1037+
}␊
1038+
}, "slslsl-jsj"), /*#__PURE__*/ React.createElement("span", {␊
1039+
className: "txt1",␊
1040+
style: {␊
1041+
color: "#222",␊
1042+
fontSize: scalePx2dp(11),␊
1043+
textAlign: "center",␊
1044+
flexShrink: 0␊
1045+
}␊
1046+
}, "复制"))), /*#__PURE__*/ React.createElement("span", {␊
1047+
className: "line1 txt",␊
1048+
style: {␊
1049+
whiteSpace: "nowrap",␊
1050+
overflow: "hidden",␊
1051+
flex: \`2 2 10%\`,␊
1052+
borderRadius: "50%",␊
1053+
alignItems: "flex-end",␊
1054+
color: "#999",␊
1055+
fontSize: scalePx2dp(12),␊
1056+
flexShrink: 0,␊
1057+
display: "flex"␊
1058+
}␊
1059+
}, "成员123: 4000+"), /*#__PURE__*/ React.createElement("div", {␊
1060+
className: "cnt_row4"␊
1061+
}, /*#__PURE__*/ React.createElement("img", {␊
1062+
className: "icon2",␊
1063+
src: "//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png"␊
1064+
}), /*#__PURE__*/ React.createElement("span", {␊
1065+
className: "instruction3"␊
1066+
}, "slslsl-jsj"), /*#__PURE__*/ React.createElement("span", {␊
1067+
className: "txt3"␊
1068+
}, "复制")));␊
1069+
}␊
9381070
let __inner_style_data__;␊
9391071
function __inner_style__() {␊
9401072
if (__inner_style_data__) return __inner_style_data__;␊
@@ -1161,138 +1293,6 @@ Generated by [AVA](https://avajs.dev).
11611293
};␊
11621294
return __inner_style_data__;␊
11631295
}␊
1164-
function Bb() {␊
1165-
return /*#__PURE__*/ React.createElement("div", {␊
1166-
className: boxClass.join(' '),␊
1167-
style: calcDynamicStyle(__inner_style__(), boxClass.join(' '), bannerStyle)␊
1168-
}, /*#__PURE__*/ React.createElement("div", {␊
1169-
className: "cnt_row1",␊
1170-
style: {␊
1171-
alignItems: "center",␊
1172-
display: "flex",␊
1173-
gap: scalePx2dp(10),␊
1174-
height: scalePx2dp(50),␊
1175-
justifyContent: "flex-start",␊
1176-
position: "relative",␊
1177-
width: scalePx2dp(311)␊
1178-
}␊
1179-
}, /*#__PURE__*/ React.createElement("img", {␊
1180-
className: "img",␊
1181-
src: "//img12.360buyimg.com/img/jfs/t1/100881/15/44805/18567/64f58062F1b45e0cb/caf065a7410087ce.png",␊
1182-
style: {␊
1183-
width: scalePx2dp(50),␊
1184-
height: scalePx2dp(50)␊
1185-
}␊
1186-
}), /*#__PURE__*/ React.createElement("div", {␊
1187-
className: "cnt_col",␊
1188-
style: {␊
1189-
display: "flex",␊
1190-
flexDirection: "column",␊
1191-
alignItems: "flex-start",␊
1192-
justifyContent: "center",␊
1193-
gap: scalePx2dp(8),␊
1194-
width: scalePx2dp(145),␊
1195-
height: scalePx2dp(50)␊
1196-
}␊
1197-
}, /*#__PURE__*/ React.createElement("span", {␊
1198-
className: "line1 instruction1",␊
1199-
style: {␊
1200-
whiteSpace: "nowrap",␊
1201-
overflow: "hidden",␊
1202-
flex: \`2 2 10%\`,␊
1203-
borderRadius: "50%",␊
1204-
alignItems: "flex-end",␊
1205-
color: "#000",␊
1206-
fontSize: scalePx2dp(16),␊
1207-
flexShrink: 0,␊
1208-
width: scalePx2dp(116),␊
1209-
display: "flex"␊
1210-
}␊
1211-
}, "巴拉巴拉小魔仙"), /*#__PURE__*/ React.createElement("span", {␊
1212-
className: "line1 txt",␊
1213-
style: {␊
1214-
whiteSpace: "nowrap",␊
1215-
overflow: "hidden",␊
1216-
flex: \`2 2 10%\`,␊
1217-
borderRadius: "50%",␊
1218-
alignItems: "flex-end",␊
1219-
color: "#999",␊
1220-
fontSize: scalePx2dp(12),␊
1221-
flexShrink: 0,␊
1222-
display: "flex"␊
1223-
}␊
1224-
}, "成员: 4000+")), /*#__PURE__*/ React.createElement("div", {␊
1225-
className: "cnt_row2",␊
1226-
style: {␊
1227-
alignItems: "flex-start",␊
1228-
borderColor: "#999",␊
1229-
borderRadius: scalePx2dp(12),␊
1230-
borderStyle: "solid",␊
1231-
borderTopLeftRadius: scalePx2dp(1000),␊
1232-
borderWidth: scalePx2dp(1),␊
1233-
display: "flex",␊
1234-
flexShrink: 0,␊
1235-
gap: scalePx2dp(-94),␊
1236-
height: scalePx2dp(23),␊
1237-
justifyContent: "space-between",␊
1238-
left: scalePx2dp(201),␊
1239-
paddingBottom: scalePx2dp(4),␊
1240-
paddingLeft: scalePx2dp(8),␊
1241-
paddingRight: scalePx2dp(8),␊
1242-
paddingTop: scalePx2dp(4),␊
1243-
position: "absolute",␊
1244-
top: scalePx2dp(3),␊
1245-
width: scalePx2dp(110),␊
1246-
zIndex: 12␊
1247-
}␊
1248-
}, this.getDom(), /*#__PURE__*/ React.createElement("img", {␊
1249-
className: "icon1",␊
1250-
src: "//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png",␊
1251-
style: {␊
1252-
width: scalePx2dp(15),␊
1253-
height: scalePx2dp(15)␊
1254-
}␊
1255-
}), /*#__PURE__*/ React.createElement("span", {␊
1256-
className: "instruction2",␊
1257-
style: {␊
1258-
color: "#999",␊
1259-
fontSize: scalePx2dp(11),␊
1260-
textAlign: "center",␊
1261-
flexShrink: 0,␊
1262-
overflow: "hidden"␊
1263-
}␊
1264-
}, "slslsl-jsj"), /*#__PURE__*/ React.createElement("span", {␊
1265-
className: "txt1",␊
1266-
style: {␊
1267-
color: "#222",␊
1268-
fontSize: scalePx2dp(11),␊
1269-
textAlign: "center",␊
1270-
flexShrink: 0␊
1271-
}␊
1272-
}, "复制"))), /*#__PURE__*/ React.createElement("span", {␊
1273-
className: "line1 txt",␊
1274-
style: {␊
1275-
whiteSpace: "nowrap",␊
1276-
overflow: "hidden",␊
1277-
flex: \`2 2 10%\`,␊
1278-
borderRadius: "50%",␊
1279-
alignItems: "flex-end",␊
1280-
color: "#999",␊
1281-
fontSize: scalePx2dp(12),␊
1282-
flexShrink: 0,␊
1283-
display: "flex"␊
1284-
}␊
1285-
}, "成员123: 4000+"), /*#__PURE__*/ React.createElement("div", {␊
1286-
className: "cnt_row4"␊
1287-
}, /*#__PURE__*/ React.createElement("img", {␊
1288-
className: "icon2",␊
1289-
src: "//img11.360buyimg.com/img/jfs/t1/175578/35/40256/1981/64f58062Fddaf1a21/f1111d9988a65ccc.png"␊
1290-
}), /*#__PURE__*/ React.createElement("span", {␊
1291-
className: "instruction3"␊
1292-
}, "slslsl-jsj"), /*#__PURE__*/ React.createElement("span", {␊
1293-
className: "txt3"␊
1294-
}, "复制")));␊
1295-
}␊
12961296
function Cc() {␊
12971297
return <div className={boxClass.join(' ')} style={calcDynamicStyle(__inner_style__(), boxClass.join(' '), bannerStyle)}>␊
12981298

__test__/index.spec.mjs.snap

70 Bytes
Binary file not shown.

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> S
4141
_ => Platform::Harmony
4242
};
4343

44-
let is_enable_nesting = options.is_enable_nesting.map_or(false, |item| item);
44+
let mut is_enable_nesting = options.is_enable_nesting.map_or(false, |item| item);
4545

4646
// 解析组件文件
4747
let cm: Lrc<SourceMap> = Default::default();
@@ -55,6 +55,11 @@ pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> S
5555
style_parser.parse(&css);
5656
let style_data = style_parser.calc();
5757

58+
// 判断计算的结果是否会含有嵌套选择器
59+
if is_enable_nesting {
60+
is_enable_nesting = style_data.has_nesting;
61+
}
62+
5863
let program = Rc::new(RefCell::new(document.program.as_ref().unwrap().clone()));
5964
let jsx_record = Rc::new(RefCell::new(document.jsx_record.as_ref().unwrap().clone()));
6065
let mut style_write = StyleWrite::new(

0 commit comments

Comments
 (0)