11import type vueTemplateParser from "vue-template-compiler" ;
22import type ts from "typescript" ;
33import { ASTResult , ASTResultKind , ReferenceKind } from "./plugins/types" ;
4+ import { TsHelper } from "./helpers/TsHelper" ;
5+
46export function isVueFile ( path : string ) : boolean {
57 return path . endsWith ( ".vue" ) ;
68}
@@ -85,80 +87,8 @@ export function isPrimitiveType(tsModule: typeof ts, returnType: ts.Type): boole
8587 ) ;
8688}
8789
88- export function copySyntheticComments < T extends ts . Node > (
89- tsModule : typeof ts ,
90- node : T ,
91- copyNode : ts . Node
92- ) : T {
93- const leadingComments =
94- tsModule . getLeadingCommentRanges ( copyNode . getSourceFile ( ) . getFullText ( ) , copyNode . pos ) || [ ] ;
95- const trailingComments =
96- tsModule . getTrailingCommentRanges ( copyNode . getSourceFile ( ) . getFullText ( ) , copyNode . end ) || [ ] ;
97-
98- const getCommentText = ( comment : ts . CommentRange ) => {
99- return copyNode
100- . getSourceFile ( )
101- . getFullText ( )
102- . slice ( comment . pos , comment . end )
103- . replace ( / \/ \/ / g, "" )
104- . replace ( / \/ \* / g, "" )
105- . replace ( / \* \/ / g, "" )
106- . replace ( / { 2 } \* ? / g, "* " )
107- . replace ( / \* \/ / g, "*/" )
108- . replace ( / { 2 } $ / g, "" ) ;
109- } ;
110-
111- let result = node ;
112- for ( const comment of leadingComments ) {
113- const text = getCommentText ( comment ) ;
114- result = tsModule . addSyntheticLeadingComment (
115- result ,
116- comment . kind ,
117- text ,
118- comment . hasTrailingNewLine
119- ) ;
120- }
121-
122- for ( const comment of trailingComments ) {
123- const text = getCommentText ( comment ) ;
124- result = tsModule . addSyntheticTrailingComment (
125- result ,
126- comment . kind ,
127- text ,
128- comment . hasTrailingNewLine
129- ) ;
130- }
131-
132- return node ;
133- }
134-
135- export function removeComments < T extends ts . Node > (
136- tsModule : typeof ts ,
137- node : T
138- ) : T | ts . StringLiteral {
139- if ( tsModule . isStringLiteral ( node ) ) {
140- return tsModule . createStringLiteral ( node . text ) ;
141- }
142- return node ;
143- }
144-
145- export function addTodoComment < T extends ts . Node > (
146- tsModule : typeof ts ,
147- node : T ,
148- text : string ,
149- multiline : boolean
150- ) : T {
151- return tsModule . addSyntheticLeadingComment (
152- node ,
153- multiline
154- ? tsModule . SyntaxKind . MultiLineCommentTrivia
155- : tsModule . SyntaxKind . SingleLineCommentTrivia ,
156- ` TODO: ${ text } `
157- ) ;
158- }
159-
16090export function convertNodeToASTResult < T extends ts . Node > (
161- tsModule : typeof ts ,
91+ tsHelper : TsHelper ,
16292 node : T
16393) : ASTResult < T > {
16494 return {
@@ -167,18 +97,10 @@ export function convertNodeToASTResult<T extends ts.Node>(
16797 reference : ReferenceKind . NONE ,
16898 attributes : [ ] ,
16999 tag : "IheritObjProperty" ,
170- nodes : [ addTodoComment ( tsModule , node , "Can't convert this object property." , false ) ] ,
100+ nodes : [ tsHelper . addTodoComment ( node , "Can't convert this object property." , false ) ] ,
171101 } ;
172102}
173103
174- // ts.createIdentifier() cannot call getText function, it's a hack.
175- export function createIdentifier ( tsModule : typeof ts , text : string ) : ts . Identifier {
176- const temp = tsModule . factory . createIdentifier ( text ) ;
177- // eslint-disable-next-line @typescript-eslint/unbound-method
178- temp . getText = ( ) => text ;
179- return temp ;
180- }
181-
182- export function isString ( val : any ) : val is string {
104+ export function isString ( val : unknown ) : val is string {
183105 return typeof val === "string" ;
184106}
0 commit comments