forked from gberaudo/eslint-plugin-googshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
81 lines (69 loc) · 2.43 KB
/
util.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
function isGoogCallExpression(node, name) {
if (!node) {
return false;
}
const callee = node.callee;
return callee && callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' && callee.object.name === 'goog' &&
callee.property.type === 'Identifier' && !callee.property.computed &&
callee.property.name === name;
}
function isGoogStatement(node, name) {
return node.expression && node.expression.type === 'CallExpression' &&
isGoogCallExpression(node.expression, name);
}
function isGoogVariableDeclaration(node, name) {
return node.type === 'VariableDeclaration' && node.declarations &&
node.declarations.length > 0 && isGoogCallExpression(node.declarations[0].init, name);
}
exports.isModuleExpression = function(node) {
return isGoogCallExpression(node, 'module');
};
exports.isModuleStatement = function(node) {
return isGoogStatement(node, 'module');
};
exports.isModuleLegacyStatement = function(node) {
if (!node.expression || !node.expression.callee || !node.expression.callee.object) {
return false;
}
const callee = node.expression.callee;
return node.expression.type === 'CallExpression' &&
callee.object && callee.object.object &&
callee.object.object && callee.object.object.name === 'goog' &&
callee.object.property && callee.object.property.name === 'module' &&
callee.property && callee.property.name === 'declareLegacyNamespace';
}
exports.isProvideExpression = function(node) {
return isGoogCallExpression(node, 'provide');
};
exports.isProvideStatement = function(node) {
return isGoogStatement(node, 'provide');
};
exports.isRequireExpression = function(node) {
return isGoogCallExpression(node, 'require');
};
exports.isRequireStatement = function(node) {
return isGoogStatement(node, 'require');
};
exports.isRequireVariableDeclaration = function(node) {
return isGoogVariableDeclaration(node, 'require');
};
var getName = exports.getName = function(node) {
if (node.type !== 'MemberExpression') {
return;
}
if (node.property.type !== 'Identifier' || node.property.computed) {
return;
}
let objectName;
if (node.object.type === 'Identifier' && !node.object.computed) {
objectName = node.object.name;
} else if (node.object.type === 'MemberExpression' && !node.object.computed) {
objectName = getName(node.object);
}
if (!objectName) {
return;
}
return `${objectName}.${node.property.name}`;
};