UNPKG

2.43 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 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 * issue #466
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};