Skip to content

Commit ba1d55e

Browse files
committed
Add/remove imports
1 parent cdc154a commit ba1d55e

File tree

5 files changed

+93
-30
lines changed

5 files changed

+93
-30
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![visitors](https://visitor-badge.laobi.icu/badge?page_id=kunaltaitkar.vue-script-parser)
44

5-
This package can be used to parse vue script section and it provides an interface to add, update and delete entities of different life cycles hooks of VueJS.
5+
This package is used to parse vue script section and it provides an interface to add, update and delete entities of different life cycles hooks of VueJS.
66

77

88
## Installation
@@ -79,3 +79,22 @@ export default {
7979
}
8080
```
8181

82+
## Data
83+
84+
### To add new variable in data
85+
86+
```
87+
let instance = new VueScriptParser(code)
88+
instace.addData('country','INDIA')
89+
```
90+
91+
#### Output
92+
93+
```
94+
95+
```
96+
97+
98+
99+
100+

example/Demo.vue

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<button @click="downloadScript()">Download Script</button>
1212
<button @click="addMethod">Add method</button>
1313
<button @click="removeMethod">Remove method</button>
14+
<button @click="addImport">Add import</button>
15+
<button @click="removeImport">Remove import</button>
1416
<p align="center">Input</p>
1517
<textarea
1618
cols="91"
@@ -36,7 +38,9 @@ import VueScriptParser from '../index'
3638
export default {
3739
data() {
3840
return {
39-
input: `export default {
41+
input: `
42+
import a from 'a'
43+
export default {
4044
data() {
4145
return {}
4246
},
@@ -68,6 +72,7 @@ export default {
6872
key: '',
6973
val:''
7074
}
75+
this.updateJSON()
7176
},
7277
removeVariable() {
7378
this.instance.removeData(this.variable.key)
@@ -76,6 +81,7 @@ export default {
7681
key: '',
7782
val:''
7883
}
84+
this.updateJSON()
7985
},
8086
downloadScript() {
8187
alert(this.instance.script)
@@ -84,15 +90,31 @@ export default {
8490
this.instance.addMounted(this.mountedBody)
8591
this.input = this.instance.script
8692
this.mountedBody = ''
93+
this.updateJSON()
8794
},
8895
addMethod() {
8996
this.instance.addMethod('test', "console.log('this is test method')", "args1,arg2")
9097
this.instance.addMethod('test2', "console.log('this is test 2 method')")
9198
this.input = this.instance.script
99+
this.updateJSON()
92100
},
93101
removeMethod() {
94102
this.instance.removeMethod('test2')
95103
this.input = this.instance.script
104+
this.updateJSON()
105+
},
106+
addImport() {
107+
this.instance.addImport("import c from 'c'")
108+
this.input = this.instance.script
109+
this.updateJSON()
110+
},
111+
removeImport() {
112+
this.instance.removeImport("import c from 'c'")
113+
this.input = this.instance.script
114+
this.updateJSON()
115+
},
116+
updateJSON() {
117+
this.output = JSON.stringify(this.instance, undefined, 2)
96118
}
97119
},
98120
}

index.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,54 @@ import { pattern } from './modules/constant'
77
class 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

modules/constant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
const pattern = {
4+
importsRegex: /import .*/g,
45
dataRegex: /data\s*\(\s*\)\s*\{\s*return\s*{([^]*)}/g,
56
mountedRegex: /mounted\s*\(\s*\)\s*{([^]*)}/g,
67
methodsRegex: /methods\s*:\s*{([^]*)}/g

modules/imports.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)