Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

fix: support optional call expression #70

Merged
merged 1 commit into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/core/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ export function getIdentifierUsages(node?: Expression | TSType | SpreadElement |
else if (node.type === 'Identifier') {
identifiers.add(node.name)
}
else if (node.type === 'MemberExpression') {
else if (node.type === 'MemberExpression' || node.type === 'OptionalMemberExpression') {
getIdentifierUsages(node.object, identifiers)
}
else if (node.type === 'OptionalMemberExpression') {
getIdentifierUsages(node.object, identifiers)
}
else if (node.type === 'CallExpression') {
else if (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') {
getIdentifierUsages(node.callee as Expression, identifiers)
node.arguments.forEach(arg => getIdentifierUsages(arg as Expression, identifiers))
}
Expand Down
29 changes: 29 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,35 @@ export default __sfc_main;
"
`;

exports[`transform fixtures test/fixtures/TemplateOptionalChaining.vue 1`] = `
"<template>
<div @click=\\"callback?.()\\">
{{ text?.length ?? textLengthDefault }}
{{ classes?.[0] }}
</div>
</template>

<script lang=\\"ts\\">
const __sfc_main = {};

__sfc_main.setup = (__props, __ctx) => {
const text = 'hello';
const textLengthDefault = 0;
const callback = (undefined as undefined | (() => void));
const classes = (undefined as undefined | string[]);
return {
text,
textLengthDefault,
callback,
classes
};
};

export default __sfc_main;
</script>
"
`;

exports[`transform fixtures test/fixtures/VFor.vue 1`] = `
"<template>
<div>
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/TemplateOptionalChaining.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<template>
<div @click="callback?.()">
{{ text?.length ?? textLengthDefault }}
{{ classes?.[0] }}
</div>
</template>

<script setup lang="ts">
const text = 'hello'
const textLengthDefault = 0
const callback = undefined as undefined | (() => void)
const classes = undefined as undefined | string[]
</script>