Skip to content

Commit 5f7932c

Browse files
committed
feat: 支持解析hoc
1 parent 7522e87 commit 5f7932c

File tree

4 files changed

+292
-0
lines changed

4 files changed

+292
-0
lines changed

__test__/index.spec.mjs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,71 @@ test('Harmony attrbute test animation', t => {
760760
})
761761
t.snapshot(code)
762762
})
763+
764+
test('Harmony combine test function component', t => {
765+
const { code } = parse(`
766+
import { View } from '@tarojs/components'
767+
function FunctionComponent() {
768+
return <View className='index' />
769+
}
770+
`, [`
771+
.a > .b {
772+
height: 100px;
773+
}
774+
`], {
775+
platformString: 'Harmony'
776+
})
777+
t.snapshot(code)
778+
})
779+
780+
test('Harmony combine test arrow component', t => {
781+
const { code } = parse(`
782+
import { View } from '@tarojs/components'
783+
const ArrowComponent = () => {
784+
return <View className='index' />
785+
}
786+
`, [`
787+
.a > .b {
788+
height: 100px;
789+
}
790+
`], {
791+
platformString: 'Harmony'
792+
})
793+
t.snapshot(code)
794+
})
795+
796+
797+
test('Harmony combine test hoc', t => {
798+
const { code } = parse(`
799+
import { View } from '@tarojs/components'
800+
export default function Pesudo() {
801+
return () => {
802+
return <View className='index' />
803+
}
804+
}
805+
`, [`
806+
.a > .b {
807+
height: 100px;
808+
}
809+
`], {
810+
platformString: 'Harmony'
811+
})
812+
t.snapshot(code)
813+
})
814+
815+
816+
test('Harmony combine test useHoc', t => {
817+
const { code } = parse(`
818+
import { View } from '@tarojs/components'
819+
export const Pesudo = withLogin(() => {
820+
return <View className='index' />
821+
})
822+
`, [`
823+
.a > .b {
824+
height: 100px;
825+
}
826+
`], {
827+
platformString: 'Harmony'
828+
})
829+
t.snapshot(code)
830+
})

