@@ -34,8 +34,8 @@ export function parseScriptSetupRanges(
3434 define ?: ReturnType < typeof parseDefineFunction > ;
3535 } = { } ;
3636
37- const definePropProposalA = vueCompilerOptions . experimentalDefinePropProposal === 'kevinEdition' || ast . getFullText ( ) . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=kevinEdition' ) ;
38- const definePropProposalB = vueCompilerOptions . experimentalDefinePropProposal === 'johnsonEdition' || ast . getFullText ( ) . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=johnsonEdition' ) ;
37+ const definePropProposalA = vueCompilerOptions . experimentalDefinePropProposal === 'kevinEdition' || ast . text . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=kevinEdition' ) ;
38+ const definePropProposalB = vueCompilerOptions . experimentalDefinePropProposal === 'johnsonEdition' || ast . text . trimStart ( ) . startsWith ( '// @experimentalDefinePropProposal=johnsonEdition' ) ;
3939 const defineProp : {
4040 name : TextRange | undefined ;
4141 nameIsString : boolean ;
@@ -44,10 +44,10 @@ export function parseScriptSetupRanges(
4444 required : boolean ;
4545 } [ ] = [ ] ;
4646 const bindings = parseBindingRanges ( ts , ast ) ;
47- const text = ast . getFullText ( ) ;
47+ const text = ast . text ;
4848 const leadingCommentEndOffset = ts . getLeadingCommentRanges ( text , 0 ) ?. reverse ( ) [ 0 ] . end ?? 0 ;
4949
50- ast . forEachChild ( node => {
50+ ts . forEachChild ( ast , node => {
5151 const isTypeExport = ( ts . isTypeAliasDeclaration ( node ) || ts . isInterfaceDeclaration ( node ) ) && node . modifiers ?. some ( mod => mod . kind === ts . SyntaxKind . ExportKeyword ) ;
5252 if (
5353 ! foundNonImportExportNode
@@ -57,18 +57,18 @@ export function parseScriptSetupRanges(
5757 // fix https://github.com/vuejs/language-tools/issues/1223
5858 && ! ts . isImportEqualsDeclaration ( node )
5959 ) {
60- const commentRanges = ts . getLeadingCommentRanges ( text , node . getFullStart ( ) ) ;
60+ const commentRanges = ts . getLeadingCommentRanges ( text , node . pos ) ;
6161 if ( commentRanges ?. length ) {
6262 const commentRange = commentRanges . sort ( ( a , b ) => a . pos - b . pos ) [ 0 ] ;
6363 importSectionEndOffset = commentRange . pos ;
6464 }
6565 else {
66- importSectionEndOffset = node . getStart ( ast ) ;
66+ importSectionEndOffset = getStartEnd ( ts , node , ast ) . start ;
6767 }
6868 foundNonImportExportNode = true ;
6969 }
7070 } ) ;
71- ast . forEachChild ( child => visitNode ( child , [ ast ] ) ) ;
71+ ts . forEachChild ( ast , child => visitNode ( child , [ ast ] ) ) ;
7272
7373 return {
7474 leadingCommentEndOffset,
@@ -82,7 +82,7 @@ export function parseScriptSetupRanges(
8282 } ;
8383
8484 function _getStartEnd ( node : ts . Node ) {
85- return getStartEnd ( node , ast ) ;
85+ return getStartEnd ( ts , node , ast ) ;
8686 }
8787
8888 function parseDefineFunction ( node : ts . CallExpression ) : TextRange & {
@@ -102,7 +102,7 @@ export function parseScriptSetupRanges(
102102 ts . isCallExpression ( node )
103103 && ts . isIdentifier ( node . expression )
104104 ) {
105- const callText = node . expression . getText ( ast ) ;
105+ const callText = getNodeText ( ts , node . expression , ast ) ;
106106 if ( vueCompilerOptions . macros . defineModel . includes ( callText ) ) {
107107 let name : TextRange | undefined ;
108108 let options : ts . Node | undefined ;
@@ -121,7 +121,7 @@ export function parseScriptSetupRanges(
121121 let required = false ;
122122 if ( options && ts . isObjectLiteralExpression ( options ) ) {
123123 for ( const property of options . properties ) {
124- if ( ts . isPropertyAssignment ( property ) && ts . isIdentifier ( property . name ) && property . name . getText ( ast ) === 'required' && property . initializer . kind === ts . SyntaxKind . TrueKeyword ) {
124+ if ( ts . isPropertyAssignment ( property ) && ts . isIdentifier ( property . name ) && getNodeText ( ts , property . name , ast ) === 'required' && property . initializer . kind === ts . SyntaxKind . TrueKeyword ) {
125125 required = true ;
126126 break ;
127127 }
@@ -142,7 +142,7 @@ export function parseScriptSetupRanges(
142142 const secondArg = node . arguments [ 1 ] ;
143143 if ( ts . isObjectLiteralExpression ( secondArg ) ) {
144144 for ( const property of secondArg . properties ) {
145- if ( ts . isPropertyAssignment ( property ) && ts . isIdentifier ( property . name ) && property . name . getText ( ast ) === 'required' && property . initializer . kind === ts . SyntaxKind . TrueKeyword ) {
145+ if ( ts . isPropertyAssignment ( property ) && ts . isIdentifier ( property . name ) && getNodeText ( ts , property . name , ast ) === 'required' && property . initializer . kind === ts . SyntaxKind . TrueKeyword ) {
146146 required = true ;
147147 break ;
148148 }
@@ -181,13 +181,13 @@ export function parseScriptSetupRanges(
181181 else if ( vueCompilerOptions . macros . defineSlots . includes ( callText ) ) {
182182 slots . define = parseDefineFunction ( node ) ;
183183 if ( ts . isVariableDeclaration ( parent ) ) {
184- slots . name = parent . name . getText ( ast ) ;
184+ slots . name = getNodeText ( ts , parent . name , ast ) ;
185185 }
186186 }
187187 else if ( vueCompilerOptions . macros . defineEmits . includes ( callText ) ) {
188188 emits . define = parseDefineFunction ( node ) ;
189189 if ( ts . isVariableDeclaration ( parent ) ) {
190- emits . name = parent . name . getText ( ast ) ;
190+ emits . name = getNodeText ( ts , parent . name , ast ) ;
191191 }
192192 }
193193 else if ( vueCompilerOptions . macros . defineExpose . includes ( callText ) ) {
@@ -199,7 +199,7 @@ export function parseScriptSetupRanges(
199199 for ( let i = parents . length - 1 ; i >= 0 ; i -- ) {
200200 if ( ts . isStatement ( parents [ i ] ) ) {
201201 const statement = parents [ i ] ;
202- statement . forEachChild ( child => {
202+ ts . forEachChild ( statement , child => {
203203 const range = _getStartEnd ( child ) ;
204204 statementRange ??= range ;
205205 statementRange . end = range . end ;
@@ -217,7 +217,7 @@ export function parseScriptSetupRanges(
217217 } ;
218218
219219 if ( ts . isVariableDeclaration ( parent ) ) {
220- props . name = parent . name . getText ( ast ) ;
220+ props . name = getNodeText ( ts , parent . name , ast ) ;
221221 }
222222 if ( node . arguments . length ) {
223223 props . define . arg = _getStartEnd ( node . arguments [ 0 ] ) ;
@@ -233,11 +233,11 @@ export function parseScriptSetupRanges(
233233 props . withDefaults . arg = _getStartEnd ( arg ) ;
234234 }
235235 if ( ts . isVariableDeclaration ( parent ) ) {
236- props . name = parent . name . getText ( ast ) ;
236+ props . name = getNodeText ( ts , parent . name , ast ) ;
237237 }
238238 }
239239 }
240- node . forEachChild ( child => {
240+ ts . forEachChild ( node , child => {
241241 parents . push ( node ) ;
242242 visitNode ( child , parents ) ;
243243 parents . pop ( ) ;
@@ -247,7 +247,7 @@ export function parseScriptSetupRanges(
247247
248248export function parseBindingRanges ( ts : typeof import ( 'typescript/lib/tsserverlibrary' ) , sourceFile : ts . SourceFile ) {
249249 const bindings : TextRange [ ] = [ ] ;
250- sourceFile . forEachChild ( node => {
250+ ts . forEachChild ( sourceFile , node => {
251251 if ( ts . isVariableStatement ( node ) ) {
252252 for ( const node_2 of node . declarationList . declarations ) {
253253 const vars = _findBindingVars ( node_2 . name ) ;
@@ -290,7 +290,7 @@ export function parseBindingRanges(ts: typeof import('typescript/lib/tsserverlib
290290 } ) ;
291291 return bindings ;
292292 function _getStartEnd ( node : ts . Node ) {
293- return getStartEnd ( node , sourceFile ) ;
293+ return getStartEnd ( ts , node , sourceFile ) ;
294294 }
295295 function _findBindingVars ( left : ts . BindingName ) {
296296 return findBindingVars ( ts , left , sourceFile ) ;
@@ -303,7 +303,7 @@ export function findBindingVars(ts: typeof import('typescript/lib/tsserverlibrar
303303 return vars ;
304304 function worker ( _node : ts . Node ) {
305305 if ( ts . isIdentifier ( _node ) ) {
306- vars . push ( getStartEnd ( _node , sourceFile ) ) ;
306+ vars . push ( getStartEnd ( ts , _node , sourceFile ) ) ;
307307 }
308308 // { ? } = ...
309309 // [ ? ] = ...
@@ -320,7 +320,7 @@ export function findBindingVars(ts: typeof import('typescript/lib/tsserverlibrar
320320 }
321321 // { foo } = ...
322322 else if ( ts . isShorthandPropertyAssignment ( _node ) ) {
323- vars . push ( getStartEnd ( _node . name , sourceFile ) ) ;
323+ vars . push ( getStartEnd ( ts , _node . name , sourceFile ) ) ;
324324 }
325325 // { ...? } = ...
326326 // [ ...? ] = ...
@@ -330,9 +330,22 @@ export function findBindingVars(ts: typeof import('typescript/lib/tsserverlibrar
330330 }
331331}
332332
333- export function getStartEnd ( node : ts . Node , sourceFile : ts . SourceFile ) {
333+ export function getStartEnd (
334+ ts : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
335+ node : ts . Node ,
336+ sourceFile : ts . SourceFile
337+ ) {
334338 return {
335- start : node . getStart ( sourceFile ) ,
336- end : node . getEnd ( ) ,
339+ start : ( ts as any ) . getTokenPosOfNode ( node , sourceFile ) as number ,
340+ end : node . end ,
337341 } ;
338342}
343+
344+ export function getNodeText (
345+ ts : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
346+ node : ts . Node ,
347+ sourceFile : ts . SourceFile
348+ ) {
349+ const { start, end } = getStartEnd ( ts , node , sourceFile ) ;
350+ return sourceFile . text . substring ( start , end ) ;
351+ }
0 commit comments