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

不能自动识别 Vue3 中带setup 的 script 解析 #159

Open
ATQQ opened this issue May 17, 2022 · 4 comments
Open

不能自动识别 Vue3 中带setup 的 script 解析 #159

ATQQ opened this issue May 17, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@ATQQ
Copy link
Contributor

ATQQ commented May 17, 2022

测试文件

<template>
  <div class="a">1</div>
  <div class="a">2</div>
</template>

<script setup lang="ts">
const a = 1
const b = 2
</script>

测试代码

const $ = require('gogocode')
const fs = require('fs')
const path = require('path')

const content = fs.readFileSync(path.join(__dirname, 'index.vue'), { encoding: 'utf-8' })

const ast = $(content, { parseOptions: { language: 'vue' } })

const script = ast.find('<script></script>')

console.log(script.generate());

具体结果可以看上述 playground示例

@ATQQ ATQQ added the bug Something isn't working label May 17, 2022
@ATQQ
Copy link
Contributor Author

ATQQ commented May 17, 2022

看了一下实现 写死了判断逻辑

} else if (selector == '<script></script>') {
parseOptions.language = 'js'
parseOptions.rootLanguage = 'vue';
if (ast.scriptAst) {
newAst = ast.scriptAst;
} else {
ast.scriptAst = core.getScript(ast, { parseOptions });
newAst = ast.scriptAst;
}
} else if (selector == '<script setup></script>') {
parseOptions.language = 'js'
parseOptions.rootLanguage = 'vue';
if (ast.scriptSetupAst) {
newAst = ast.scriptSetupAst;
} else {
ast.scriptSetupAst = core.getScript(ast, { isSetup: true, parseOptions });
newAst = ast.scriptSetupAst;
}
}

@ATQQ
Copy link
Contributor Author

ATQQ commented May 17, 2022

我提个PR 看看 整智能点,自动识别

@ATQQ ATQQ changed the title 不支持 Vue3 中带setup 的 script 解析 不能自动识别 Vue3 中带setup 的 script 解析 May 17, 2022
@emosheeep
Copy link

这个问题现在是什么情况呀,挺蛋疼的感觉😂

@ATQQ
Copy link
Contributor Author

ATQQ commented Aug 6, 2022

这个问题现在是什么情况呀,挺蛋疼的感觉😂

看上面我贴的逻辑

如果你要提取带setup的 script 使用 <script setup></script>
否则<script></script>

调用前判断用哪一个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants