1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const babel = require("@babel/types");
|
17 | const esutil_1 = require("./esutil");
|
18 | const jsdoc = require("./jsdoc");
|
19 |
|
20 |
|
21 |
|
22 | function literalToValue(literal) {
|
23 | if (babel.isBooleanLiteral(literal) || babel.isNumericLiteral(literal) ||
|
24 | babel.isStringLiteral(literal)) {
|
25 | return literal.value;
|
26 | }
|
27 | if (babel.isNullLiteral(literal)) {
|
28 | return null;
|
29 | }
|
30 | if (babel.isTemplateLiteral(literal)) {
|
31 | return templateLiteralToValue(literal);
|
32 | }
|
33 |
|
34 | return undefined;
|
35 | }
|
36 | function templateLiteralToValue(literal) {
|
37 | const len = literal.quasis.length - 1;
|
38 | let value = '';
|
39 | for (let i = 0; i < len; i++) {
|
40 | value += literal.quasis[i].value.raw;
|
41 | const v = expressionToValue(literal.expressions[i]);
|
42 | if (v === undefined) {
|
43 | return;
|
44 | }
|
45 | value += `${v}`;
|
46 | }
|
47 | value += literal.quasis[len].value.raw;
|
48 | return value;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 | function unaryToValue(unary) {
|
54 | const operand = expressionToValue(unary.argument);
|
55 | switch (unary.operator) {
|
56 | case '!':
|
57 |
|
58 | return !operand;
|
59 | case '-':
|
60 |
|
61 | return -operand;
|
62 | case '+':
|
63 |
|
64 | return +operand;
|
65 | case '~':
|
66 |
|
67 | return ~operand;
|
68 | case 'typeof':
|
69 | return typeof operand;
|
70 | case 'void':
|
71 | return void operand;
|
72 | case 'delete':
|
73 | return undefined;
|
74 | default:
|
75 | const never = unary.operator;
|
76 | throw new Error(`Unknown unary operator found: ${never}`);
|
77 | }
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 | function functionDeclarationToValue(fn) {
|
83 | if (babel.isBlockStatement(fn.body)) {
|
84 | return blockStatementToValue(fn.body);
|
85 | }
|
86 | }
|
87 | function functionExpressionToValue(fn) {
|
88 | if (babel.isBlockStatement(fn.body)) {
|
89 | return blockStatementToValue(fn.body);
|
90 | }
|
91 | }
|
92 | function arrowFunctionExpressionToValue(fn) {
|
93 | if (babel.isBlockStatement(fn.body)) {
|
94 | return blockStatementToValue(fn.body);
|
95 | }
|
96 | else {
|
97 | return expressionToValue(fn.body);
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 | function blockStatementToValue(block) {
|
104 | for (let i = block.body.length - 1; i >= 0; i--) {
|
105 | const body = block.body[i];
|
106 | if (babel.isReturnStatement(body)) {
|
107 | return returnStatementToValue(body);
|
108 | }
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 | function returnStatementToValue(ret) {
|
115 | return expressionToValue(ret.argument || { type: 'Literal', value: null, raw: 'null' });
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 | function arrayExpressionToValue(arry) {
|
121 | const value = [];
|
122 | for (let i = 0; i < arry.elements.length; i++) {
|
123 | const v = expressionToValue(arry.elements[i]);
|
124 | if (v === undefined) {
|
125 | continue;
|
126 | }
|
127 | value.push(v);
|
128 | }
|
129 | return value;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | function objectExpressionToValue(obj) {
|
135 | const evaluatedObjectExpression = {};
|
136 | for (const prop of esutil_1.getSimpleObjectProperties(obj)) {
|
137 | const evaluatedKey = esutil_1.getPropertyName(prop);
|
138 | if (evaluatedKey === undefined) {
|
139 | return;
|
140 | }
|
141 | const evaluatedValue = expressionToValue(prop.value);
|
142 | if (evaluatedValue === undefined) {
|
143 | return;
|
144 | }
|
145 | evaluatedObjectExpression[evaluatedKey] = evaluatedValue;
|
146 | }
|
147 | return evaluatedObjectExpression;
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 | function binaryExpressionToValue(member) {
|
153 | const left = expressionToValue(member.left);
|
154 | const right = expressionToValue(member.right);
|
155 | if (left == null || right == null) {
|
156 | return;
|
157 | }
|
158 | if (member.operator === '+') {
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | return left + right;
|
164 | }
|
165 | return;
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 | function expressionToValue(valueExpression) {
|
171 | if (babel.isLiteral(valueExpression)) {
|
172 | return literalToValue(valueExpression);
|
173 | }
|
174 | if (babel.isUnaryExpression(valueExpression)) {
|
175 | return unaryToValue(valueExpression);
|
176 | }
|
177 | if (babel.isFunctionDeclaration(valueExpression)) {
|
178 | return functionDeclarationToValue(valueExpression);
|
179 | }
|
180 | if (babel.isFunctionExpression(valueExpression)) {
|
181 | return functionExpressionToValue(valueExpression);
|
182 | }
|
183 | if (babel.isArrowFunctionExpression(valueExpression)) {
|
184 | return arrowFunctionExpressionToValue(valueExpression);
|
185 | }
|
186 | if (babel.isArrayExpression(valueExpression)) {
|
187 | return arrayExpressionToValue(valueExpression);
|
188 | }
|
189 | if (babel.isObjectExpression(valueExpression)) {
|
190 | return objectExpressionToValue(valueExpression);
|
191 | }
|
192 | if (babel.isBinaryExpression(valueExpression)) {
|
193 | return binaryExpressionToValue(valueExpression);
|
194 | }
|
195 | }
|
196 | exports.expressionToValue = expressionToValue;
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function getIdentifierName(node) {
|
204 | if (babel.isIdentifier(node)) {
|
205 | return node.name;
|
206 | }
|
207 | if (babel.isMemberExpression(node)) {
|
208 | const object = getIdentifierName(node.object);
|
209 | let property;
|
210 | if (node.computed) {
|
211 | property = expressionToValue(node.property);
|
212 | }
|
213 | else {
|
214 | property = getIdentifierName(node.property);
|
215 | }
|
216 | if (object != null && property != null) {
|
217 | return `${object}.${property}`;
|
218 | }
|
219 | }
|
220 | }
|
221 | exports.getIdentifierName = getIdentifierName;
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | function getNamespacedIdentifier(name, docs) {
|
227 | if (!docs) {
|
228 | return name;
|
229 | }
|
230 | const memberofTag = jsdoc.getTag(docs, 'memberof');
|
231 | const namespace = memberofTag && memberofTag.description;
|
232 | if (namespace) {
|
233 | const rightMostIdentifierName = name.substring(name.lastIndexOf('.') + 1);
|
234 | return namespace + '.' + rightMostIdentifierName;
|
235 | }
|
236 | else {
|
237 | return name;
|
238 | }
|
239 | }
|
240 | exports.getNamespacedIdentifier = getNamespacedIdentifier;
|
241 | exports.CANT_CONVERT = 'UNKNOWN';
|
242 |
|
\ | No newline at end of file |