UNPKG

5.67 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const babel = require("@babel/types");
17const astValue = require("../javascript/ast-value");
18const esutil_1 = require("../javascript/esutil");
19const model_1 = require("../model/model");
20const analyze_properties_1 = require("./analyze-properties");
21const expression_scanner_1 = require("./expression-scanner");
22function 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}
42exports.getBehaviorReference = getBehaviorReference;
43/**
44 * Returns an object containing functions that will annotate `declaration` with
45 * the polymer-specific meaning of the value nodes for the named properties.
46 */
47function 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 // TODO (maklesoft): Notifiy the user somehow that a listener entry
98 // was not extracted
99 // because the event or handler namecould not be statically analyzed.
100 // E.g. add a low-severity
101 // warning once opting out of rules is supported.
102 continue;
103 }
104 declaration.listeners.push({ event: evtName, handler: handler });
105 }
106 },
107 _template() {
108 // Not an interesting property.
109 }
110 };
111}
112exports.declarationPropertyHandlers = declarationPropertyHandlers;
113function 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}
134exports.extractObservers = extractObservers;
135//# sourceMappingURL=declaration-property-handlers.js.map
\No newline at end of file