@@ -7,25 +7,54 @@ import { pattern } from './modules/constant'
77class VueScriptParser {
88 constructor ( vueScript = '' ) {
99
10- //set original script
10+ // set original script
1111 this . script = vueScript
1212
13- //load imports
13+ // load imports
14+ this . imports = this . importsReader ( )
1415
15-
16- //load data
16+ // load data
1717 this . data = this . dataReader ( )
1818
19-
20- //load mounted
19+ // load mounted
2120 this . mounted = this . mountedReader ( )
2221
23-
24- //load methods
22+ // load methods
2523 this . methods = this . methodsReader ( )
2624
2725 }
26+ // to read imports from given script and parse
27+ importsReader ( ) {
28+ return this . script . match ( pattern . importsRegex ) || [ ]
29+ }
30+ // to add new import statement
31+ addImport ( statement = '' ) {
32+ if ( ! statement ) {
33+ return
34+ }
35+
36+ ( this . imports || [ ] ) . push ( statement )
37+
38+ let importStr = '' ;
39+
40+ ( this . imports || [ ] ) . forEach ( ( imp ) => {
41+ importStr += imp + '\n'
42+ } )
43+
44+ this . script = this . script . replace ( pattern . importsRegex , importStr )
45+ this . imports = this . importsReader ( ) || [ ]
46+
47+ }
48+ // to remove import from given script
49+ removeImport ( statement ) {
50+ if ( ! statement ) {
51+ return
52+ }
2853
54+ this . script = this . script . replace ( statement , '' )
55+ this . imports = this . importsReader ( ) || [ ]
56+ }
57+ // to read data section from given script and parses
2958 dataReader ( ) {
3059 let processedData = extractAndUpdateData ( this . script , pattern . dataRegex )
3160
@@ -47,7 +76,7 @@ class VueScriptParser {
4776
4877 return result
4978 }
50-
79+ // to add new variable in data section of given script
5180 addData ( key , value ) {
5281 let processedData = extractAndUpdateData ( this . script , pattern . dataRegex )
5382
@@ -77,9 +106,9 @@ class VueScriptParser {
77106 pattern . dataRegex ,
78107 script
79108 )
80-
109+ this . data = this . dataReader ( ) || [ ]
81110 }
82-
111+ // to remove variable from data section of given script
83112 removeData ( key ) {
84113
85114 let processedData = extractAndUpdateData ( this . script , pattern . dataRegex )
@@ -110,9 +139,10 @@ class VueScriptParser {
110139 pattern . dataRegex ,
111140 script
112141 )
113-
142+ this . data = this . dataReader ( ) || [ ]
114143
115144 }
145+ // to read mounted section body from given script
116146 mountedReader ( ) {
117147
118148 let newScript = ''
@@ -127,7 +157,7 @@ class VueScriptParser {
127157
128158 return newScript
129159 }
130-
160+ // to add/update mounted body in given script
131161 addMounted ( body ) {
132162
133163 let updatedVueScript = extractAndUpdateMounted (
@@ -139,8 +169,9 @@ class VueScriptParser {
139169 if ( updatedVueScript ) {
140170 this . script = updatedVueScript
141171 }
172+ this . mounted = this . mountedReader ( ) || ''
142173 }
143-
174+ // to read methods section from given script and parse
144175 methodsReader ( ) {
145176 let matchMethods = this . script . match ( pattern . methodsRegex )
146177 if ( ! matchMethods ) {
@@ -156,7 +187,7 @@ class VueScriptParser {
156187 return methodsData || [ ]
157188
158189 }
159-
190+ // to add new method in given script
160191 addMethod ( name = '' , body = '' , args = '' ) {
161192
162193 let newMethod = {
@@ -190,8 +221,9 @@ class VueScriptParser {
190221 methodsString . substring ( lastIndex , methodsString . length )
191222
192223 this . script = this . script . replace ( pattern . methodsRegex , updatedMethods )
224+ this . methods = this . methodsReader ( ) || [ ]
193225 }
194-
226+ // to remove method from given script
195227 removeMethod ( name = '' ) {
196228
197229 let matchMethods = this . script . match ( pattern . methodsRegex )
@@ -241,6 +273,7 @@ class VueScriptParser {
241273 methodsString . substring ( lastIndex , methodsString . length )
242274
243275 this . script = this . script . replace ( pattern . methodsRegex , updatedMethodsData )
276+ this . methods = this . methodsReader ( ) || [ ]
244277 }
245278}
246279
0 commit comments