UNPKG

2.62 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 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 ast_value_1 = require("../javascript/ast-value");
19const analyze_properties_1 = require("./analyze-properties");
20function getStaticGetterValue(node, name) {
21 const getter = node.body.body.find((n) => babel.isClassMethod(n) && n.static === true &&
22 n.kind === 'get' && ast_value_1.getIdentifierName(n.key) === name);
23 if (!getter) {
24 return undefined;
25 }
26 // TODO(justinfagnani): consider generating warnings for these checks
27 // TODO(usergenic): I'm not sure this conversion below here makes sense... Do
28 // we semantically want this `getter.body` to replace `getter.value.body`?
29 const getterBody = getter.body;
30 if (getterBody.body.length !== 1) {
31 // not a single statement function
32 return undefined;
33 }
34 const statement = getterBody.body[0];
35 if (!babel.isReturnStatement(statement)) {
36 // we only support a return statement
37 return undefined;
38 }
39 return statement.argument;
40}
41exports.getStaticGetterValue = getStaticGetterValue;
42function getIsValue(node) {
43 const getterValue = getStaticGetterValue(node, 'is');
44 if (!getterValue || !babel.isLiteral(getterValue)) {
45 // we only support literals
46 return undefined;
47 }
48 const value = astValue.expressionToValue(getterValue);
49 if (typeof value !== 'string') {
50 return undefined;
51 }
52 return value;
53}
54exports.getIsValue = getIsValue;
55/**
56 * Returns the properties defined in a Polymer config object literal.
57 */
58function getPolymerProperties(node, document) {
59 if (!babel.isClassDeclaration(node) && !babel.isClassExpression(node)) {
60 return [];
61 }
62 const propertiesNode = getStaticGetterValue(node, 'properties');
63 return propertiesNode ? analyze_properties_1.analyzeProperties(propertiesNode, document) : [];
64}
65exports.getPolymerProperties = getPolymerProperties;
66//# sourceMappingURL=polymer2-config.js.map
\No newline at end of file