Skip to content

Commit 7318f93

Browse files
committed
Merge pull request #195 from tilmanpotthof/issue-179/ng_file_name-add-ignorePrefix-option
Issue #179 - add options ignorePrefix with tests and documentation
2 parents fe1f0d2 + 4e9d0ce commit 7318f93

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ We provide also three samples :
8585
| 'directive-name': 0 | All your directives should have a name starting with the parameter you can define in your config object. The second parameter can be a Regexp wrapped in quotes. You can not prefix your directives by "ng" (reserved keyword for AngularJS directives) ("directive-name": [2, "ng"]) [Y073](https://github.com/johnpapa/angular-styleguide#style-y073), [Y126](https://github.com/johnpapa/angular-styleguide#style-y126) |
8686
| 'document-service': 2 | Instead of the default document object, you should prefer the AngularJS wrapper service $document. [Y180](https://github.com/johnpapa/angular-styleguide#style-y180) |
8787
| 'empty-controller': 0 | If you have one empty controller, maybe you have linked it in your Router configuration or in one of your views. You can remove this declaration because this controller is useless |
88-
| 'file-name': 0 | 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}] 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`. [Y120](https://github.com/johnpapa/angular-styleguide#style-y120) [Y121](https://github.com/johnpapa/angular-styleguide#style-y121) |
88+
| 'file-name': 0 | 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'. [Y120](https://github.com/johnpapa/angular-styleguide#style-y120) [Y121](https://github.com/johnpapa/angular-styleguide#style-y121) |
8989
| 'filter-name': 0 | All your filters should have a name starting with the parameter you can define in your config object. The second parameter can be a Regexp wrapped in quotes. ("filter-name": [2, "ng"]) |
9090
| 'foreach': 0 | You should use the angular.forEach method instead of the default JavaScript implementation [].forEach. |
9191
| 'function-type': 0 | Anonymous or named functions inside AngularJS components. The first parameter sets which type of function is required and can be 'named' or 'anonymous'. The second parameter is an optional list of angular object names. [Y024](https://github.com/johnpapa/angular-styleguide/blob/master/README.md#style-y024) |

rules/file-name.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ module.exports = (function () {
2727
firstToUpper: function (value) {
2828
return value[0].toUpperCase() + value.slice(1);
2929
},
30+
firstToLower: function (value) {
31+
return value[0].toLowerCase() + value.slice(1);
32+
},
3033
removeTypeSuffix: function (name, type) {
3134
var nameTypeLengthDiff = name.length - type.length;
3235
if (nameTypeLengthDiff <= 0) {
@@ -39,6 +42,13 @@ module.exports = (function () {
3942
return name;
4043
}
4144
},
45+
removePrefix: function (name, options) {
46+
if (new RegExp('^' + options.ignorePrefix + '[A-Z]').test(name)) {
47+
return this.firstToLower(name.slice(options.ignorePrefix.length));
48+
} else {
49+
return name;
50+
}
51+
},
4252
transformComponentName: function (name, options) {
4353
var nameStyle = options.nameStyle;
4454
var nameSeparator = separators[nameStyle];
@@ -54,6 +64,9 @@ module.exports = (function () {
5464
if (options.ignoreTypeSuffix) {
5565
name = filenameUtil.removeTypeSuffix(name, type);
5666
}
67+
if (options.ignorePrefix && options.ignorePrefix.length > 0) {
68+
name = filenameUtil.removePrefix(name, options);
69+
}
5770
if (options.nameStyle) {
5871
name = filenameUtil.transformComponentName(name, options);
5972
}

test/file-name.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ eslintTester.run('file-name', rule, {
124124
typeSeparator: 'dot',
125125
nameStyle: 'underscore'
126126
}]
127+
}, {
128+
// ignorePrefix xp with typeSeparator dot and ignoreTypeSuffix
129+
filename: 'src/app/asset.service.js',
130+
code: 'angular.factory("xpAssetService", xpAssetService)',
131+
options: [{
132+
typeSeparator: 'dot',
133+
ignoreTypeSuffix: true,
134+
ignorePrefix: 'xp'
135+
}]
136+
}, {
137+
// ignorePrefix st with typeSeparator dash
138+
filename: 'src/app/appUtils-service.js',
139+
code: 'angular.factory("stAppUtils", stAppUtils)',
140+
options: [{
141+
typeSeparator: 'dash',
142+
ignorePrefix: 'st'
143+
}]
144+
}, {
145+
// test to detect false positives for ignorePrefix
146+
filename: 'staging_service.js',
147+
code: 'angular.factory("staging", staging)',
148+
options: [{
149+
typeSeparator: 'underscore',
150+
ignorePrefix: 'st'
151+
}]
127152
}],
128153
invalid: [{
129154
filename: 'src/app/filters.js',
@@ -170,5 +195,15 @@ eslintTester.run('file-name', rule, {
170195
nameStyle: 'dash'
171196
}],
172197
errors: [{ message: 'Filename must be "avanger-profile.directive.js"'}]
198+
}, {
199+
// ignorePrefix xp
200+
filename: 'src/app/xpAsset.service.js',
201+
code: 'angular.factory("xpAssetService", xpAssetService)',
202+
options: [{
203+
typeSeparator: 'dot',
204+
ignoreTypeSuffix: true,
205+
ignorePrefix: 'xp'
206+
}],
207+
errors: [{ message: 'Filename must be "asset.service.js"'}]
173208
}]
174209
});

0 commit comments

Comments
 (0)