Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 60f8c35

Browse files
joelrbrandtmikesherov
authored andcommitted
implement fileExtensions option
1 parent 76a0616 commit 60f8c35

File tree

7 files changed

+161
-2
lines changed

7 files changed

+161
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ Values: Array of file matching patterns
129129
"excludeFiles": ["node_modules/**"]
130130
```
131131

132+
### fileExtensions
133+
134+
Changes the set of file extensions that will be processed.
135+
136+
Type: `Array` or `String` or `"*"`
137+
138+
Values: A single file extension or an Array of file extensions, beginning with a `.`. The matching is case _insensitive_. If `"*"` is provided, all files regardless of extension will match.
139+
140+
#### Example
141+
142+
```js
143+
"fileExtensions": [".js", ".jsx"]
144+
```
145+
146+
#### Default
147+
148+
```js
149+
"fileExtensions": [".js"]
150+
```
151+
132152
## Rules
133153

134154
### requireCurlyBraces

lib/checker.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var path = require('path');
66

77
var additionalRules = require('./options/additional-rules');
88
var excludeFiles = require('./options/exclude-files');
9+
var fileExtensions = require('./options/file-extensions');
910

1011
/**
1112
* Starts Code Style checking process.
@@ -26,6 +27,7 @@ utils.inherits(Checker, StringChecker);
2627
Checker.prototype.configure = function(config) {
2728
var cwd = config.configPath ? path.dirname(config.configPath) : process.cwd();
2829

30+
fileExtensions(config, this);
2931
excludeFiles(config, this, cwd);
3032
additionalRules(config, this, cwd);
3133

@@ -40,7 +42,7 @@ Checker.prototype.configure = function(config) {
4042
*/
4143
Checker.prototype.checkFile = function(path) {
4244
var _this = this;
43-
if (this._shouldProcess(path) && path.match(/\.js$/)) {
45+
if (this._shouldProcess(path)) {
4446
return vowFs.read(path, 'utf8').then(function(data) {
4547
return _this.checkString(data, path);
4648
});
@@ -117,13 +119,20 @@ Checker.prototype.checkPath = function(path) {
117119
};
118120

119121
/**
120-
* Returns true if specified path is not in exluded list.
122+
* Returns true if specified path is not in exluded list and if
123+
* the file extension matches a file extension to process.
121124
*
122125
* @returns {Boolean}
123126
*/
124127
Checker.prototype._shouldProcess = function(testPath) {
125128
testPath = path.resolve(testPath);
126129

130+
var extension = path.extname(testPath).toLowerCase();
131+
if (this._fileExtensions.indexOf(extension) < 0 &&
132+
this._fileExtensions.indexOf('*') < 0) {
133+
return false;
134+
}
135+
127136
return this._excludes.every(function(exclude) {
128137
return !exclude.match(testPath);
129138
});

lib/options/file-extensions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var DEFAULT_FILE_EXTENSIONS = ['.js'];
2+
3+
module.exports = function(config, instance) {
4+
if (typeof config.fileExtensions === 'string') {
5+
instance._fileExtensions = [config.fileExtensions.toLowerCase()];
6+
} else if (Array.isArray(config.fileExtensions)) {
7+
instance._fileExtensions = config.fileExtensions.map(
8+
function(s) {
9+
return s.toLowerCase();
10+
}
11+
);
12+
} else {
13+
instance._fileExtensions = DEFAULT_FILE_EXTENSIONS;
14+
}
15+
delete config.fileExtensions;
16+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
with (x) { y++; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
with (x) { y++; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
with (x) { y++; }

test/options/file-extensions.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
var Checker = require('../../lib/checker');
2+
var assert = require('assert');
3+
4+
describe('options/file-extensions', function() {
5+
var checker;
6+
7+
beforeEach(function() {
8+
checker = new Checker();
9+
checker.registerDefaultRules();
10+
});
11+
12+
describe('default config', function() {
13+
14+
beforeEach(function() {
15+
checker.configure({
16+
disallowKeywords: ['with']
17+
});
18+
});
19+
20+
it('should not report any errors for non-matching extensions with default config', function() {
21+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.jsx') === null);
22+
});
23+
24+
it('should report errors for matching extensions with default config', function() {
25+
// errors
26+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.js') !== null);
27+
});
28+
29+
it('should report errors for matching extensions (case insensitive) with default config', function() {
30+
// errors
31+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions-2.jS') !== null);
32+
});
33+
34+
});
35+
36+
describe('custom config', function() {
37+
38+
it('should not report any errors for non-matching extensions with custom config', function() {
39+
checker.configure({
40+
fileExtensions: ['.jsx'],
41+
disallowKeywords: ['with']
42+
});
43+
44+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.js') === null);
45+
});
46+
47+
it('should report errors for matching extensions with custom config', function() {
48+
checker.configure({
49+
fileExtensions: ['.jsx'],
50+
disallowKeywords: ['with']
51+
});
52+
53+
// errors
54+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.jsx') !== null);
55+
});
56+
57+
it('should report errors for matching extensions (case insensitive) with custom config', function() {
58+
checker.configure({
59+
fileExtensions: ['.JS'],
60+
disallowKeywords: ['with']
61+
});
62+
63+
// errors
64+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions-2.jS') !== null);
65+
});
66+
67+
it('should report errors for matching extensions (case insensitive) with string value', function() {
68+
checker.configure({
69+
fileExtensions: '.JS',
70+
disallowKeywords: ['with']
71+
});
72+
73+
// errors
74+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions-2.jS') !== null);
75+
});
76+
77+
it('should report errors for matching extensions with custom config with multiple extensions', function() {
78+
checker.configure({
79+
fileExtensions: ['.js', '.jsx'],
80+
disallowKeywords: ['with']
81+
});
82+
83+
// errors
84+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.js') !== null);
85+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.jsx') !== null);
86+
});
87+
88+
it('should report errors for matching extensions with Array *', function() {
89+
checker.configure({
90+
fileExtensions: ['*'],
91+
disallowKeywords: ['with']
92+
});
93+
94+
// errors
95+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.js') !== null);
96+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.jsx') !== null);
97+
});
98+
99+
it('should report errors for matching extensions with string *', function() {
100+
checker.configure({
101+
fileExtensions: '*',
102+
disallowKeywords: ['with']
103+
});
104+
105+
// errors
106+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.js') !== null);
107+
assert(checker.checkFile('./test/data/configs/fileExtensions/file-extensions.jsx') !== null);
108+
});
109+
});
110+
111+
});

0 commit comments

Comments
 (0)