-
Notifications
You must be signed in to change notification settings - Fork 1
/
no-duplicate-requires.js
48 lines (40 loc) · 1.18 KB
/
no-duplicate-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
41
42
43
44
45
46
47
48
'use strict';
const util = require('./util');
exports.rule = {
meta: {
docs: {
description: 'disallow duplicate goog.require() calls'
},
fixable: 'code'
},
create: function(context) {
const alreadyRequired = {};
return {
ExpressionStatement: function(statement) {
if (util.isRequireStatement(statement)) {
const expression = statement.expression;
if (!expression.arguments[0]) {
return;
}
const name = expression.arguments[0].value;
if (alreadyRequired[name]) {
const source = context.getSourceCode();
return context.report({
node: statement,
message: `Duplicate goog.require('${name}')`,
fix: function(fixer) {
const afterToken = source.getTokenAfter(statement, {includeComments: true});
const range = [
statement.range[0],
afterToken ? afterToken.range[0] : statement.range[1]
];
return fixer.removeRange(range);
}
});
}
alreadyRequired[name] = true;
}
}
};
}
};