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