-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-jsx.js
165 lines (155 loc) · 4.88 KB
/
parse-jsx.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const babylon = require("babylon");
const postcss = require('postcss');
const {
default: traverse
} = require("babel-traverse");
const fs = require('fs');
const astCache = [];
// const csscode = fs.readFileSync('./test/a.less', 'utf8').toString();
// const code = fs.readFileSync('./test/a.jsx', 'utf8').toString();
// getInfo(code, 4603);
// postcss().process(csscode).then(result => {
// const info = getInfo(code, 4603);
// const styleStack=info.styleStack;
// styleStack.shift();
// let styleArr=[];
// styleStack.forEach((v)=>{
// if(Array.isArray(v)){
// styleArr=styleArr.concat(v);
// }else{
// styleArr.push(v);
// }
// });
// console.log(styleArr);
// styleArr.forEach((className)=>{
// const pos=lessGetPosition(result,info.selectorName,className);
// console.log(pos);
// });
// });
// console.log(code)
function getCodeofNode(code, node) {
return code.slice(node.start, node.end);
}
function getAst(code) {
let ast = astCache.find(v => v.code === code);
if (ast) {
return ast.ast;
}
ast = babylon.parse(code, {
sourceType: 'module',
plugins: [
// enable jsx and flow syntax
"jsx",
"flow",
"decorators",
"typescript",
"doExpressions",
"objectRestSpread",
"classProperties",
"classPrivateProperties",
"asyncGenerators",
"functionBind",
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding'
]
});
astCache.push({ code, ast });
if (astCache.length > 10) {
astCache.shift();
}
return ast;
}
function getInfo(code, position) {
// console.log(code.slice(position, position + 40))
const ast = getAst(code);
const stack = [];
traverse(ast, {
enter(path) {
if (path.node.start <= position && path.node.end >= position) {
stack.push(path);
// console.log(path.node.type);
}
}
});
let styleName;
let styleStack = [];
for (let i = stack.length - 1; i >= 0; i--) {
const path = stack[i];
const node = path.node;
if (!node) {
continue;
}
if (node.type === 'JSXElement') {
// console.log(node.openingElement.attributes);
// console.log(getCodeofNode(code,node))
node.openingElement.attributes.forEach((attr) => {
// console.log(node.openingElement);
if (attr.name && attr.name.name === 'style') {
let value = attr.value;
if (value.type === 'JSXExpressionContainer') {
value = value.expression;
}
if (value.type === 'StringLiteral') {
// console.log(value.value);
styleStack.push(value.value);
} else if (value.type === 'ArrayExpression') {
styleStack.push(value.elements.filter((prop) => {
return prop.type === 'StringLiteral'
}).map((prop) => {
return prop.value
}));
}
}
});
}
if (node.type === 'JSXAttribute') {
const attrName = node.name.name;
if (attrName !== 'style') {
return false;
}
}
if (node.type === 'ClassDeclaration') {
const decorators = node.decorators;
// console.log(decorators);
decorators.forEach((node) => {
// console.log(node.expression);
if (!node.expression || !node.expression.callee) {
return;
}
if (node.expression.callee.name === 'rnLess') {
const args = node.expression.arguments;
if (!args.length) {
return;
}
const expression = getCodeofNode(code, args[0]);
// console.log('args',expression);
styleName = expression.match(/[\w$-]+/g);
if (styleName) {
styleName = styleName.pop();
}
}
});
}
}
// console.log('jsxStack',styleStack);
if (!styleName) {
return false;
}
return { styleName, styleStack };
}
module.exports = {
getInfo: function (code, position) {
try {
return getInfo(code, position);
} catch (e) {
console.log(e);
return false;
}
}
}
// console.log(ast);