UNPKG

2.26 kBJavaScriptView Raw
1/**
2 * require and specify a prefix for all controller names
3 *
4 * All your controllers should have a name starting with the parameter you can define in your config object.
5 * The second parameter can be a Regexp wrapped in quotes.
6 * ("controller-name": [2, "ng"])
7 *
8 * @styleguideReference {johnpapa} `y123` Controller Names
9 * @styleguideReference {johnpapa} `y124` Controller Name Suffix
10 * @version 0.1.0
11 * @category naming
12 * @sinceAngularVersion 1.x
13 */
14'use strict';
15
16var utils = require('./utils/utils');
17
18module.exports = {
19 meta: {
20 schema: [
21 {
22 type: 'string'
23 }
24 ]
25 },
26 create: function(context) {
27 return {
28
29 CallExpression: function(node) {
30 var prefix = context.options[0] || '/^[A-Z][a-zA-Z0-9]*Controller$/';
31 var convertedPrefix; // convert string from JSON .eslintrc to regex
32
33 convertedPrefix = utils.convertPrefixToRegex(prefix);
34
35 var callee = node.callee;
36 if (callee.type === 'MemberExpression' && callee.property.name === 'controller') {
37 /**
38 * Allow the usage of element.controller() and element.controller('directiveName') in unittests
39 */
40 if (node.arguments.length < 2) {
41 return;
42 }
43
44 var name = node.arguments[0].value;
45
46 if (name !== undefined && !convertedPrefix.test(name)) {
47 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
48 context.report(node, 'The {{controller}} controller should be prefixed by {{prefix}}', {
49 controller: name,
50 prefix: prefix
51 });
52 } else {
53 context.report(node, 'The {{controller}} controller should follow this pattern: {{prefix}}', {
54 controller: name,
55 prefix: prefix.toString()
56 });
57 }
58 }
59 }
60 }
61 };
62 }
63};