diff --git a/.changeset/wild-tomatoes-whisper.md b/.changeset/wild-tomatoes-whisper.md new file mode 100644 index 000000000..b6be0fa8b --- /dev/null +++ b/.changeset/wild-tomatoes-whisper.md @@ -0,0 +1,5 @@ +--- +"@vue-macros/volar": patch +--- + +refactor(volar): use `scriptSetup.ast` instead of scriptSetupAst diff --git a/packages/volar/src/common.ts b/packages/volar/src/common.ts index 43dbff534..ce4df7345 100644 --- a/packages/volar/src/common.ts +++ b/packages/volar/src/common.ts @@ -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) && diff --git a/packages/volar/src/define-models.ts b/packages/volar/src/define-models.ts index f20ef7dce..24e31f190 100644 --- a/packages/volar/src/define-models.ts +++ b/packages/volar/src/define-models.ts @@ -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) @@ -116,7 +116,7 @@ const plugin: VueLanguagePlugin = ({ if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/define-options.ts b/packages/volar/src/define-options.ts index 210951c31..0c8dce255 100644 --- a/packages/volar/src/define-options.ts +++ b/packages/volar/src/define-options.ts @@ -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) @@ -60,7 +60,7 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => { if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/define-slots.ts b/packages/volar/src/define-slots.ts index 6bbd85bd1..bf5a16189 100644 --- a/packages/volar/src/define-slots.ts +++ b/packages/volar/src/define-slots.ts @@ -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)) { @@ -82,7 +82,7 @@ const plugin: VueLanguagePlugin = ({ if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/export-expose.ts b/packages/volar/src/export-expose.ts index f7a9b83b0..24f874917 100644 --- a/packages/volar/src/export-expose.ts +++ b/packages/volar/src/export-expose.ts @@ -34,21 +34,21 @@ function transform({ const exposed: Record> = 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] } @@ -82,7 +82,7 @@ const plugin: VueLanguagePlugin = ({ if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/export-props.ts b/packages/volar/src/export-props.ts index e77094dcd..66272352e 100644 --- a/packages/volar/src/export-props.ts +++ b/packages/volar/src/export-props.ts @@ -32,14 +32,14 @@ function transform({ const props: Record = 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 @@ -76,7 +76,7 @@ const plugin: VueLanguagePlugin = ({ if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/export-render.ts b/packages/volar/src/export-render.ts index 65cf7361f..57b0b96a8 100644 --- a/packages/volar/src/export-render.ts +++ b/packages/volar/src/export-render.ts @@ -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(' ) @@ -55,7 +55,7 @@ const plugin: VueLanguagePlugin = ({ if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return diff --git a/packages/volar/src/jsx-directive.ts b/packages/volar/src/jsx-directive.ts index 7a70609b0..fad0b93ca 100644 --- a/packages/volar/src/jsx-directive.ts +++ b/packages/volar/src/jsx-directive.ts @@ -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 } @@ -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 @@ -437,7 +437,7 @@ function transformJsxDirective({ ) }) } - sfc[`${source}Ast`]!.forEachChild(walkJsxDirective) + sfc[source]?.ast.forEachChild(walkJsxDirective) transformVSlot({ nodes: Array.from(vSlotNodeSet), diff --git a/packages/volar/src/setup-jsdoc.ts b/packages/volar/src/setup-jsdoc.ts index e50f4b6e2..8865e4174 100644 --- a/packages/volar/src/setup-jsdoc.ts +++ b/packages/volar/src/setup-jsdoc.ts @@ -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) @@ -56,7 +56,7 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => { if ( embeddedFile.kind !== FileKind.TypeScriptHostFile || !sfc.scriptSetup || - !sfc.scriptSetupAst + !sfc.scriptSetup.ast ) return