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 a18c557
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/hip-pears-sneeze.md
@@ -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
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
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
@@ -0,0 +1,6 @@
<script>
console.log(1)
</script>
<script setup lang="ts">
const foo = $ref('')
</script>
1 change: 1 addition & 0 deletions tsconfig.fixture.json
Expand Up @@ -5,6 +5,7 @@
},
"include": ["**/fixtures/**/*", "packages/macros/macros-global.d.ts"],
"vueCompilerOptions": {
"experimentalDefinePropProposal": "kevinEdition",
"plugins": [
"@vue-macros/volar/define-options",
"@vue-macros/volar/define-models",
Expand Down

0 comments on commit a18c557

Please sign in to comment.