Skip to content

Commit

Permalink
Handle unset and invalid values from editor config #47
Browse files Browse the repository at this point in the history
Co-authored-by: Barthélemy Laurans <barthelemy.laurans.external@salomon.com>
  • Loading branch information
schorfES and Barthélemy Laurans committed Oct 15, 2024
1 parent 429e716 commit 17d3411
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 15 deletions.
24 changes: 20 additions & 4 deletions src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const editorconfig = require('editorconfig');
const DEFAULTS = extend({}, require('./constants/defaults'));
const MESSAGES = require('./constants/messages');
const PATTERNS = require('./constants/ignorePatterns');
const MAPPINGS = require('./constants/editorconfig-mappings');
const MAPPINGS = require('./constants/editorconfigMappings');

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

Expand Down Expand Up @@ -255,15 +255,31 @@ class Validator {
if (typeof config === 'object') {
// Merge editorconfig values into the correct settings names:
for (key in config) {
if (typeof MAPPINGS[key] === 'string') {
if (typeof MAPPINGS[key] === 'object') {
// Handle "unset" special value given by editorconfig file
// and consider not to parse invalid types and value.
// See: Issue #47
if (
config[key] === 'unset' ||
!MAPPINGS[key].types.includes(typeof config[key]) ||
(
typeof config[key] === 'string' &&
MAPPINGS[key].regexp instanceof RegExp &&
!MAPPINGS[key].regexp.test(config[key])
)
) {
this._settings[MAPPINGS[key].name] = false;
continue;
}

switch (key) {
case 'indent_style':
// The 'indent_style' property value isn't
// equal to the expected setting value:
this._settings[MAPPINGS[key]] = config[key] + 's';
this._settings[MAPPINGS[key].name] = config[key] + 's';
break;
default:
this._settings[MAPPINGS[key]] = config[key];
this._settings[MAPPINGS[key].name] = config[key];
break;
}
}
Expand Down
54 changes: 52 additions & 2 deletions src/Validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('The validator', () => {
expect(validator._settings.trailingspaces).toBeFalsy();
expect(validator._settings.newline).toBeFalsy();
expect(validator._settings.indentation).toBe('tabs');
expect(validator._settings.spaces).toBe('tab');
expect(validator._settings.spaces).toBe(false);
expect(validator._settings.endOfLine).toBe('lf');

// Unchanged:
Expand Down Expand Up @@ -210,11 +210,61 @@ describe('The validator', () => {

// test for expected properties by editorconfig:
expect(validator._settings.indentation).toBe('tabs');
expect(validator._settings.spaces).toBe('tab');
expect(validator._settings.spaces).toBe(false);
expect(validator._settings.trailingspaces).toBeTruthy();
expect(validator._settings.newline).toBeTruthy();
});

it('should parse "unset" value as false', () => {
// fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig.unset'),

trailingspaces: true,
newline: true,

indentation: 'spaces',
spaces: 2,
endOfLine: true,
});

// Load editorconfig with extension where options are disabled:
validator._path = __fromFixtures('core.fixture');
validator._loadSettings();
expect(validator._settings).toEqual(expect.objectContaining({
trailingspaces: false,
newline: false,
indentation: false,
spaces: false,
endOfLine: false,
}))
});

it('should parse invalid value as false', () => {
// fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig.invalid'),

trailingspaces: true,
newline: true,

indentation: 'spaces',
spaces: 2,
endOfLine: true,
});

// Load editorconfig with extension where options are disabled:
validator._path = __fromFixtures('core.fixture');
validator._loadSettings();
expect(validator._settings).toEqual(expect.objectContaining({
trailingspaces: false,
newline: false,
indentation: false,
spaces: false,
endOfLine: false,
}))
});

it('should throw if is not a file', () => {
const file = __fromFixtures('core.fixture');
[
Expand Down
15 changes: 15 additions & 0 deletions src/__fixtures__/.editorconfig.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = cf
insert_final_newline = true
trim_trailing_whitespace = true

[*.fixture]
indent_style = foo
indent_size = bar
end_of_line = baz
insert_final_newline = qux
trim_trailing_whitespace = quux
15 changes: 15 additions & 0 deletions src/__fixtures__/.editorconfig.unset
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = cf
insert_final_newline = true
trim_trailing_whitespace = true

[*.fixture]
indent_style = unset
indent_size = unset
end_of_line = unset
insert_final_newline = unset
trim_trailing_whitespace = unset
2 changes: 1 addition & 1 deletion src/constants/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ module.exports = {
editorconfig: false, // path to editor-config file
rcconfig: false, // path to rc-config file
allowsBOM: false,
end_of_line: false, // 'LF' or 'CRLF' or 'CR' or false to disable checking
endOfLine: false, // 'LF' or 'CRLF' or 'CR' or false to disable checking
};
8 changes: 0 additions & 8 deletions src/constants/editorconfig-mappings.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/constants/editorconfigMappings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
charset: {
name: 'encoding',
types: ['string'],
regexp: /^.*$/,
},
insert_final_newline: {
name: 'newline',
types: ['boolean'],
regexp: false,
},
indent_style: {
name: 'indentation',
types: ['string'],
regexp: /^tab|space$/i,
},
indent_size: {
name: 'spaces',
types: ['number'],
regexp: false,
},
trim_trailing_whitespace: {
name: 'trailingspaces',
types: ['boolean'],
regexp: false,
},
end_of_line: {
name: 'endOfLine',
types: ['string'],
regexp: /^lf|crlf|cr$/i,
},
};

0 comments on commit 17d3411

Please sign in to comment.