Skip to content

Commit

Permalink
fix(common): throw error when sfc lang is different
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 5, 2023
1 parent a8531d3 commit ed7ca8c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/hip-pears-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@vue-macros/common': patch
'unplugin-vue-macros': patch
---

throw error when sfc lang is different
16 changes: 15 additions & 1 deletion packages/common/src/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ export function parseSFC(code: string, id: string): SFC {
filename: id,
})
const { descriptor, errors } = sfc
const lang = (descriptor.script || descriptor.scriptSetup)?.lang

const scriptLang = sfc.descriptor.script?.lang
const scriptSetupLang = sfc.descriptor.scriptSetup?.lang

if (
sfc.descriptor.script &&
sfc.descriptor.scriptSetup &&
(scriptLang || 'js') !== (scriptSetupLang || 'js')
) {
throw new Error(
`[unplugin-vue-macros] <script> and <script setup> must have the same language type.`
)
}

const lang = scriptLang || scriptSetupLang

return {
sfc,
Expand Down
2 changes: 2 additions & 0 deletions packages/macros/tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export { definePropsRefs as default };
"
`;

exports[`fixtures > tests/fixtures/error-diff-lang.vue 1`] = `"[unplugin-vue-macros] <script> and <script setup> must have the same language type."`;
exports[`fixtures > tests/fixtures/hoist-static.vue 1`] = `
"import { defineComponent } from 'vue';
import _export_sfc from '/plugin-vue/export-helper';
Expand Down
6 changes: 6 additions & 0 deletions packages/macros/tests/fixtures/error-diff-lang.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
console.log(1)
</script>
<script setup lang="ts">
const foo = $ref('')
</script>

0 comments on commit ed7ca8c

Please sign in to comment.