-
Notifications
You must be signed in to change notification settings - Fork 15
/
jsx-helper.js
207 lines (202 loc) · 7.63 KB
/
jsx-helper.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module.exports = (eslint = {}) => {
const CLIEngine = (eslint && eslint.CLIEngine) || null;
const cli = CLIEngine ? new CLIEngine({
useEslintrc: false,
fix: true,
rules: { 'prefer-template': 'error' },
}) : {};
const constant = {
LESS_THAN: '__less_than__',
GREATER_THAN: '__greater_than__',
};
const eslintFix = (source) => {
const report = typeof cli.executeOnText === 'function' ? cli.executeOnText(source) : source;
if (report && report.results && report.results[0] && report.results[0].output) {
return report.results[0].output
.replace(/\s+/g, ' ')
.replace(/{\s+([a-zA-z0-9_])/g, '{$1')
.replace(/([a-zA-z0-9_])\s+}/g, '$1}')
.replace(/`(.*?)`/g, '__tick_start__$1__tick_end__')
.replace(/__tick_end__\s*\+\s*__tick_start__/g, '')
.replace(/__tick_(start|end)__/g, '`')
.trim();
}
return source;
};
const jsxHelper = {
TEXT_NODE: 'TEXT_NODE',
blocks: {},
quotes: {},
preserveQuote(source) {
const self = this;
return source.replace(/"(.*?)"/g, (whole, p1) => {
const randomId = Math.random();
self.quotes[randomId] = p1;
return `__quote:${randomId}__`;
});
},
preserveBlock(source) {
const self = this;
let replaced = source.replace(/{([^{}]+)}/g, (whole, p1) => {
const randomId = Math.random();
self.blocks[randomId] = p1;
return `__block:${randomId}__`;
});
if (replaced.match(/{([^}]+)}/g)) {
replaced = self.preserveBlock(replaced);
}
return replaced;
},
parseJSX(jsx, children = [], stack = []) {
const self = this;
let cursor = children;
jsx.split(/(<[^>]+>)/).filter(each => each.trim()).forEach((_chunk) => {
const chunk = _chunk.trim();
const is = { textNode: true };
if (chunk.search(/^<[^/]/) !== -1) {
is.open = true;
delete is.textNode;
}
if (chunk.search(/^<\//) !== -1 || chunk.search(/^<.+\/>$/) !== -1) {
is.close = true;
delete is.textNode;
}
if (is.open) {
const elem = {
type: chunk.replace(/^<([a-zA-Z0-9.]+)/, '$1 ').split(/\s+/)[0],
props: ((chunk.replace(/(\/){0,1}>/, ' ').match(/([a-zA-Z0-9-]+)=([^\s]+)/g)) || []).reduce((props, curr) => {
const [, name, value] = curr.match(/([a-zA-Z0-9-]+)=([^\s]+)/);
return Object.assign({}, props, {
[name]: value,
});
}, {}),
children: [],
};
cursor.push(elem);
cursor = elem.children;
stack.push(elem);
}
if (is.textNode) {
chunk.split(/(__block:[0-9.]+__)/).forEach((each) => {
if (!each) {
return;
}
const found = each.match(/__block:([0-9.]+)__/);
if (found && self.blocks[found[1]]) {
self.parseJSX(`{${self.blocks[found[1]]}}`, cursor, stack);
return;
}
const elem = {
type: self.TEXT_NODE,
context: each,
};
cursor.push(elem);
if (chunk.search(/\.map\s*\(.*?\)\s*=>/) !== -1) {
cursor.mapFn = true;
elem.indent = true;
} else if (cursor.mapFn && chunk.search(/^\s*\)\s*}/) !== -1) {
delete cursor.mapFn;
elem.outdent = true;
}
});
}
if (is.close) {
stack.pop();
if (stack.length > 0) {
cursor = stack[stack.length - 1].children;
} else {
cursor = children;
}
}
});
return children;
},
retrieveBlock(source) {
const self = this;
return source.replace(/__quote:([0-9.]+)__/g, (whole, p1) => `'${self.quotes[p1]}'`)
.replace(/__block:([0-9.]+)__/g, (whole, p1) => {
self.blocks[p1] = self.blocks[p1].replace(/__quote:([0-9.]+)__/g, (_whole, q1) => `'${self.quotes[q1]}'`);
if (self.blocks[p1].match(/__block:([0-9.]+)__/)) {
self.blocks[p1] = self.retrieveBlock(self.blocks[p1]);
}
return `{${eslintFix(self.blocks[p1])}}`;
});
},
printElem(elem, indent, options, closeTag) {
const self = this;
const replaced = elem.type + Object.keys(elem.props).sort().reduce((prev, curr) => {
let value = elem.props[curr];
value = value.replace(/__quote:([0-9.]+)__/g, (whole, p1) => `"${self.quotes[p1]}"`);
value = self.retrieveBlock(value);
prev.push(`${curr}=${value}`);
return prev;
}, ['']).join('__attributes__');
let result = `${indent}<${replaced.replace(/__attributes__/g, ' ')}${closeTag ? ' />' : '>'}`;
if (options.maxLineLength && options.maxLineLength < result.length) {
result = `${indent}<${replaced.replace(/__attributes__/g, `\n${indent} `)}\n${indent}${closeTag ? '/>' : '>'}`;
}
return result;
},
printJSX(children, options = {}, buffer = [], _indent = '') {
const self = this;
let indent = _indent;
children.forEach((each) => {
if (each.context === ');}') {
each.outdent = true;
}
if (each.outdent) {
indent = indent.substr(2);
}
if (each.type === self.TEXT_NODE) {
buffer.push(self.retrieveBlock(`${indent}__textnode_start__${each.context}__textnode_end__`));
} else if (each.children && each.children.length > 0) {
if (each.children.length === 1 && each.children[0].type === self.TEXT_NODE) {
const replaced = `${self.printElem(each, indent, options)}${each.children[0].context}</${each.type}>`;
if (options.maxLineLength && options.maxLineLength < replaced.length) {
buffer.push(`${self.printElem(each, indent, options)}\n${indent} ${each.children[0].context}\n${indent}</${each.type}>`);
} else {
buffer.push(replaced);
}
} else {
buffer.push(`${self.printElem(each, indent, options)}`);
self.printJSX(each.children, options, buffer, `${indent} `);
buffer.push(`${indent}</${each.type}>`);
}
} else {
buffer.push(`${self.printElem(each, indent, options, true)}`);
}
if (each.indent) {
indent += ' ';
}
});
const padding = Array((options.indent || 0) + 1).join(' ');
return padding + buffer.join('\n').replace(/\n/g, `\n${padding}`);
},
beautify(jsx, options) {
let replaced = jsx.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim();
const self = this;
self.quotes = {};
self.blocks = {};
replaced = self.preserveQuote(replaced);
replaced = self.preserveBlock(replaced);
replaced = self.printJSX(self.parseJSX(replaced), options);
replaced = replaced.replace(/__textnode_end__\n\s*__textnode_start__/g, '')
.replace(/\n(\s*)__textnode_start__/gm, '\n$1')
.replace(/__textnode_start__(\s*)\n/gm, '$1\n')
.replace(/__textnode_(start|end)__/g, '');
if (options && options.lineDivider) {
replaced = replaced.replace(new RegExp(`(\\s+)(.*?){\\s*\\/\\*\\s*${options.lineDivider}\\s*\\*\\/\\s*}(.*)`, 'g'), '\n$1$2\n$1$3');
}
// convert protected inequality symbols
replaced = replaced
.replace(new RegExp(constant.LESS_THAN, 'g'), '<')
.replace(new RegExp(constant.GREATER_THAN, 'g'), '>')
.replace(/jsx-syntax-[0-9]+--=/g, '');
return replaced.replace(/\s+$/gm, '');
},
};
return {
constant,
beautify: (jsx, options) => jsxHelper.beautify(jsx, options),
};
};