forked from EmmanuelDemey/eslint-plugin-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
on-destroy.js
65 lines (57 loc) · 1.78 KB
/
on-destroy.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
/**
* Check for common misspelling $on('destroy', ...).
*
* It should be $on('$destroy', ...).
* @version 0.1.0
* @category misspelling
* @sinceAngularVersion 1.x
*/
'use strict';
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/on-destroy.md'
},
schema: []
},
create: function(context) {
function report(node) {
context.report(node, 'You probably misspelled $on("$destroy").');
}
/**
* Return true if the given node is a call expression calling a function
* named '$on'.
*/
function isOn(node) {
var calledFunction = node.callee;
if (calledFunction.type !== 'MemberExpression') {
return false;
}
// can only easily tell what name was used if a simple
// identifiers were used to access it.
var accessedFunction = calledFunction.property;
if (accessedFunction.type !== 'Identifier') {
return false;
}
var functionName = accessedFunction.name;
return functionName === '$on';
}
/**
* Return true if the given node is a call expression that has a first
* argument of the string '$destroy'.
*/
function isFirstArgDestroy(node) {
var args = node.arguments;
return (args.length >= 1 &&
args[0].type === 'Literal' &&
args[0].value === 'destroy');
}
return {
CallExpression: function(node) {
if (isOn(node) && isFirstArgDestroy(node)) {
report(node);
}
}
};
}
};