UNPKG

5.94 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 "use strict";
18
19var util = require('../lib/util.js');
20
21// This structure follows the same schema of the whitelisting mechanism from SES
22// for ecma intrinsics, more details on ../lib/3rdparty/ses/whiteslit.js
23var AuraAPI = {
24 $A: {
25 createComponent: true,
26 createComponents: true,
27 enqueueAction: true,
28 get: true,
29 getCallback: true,
30 getComponent: true,
31 getReference: true,
32 getRoot: true,
33 log: true,
34 reportError: true,
35 toString: true,
36 warning: true,
37 util: {
38 addClass: true,
39 getBooleanValue: true,
40 hasClass: true,
41 isArray: true,
42 isEmpty: true,
43 isObject: true,
44 isUndefined: true,
45 isUndefinedOrNull: true,
46 removeClass: true,
47 toggleClass: true
48 },
49 localizationService: {
50 displayDuration: true,
51 displayDurationInDays: true,
52 displayDurationInHours: true,
53 displayDurationInMilliseconds: true,
54 displayDurationInMinutes: true,
55 displayDurationInMonths: true,
56 displayDurationInSeconds: true,
57 duration: true,
58 endOf: true,
59 formatCurrency: true,
60 formatDate: true,
61 formatDateTime: true,
62 formatDateTimeUTC: true,
63 formatDateUTC: true,
64 formatNumber: true,
65 formatPercent: true,
66 formatTime: true,
67 formatTimeUTC: true,
68 getDateStringBasedOnTimezone: true,
69 getDaysInDuration: true,
70 getDefaultCurrencyFormat: true,
71 getDefaultNumberFormat: true,
72 getDefaultPercentFormat: true,
73 getHoursInDuration: true,
74 getLocalizedDateTimeLabels: true,
75 getMillisecondsInDuration: true,
76 getMinutesInDuration: true,
77 getMonthsInDuration: true,
78 getNumberFormat: true,
79 getSecondsInDuration: true,
80 getToday: true,
81 getYearsInDuration: true,
82 isAfter: true,
83 isBefore: true,
84 isPeriodTimeView: true,
85 isSame: true,
86 parseDateTime: true,
87 parseDateTimeISO8601: true,
88 parseDateTimeUTC: true,
89 startOf: true,
90 toISOString: true,
91 translateFromLocalizedDigits: true,
92 translateFromOtherCalendar: true,
93 translateToLocalizedDigits: true,
94 translateToOtherCalendar: true,
95 UTCToWallTime: true,
96 WallTimeToUTC: true
97 }
98 }
99};
100
101module.exports = function(context) {
102 var globalScope;
103
104 return {
105
106 "Program": function() {
107 globalScope = context.getScope();
108 },
109
110 MemberExpression: function(node) {
111 if (node.parent.type === "MemberExpression") {
112 // ignoring intermediate member expressions
113 return;
114 }
115 var currentScope = context.getScope();
116 var ns = util.buildMemberExpressionNamespace(currentScope, globalScope, node);
117 if (ns.length > 0) {
118 var rootIdentifier = ns[0];
119 if (rootIdentifier.type !== "Identifier" || rootIdentifier.name !== "$A" || util.isShadowed(currentScope, globalScope, rootIdentifier)) {
120 return;
121 }
122 var api = AuraAPI;
123 for (var i = 0; i < ns.length; i++) {
124 var identifier = ns[i];
125 if (identifier.type !== 'Identifier') {
126 context.report(node, "Invalid Aura API, use dot notation instead");
127 return;
128 }
129 var token = identifier.name;
130 var nextIdentifier = ns[i + 1];
131 if (typeof api !== "object") {
132 context.report(node, "Invalid Aura API");
133 return;
134 }
135 if (!api.hasOwnProperty(token)) {
136 context.report(node, "Invalid Aura API");
137 return;
138 }
139 if (api[token] === '*') {
140 // anything from this point on is good
141 return;
142 }
143 if (typeof (api[token]) === 'object' && Object.keys(api[token]).length === 0) {
144 // nothing else to inspect
145 return;
146 }
147 if (api[token] === true && !nextIdentifier) {
148 // function call
149 return;
150 }
151 if (api[token] === true && nextIdentifier && nextIdentifier.type === 'Identifier' && (nextIdentifier.name === 'apply' || nextIdentifier.name === 'call')) {
152 // function call with .apply() or .call() are still valid
153 return;
154 }
155 if (api[token] === false && nextIdentifier === undefined) {
156 return;
157 }
158 api = api[token];
159 }
160 }
161 }
162 };
163
164};
165
166module.exports.schema = [];