1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
16 | return new (P || (P = Promise))(function (resolve, reject) {
|
17 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
18 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
19 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
20 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
21 | });
|
22 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | const babel = require("@babel/types");
|
25 | const ast_value_1 = require("../javascript/ast-value");
|
26 | const esutil = require("../javascript/esutil");
|
27 | const jsdoc = require("../javascript/jsdoc");
|
28 | const model_1 = require("../model/model");
|
29 | const declaration_property_handlers_1 = require("./declaration-property-handlers");
|
30 | const polymer_element_1 = require("./polymer-element");
|
31 | class PolymerElementScanner {
|
32 | scan(document, visit) {
|
33 | return __awaiter(this, void 0, void 0, function* () {
|
34 | const visitor = new ElementVisitor(document);
|
35 | yield visit(visitor);
|
36 | return { features: visitor.features, warnings: visitor.warnings };
|
37 | });
|
38 | }
|
39 | }
|
40 | exports.PolymerElementScanner = PolymerElementScanner;
|
41 |
|
42 |
|
43 |
|
44 | class ElementVisitor {
|
45 | constructor(document) {
|
46 | this.features = [];
|
47 | this.warnings = [];
|
48 | this.document = document;
|
49 | }
|
50 | enterCallExpression(node, parent, path) {
|
51 | const callee = node.callee;
|
52 | if (!babel.isIdentifier(callee) || callee.name !== 'Polymer') {
|
53 | return;
|
54 | }
|
55 | const rawDescription = esutil.getAttachedComment(parent);
|
56 | let className = undefined;
|
57 | if (babel.isAssignmentExpression(parent)) {
|
58 | className = ast_value_1.getIdentifierName(parent.left);
|
59 | }
|
60 | else if (babel.isVariableDeclarator(parent)) {
|
61 | className = ast_value_1.getIdentifierName(parent.id);
|
62 | }
|
63 | const jsDoc = jsdoc.parseJsdoc(rawDescription || '');
|
64 | const element = new polymer_element_1.ScannedPolymerElement({
|
65 | className,
|
66 | astNode: { node, language: 'js', containingDocument: this.document },
|
67 | statementAst: esutil.getCanonicalStatement(path),
|
68 | description: jsDoc.description,
|
69 | events: esutil.getEventComments(parent),
|
70 | sourceRange: this.document.sourceRangeForNode(node.arguments[0]),
|
71 | privacy: esutil.getOrInferPrivacy('', jsDoc),
|
72 | abstract: jsdoc.hasTag(jsDoc, 'abstract'),
|
73 | attributes: new Map(),
|
74 | properties: [],
|
75 | behaviors: [],
|
76 | extends: undefined,
|
77 | jsdoc: jsDoc,
|
78 | listeners: [],
|
79 | methods: new Map(),
|
80 | staticMethods: new Map(),
|
81 | mixins: [],
|
82 | observers: [],
|
83 | superClass: undefined,
|
84 | tagName: undefined,
|
85 | isLegacyFactoryCall: true,
|
86 | });
|
87 | element.description = (element.description || '').trim();
|
88 | const propertyHandlers = declaration_property_handlers_1.declarationPropertyHandlers(element, this.document, path);
|
89 | const argument = node.arguments[0];
|
90 | if (babel.isObjectExpression(argument)) {
|
91 | this.handleObjectExpression(argument, propertyHandlers, element);
|
92 | }
|
93 | this.features.push(element);
|
94 | }
|
95 | handleObjectExpression(node, propertyHandlers, element) {
|
96 | for (const prop of esutil.getSimpleObjectProperties(node)) {
|
97 | const name = esutil.getPropertyName(prop);
|
98 | if (!name) {
|
99 | element.warnings.push(new model_1.Warning({
|
100 | message: `Can't determine name for property key from expression ` +
|
101 | `with type ${prop.key.type}.`,
|
102 | code: 'cant-determine-property-name',
|
103 | severity: model_1.Severity.WARNING,
|
104 | sourceRange: this.document.sourceRangeForNode(prop.key),
|
105 | parsedDocument: this.document
|
106 | }));
|
107 | continue;
|
108 | }
|
109 | if (name in propertyHandlers) {
|
110 | propertyHandlers[name](prop.value);
|
111 | }
|
112 | else if ((babel.isMethod(prop) && prop.kind === 'method') ||
|
113 | babel.isFunction(prop.value)) {
|
114 | const method = esutil.toScannedMethod(prop, this.document.sourceRangeForNode(prop), this.document);
|
115 | element.addMethod(method);
|
116 | }
|
117 | }
|
118 | for (const prop of esutil
|
119 | .extractPropertiesFromClassOrObjectBody(node, this.document)
|
120 | .values()) {
|
121 | if (prop.name in propertyHandlers) {
|
122 | continue;
|
123 | }
|
124 | element.addProperty(Object.assign({}, prop, { isConfiguration: esutil.configurationProperties.has(prop.name) }));
|
125 | }
|
126 | }
|
127 | }
|
128 |
|
\ | No newline at end of file |