1 | var path = require('path');
|
2 | var Vow = require('vow');
|
3 | var reservedWords = require('reserved-words');
|
4 |
|
5 | var IDENTIFIER_NAME_ES5_RE = require('../patterns/identifiers-ES5');
|
6 | var IDENTIFIER_NAME_ES6_RE = require('../patterns/identifiers-ES6');
|
7 |
|
8 | var TRAILING_UNDERSCORES_RE = /(^_+|_+$)/g;
|
9 |
|
10 | var SNAKE_CASE_RE = /^([a-z$][a-z0-9$]+)(_[a-z0-9$]+)+$/i;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | exports.spacedKeywords = [
|
17 | 'do',
|
18 | 'for',
|
19 | 'if',
|
20 | 'else',
|
21 | 'switch',
|
22 | 'case',
|
23 | 'try',
|
24 | 'catch',
|
25 | 'finally',
|
26 | 'void',
|
27 | 'while',
|
28 | 'with',
|
29 | 'return',
|
30 | 'typeof',
|
31 | 'function'
|
32 | ];
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.curlyBracedKeywords = [
|
39 | 'if',
|
40 | 'else',
|
41 | 'for',
|
42 | 'while',
|
43 | 'do',
|
44 | 'case',
|
45 | 'default',
|
46 | 'with'
|
47 | ];
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | exports.isValidIdentifierName = function(name, dialect) {
|
57 | dialect = dialect || 'es5';
|
58 | var identifierRegex = dialect === 'es5' ? IDENTIFIER_NAME_ES5_RE : IDENTIFIER_NAME_ES6_RE;
|
59 | return !reservedWords.check(name, dialect, true) && identifierRegex.test(name);
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | exports.isSnakeCased = function(name) {
|
69 | return SNAKE_CASE_RE.test(name);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | exports.getFunctionNodeFromIIFE = function(node) {
|
80 | if (node.type !== 'CallExpression') {
|
81 | return null;
|
82 | }
|
83 |
|
84 | var callee = node.callee;
|
85 |
|
86 | if (callee.type === 'FunctionExpression') {
|
87 | return callee;
|
88 | }
|
89 |
|
90 | if (callee.type === 'MemberExpression' &&
|
91 | callee.object.type === 'FunctionExpression' &&
|
92 | callee.property.type === 'Identifier' &&
|
93 | (callee.property.name === 'call' || callee.property.name === 'apply')
|
94 | ) {
|
95 | return callee.object;
|
96 | }
|
97 |
|
98 | return null;
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | exports.trimUnderscores = function(name) {
|
108 | var res = name.replace(TRAILING_UNDERSCORES_RE, '');
|
109 | return res ? res : name;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | exports.isRelativePath = function(path) {
|
119 |
|
120 | var start = path.substring(0, 2);
|
121 | return start === './' || start === '..';
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | exports.normalizePath = function(filepath, basePath) {
|
133 | if (this.isRelativePath(filepath)) {
|
134 | return path.resolve(basePath, filepath);
|
135 | }
|
136 |
|
137 | return filepath;
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | exports.promisify = function(fn) {
|
149 | return function() {
|
150 | var deferred = Vow.defer();
|
151 | var args = [].slice.call(arguments);
|
152 |
|
153 | args.push(function(err, result) {
|
154 | if (err) {
|
155 | deferred.reject(err);
|
156 | } else {
|
157 | deferred.resolve(result);
|
158 | }
|
159 | });
|
160 |
|
161 | fn.apply(null, args);
|
162 |
|
163 | return deferred.promise();
|
164 | };
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | exports.binaryOperators = [
|
172 |
|
173 |
|
174 | '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=',
|
175 | '&=', '|=', '^=',
|
176 |
|
177 | '+', '-', '*', '/', '%', '<<', '>>', '>>>', '&',
|
178 | '|', '^', '&&', '||', '===', '==', '>=',
|
179 | '<=', '<', '>', '!=', '!=='
|
180 | ];
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | exports.incrementAndDecrementOperators = ['++', '--'];
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | exports.unaryOperators = ['-', '+', '!', '~'].concat(exports.incrementAndDecrementOperators);
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | exports.operators = exports.binaryOperators.concat(exports.unaryOperators);
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | exports.isPragma = function(additionalExceptions) {
|
208 | var pragmaKeywords = [
|
209 | 'eslint',
|
210 | 'eslint-env',
|
211 | 'eslint-enable',
|
212 | 'eslint-disable',
|
213 | 'eslint-disable-line',
|
214 | 'global',
|
215 | 'jshint',
|
216 | 'jslint',
|
217 | 'globals',
|
218 | 'falls through',
|
219 | 'exported',
|
220 | 'jscs:',
|
221 | 'jscs:enable',
|
222 | 'jscs:disable',
|
223 | 'jscs:ignore',
|
224 | 'istanbul'
|
225 | ];
|
226 | if (additionalExceptions && Array.isArray(additionalExceptions)) {
|
227 | pragmaKeywords = pragmaKeywords.concat(additionalExceptions);
|
228 | }
|
229 |
|
230 | return function(comment) {
|
231 |
|
232 | var trimmedComment = comment.trim() + ' ';
|
233 | for (var i = 0; i < pragmaKeywords.length; i++) {
|
234 | if (trimmedComment.indexOf(pragmaKeywords[i] + ' ') === 0) {
|
235 | return true;
|
236 | }
|
237 | }
|
238 | return false;
|
239 | };
|
240 | };
|