1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var angularRule = require('./utils/angular-rule');
|
14 |
|
15 |
|
16 | module.exports = {
|
17 | meta: {
|
18 | schema: [{
|
19 | type: 'object',
|
20 | properties: {
|
21 | allowParams: {
|
22 | type: 'boolean'
|
23 | }
|
24 | }
|
25 | }]
|
26 | },
|
27 | create: angularRule(function(context) {
|
28 | var options = context.options[0] || {};
|
29 | var allowParams = options.allowParams !== false;
|
30 |
|
31 | function report(node) {
|
32 | context.report(node, 'The run function may only contain call expressions');
|
33 | }
|
34 |
|
35 | return {
|
36 | 'angular?run': function(callExpression, fn) {
|
37 | if (!fn) {
|
38 | return;
|
39 | }
|
40 | fn.body.body.forEach(function(statement) {
|
41 | if (statement.type !== 'ExpressionStatement') {
|
42 | return report(statement);
|
43 | }
|
44 | var expression = statement.expression;
|
45 |
|
46 | |
47 |
|
48 |
|
49 | if (expression.type === 'Literal' && expression.value.indexOf('use strict') >= 0) {
|
50 | return;
|
51 | }
|
52 |
|
53 | if (expression.type !== 'CallExpression') {
|
54 | return report(statement);
|
55 | }
|
56 | if (expression.callee.type === 'MemberExpression' && expression.callee.object.type !== 'Identifier') {
|
57 | return report(statement);
|
58 | }
|
59 | if (!allowParams && expression.arguments.length) {
|
60 | return context.report(expression, 'Run function call expressions may not take any arguments');
|
61 | }
|
62 | expression.arguments.forEach(function(argument) {
|
63 | if (argument && argument.type !== 'Literal' && argument.type !== 'Identifier') {
|
64 | context.report(argument, 'Run function call expressions may only take simple arguments');
|
65 | }
|
66 | });
|
67 | });
|
68 | }
|
69 | };
|
70 | })
|
71 | };
|