From c5ded374aa988946e95551b0de9f0dd6e5dc18fc Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Sat, 14 Dec 2024 04:31:16 +0800 Subject: [PATCH] refactor(language-core): simplify frequent function parameter passing --- .../lib/parsers/scriptSetupRanges.ts | 145 ++++++++++-------- 1 file changed, 79 insertions(+), 66 deletions(-) diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts index 0fe1995bd3..1aa9c03eb2 100644 --- a/packages/language-core/lib/parsers/scriptSetupRanges.ts +++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts @@ -11,10 +11,6 @@ export function parseScriptSetupRanges( ast: ts.SourceFile, vueCompilerOptions: VueCompilerOptions ) { - - let foundNonImportExportNode = false; - let importSectionEndOffset = 0; - const props: { name?: string; destructured?: Set; @@ -76,6 +72,8 @@ export function parseScriptSetupRanges( )?.end ?? 0; let bindings = parseBindingRanges(ts, ast); + let foundNonImportExportNode = false; + let importSectionEndOffset = 0; ts.forEachChild(ast, node => { const isTypeExport = (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node)) && node.modifiers?.some(mod => mod.kind === ts.SyntaxKind.ExportKeyword); @@ -103,9 +101,9 @@ export function parseScriptSetupRanges( && node.importClause?.name && !node.importClause.isTypeOnly ) { - const moduleName = getNodeText(ts, node.moduleSpecifier, ast).slice(1, -1); + const moduleName = _getNodeText(node.moduleSpecifier).slice(1, -1); if (vueCompilerOptions.extensions.some(ext => moduleName.endsWith(ext))) { - importComponentNames.add(getNodeText(ts, node.importClause.name, ast)); + importComponentNames.add(_getNodeText(node.importClause.name)); } } }); @@ -132,26 +130,13 @@ export function parseScriptSetupRanges( templateRefs, }; - function _getStartEnd(node: ts.Node) { - return getStartEnd(ts, node, ast); - } - - function parseDefineFunction(node: ts.CallExpression) { - return { - ..._getStartEnd(node), - exp: _getStartEnd(node.expression), - arg: node.arguments.length ? _getStartEnd(node.arguments[0]) : undefined, - typeArg: node.typeArguments?.length ? _getStartEnd(node.typeArguments[0]) : undefined, - }; - } - function visitNode(node: ts.Node, parents: ts.Node[]) { const parent = parents[parents.length - 1]; if ( ts.isCallExpression(node) && ts.isIdentifier(node.expression) ) { - const callText = getNodeText(ts, node.expression, ast); + const callText = _getNodeText(node.expression); if (vueCompilerOptions.macros.defineModel.includes(callText)) { let localName: TextRange | undefined; let propName: TextRange | undefined; @@ -169,7 +154,7 @@ export function parseScriptSetupRanges( options = node.arguments[1]; } else if (node.arguments.length >= 1) { - if (ts.isStringLiteral(node.arguments[0])) { + if (ts.isStringLiteralLike(node.arguments[0])) { propName = _getStartEnd(node.arguments[0]); } else { @@ -185,7 +170,7 @@ export function parseScriptSetupRanges( if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) { continue; } - const text = getNodeText(ts, property.name, ast); + const text = _getNodeText(property.name); if (text === 'type') { runtimeType = _getStartEnd(property.initializer); } @@ -236,7 +221,7 @@ export function parseScriptSetupRanges( if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) { continue; } - const text = getNodeText(ts, property.name, ast); + const text = _getNodeText(property.name); if (text === 'type') { runtimeType = _getStartEnd(property.initializer); } @@ -267,7 +252,7 @@ export function parseScriptSetupRanges( if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) { continue; } - const text = getNodeText(ts, property.name, ast); + const text = _getNodeText(property.name); if (text === 'type') { runtimeType = _getStartEnd(property.initializer); } @@ -291,7 +276,7 @@ export function parseScriptSetupRanges( }; if (ts.isVariableDeclaration(parent)) { if (ts.isIdentifier(parent.name)) { - slots.name = getNodeText(ts, parent.name, ast); + slots.name = _getNodeText(parent.name); } else { slots.isObjectBindingPattern = ts.isObjectBindingPattern(parent.name); @@ -304,7 +289,7 @@ export function parseScriptSetupRanges( statement: getStatementRange(ts, parents, node, ast) }; if (ts.isVariableDeclaration(parent)) { - emits.name = getNodeText(ts, parent.name, ast); + emits.name = _getNodeText(parent.name); } if (node.typeArguments?.length && ts.isTypeLiteralNode(node.typeArguments[0])) { for (const member of node.typeArguments[0].members) { @@ -327,7 +312,7 @@ export function parseScriptSetupRanges( props.destructured = new Set(); const identifiers = collectIdentifiers(ts, parent.name, []); for (const [id, isRest] of identifiers) { - const name = getNodeText(ts, id, ast); + const name = _getNodeText(id); if (isRest) { props.destructuredRest = name; } @@ -337,7 +322,7 @@ export function parseScriptSetupRanges( } } else { - props.name = getNodeText(ts, parent.name, ast); + props.name = _getNodeText(parent.name); } } @@ -360,33 +345,39 @@ export function parseScriptSetupRanges( props.withDefaults.arg = _getStartEnd(arg); } if (ts.isVariableDeclaration(parent)) { - props.name = getNodeText(ts, parent.name, ast); + props.name = _getNodeText(parent.name); } } - else if (vueCompilerOptions.macros.defineOptions.includes(callText)) { - if (node.arguments.length && ts.isObjectLiteralExpression(node.arguments[0])) { - const obj = node.arguments[0]; - ts.forEachChild(obj, node => { - if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) { - const name = getNodeText(ts, node.name, ast); - if (name === 'inheritAttrs') { - options.inheritAttrs = getNodeText(ts, node.initializer, ast); - } - } - }); - for (const prop of node.arguments[0].properties) { - if (ts.isPropertyAssignment(prop) && getNodeText(ts, prop.name, ast) === 'name' && ts.isStringLiteral(prop.initializer)) { - options.name = prop.initializer.text; + else if ( + vueCompilerOptions.macros.defineOptions.includes(callText) + && node.arguments.length + && ts.isObjectLiteralExpression(node.arguments[0]) + ) { + const obj = node.arguments[0]; + ts.forEachChild(obj, node => { + if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) { + const name = _getNodeText(node.name); + if (name === 'inheritAttrs') { + options.inheritAttrs = _getNodeText(node.initializer); } } + }); + for (const prop of obj.properties) { + if ( + ts.isPropertyAssignment(prop) + && _getNodeText(prop.name) === 'name' && ts.isStringLiteral(prop.initializer) + ) { + options.name = prop.initializer.text; + } } } - else if (vueCompilerOptions.composables.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) { + else if ( + vueCompilerOptions.composables.useTemplateRef.includes(callText) + && node.arguments.length + && !node.typeArguments?.length + ) { const define = parseDefineFunction(node); - let name; - if (ts.isVariableDeclaration(parent)) { - name = getNodeText(ts, parent.name, ast); - } + const name = ts.isVariableDeclaration(parent) ? _getNodeText(parent.name) : undefined; templateRefs.push({ name, define @@ -399,23 +390,39 @@ export function parseScriptSetupRanges( }); } } + ts.forEachChild(node, child => { parents.push(node); visitNode(child, parents); parents.pop(); }); } + + function parseDefineFunction(node: ts.CallExpression) { + return { + ..._getStartEnd(node), + exp: _getStartEnd(node.expression), + arg: node.arguments.length ? _getStartEnd(node.arguments[0]) : undefined, + typeArg: node.typeArguments?.length ? _getStartEnd(node.typeArguments[0]) : undefined, + }; + } + + function _getStartEnd(node: ts.Node) { + return getStartEnd(ts, node, ast); + } + + function _getNodeText(node: ts.Node) { + return getNodeText(ts, node, ast); + } } export function parseBindingRanges(ts: typeof import('typescript'), sourceFile: ts.SourceFile) { const bindings: TextRange[] = []; ts.forEachChild(sourceFile, node => { if (ts.isVariableStatement(node)) { - for (const node_2 of node.declarationList.declarations) { - const vars = _findBindingVars(node_2.name); - for (const _var of vars) { - bindings.push(_var); - } + for (const decl of node.declarationList.declarations) { + const vars = _findBindingVars(decl.name); + bindings.push(...vars); } } else if (ts.isFunctionDeclaration(node)) { @@ -454,43 +461,49 @@ export function parseBindingRanges(ts: typeof import('typescript'), sourceFile: } }); return bindings; + function _getStartEnd(node: ts.Node) { return getStartEnd(ts, node, sourceFile); } + function _findBindingVars(left: ts.BindingName) { return findBindingVars(ts, left, sourceFile); } } -export function findBindingVars(ts: typeof import('typescript'), left: ts.BindingName, sourceFile: ts.SourceFile) { +export function findBindingVars( + ts: typeof import('typescript'), + left: ts.BindingName, + sourceFile: ts.SourceFile +) { const vars: TextRange[] = []; worker(left); return vars; - function worker(_node: ts.Node) { - if (ts.isIdentifier(_node)) { - vars.push(getStartEnd(ts, _node, sourceFile)); + function worker(node: ts.Node) { + if (ts.isIdentifier(node)) { + vars.push(getStartEnd(ts, node, sourceFile)); } // { ? } = ... // [ ? ] = ... - else if (ts.isObjectBindingPattern(_node) || ts.isArrayBindingPattern(_node)) { - for (const property of _node.elements) { + else if (ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) { + for (const property of node.elements) { if (ts.isBindingElement(property)) { worker(property.name); } } } // { foo: ? } = ... - else if (ts.isPropertyAssignment(_node)) { - worker(_node.initializer); + else if (ts.isPropertyAssignment(node)) { + worker(node.initializer); } // { foo } = ... - else if (ts.isShorthandPropertyAssignment(_node)) { - vars.push(getStartEnd(ts, _node.name, sourceFile)); + else if (ts.isShorthandPropertyAssignment(node)) { + vars.push(getStartEnd(ts, node.name, sourceFile)); } // { ...? } = ... // [ ...? ] = ... - else if (ts.isSpreadAssignment(_node) || ts.isSpreadElement(_node)) { - worker(_node.expression); + else if (ts.isSpreadAssignment(node) || ts.isSpreadElement(node)) { + worker(node.expression); } } } @@ -537,4 +550,4 @@ function getStatementRange( statementRange = getStartEnd(ts, node, sourceFile); } return statementRange; -} \ No newline at end of file +}