Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 解析Vue script时自动识别setup的解析 #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/gogocode-core/src/vue-core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const core = {
if (ast.scriptAst) {
newAst = ast.scriptAst;
} else {
ast.scriptAst = core.getScript(ast, { parseOptions });
const isSetup = ast.scriptSetup && ast.scriptSetup.setup;
ast.scriptAst = core.getScript(ast, { isSetup, parseOptions });
newAst = ast.scriptAst;
}
} else if (selector == '<script setup></script>') {
Expand Down Expand Up @@ -52,7 +53,7 @@ const core = {
return undefined;
}
},
getScript(ast, { isSetup = false, parseOptions } = {} ) {
getScript(ast, { isSetup = false, parseOptions } = {}) {
// 仅针对vue,取script,后续通过jscore处理
let content;
if (isSetup && ast.scriptSetup) {
Expand Down Expand Up @@ -89,7 +90,7 @@ const core = {
} else {
return null;
}
} catch(e) {
} catch (e) {
console.log('buildAstByAstStr failed:' + e)
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/gogocode-core/test/G.vue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,46 @@ test('test vue parseoptions', () => {
const res = ast.generate()
expect(res.match('&&')).toBeTruthy()
})


test('test vue3 setup by script', () => {
const ast = $(
`<template>
<div class="a">1</div>
<div class="a">2</div>
</template>

<script setup lang="ts">
const a = 1
const b = 2
</script>
`,
{ parseOptions: { language: 'vue' } }
)
const res = ast.find('<script></script>').generate()
expect(res===`
const a = 1
const b = 2
`).toBeTruthy()
})

test('test vue3 setup by script setup', () => {
const ast = $(
`<template>
<div class="a">1</div>
<div class="a">2</div>
</template>

<script setup lang="ts">
const a = 1
const b = 2
</script>
`,
{ parseOptions: { language: 'vue' } }
)
const res = ast.find('<script setup></script>').generate()
expect(res===`
const a = 1
const b = 2
`).toBeTruthy()
})