UNPKG

3.79 kBJavaScriptView Raw
1/*
2 * Copyright (C) 2016 salesforce.com, inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Returns the property name of a MemberExpression.
19 * @param {ASTNode} memberExpressionNode The MemberExpression node.
20 * @returns {string|null} Returns the property name if available, null else.
21 */
22function getPropertyName(memberExpressionNode) {
23 if (memberExpressionNode.computed) {
24 if (memberExpressionNode.property.type === "Literal") {
25 return memberExpressionNode.property.value;
26 }
27 } else {
28 return memberExpressionNode.property.name;
29 }
30 return null;
31}
32
33/**
34 * Finds the escope reference in the given scope.
35 * @param {Object} scope The scope to search.
36 * @param {ASTNode} node The identifier node.
37 * @returns {Reference|null} Returns the found reference or null if none were found.
38 */
39function findReference(scope, node) {
40 var references = scope.references.filter(function(reference) {
41 return reference.identifier.range[0] === node.range[0] &&
42 reference.identifier.range[1] === node.range[1];
43 });
44
45 if (references.length === 1) {
46 return references[0];
47 }
48 return null;
49}
50
51/**
52 * Checks if the given identifier node is shadowed in the given scope.
53 * @param {Object} scope The current scope.
54 * @param {Object} globalScope The global scope.
55 * @param {string} node The identifier node to check
56 * @returns {boolean} Whether or not the name is shadowed.
57 */
58function isShadowed(scope, globalScope, node) {
59 var reference = findReference(scope, node);
60 return reference && reference.resolved && reference.resolved.defs.length > 0;
61}
62
63/**
64 * Finds all the nodes used by a composed member expression.
65 * E.g.: Array.prototype.slice should produce
66 * [{type: "Identifier", name: "Array"}, {type: "Identifier", name: "prototype"}, {type: "Identifier", name: "slice"}]
67 * @param {ASTNode} node The MemberExpression node.
68 * @returns {Array} Returns a list of nodes that represent the namespace.
69 */
70function buildMemberExpressionNamespace(currentScope, globalScope, node) {
71 var ns = [];
72 do {
73 ns.unshift(node.property);
74 if (node.object.type === "MemberExpression") {
75 node = node.object;
76 } else if (!isGlobalThisReferenceOrGlobalWindow(currentScope, globalScope, node.object)) {
77 ns.unshift(node.object);
78 node = undefined;
79 } else {
80 node = undefined;
81 }
82 } while (node);
83 return ns;
84}
85
86/**
87 * Checks if the given identifier node is a ThisExpression in the global scope or the global window property.
88 * @param {Object} scope The current scope.
89 * @param {Object} globalScope The global scope.
90 * @param {string} node The identifier node to check
91 * @returns {boolean} Whether or not the node is a reference to the global object.
92 */
93function isGlobalThisReferenceOrGlobalWindow(scope, globalScope, node) {
94 if (scope.type === "global" && node.type === "ThisExpression") {
95 return true;
96 } else if (node.name === "window") {
97 return !isShadowed(scope, globalScope, node);
98 }
99
100 return false;
101}
102
103module.exports = {
104 getPropertyName,
105 findReference,
106 isShadowed,
107 isGlobalThisReferenceOrGlobalWindow,
108 buildMemberExpressionNamespace
109};