|
1 | 1 | import type * as ts from 'typescript'; |
2 | | -import { collectIdentifiers } from '../codegen/utils'; |
3 | 2 | import type { TextRange, VueCompilerOptions } from '../types'; |
| 3 | +import { collectBindingIdentifiers, collectBindingRanges } from '../utils/collectBindings'; |
4 | 4 | import { getNodeText, getStartEnd } from '../utils/shared'; |
5 | 5 |
|
6 | 6 | const tsCheckReg = /^\/\/\s*@ts-(?:no)?check($|\s)/; |
@@ -236,7 +236,7 @@ export function parseScriptSetupRanges( |
236 | 236 | }; |
237 | 237 | if (ts.isVariableDeclaration(parent) && ts.isObjectBindingPattern(parent.name)) { |
238 | 238 | defineProps.destructured = new Map(); |
239 | | - const identifiers = collectIdentifiers(ts, parent.name); |
| 239 | + const identifiers = collectBindingIdentifiers(ts, parent.name); |
240 | 240 | for (const { id, isRest, initializer } of identifiers) { |
241 | 241 | const name = _getNodeText(id); |
242 | 242 | if (isRest) { |
@@ -374,8 +374,8 @@ export function parseBindingRanges(ts: typeof import('typescript'), ast: ts.Sour |
374 | 374 | ts.forEachChild(ast, node => { |
375 | 375 | if (ts.isVariableStatement(node)) { |
376 | 376 | for (const decl of node.declarationList.declarations) { |
377 | | - const vars = _findBindingVars(decl.name); |
378 | | - bindings.push(...vars.map(range => ({ range }))); |
| 377 | + const ranges = collectBindingRanges(ts, decl.name, ast); |
| 378 | + bindings.push(...ranges.map(range => ({ range }))); |
379 | 379 | } |
380 | 380 | } |
381 | 381 | else if (ts.isFunctionDeclaration(node)) { |
@@ -445,47 +445,6 @@ export function parseBindingRanges(ts: typeof import('typescript'), ast: ts.Sour |
445 | 445 | function _getNodeText(node: ts.Node) { |
446 | 446 | return getNodeText(ts, node, ast); |
447 | 447 | } |
448 | | - |
449 | | - function _findBindingVars(left: ts.BindingName) { |
450 | | - return findBindingVars(ts, left, ast); |
451 | | - } |
452 | | -} |
453 | | - |
454 | | -export function findBindingVars( |
455 | | - ts: typeof import('typescript'), |
456 | | - left: ts.BindingName, |
457 | | - ast: ts.SourceFile, |
458 | | -) { |
459 | | - const vars: TextRange[] = []; |
460 | | - worker(left); |
461 | | - return vars; |
462 | | - function worker(node: ts.Node) { |
463 | | - if (ts.isIdentifier(node)) { |
464 | | - vars.push(getStartEnd(ts, node, ast)); |
465 | | - } |
466 | | - // { ? } = ... |
467 | | - // [ ? ] = ... |
468 | | - else if (ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) { |
469 | | - for (const property of node.elements) { |
470 | | - if (ts.isBindingElement(property)) { |
471 | | - worker(property.name); |
472 | | - } |
473 | | - } |
474 | | - } |
475 | | - // { foo: ? } = ... |
476 | | - else if (ts.isPropertyAssignment(node)) { |
477 | | - worker(node.initializer); |
478 | | - } |
479 | | - // { foo } = ... |
480 | | - else if (ts.isShorthandPropertyAssignment(node)) { |
481 | | - vars.push(getStartEnd(ts, node.name, ast)); |
482 | | - } |
483 | | - // { ...? } = ... |
484 | | - // [ ...? ] = ... |
485 | | - else if (ts.isSpreadAssignment(node) || ts.isSpreadElement(node)) { |
486 | | - worker(node.expression); |
487 | | - } |
488 | | - } |
489 | 448 | } |
490 | 449 |
|
491 | 450 | function getStatementRange( |
|
0 commit comments