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 astValue = require("../javascript/ast-value");
|
18 | const esutil_1 = require("../javascript/esutil");
|
19 | const model_1 = require("../model/model");
|
20 | const analyze_properties_1 = require("./analyze-properties");
|
21 | const expression_scanner_1 = require("./expression-scanner");
|
22 | function getBehaviorReference(argNode, document, path) {
|
23 | const behaviorName = astValue.getIdentifierName(argNode);
|
24 | if (!behaviorName) {
|
25 | return {
|
26 | successful: false,
|
27 | error: new model_1.Warning({
|
28 | code: 'could-not-determine-behavior-name',
|
29 | message: `Could not determine behavior name from expression of type ` +
|
30 | `${argNode.type}`,
|
31 | severity: model_1.Severity.WARNING,
|
32 | sourceRange: document.sourceRangeForNode(argNode),
|
33 | parsedDocument: document
|
34 | })
|
35 | };
|
36 | }
|
37 | return {
|
38 | successful: true,
|
39 | value: new model_1.ScannedReference('behavior', behaviorName, document.sourceRangeForNode(argNode), { language: 'js', node: argNode, containingDocument: document }, path)
|
40 | };
|
41 | }
|
42 | exports.getBehaviorReference = getBehaviorReference;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function declarationPropertyHandlers(declaration, document, path) {
|
48 | return {
|
49 | is(node) {
|
50 | if (babel.isLiteral(node)) {
|
51 | declaration.tagName = '' + astValue.expressionToValue(node);
|
52 | }
|
53 | },
|
54 | properties(node) {
|
55 | for (const prop of analyze_properties_1.analyzeProperties(node, document)) {
|
56 | declaration.addProperty(prop);
|
57 | }
|
58 | },
|
59 | behaviors(node) {
|
60 | if (!babel.isArrayExpression(node)) {
|
61 | return;
|
62 | }
|
63 | for (const element of node.elements) {
|
64 | const result = getBehaviorReference(element, document, path);
|
65 | if (result.successful === false) {
|
66 | declaration.warnings.push(result.error);
|
67 | }
|
68 | else {
|
69 | declaration.behaviorAssignments.push(result.value);
|
70 | }
|
71 | }
|
72 | },
|
73 | observers(node) {
|
74 | const observers = extractObservers(node, document);
|
75 | if (!observers) {
|
76 | return;
|
77 | }
|
78 | declaration.warnings = declaration.warnings.concat(observers.warnings);
|
79 | declaration.observers = declaration.observers.concat(observers.observers);
|
80 | },
|
81 | listeners(node) {
|
82 | if (!babel.isObjectExpression(node)) {
|
83 | declaration.warnings.push(new model_1.Warning({
|
84 | code: 'invalid-listeners-declaration',
|
85 | message: '`listeners` property should be an object expression',
|
86 | severity: model_1.Severity.WARNING,
|
87 | sourceRange: document.sourceRangeForNode(node),
|
88 | parsedDocument: document
|
89 | }));
|
90 | return;
|
91 | }
|
92 | for (const p of esutil_1.getSimpleObjectProperties(node)) {
|
93 | const evtName = babel.isLiteral(p.key) && astValue.expressionToValue(p.key) ||
|
94 | babel.isIdentifier(p.key) && p.key.name;
|
95 | const handler = !babel.isLiteral(p.value) || astValue.expressionToValue(p.value);
|
96 | if (typeof evtName !== 'string' || typeof handler !== 'string') {
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | continue;
|
103 | }
|
104 | declaration.listeners.push({ event: evtName, handler: handler });
|
105 | }
|
106 | },
|
107 | _template() {
|
108 |
|
109 | }
|
110 | };
|
111 | }
|
112 | exports.declarationPropertyHandlers = declarationPropertyHandlers;
|
113 | function extractObservers(observersArray, document) {
|
114 | if (!babel.isArrayExpression(observersArray)) {
|
115 | return;
|
116 | }
|
117 | let warnings = [];
|
118 | const observers = [];
|
119 | for (const element of observersArray.elements) {
|
120 | let v = astValue.expressionToValue(element);
|
121 | if (v === undefined) {
|
122 | v = astValue.CANT_CONVERT;
|
123 | }
|
124 | const parseResult = expression_scanner_1.parseExpressionInJsStringLiteral(document, element, 'callExpression');
|
125 | warnings = warnings.concat(parseResult.warnings);
|
126 | observers.push({
|
127 | javascriptNode: element,
|
128 | expression: v,
|
129 | parsedExpression: parseResult.databinding
|
130 | });
|
131 | }
|
132 | return { observers, warnings };
|
133 | }
|
134 | exports.extractObservers = extractObservers;
|
135 |
|
\ | No newline at end of file |