Skip to content

Commit

Permalink
refactor(volar): use scriptSetup.ast instead of scriptSetupAst (#539)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
  • Loading branch information
zhiyuanzmj and sxzz committed Oct 26, 2023
1 parent b1dee80 commit f38a538
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-tomatoes-whisper.md
@@ -0,0 +1,5 @@
---
"@vue-macros/volar": patch
---

refactor(volar): use `scriptSetup.ast` instead of scriptSetupAst
2 changes: 1 addition & 1 deletion packages/volar/src/common.ts
Expand Up @@ -57,7 +57,7 @@ export function getImportNames(
sfc: Sfc
) {
const names: string[] = []
const sourceFile = sfc.scriptSetupAst!
const sourceFile = sfc.scriptSetup!.ast
sourceFile.forEachChild((node) => {
if (
ts.isImportDeclaration(node) &&
Expand Down
4 changes: 2 additions & 2 deletions packages/volar/src/define-models.ts
Expand Up @@ -92,7 +92,7 @@ function getTypeArg(
return node.typeArguments[0]
}

const sourceFile = sfc.scriptSetupAst!
const sourceFile = sfc.scriptSetup!.ast
return sourceFile.forEachChild((node) => {
if (ts.isExpressionStatement(node)) {
return getCallArg(node.expression)
Expand All @@ -116,7 +116,7 @@ const plugin: VueLanguagePlugin = ({
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
4 changes: 2 additions & 2 deletions packages/volar/src/define-options.ts
Expand Up @@ -39,7 +39,7 @@ function getArg(ts: typeof import('typescript/lib/tsserverlibrary'), sfc: Sfc) {
return node.arguments[0]
}

const sourceFile = sfc.scriptSetupAst!
const sourceFile = sfc.scriptSetup!.ast
return sourceFile.forEachChild((node) => {
if (ts.isExpressionStatement(node)) {
return getCallArg(node.expression)
Expand All @@ -60,7 +60,7 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
4 changes: 2 additions & 2 deletions packages/volar/src/define-slots.ts
Expand Up @@ -59,7 +59,7 @@ function getTypeArg(
return node.typeArguments[0]
}

return sfc.scriptSetupAst?.forEachChild((node) => {
return sfc.scriptSetup?.ast.forEachChild((node) => {
if (ts.isExpressionStatement(node)) {
return getCallArg(node.expression)
} else if (ts.isVariableStatement(node)) {
Expand All @@ -82,7 +82,7 @@ const plugin: VueLanguagePlugin = ({
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
8 changes: 4 additions & 4 deletions packages/volar/src/export-expose.ts
Expand Up @@ -34,21 +34,21 @@ function transform({
const exposed: Record<string, Segment<FileRangeCapabilities>> = Object.create(
null
)
for (const stmt of sfc.scriptSetupAst!.statements) {
for (const stmt of sfc.scriptSetup!.ast.statements) {
if (!ts.isVariableStatement(stmt)) continue
const exportModifier = stmt.modifiers?.find(
(m) => m.kind === ts.SyntaxKind.ExportKeyword
)
if (!exportModifier) continue

const start = exportModifier.getStart(sfc.scriptSetupAst!)
const start = exportModifier.getStart(sfc.scriptSetup?.ast)
const end = exportModifier.getEnd()
replaceSourceRange(file.content, 'scriptSetup', start, end)

for (const decl of stmt.declarationList.declarations) {
if (!ts.isIdentifier(decl.name)) continue
const name = decl.name.text
const start = decl.name.getStart(sfc.scriptSetupAst!)
const start = decl.name.getStart(sfc.scriptSetup?.ast)

exposed[name] = [name, 'scriptSetup', start, FileRangeCapabilities.full]
}
Expand Down Expand Up @@ -82,7 +82,7 @@ const plugin: VueLanguagePlugin = ({
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
6 changes: 3 additions & 3 deletions packages/volar/src/export-props.ts
Expand Up @@ -32,14 +32,14 @@ function transform({

const props: Record<string, boolean> = Object.create(null)
let changed = false
for (const stmt of sfc.scriptSetupAst!.statements) {
for (const stmt of sfc.scriptSetup!.ast.statements) {
if (!ts.isVariableStatement(stmt)) continue
const exportModifier = stmt.modifiers?.find(
(m) => m.kind === ts.SyntaxKind.ExportKeyword
)
if (!exportModifier) continue

const start = exportModifier.getStart(sfc.scriptSetupAst!)
const start = exportModifier.getStart(sfc.scriptSetup?.ast)
const end = exportModifier.getEnd()
replaceSourceRange(file.content, 'scriptSetup', start, end)
changed = true
Expand Down Expand Up @@ -76,7 +76,7 @@ const plugin: VueLanguagePlugin = ({
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
8 changes: 4 additions & 4 deletions packages/volar/src/export-render.ts
Expand Up @@ -28,15 +28,15 @@ function transform({
)
if (!filter(fileName)) return

for (const stmt of sfc.scriptSetupAst!.statements) {
for (const stmt of sfc.scriptSetup!.ast.statements) {
if (!ts.isExportAssignment(stmt)) continue

const start = stmt.expression.getStart(sfc.scriptSetupAst)
const start = stmt.expression.getStart(sfc.scriptSetup?.ast)
const end = stmt.expression.getEnd()
replaceSourceRange(
file.content,
'scriptSetup',
stmt.getStart(sfc.scriptSetupAst),
stmt.getStart(sfc.scriptSetup?.ast),
start,
'defineRender('
)
Expand All @@ -55,7 +55,7 @@ const plugin: VueLanguagePlugin = ({
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down
8 changes: 4 additions & 4 deletions packages/volar/src/jsx-directive.ts
Expand Up @@ -332,12 +332,12 @@ function transformVModel({
source,
}: TransformOptions & { nodes: JsxAttributeNode[] }) {
nodes.forEach(({ attribute }) => {
const start = attribute.getStart(sfc[`${source}Ast`])
const start = attribute.getStart(sfc[source]?.ast)
let end = start + 7
let name = 'modelValue'

if (ts.isJsxNamespacedName(attribute.name)) {
name = attribute.name.name.getText(sfc[`${source}Ast`])
name = attribute.name.name.getText(sfc[source]?.ast)
end += 1 + name.length
}

Expand Down Expand Up @@ -402,7 +402,7 @@ function transformJsxDirective({
(ts.isJsxNamespacedName(attribute.name)
? attribute.name.namespace
: attribute.name
).getText(sfc[`${source}Ast`])
).getText(sfc[source]?.ast)
)
) {
vModelAttribute = attribute
Expand Down Expand Up @@ -437,7 +437,7 @@ function transformJsxDirective({
)
})
}
sfc[`${source}Ast`]!.forEachChild(walkJsxDirective)
sfc[source]?.ast.forEachChild(walkJsxDirective)

transformVSlot({
nodes: Array.from(vSlotNodeSet),
Expand Down
8 changes: 4 additions & 4 deletions packages/volar/src/setup-jsdoc.ts
Expand Up @@ -28,12 +28,12 @@ function transform({
ts: typeof import('typescript/lib/tsserverlibrary')
}) {
let jsDoc
if (hasJSDocNodes(sfc.scriptSetupAst!.statements[0])) {
jsDoc = sfc.scriptSetupAst!.statements[0].jsDoc.at(-1)
if (hasJSDocNodes(sfc.scriptSetup!.ast.statements[0])) {
jsDoc = sfc.scriptSetup!.ast.statements[0].jsDoc.at(-1)
}

// With exportRender plugin.
for (const stmt of sfc.scriptSetupAst!.statements) {
for (const stmt of sfc.scriptSetup!.ast.statements) {
if (!ts.isExportAssignment(stmt)) continue

if (hasJSDocNodes(stmt)) jsDoc ??= stmt.jsDoc.at(-1)
Expand All @@ -56,7 +56,7 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
if (
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
!sfc.scriptSetup ||
!sfc.scriptSetupAst
!sfc.scriptSetup.ast
)
return

Expand Down

0 comments on commit f38a538

Please sign in to comment.