1 | 'use strict';
|
2 | const _ = require('lodash');
|
3 | const esprima = require('esprima');
|
4 | const rtError = require('./RTCodeError');
|
5 | const RTCodeError = rtError.RTCodeError;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function validateJS(code, node, context) {
|
13 | try {
|
14 | esprima.parse(code);
|
15 | } catch (e) {
|
16 | throw RTCodeError.build(context, node, e.description);
|
17 | }
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function normalizeName(name) {
|
25 | return name.replace(/-/g, '_');
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function isStringOnlyCode(txt) {
|
33 | return /^\s*\{.*}\s*$/g.test(txt);
|
34 |
|
35 |
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function addIfMissing(array, obj) {
|
43 | if (!_.includes(array, obj)) {
|
44 | array.push(obj);
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function concatChildren(children) {
|
53 | let res = '';
|
54 | _.forEach(children, child => {
|
55 | if (child && !_.startsWith(child, ' /*')) {
|
56 | res += ',';
|
57 | }
|
58 | res += child;
|
59 | });
|
60 | return res;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function validate(options, context, reportContext, node) {
|
71 | if (node.type === 'tag' && node.attribs['rt-if'] && !node.attribs.key) {
|
72 | const loc = rtError.getNodeLoc(context, node);
|
73 | reportContext.warn('rt-if without a key', options.fileName, loc.pos.line, loc.pos.col, loc.start, loc.end);
|
74 | }
|
75 | if (node.type === 'tag' && node.attribs['rt-require'] && (node.attribs.dependency || node.attribs.as)) {
|
76 | const loc = rtError.getNodeLoc(context, node);
|
77 | reportContext.warn("'rt-require' is obsolete, use 'rt-import' instead", options.fileName, loc.pos.line, loc.pos.col, loc.start, loc.end);
|
78 | }
|
79 | if (node.children) {
|
80 | node.children.forEach(validate.bind(this, options, context, reportContext));
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function usesScopeName(scopeNames, node) {
|
90 | function usesScope(root) {
|
91 | return usesScopeName(scopeNames, root);
|
92 | }
|
93 | if (_.isEmpty(scopeNames)) {
|
94 | return false;
|
95 | }
|
96 |
|
97 | if (node.type === 'Identifier') {
|
98 | return _.includes(scopeNames, node.name);
|
99 | }
|
100 |
|
101 | if (node.type === 'Property') {
|
102 | return usesScope(node.value);
|
103 | }
|
104 |
|
105 | if (node.type === 'MemberExpression') {
|
106 | return node.computed ? usesScope(node.object) || usesScope(node.property) : usesScope(node.object);
|
107 | }
|
108 |
|
109 | if (node.type === 'UnaryExpression') {
|
110 | return usesScope(node.argument);
|
111 | }
|
112 |
|
113 | if (node.type === 'LogicalExpression' || node.type === 'BinaryExpression') {
|
114 | return usesScope(node.left) || usesScope(node.right);
|
115 | }
|
116 |
|
117 | if (node.type === 'CallExpression') {
|
118 | return usesScope(node.callee) || _.some(node.arguments, usesScope);
|
119 | }
|
120 |
|
121 | if (node.type === 'ObjectExpression') {
|
122 | return _.some(node.properties, usesScope);
|
123 | }
|
124 |
|
125 | if (node.type === 'ArrayExpression') {
|
126 | return _.some(node.elements, usesScope);
|
127 | }
|
128 | return false;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | const curlyMap = {'{': 1, '}': -1};
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function convertText(node, context, txt) {
|
152 | let res = '';
|
153 | let first = true;
|
154 | const concatChar = node.type === 'text' ? ',' : '+';
|
155 | while (_.includes(txt, '{')) {
|
156 | const start = txt.indexOf('{');
|
157 | const pre = txt.substr(0, start);
|
158 | if (pre) {
|
159 | res += (first ? '' : concatChar) + JSON.stringify(pre);
|
160 | first = false;
|
161 | }
|
162 | let curlyCounter = 1;
|
163 | let end = start;
|
164 | while (++end < txt.length && curlyCounter > 0) {
|
165 | curlyCounter += curlyMap[txt.charAt(end)] || 0;
|
166 | }
|
167 | if (curlyCounter === 0) {
|
168 | const needsParens = start !== 0 || end !== txt.length - 1;
|
169 | res += (first ? '' : concatChar) + (needsParens ? '(' : '') + txt.substr(start + 1, end - start - 2) + (needsParens ? ')' : '');
|
170 | first = false;
|
171 | txt = txt.substr(end);
|
172 | } else {
|
173 | throw RTCodeError.build(context, node, `Failed to parse text '${txt}'`);
|
174 | }
|
175 | }
|
176 | if (txt) {
|
177 | res += (first ? '' : concatChar) + JSON.stringify(txt);
|
178 | }
|
179 | if (res === '') {
|
180 | res = 'true';
|
181 | }
|
182 | return res;
|
183 | }
|
184 |
|
185 |
|
186 | module.exports = {
|
187 | usesScopeName,
|
188 | normalizeName,
|
189 | validateJS,
|
190 | isStringOnlyCode,
|
191 | concatChildren,
|
192 | validate,
|
193 | addIfMissing,
|
194 | convertText
|
195 | };
|