Skip to content

Commit 80d0c84

Browse files
committed
Initial commit
1 parent 655ded1 commit 80d0c84

File tree

9 files changed

+337
-0
lines changed

9 files changed

+337
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "vue-script-parser",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/kunaltaitkar/vue-script-parser.git"
12+
},
13+
"keywords": [
14+
"vue",
15+
"vue-parser",
16+
"vue-script",
17+
"vue",
18+
"script",
19+
"javascript",
20+
"vue",
21+
"code",
22+
"parser"
23+
],
24+
"author": "Kunal Taitkar",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/kunaltaitkar/vue-script-parser/issues"
28+
},
29+
"homepage": "https://github.com/kunaltaitkar/vue-script-parser#readme"
30+
}

src/Demo.vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<div>
3+
<div class="inline-div">
4+
<p align="center">Input</p>
5+
<textarea cols="91" @input="parseCode" rows="39" class="inline-txtarea" v-model="input"></textarea>
6+
</div>
7+
<div class="inline-div">
8+
<p align="center">Output</p>
9+
<textarea cols="91" rows="39" class="inline-txtarea" v-model="output"></textarea>
10+
</div>
11+
</div>
12+
</template>
13+
14+
<script>
15+
import VueScriptParser from './plugin/index'
16+
export default {
17+
data() {
18+
return {
19+
input: ``,
20+
output: '{}',
21+
}
22+
},
23+
methods: {
24+
parseCode() {
25+
let instance = new VueScriptParser(this.input)
26+
this.output = JSON.stringify(instance, undefined, 2)
27+
},
28+
},
29+
}
30+
</script>
31+
32+
<style>
33+
.inline-div {
34+
display: inline-block;
35+
}
36+
.inline-txtarea {
37+
resize: none;
38+
border: 2px solid;
39+
/* height:125px; */
40+
}
41+
</style>

src/plugin/common.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
const scriptSectionProcessing = function (vueScript, regx) {
3+
let methodsString = vueScript
4+
if (regx) {
5+
var extract = vueScript.match(regx)
6+
methodsString = extract[0]
7+
}
8+
let stackP = []
9+
let firstIndex = null
10+
let lastIndex = null
11+
for (let i = 0; i < methodsString.length; i++) {
12+
if (methodsString[i] === '{') {
13+
if (stackP.length === 0) {
14+
firstIndex = i
15+
}
16+
stackP.push(methodsString[i])
17+
}
18+
if (methodsString[i] === '}') {
19+
stackP.pop()
20+
if (stackP.length === 0) {
21+
lastIndex = i
22+
break
23+
}
24+
}
25+
}
26+
return {
27+
methodsString,
28+
firstIndex,
29+
lastIndex
30+
}
31+
}
32+
33+
export { scriptSectionProcessing }

