forked from stolksdorf/jsx2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx.parser.js
75 lines (70 loc) · 1.83 KB
/
jsx.parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module.exports = (tokens, opts={})=>{
let nodes = [];
let current = 0;
let token = tokens[current];
const parseProps = ()=>{
let props = {};
let key = null;
let last = null;
while(current < tokens.length && token.type != 'endTag' && token.type != 'closeTag'){
if(last && token.type == 'word'){
props[last] = true;
last = token.value;
}else if(!key && token.type == 'word'){
last = token.value;
}else if(last && token.type == 'equals'){
key = last;
last = null;
}else if(key && token.type == 'code'){
if (token.value.match(/^(?:tru|fals)e$/)) {
props[key] = token.value === 'true';
}else if(opts.useEval){
props[key] = eval(`(()=>{ return ${token.value}})()`);
}else{
props[key] = token.value;
}
key = null;
last = null;
}else if(key && (token.type == 'number' || token.type == 'text' || token.type == 'boolean')){
props[key] = token.value;
key = null;
last = null;
}else{
throw `Invalid property value: ${key}=${token.value}`;
}
token = tokens[++current];
}
if(last) props[last] = true;
return props;
}
const genNode = (tagType)=>{
token = tokens[++current];
return {
type : tagType,
props : parseProps(),
children : getChildren(tagType)
};
};
const getChildren = (tagType)=>{
let children = [];
while(current < tokens.length){
if(token.type == 'endTag'){
if(token.value && token.value != tagType){
throw `Invalid closing tag: ${token.value}. Expected closing tag of type: ${tagType}`
}else{
break;
}
}
if(token.type == 'openTag'){
children.push(genNode(token.value));
}else if(token.type == 'text'){
children.push(token.value);
}
token = tokens[++current];
}
return children;
}
const result = getChildren();
if(result.length == 1) return result[0];
return result;
};