Skip to content

Commit 7487da5

Browse files
committed
feat(volar): support TSNonNullExpression
1 parent 82b1290 commit 7487da5

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/volar.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ function transformReactivityFunction(options: {
7777
node.range = [node.start, node.end]
7878
},
7979
enter(node, parent) {
80+
let tsNonNullExpressionEnd = 0
8081
if (node.type === 'TSNonNullExpression') {
8182
node = node.expression
83+
tsNonNullExpressionEnd = node.end
8284
}
85+
8386
if (node.type === 'CallExpression') {
8487
const calleeName = text.slice(node.callee.start, node.callee.end)
8588
if (calleeName === '$$') {
@@ -171,15 +174,15 @@ function transformReactivityFunction(options: {
171174
replaceSourceRange(
172175
codes,
173176
source,
174-
node.end,
175-
node.end,
177+
tsNonNullExpressionEnd || node.end,
178+
tsNonNullExpressionEnd || node.end,
176179
'\n,',
177180
...toRefs.join('\n,'),
178181
'\n,',
179182
`${toValuesName} = `,
180183
parent.id.type === 'ArrayPattern' ? '[' : '{',
181-
hasRest ? `...${refName},` : '',
182-
...toValues.join('\n,'),
184+
hasRest ? `...${refName}, ` : '',
185+
...toValues.join(', '),
183186
parent.id.type === 'ArrayPattern' ? ']' : '}',
184187
'\n,',
185188
[

tests/__snapshots__/basic.test.ts.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ let __MACROS_ref0 = useApi(),
1616
id1 = toRef(__MACROS_ref0, 'id', 0),
1717
name = toRef(__MACROS_ref0, 'name'),
1818
rest1 = createPropsRestProxy(__MACROS_ref0, ['id', 'name']);
19-
;
2019
id1.value == 0;
2120
console.log({
2221
id1: id1.value,

tests/fixtures/basic.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ function useApi$(defaultName = ref('')) {
99
}
1010
}
1111

12-
let { id: id1 = 0, name, ...rest1 } = $useApi();
13-
;id1 == 0
12+
let { id: id1 = 0, name, ...rest1 } = $useApi()!
13+
id1 == 0
1414
console.log({
1515
id1,
1616
name,
17-
rest1
17+
rest1,
1818
})
1919
useApi$(name)
2020

2121
function useArray$() {
2222
const foo = $ref(1)
23-
const bar = $ref(2)
24-
;[$$(foo),$$(bar)]
23+
const bar = $ref(2)
24+
;[$$(foo), $$(bar)]
2525
return [foo, bar]
2626
}
2727

2828
let [foo = 0] = $useArray()
29-
foo === 1
29+
foo === 1
3030
foo = 1
3131

3232
// @ts-expect-error
@@ -36,14 +36,18 @@ console.log$(name)
3636
watch$(name, () => {
3737
let name = 1
3838
return {
39-
name
39+
name,
4040
}
4141
})
4242

4343
defineExpose$({
4444
name,
45-
})
46-
const title = $ref<string>('title')
45+
})
46+
const title = $ref<string>('title')
4747
console.log($$(title))
48-
const Comp = ({ title }: { title: Ref<string>, foo: string }) => title.value
49-
export default () => <Comp title$={title} foo={title}>{title}</Comp>
48+
const Comp = ({ title }: { title: Ref<string>; foo: string }) => title.value
49+
export default () => (
50+
<Comp title$={title} foo={title}>
51+
{title}
52+
</Comp>
53+
)

0 commit comments

Comments
 (0)