1 | /**
|
2 | * Check for common misspelling $on('destroy', ...).
|
3 | *
|
4 | * It should be $on('$destroy', ...).
|
5 | * @version 0.1.0
|
6 | * @category misspelling
|
7 | * @sinceAngularVersion 1.x
|
8 | */
|
9 | ;
|
10 |
|
11 | module.exports = {
|
12 | meta: {
|
13 | schema: []
|
14 | },
|
15 | create: function(context) {
|
16 | function report(node) {
|
17 | context.report(node, 'You probably misspelled $on("$destroy").');
|
18 | }
|
19 |
|
20 | /**
|
21 | * Return true if the given node is a call expression calling a function
|
22 | * named '$on'.
|
23 | */
|
24 | function isOn(node) {
|
25 | var calledFunction = node.callee;
|
26 | if (calledFunction.type !== 'MemberExpression') {
|
27 | return false;
|
28 | }
|
29 |
|
30 | // can only easily tell what name was used if a simple
|
31 | // identifiers were used to access it.
|
32 | var accessedFunction = calledFunction.property;
|
33 | if (accessedFunction.type !== 'Identifier') {
|
34 | return false;
|
35 | }
|
36 |
|
37 | var functionName = accessedFunction.name;
|
38 |
|
39 | return functionName === '$on';
|
40 | }
|
41 |
|
42 | /**
|
43 | * Return true if the given node is a call expression that has a first
|
44 | * argument of the string '$destroy'.
|
45 | */
|
46 | function isFirstArgDestroy(node) {
|
47 | var args = node.arguments;
|
48 |
|
49 | return (args.length >= 1 &&
|
50 | args[0].type === 'Literal' &&
|
51 | args[0].value === 'destroy');
|
52 | }
|
53 |
|
54 | return {
|
55 | CallExpression: function(node) {
|
56 | if (isOn(node) && isFirstArgDestroy(node)) {
|
57 | report(node);
|
58 | }
|
59 | }
|
60 | };
|
61 | }
|
62 | };
|