Skip to content

Commit

Permalink
fix(define-options): <script setup> scope bindings
Browse files Browse the repository at this point in the history
closes #37
  • Loading branch information
sxzz committed Aug 30, 2022
1 parent a7d9e27 commit c14ddaa
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -24,6 +24,7 @@
"@pnpm/logger": "^4.0.0",
"@sxzz/eslint-config": "^2.4.1",
"@types/node": "*",
"@vitest/ui": "^0.22.1",
"@vue-macros/test-utils": "workspace:*",
"bumpp": "^8.2.1",
"eslint": "^8.23.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/define-model/package.json
Expand Up @@ -70,7 +70,7 @@
"dependencies": {
"@rollup/pluginutils": "^4.2.1",
"@vue-macros/common": "workspace:*",
"ast-walker-scope": "^0.2.2",
"ast-walker-scope": "^0.2.3",
"unplugin": "^0.9.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/define-options/package.json
Expand Up @@ -69,7 +69,7 @@
"dependencies": {
"@rollup/pluginutils": "^4.2.1",
"@vue-macros/common": "workspace:*",
"ast-walker-scope": "^0.2.2",
"ast-walker-scope": "^0.2.3",
"unplugin": "^0.9.5"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/define-options/src/core/transform.ts
Expand Up @@ -27,8 +27,8 @@ export const transform = (code: string, id: string) => {

if (script) checkDefaultExport(scriptCompiled.scriptAst as any)

const scriptBindings = sfc.scriptCompiled.scriptSetupAst
? getIdentifiers(sfc.scriptCompiled.scriptSetupAst as any)
const setupBindings = scriptCompiled.scriptSetupAst
? getIdentifiers(scriptCompiled.scriptSetupAst as any)
: []

const s = new MagicString(code)
Expand All @@ -51,7 +51,7 @@ export default /*#__PURE__*/ DO_defineComponent(`
`${DEFINE_OPTIONS}() please use defineProps or defineEmits instead.`
)

checkInvalidScopeReference(arg, DEFINE_OPTIONS, scriptBindings)
checkInvalidScopeReference(arg, DEFINE_OPTIONS, setupBindings)

s.moveNode(arg, scriptOffset, { offset: setupOffset })

Expand Down
31 changes: 31 additions & 0 deletions packages/macros/tests/__snapshots__/fixtures.test.ts.snap
Expand Up @@ -80,6 +80,37 @@ export { hoistStatic as default };
"
`;

exports[`fixtures > tests/fixtures/hoist-static-scope.vue 1`] = `
"import { defineComponent, openBlock, createElementBlock } from 'vue';
const [name] = [\\"VarCompName\\"];
const __default__ = defineComponent({
name
});
var _sfc_main = /* @__PURE__ */ defineComponent({
...__default__,
setup(__props) {
const test = (name2) => name2;
return (_ctx, _cache) => {
return openBlock(), createElementBlock(\\"div\\", null, \\"1\\");
};
}
});
var _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
var hoistStaticScope = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
export { hoistStaticScope as default };
"
`;

exports[`fixtures > tests/fixtures/vue2-model.vue 1`] = `
"import { defineComponent } from 'vue';
Expand Down
14 changes: 14 additions & 0 deletions packages/macros/tests/fixtures/hoist-static-scope.vue
@@ -0,0 +1,14 @@
<script lang="ts" setup>
// https://github.com/sxzz/unplugin-vue-macros/issues/37#issuecomment-1231677842
const [name] = /* hoist-static*/ ['VarCompName']
defineOptions({
name,
})
const test = (name: string) => name
</script>

<template>
<div>1</div>
</template>
25 changes: 17 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c14ddaa

Please sign in to comment.