-
Notifications
You must be signed in to change notification settings - Fork 1
/
valid-requires.js
40 lines (34 loc) · 1.41 KB
/
valid-requires.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const util = require('./util');
exports.rule = {
meta: {
docs: {
description: 'require that all goog.require() have a valid arg and appear at the top level'
}
},
create: function(context) {
return {
CallExpression: function(expression) {
if (util.isRequireExpression(expression)) {
const parent = expression.parent;
const parentIsExpression = parent.type === 'ExpressionStatement';
const parentIsVariableDeclarator = parent.type === 'VariableDeclarator';
if (!parentIsExpression && !parentIsVariableDeclarator) {
return context.report(expression, 'Expected goog.require() to be in an expression or variable declarator statement');
}
const expectedProgram = parentIsExpression ? parent.parent : parent.parent.parent;
if (expectedProgram.type !== 'Program') {
return context.report(expression, 'Expected goog.require() to be at the top level');
}
if (expression.arguments.length !== 1) {
return context.report(expression, 'Expected one argument for goog.require()');
}
const arg = expression.arguments[0];
if (arg.type !== 'Literal' || !arg.value || typeof arg.value !== 'string') {
return context.report(expression, 'Expected goog.require() to be called with a string');
}
}
}
};
}
};