UNPKG

2.57 kBJavaScriptView Raw
1/**
2 * keep run functions clean and simple
3 *
4 * Initialization logic should be moved into a factory or service. This improves testability.
5 *
6 * @styleguideReference {johnpapa} `y171` Run Blocks
7 * @version 0.15.0
8 * @category bestPractice
9 * @sinceAngularVersion 1.x
10 */
11'use strict';
12
13var angularRule = require('./utils/angular-rule');
14
15
16module.exports = {
17 meta: {
18 docs: {
19 url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-run-logic.md'
20 },
21 schema: [{
22 type: 'object',
23 properties: {
24 allowParams: {
25 type: 'boolean'
26 }
27 }
28 }]
29 },
30 create: angularRule(function(context) {
31 var options = context.options[0] || {};
32 var allowParams = options.allowParams !== false;
33
34 function report(node) {
35 context.report(node, 'The run function may only contain call expressions');
36 }
37
38 return {
39 'angular?run': function(callExpression, fn) {
40 if (!fn) {
41 return;
42 }
43 fn.body.body.forEach(function(statement) {
44 if (statement.type !== 'ExpressionStatement') {
45 return report(statement);
46 }
47 var expression = statement.expression;
48
49 /**
50 * issue #466
51 */
52 if (expression.type === 'Literal' && expression.value.indexOf('use strict') >= 0) {
53 return;
54 }
55
56 if (expression.type !== 'CallExpression') {
57 return report(statement);
58 }
59 if (expression.callee.type === 'MemberExpression' && expression.callee.object.type !== 'Identifier') {
60 return report(statement);
61 }
62 if (!allowParams && expression.arguments.length) {
63 return context.report(expression, 'Run function call expressions may not take any arguments');
64 }
65 expression.arguments.forEach(function(argument) {
66 if (argument && argument.type !== 'Literal' && argument.type !== 'Identifier') {
67 context.report(argument, 'Run function call expressions may only take simple arguments');
68 }
69 });
70 });
71 }
72 };
73 })
74};