Skip to content

Commit 7fb8aa6

Browse files
schorfESBartheleway
andcommitted
Handle unset and invalid values from editor config #47
Co-authored-by: Barthélemy Laurans <bartheleway@gmail.com>
1 parent 429e716 commit 7fb8aa6

File tree

7 files changed

+135
-15
lines changed

7 files changed

+135
-15
lines changed

src/Validator.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const editorconfig = require('editorconfig');
66
const DEFAULTS = extend({}, require('./constants/defaults'));
77
const MESSAGES = require('./constants/messages');
88
const PATTERNS = require('./constants/ignorePatterns');
9-
const MAPPINGS = require('./constants/editorconfig-mappings');
9+
const MAPPINGS = require('./constants/editorconfigMappings');
1010

1111
const ValidationError = require('./ValidationError');
1212

@@ -255,15 +255,31 @@ class Validator {
255255
if (typeof config === 'object') {
256256
// Merge editorconfig values into the correct settings names:
257257
for (key in config) {
258-
if (typeof MAPPINGS[key] === 'string') {
258+
if (typeof MAPPINGS[key] === 'object') {
259+
// Handle "unset" special value given by editorconfig file
260+
// and consider not to parse invalid types and value.
261+
// See: Issue #47
262+
if (
263+
config[key] === 'unset' ||
264+
!MAPPINGS[key].types.includes(typeof config[key]) ||
265+
(
266+
typeof config[key] === 'string' &&
267+
MAPPINGS[key].regexp instanceof RegExp &&
268+
!MAPPINGS[key].regexp.test(config[key])
269+
)
270+
) {
271+
this._settings[MAPPINGS[key].name] = false;
272+
continue;
273+
}
274+
259275
switch (key) {
260276
case 'indent_style':
261277
// The 'indent_style' property value isn't
262278
// equal to the expected setting value:
263-
this._settings[MAPPINGS[key]] = config[key] + 's';
279+
this._settings[MAPPINGS[key].name] = config[key] + 's';
264280
break;
265281
default:
266-
this._settings[MAPPINGS[key]] = config[key];
282+
this._settings[MAPPINGS[key].name] = config[key];
267283
break;
268284
}
269285
}

src/Validator.test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('The validator', () => {
159159
expect(validator._settings.trailingspaces).toBeFalsy();
160160
expect(validator._settings.newline).toBeFalsy();
161161
expect(validator._settings.indentation).toBe('tabs');
162-
expect(validator._settings.spaces).toBe('tab');
162+
expect(validator._settings.spaces).toBe(false);
163163
expect(validator._settings.endOfLine).toBe('lf');
164164

165165
// Unchanged:
@@ -210,11 +210,61 @@ describe('The validator', () => {
210210

211211
// test for expected properties by editorconfig:
212212
expect(validator._settings.indentation).toBe('tabs');
213-
expect(validator._settings.spaces).toBe('tab');
213+
expect(validator._settings.spaces).toBe(false);
214214
expect(validator._settings.trailingspaces).toBeTruthy();
215215
expect(validator._settings.newline).toBeTruthy();
216216
});
217217

218+
it('should parse "unset" value as false', () => {
219+
// fake loading:
220+
const validator = new Validator({
221+
editorconfig: __fromFixtures('.editorconfig.unset'),
222+
223+
trailingspaces: true,
224+
newline: true,
225+
226+
indentation: 'spaces',
227+
spaces: 2,
228+
endOfLine: true,
229+
});
230+
231+
// Load editorconfig with extension where options are disabled:
232+
validator._path = __fromFixtures('core.fixture');
233+
validator._loadSettings();
234+
expect(validator._settings).toEqual(expect.objectContaining({
235+
trailingspaces: false,
236+
newline: false,
237+
indentation: false,
238+
spaces: false,
239+
endOfLine: false,
240+
}))
241+
});
242+
243+
it('should parse invalid value as false', () => {
244+
// fake loading:
245+
const validator = new Validator({
246+
editorconfig: __fromFixtures('.editorconfig.invalid'),
247+
248+
trailingspaces: true,
249+
newline: true,
250+
251+
indentation: 'spaces',
252+
spaces: 2,
253+
endOfLine: true,
254+
});
255+
256+
// Load editorconfig with extension where options are disabled:
257+
validator._path = __fromFixtures('core.fixture');
258+
validator._loadSettings();
259+
expect(validator._settings).toEqual(expect.objectContaining({
260+
trailingspaces: false,
261+
newline: false,
262+
indentation: false,
263+
spaces: false,
264+
endOfLine: false,
265+
}))
266+
});
267+
218268
it('should throw if is not a file', () => {
219269
const file = __fromFixtures('core.fixture');
220270
[
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = cf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
10+
[*.fixture]
11+
indent_style = foo
12+
indent_size = bar
13+
end_of_line = baz
14+
insert_final_newline = qux
15+
trim_trailing_whitespace = quux
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = cf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
10+
[*.fixture]
11+
indent_style = unset
12+
indent_size = unset
13+
end_of_line = unset
14+
insert_final_newline = unset
15+
trim_trailing_whitespace = unset

src/constants/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ module.exports = {
1212
editorconfig: false, // path to editor-config file
1313
rcconfig: false, // path to rc-config file
1414
allowsBOM: false,
15-
end_of_line: false, // 'LF' or 'CRLF' or 'CR' or false to disable checking
15+
endOfLine: false, // 'LF' or 'CRLF' or 'CR' or false to disable checking
1616
};

src/constants/editorconfig-mappings.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
charset: {
3+
name: 'encoding',
4+
types: ['string'],
5+
regexp: /^.*$/,
6+
},
7+
insert_final_newline: {
8+
name: 'newline',
9+
types: ['boolean'],
10+
regexp: false,
11+
},
12+
indent_style: {
13+
name: 'indentation',
14+
types: ['string'],
15+
regexp: /^tab|space$/i,
16+
},
17+
indent_size: {
18+
name: 'spaces',
19+
types: ['number'],
20+
regexp: false,
21+
},
22+
trim_trailing_whitespace: {
23+
name: 'trailingspaces',
24+
types: ['boolean'],
25+
regexp: false,
26+
},
27+
end_of_line: {
28+
name: 'endOfLine',
29+
types: ['string'],
30+
regexp: /^lf|crlf|cr$/i,
31+
},
32+
};

0 commit comments

Comments
 (0)