__test__/index.spec.mjs.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,211 @@ Generated by [AVA](https://avajs.dev).
13611361
};␊
13621362
export default Index;␊
13631363
`
1364+
1365+
## Harmony combine test function component
1366+
1367+
> Snapshot 1
1368+
1369+
`import { View } from '@tarojs/components';␊
1370+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1371+
let __inner_style_data__;␊
1372+
let __nesting_style_data__;␊
1373+
function __nesting_style__() {␊
1374+
if (__nesting_style_data__) return __nesting_style_data__;␊
1375+
__nesting_style_data__ = [␊
1376+
{␊
1377+
"selectors": [␊
1378+
"a",␊
1379+
" > ",␊
1380+
"b"␊
1381+
],␊
1382+
"declaration": {␊
1383+
height: convertNumber2VP(100)␊
1384+
}␊
1385+
}␊
1386+
];␊
1387+
return __nesting_style_data__;␊
1388+
}␊
1389+
function __inner_style__() {␊
1390+
if (__inner_style_data__) return __inner_style_data__;␊
1391+
__inner_style_data__ = {};␊
1392+
return __inner_style_data__;␊
1393+
}␊
1394+
function FunctionComponent() {␊
1395+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1396+
}␊
1397+
`
1398+
1399+
## Harmony combine test arrow component
1400+
1401+
> Snapshot 1
1402+
1403+
`import { View } from '@tarojs/components';␊
1404+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1405+
let __inner_style_data__;␊
1406+
let __nesting_style_data__;␊
1407+
function __nesting_style__() {␊
1408+
if (__nesting_style_data__) return __nesting_style_data__;␊
1409+
__nesting_style_data__ = [␊
1410+
{␊
1411+
"selectors": [␊
1412+
"a",␊
1413+
" > ",␊
1414+
"b"␊
1415+
],␊
1416+
"declaration": {␊
1417+
height: convertNumber2VP(100)␊
1418+
}␊
1419+
}␊
1420+
];␊
1421+
return __nesting_style_data__;␊
1422+
}␊
1423+
function __inner_style__() {␊
1424+
if (__inner_style_data__) return __inner_style_data__;␊
1425+
__inner_style_data__ = {};␊
1426+
return __inner_style_data__;␊
1427+
}␊
1428+
const ArrowComponent = ()=>{␊
1429+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1430+
};␊
1431+
`
1432+
1433+
## Harmony combine test hoc
1434+
1435+
> Snapshot 1
1436+
1437+
`import { View } from '@tarojs/components';␊
1438+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1439+
let __inner_style_data__;␊
1440+
let __nesting_style_data__;␊
1441+
function __nesting_style__() {␊
1442+
if (__nesting_style_data__) return __nesting_style_data__;␊
1443+
__nesting_style_data__ = [␊
1444+
{␊
1445+
"selectors": [␊
1446+
"a",␊
1447+
" > ",␊
1448+
"b"␊
1449+
],␊
1450+
"declaration": {␊
1451+
height: convertNumber2VP(100)␊
1452+
}␊
1453+
}␊
1454+
];␊
1455+
return __nesting_style_data__;␊
1456+
}␊
1457+
function __inner_style__() {␊
1458+
if (__inner_style_data__) return __inner_style_data__;␊
1459+
__inner_style_data__ = {};␊
1460+
return __inner_style_data__;␊
1461+
}␊
1462+
export default function Pesudo() {␊
1463+
return ()=>{␊
1464+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1465+
};␊
1466+
}␊
1467+
`
1468+
1469+
## Harmony combine test useHoc
1470+
1471+
> Snapshot 1
1472+
1473+
`import { View } from '@tarojs/components';␊
1474+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1475+
let __inner_style_data__;␊
1476+
let __nesting_style_data__;␊
1477+
function __nesting_style__() {␊
1478+
if (__nesting_style_data__) return __nesting_style_data__;␊
1479+
__nesting_style_data__ = [␊
1480+
{␊
1481+
"selectors": [␊
1482+
"a",␊
1483+
" > ",␊
1484+
"b"␊
1485+
],␊
1486+
"declaration": {␊
1487+
height: convertNumber2VP(100)␊
1488+
}␊
1489+
}␊
1490+
];␊
1491+
return __nesting_style_data__;␊
1492+
}␊
1493+
function __inner_style__() {␊
1494+
if (__inner_style_data__) return __inner_style_data__;␊
1495+
__inner_style_data__ = {};␊
1496+
return __inner_style_data__;␊
1497+
}␊
1498+
export const Pesudo = withLogin(()=>{␊
1499+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1500+
});␊
1501+
`
1502+
1503+
## Harmony attrbute test hoc
1504+
1505+
> Snapshot 1
1506+
1507+
`import { View } from '@tarojs/components';␊
1508+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1509+
let __inner_style_data__;␊
1510+
let __nesting_style_data__;␊
1511+
function __nesting_style__() {␊
1512+
if (__nesting_style_data__) return __nesting_style_data__;␊
1513+
__nesting_style_data__ = [␊
1514+
{␊
1515+
"selectors": [␊
1516+
"a",␊
1517+
" > ",␊
1518+
"b"␊
1519+
],␊
1520+
"declaration": {␊
1521+
height: convertNumber2VP(100)␊
1522+
}␊
1523+
}␊
1524+
];␊
1525+
return __nesting_style_data__;␊
1526+
}␊
1527+
function __inner_style__() {␊
1528+
if (__inner_style_data__) return __inner_style_data__;␊
1529+
__inner_style_data__ = {};␊
1530+
return __inner_style_data__;␊
1531+
}␊
1532+
export default function Pesudo() {␊
1533+
return ()=>{␊
1534+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1535+
};␊
1536+
}␊
1537+
`
1538+
1539+
## Harmony attrbute test useHoc
1540+
1541+
> Snapshot 1
1542+
1543+
`import { View } from '@tarojs/components';␊
1544+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1545+
let __inner_style_data__;␊
1546+
let __nesting_style_data__;␊
1547+
function __nesting_style__() {␊
1548+
if (__nesting_style_data__) return __nesting_style_data__;␊
1549+
__nesting_style_data__ = [␊
1550+
{␊
1551+
"selectors": [␊
1552+
"a",␊
1553+
" > ",␊
1554+
"b"␊
1555+
],␊
1556+
"declaration": {␊
1557+
height: convertNumber2VP(100)␊
1558+
}␊
1559+
}␊
1560+
];␊
1561+
return __nesting_style_data__;␊
1562+
}␊
1563+
function __inner_style__() {␊
1564+
if (__inner_style_data__) return __inner_style_data__;␊
1565+
__inner_style_data__ = {};␊
1566+
return __inner_style_data__;␊
1567+
}␊
1568+
export const Pesudo = withLogin(()=>{␊
1569+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'index')} className='index'/>, __nesting_style__());␊
1570+
});␊
1571+
`

__test__/index.spec.mjs.snap

279 Bytes
Binary file not shown.

src/visitor.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,22 @@ impl ModuleMutVisitor {
514514
],
515515
type_args: None,
516516
})))
517+
} else {
518+
match &mut **expr_in_box {
519+
// export const Index = () => {}
520+
Expr::Arrow(ArrowExpr { body, .. }) => {
521+
body.visit_mut_children_with(self)
522+
},
523+
// export const Index = withXxxx(() => {})
524+
Expr::Call(call) => {
525+
call.visit_mut_children_with(self)
526+
},
527+
// export const Index = function() {}
528+
Expr::Fn(FnExpr { function, .. }) => {
529+
function.visit_mut_children_with(self)
530+
},
531+
_ => {}
532+
}
517533
}
518534
}
519535
}

0 commit comments

Comments
 (0)