-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathfile-name.js
190 lines (171 loc) · 7.83 KB
/
file-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* require and specify a consistent component name pattern
*
* All your file names should match the angular component name.
* The second parameter can be a config object [2, {nameStyle: 'dash', typeSeparator: 'dot', ignoreTypeSuffix: true, ignorePrefix: 'ui'}] to match 'avenger-profile.directive.js' or 'avanger-api.service.js'.
* Possible values for 'typeSeparator' and 'nameStyle' are 'dot', 'dash' and 'underscore'.
* The options 'ignoreTypeSuffix' ignores camel cased suffixes like 'someController' or 'myService' and 'ignorePrefix' ignores namespace prefixes like 'ui'.
* It's possible to specify a regexp for ignorePrefix. Example RegExp: "/^ui./".
*
* The naming scheme is <componentName><typeSeparator><componentType>.js
*
* The *componentType* for all service types (service, factory, provider, value) is 'service'.
* Since 1.5.0 it is possible to configure custom mappings for the *componentType*: {typeSeparator: 'dot', componentTypeMappings: {factory: 'factory', provider: 'provider'}.
*
* @styleguideReference {johnpapa} `y120` Naming - Naming Guidelines
* @styleguideReference {johnpapa} `y121` Naming - Feature File Names
* @version 0.7.0
* @category naming
* @sinceAngularVersion 1.x
*/
'use strict';
var path = require('path');
var utils = require('./utils/utils');
/**
* The filename is the one for the import statement if we are in a module
*
* @param {any} node The current node being linted
* @param {any} context The context
* @param {any} defaultFilename The previous filename
* @returns
*/
function handleModuleCase(node, context, defaultFilename) {
if (context.parserOptions.sourceType !== 'module') {
return defaultFilename;
}
// Handle the module case.
var name = node.arguments[1].name;
var globalScope = context.getScope();
// Retrieve the import instruction.
var variable = globalScope.variables.find(function(v) {
return v.name === name;
});
// Check that the definition is an import declaration.
if (!(variable && variable.defs && variable.defs[0] && variable.defs[0].parent && variable.defs[0].parent.type === 'ImportDeclaration')) {
return defaultFilename;
}
// Thanks to the chrome devtools to find the path of the filename. :-)
var filename = path.basename(variable.defs[0].parent.source.value);
return filename;
}
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/file-name.md'
},
schema: [{
type: ['object']
}]
},
create: (function() {
var fileEnding = '.js';
var separators = {
dot: '.',
dash: '-',
underscore: '_'
};
function createComponentTypeMappings(options) {
var componentTypeMappingOptions = options.componentTypeMappings || {};
return {
module: componentTypeMappingOptions.module || 'module',
controller: componentTypeMappingOptions.controller || 'controller',
directive: componentTypeMappingOptions.directive || 'directive',
filter: componentTypeMappingOptions.filter || 'filter',
service: componentTypeMappingOptions.service || 'service',
factory: componentTypeMappingOptions.factory || 'service',
provider: componentTypeMappingOptions.provider || 'service',
value: componentTypeMappingOptions.value || 'service',
constant: componentTypeMappingOptions.constant || 'constant',
component: componentTypeMappingOptions.component || 'component'
};
}
var filenameUtil = {
firstToUpper: function(value) {
return value[0].toUpperCase() + value.slice(1);
},
firstToLower: function(value) {
return value[0].toLowerCase() + value.slice(1);
},
removeTypeSuffix: function(name, type) {
var nameTypeLengthDiff = name.length - type.length;
if (nameTypeLengthDiff <= 0) {
return name;
}
var typeCamelCase = this.firstToUpper(type);
if (name.indexOf(typeCamelCase) === nameTypeLengthDiff) {
return name.slice(0, nameTypeLengthDiff);
}
return name;
},
removePrefix: function(name, options) {
if (utils.isStringRegexp(options.ignorePrefix)) {
return this.firstToLower(name.replace(utils.convertStringToRegex(options.ignorePrefix), ''));
}
var regName = '^' + options.ignorePrefix.replace(/[\.]/g, '\\$&');
regName += options.ignorePrefix.indexOf('\.') === -1 ? '[A-Z]' : '[a-zA-z]';
if (new RegExp(regName).test(name)) {
return this.firstToLower(name.slice(options.ignorePrefix.length));
}
return name;
},
transformComponentName: function(name, options) {
var nameStyle = options.nameStyle;
var nameSeparator = separators[nameStyle];
if (nameSeparator) {
var replacement = '$1' + nameSeparator + '$2';
name = name.replace(/([a-z0-9])([A-Z])/g, replacement).toLowerCase();
}
return name;
},
createExpectedName: function(name, type, options) {
var typeSeparator = separators[options.typeSeparator];
if (options.ignoreTypeSuffix) {
name = filenameUtil.removeTypeSuffix(name, type);
}
if (options.ignorePrefix && options.ignorePrefix.length > 0) {
name = filenameUtil.removePrefix(name, options);
}
if (options.nameStyle) {
name = filenameUtil.transformComponentName(name, options);
}
if (typeSeparator !== undefined) {
name = name + typeSeparator + type;
}
if (options.casing === 'camel') {
name = filenameUtil.firstToLower(name);
}
if (options.casing === 'pascal') {
name = filenameUtil.firstToUpper(name);
}
return name + fileEnding;
}
};
return function(context) {
var options = context.options[0] || {};
var componentTypeMappings = createComponentTypeMappings(options);
return {
CallExpression: function(node) {
if ((utils.isAngularComponent(node) || utils.isAngularComponentDeclaration(node)) && utils.isMemberExpression(node.callee)) {
var name = node.arguments[0].value;
var type = componentTypeMappings[node.callee.property.name];
var expectedName;
if (type === undefined || (type === 'service' && node.callee.object.name === '$provide')) {
return;
}
if (!name) {
return;
}
expectedName = filenameUtil.createExpectedName(name, type, options);
var filename = path.basename(context.getFilename());
filename = handleModuleCase(node, context, filename);
if (expectedName !== filename) {
context.report(node, 'Filename must be "{{expectedName}}"', {
expectedName: expectedName
});
}
}
}
};
};
}())
};