src/plugin/data.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
import Vue from 'vue'
3+
class Data {
4+
constructor(vueScript = '') {
5+
this.data = this.setData(vueScript)
6+
}
7+
8+
setData (vueScript = '') {
9+
let dataRegx = /data\s*\(\s*\)\s*\{\s*return\s*{([^]*)}/g
10+
let processedData = this.extractAndUpdateData(vueScript, dataRegx)
11+
12+
let dataFunc = new Function('return {' + processedData.oldData + '}')
13+
let vm = new Vue({
14+
data: dataFunc
15+
})
16+
17+
18+
let result = []
19+
20+
Object.keys(vm._data).forEach(key => {
21+
result.push({
22+
key: key,
23+
value: vm._data[key]
24+
})
25+
})
26+
27+
28+
return result
29+
}
30+
extractAndUpdateData (vueScript, regx) {
31+
let data = this.dataSectionProcessing(vueScript, regx)
32+
let oldData = data.methodsString.substring(
33+
data.firstIndex + 1,
34+
data.lastIndex
35+
)
36+
return { oldData, data }
37+
}
38+
dataSectionProcessing (stringToBeProcessed, regx) {
39+
let methodsString = stringToBeProcessed
40+
if (regx) {
41+
var extract = stringToBeProcessed.match(regx)
42+
methodsString = extract[0]
43+
}
44+
let stackP = []
45+
let firstIndex = null
46+
let lastIndex = null
47+
for (let i = 0; i < methodsString.length; i++) {
48+
if (methodsString[i] === '{') {
49+
if (stackP.length === 1) {
50+
firstIndex = i
51+
}
52+
stackP.push(methodsString[i])
53+
}
54+
if (methodsString[i] === '}') {
55+
stackP.pop()
56+
if (stackP.length === 1) {
57+
lastIndex = i
58+
break
59+
}
60+
}
61+
}
62+
return { methodsString, firstIndex, lastIndex }
63+
}
64+
65+
66+
}
67+
68+
export default Data

src/plugin/imports.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Imports {
2+
constructor(vueScript = '') {
3+
this.imports = this.setImports(vueScript) || []
4+
}
5+
setImports (vueScript) {
6+
return vueScript.match(/import .*/g)
7+
}
8+
9+
}
10+
11+
12+
export default Imports

src/plugin/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Data from './data'
2+
import Methods from './methods'
3+
import Mounted from './mounted'
4+
import Imports from './imports'
5+
class VueScriptParser {
6+
constructor(vueScript = '') {
7+
//load imports
8+
let importsProcessor = new Imports(vueScript)
9+
this.imports = importsProcessor.imports
10+
11+
//load data
12+
let dataProcessor = new Data(vueScript)
13+
this.data = dataProcessor.data
14+
15+
//load mounted
16+
let mountedProcessor = new Mounted(vueScript)
17+
this.mounted = mountedProcessor.mounted
18+
19+
//load methods
20+
let methodsProcessor = new Methods(vueScript)
21+
this.methods = methodsProcessor.methods
22+
}
23+
}
24+
25+
export default VueScriptParser

src/plugin/methods.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { scriptSectionProcessing } from './common'
2+
3+
class Methods {
4+
constructor(vueScript = '') {
5+
this.methods = this.setMethod(vueScript)
6+
}
7+
setMethod (vueScript = '') {
8+
let regx = /methods\s*:\s*{([^]*)}/g
9+
let matchMethods = vueScript.match(regx)
10+
if (!matchMethods) {
11+
let data = scriptSectionProcessing(vueScript)
12+
13+
vueScript =
14+
data.methodsString.substring(0, data.lastIndex) +
15+
',\n methods : {}\n' +
16+
data.methodsString[data.lastIndex]
17+
}
18+
let methodsData = this.extractMethodsFromScript(regx, vueScript)
19+
20+
return methodsData || []
21+
22+
}
23+
extractMethodsFromScript (regx, vueScript = '') {
24+
let { methodsString, firstIndex, lastIndex } = scriptSectionProcessing(
25+
vueScript,
26+
regx
27+
)
28+
return this.makeArrayOfMethods(
29+
methodsString.substring(firstIndex + 1, lastIndex)
30+
)
31+
}
32+
33+
makeArrayOfMethods (methodsStr) {
34+
let methodDetails = []
35+
let arrayOfFirstIndexes = []
36+
let arrayOfLastIndexes = []
37+
let bracesStk = []
38+
let fIndex = 0
39+
let lIndex = -2
40+
var functionName = ''
41+
if (methodsStr) {
42+
for (let i = 0; i < methodsStr.length; i++) {
43+
if (bracesStk.length === 0 && methodsStr[i] === '(') {
44+
functionName = methodsStr
45+
.substring(lIndex + 2, i)
46+
.split('\n')
47+
.join('')
48+
.split(' ')
49+
.join('')
50+
}
51+
if (methodsStr[i] === '{') {
52+
if (bracesStk.length === 0) {
53+
fIndex = i
54+
arrayOfFirstIndexes.push(fIndex)
55+
}
56+
bracesStk.push(methodsStr[i])
57+
}
58+
if (methodsStr[i] === '}') {
59+
bracesStk.pop()
60+
if (bracesStk.length === 0) {
61+
lIndex = i
62+
arrayOfLastIndexes.push(lIndex)
63+
var obj = {
64+
name: functionName,
65+
body: methodsStr.substring(fIndex + 1, lIndex),
66+
arguments: []
67+
}
68+
methodDetails.push(obj)
69+
}
70+
}
71+
}
72+
for (let i = 0; i < arrayOfFirstIndexes.length; i++) {
73+
if (i == 0) {
74+
methodDetails[i].arguments = this.collectArgs(
75+
methodsStr.substring(0, arrayOfFirstIndexes[i])
76+
)
77+
} else {
78+
methodDetails[i].arguments = this.collectArgs(
79+
methodsStr.substring(
80+
arrayOfLastIndexes[i - 1] + 2,
81+
arrayOfFirstIndexes[i]
82+
)
83+
)
84+
}
85+
}
86+
return methodDetails
87+
}
88+
return []
89+
}
90+
collectArgs (str) {
91+
let firstParentheses = null
92+
let lastParentheses = null
93+
for (let i = 0; i < str.length; i++) {
94+
if (str[i] == '(') {
95+
firstParentheses = i
96+
}
97+
if (str[i] == ')') {
98+
lastParentheses = i
99+
}
100+
}
101+
return str.substring(firstParentheses + 1, lastParentheses).split(',')
102+
}
103+
}
104+
105+
106+
export default Methods

src/plugin/mounted.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { scriptSectionProcessing } from './common'
2+
class Mounted {
3+
constructor(vueScript) {
4+
let regx = /mounted\s*\(\s*\)\s*{([^]*)}/g
5+
this.mounted = this.extractAndUpdateMounted(regx, vueScript)
6+
}
7+
extractAndUpdateMounted (regx, vueScript) {
8+
let newScript = ''
9+
let matchMounted = vueScript.match(regx)
10+
if (!matchMounted) {
11+
newScript = '\n mounted() { \n' + '\n}'
12+
} else {
13+
let data = scriptSectionProcessing(vueScript, regx)
14+
newScript = data.methodsString.substring(data.firstIndex + 1, data.lastIndex)
15+
}
16+
17+
return newScript
18+
}
19+
}
20+
21+
export default Mounted

0 commit comments

Comments
 (0)