forked from vue-vine/vue-vine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.spec.ts
132 lines (123 loc) · 3.66 KB
/
ast.spec.ts
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
import type {
ExportDefaultDeclaration,
FunctionDeclaration,
Identifier,
VariableDeclaration,
} from '@babel/types'
import { parse as babelParse } from '@babel/parser'
import {
assignmentExpression,
blockStatement,
callExpression,
expressionStatement,
functionDeclaration,
identifier,
memberExpression,
numericLiteral,
program,
} from '@babel/types'
import { describe, expect, it } from 'vitest'
import {
findVineCompFnDecls,
isDescendant,
isVineMacroOf,
} from '../src/babel-helpers/ast'
function parseForTest(content: string) {
return babelParse(content, {
sourceType: 'module',
plugins: [
'typescript',
],
})
}
describe('find Vine function component declarations', () => {
it('should be able to find out all Vine component function', () => {
const content = `
const Caculator = (props: { expr: string }) => {
return vine\`
<div class="blog-post">
{{ expr }} = {{ eval(expr) }}
</div>
\`
}
export default function App() {
const expr = ref('1 + 1')
return vine\`
<input class="input-box" type="text" v-model="title" />
<BlogPost expr={{ expr }} />
\`
}
`
const root = parseForTest(content)
const foundVCFs = findVineCompFnDecls(root)
expect(foundVCFs).toHaveLength(2)
expect(((foundVCFs[0] as VariableDeclaration).declarations[0]?.id as Identifier).name).toBe('Caculator')
expect(((foundVCFs[1] as ExportDefaultDeclaration).declaration as FunctionDeclaration).id?.name).toBe('App')
})
})
// issue#100
describe('function component can simply return vine template', () => {
it('should recongnize a simple function component', () => {
const content = `const App = () => vine\`<div>Hello Vine</div>\``
const root = parseForTest(content)
const foundVCFs = findVineCompFnDecls(root)
expect(foundVCFs).toHaveLength(1)
expect(((foundVCFs[0] as VariableDeclaration).declarations[0]?.id as Identifier).name).toBe('App')
})
})
describe('isDescendant function', () => {
it('should return true when node and potentialDescendant are the same', () => {
const node = identifier('a')
expect(isDescendant(node, node)).toBe(true)
})
it('should return true when potentialDescendant is a descendant of node', () => {
const descendant = identifier('b')
const node = program([
functionDeclaration(identifier('a'), [], blockStatement([
expressionStatement(assignmentExpression('=', descendant, numericLiteral(2))),
])),
])
expect(isDescendant(node, descendant)).toBe(true)
})
it('should return false when potentialDescendant is not a descendant of node', () => {
const descendant = identifier('b')
const node = program([
functionDeclaration(identifier('a'), [], blockStatement([
expressionStatement(assignmentExpression('=', identifier('c'), numericLiteral(2))),
])),
])
expect(isDescendant(node, descendant)).toBe(false)
})
})
describe('vine ast operations and search helpers', () => {
it('isVineMacroOf', () => {
expect(isVineMacroOf('vineProp')(callExpression(identifier('vineProp'), []))).toBe(true)
expect(isVineMacroOf('vineProp')(
callExpression(
memberExpression(
identifier('vineProp'),
identifier('validator'),
),
[],
),
)).toBe(true)
expect(isVineMacroOf('vineProp')(
callExpression(
memberExpression(
identifier('vineProp'),
identifier('default'),
),
[],
),
)).toBe(true)
expect(isVineMacroOf('vineStyle')(
callExpression(
memberExpression(
identifier('vineStyle'),
identifier('scoped'),
),
[],
),
)).toBe(true)
})
})