Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,11 @@ option and set them to `true`.

### .editorconfig option

It's possible to overwrite the default and given options by setting up a path
to an external editorconfig file by using the `editorconfig` option. For a basic
It's possible to overwrite the default and given options by setting up the
editorconfig filename with the `editorconfig` option. For a basic
configuration of a _.editorconfig_ file check out the
[EditorConfig Documentation](http://editorconfig.org/).
Pass the filename of the configuration file; EditorConfig will look for it in the current directory and all of its parent directories.

```javascript
editorconfig: '.editorconfig';
Expand Down
615 changes: 333 additions & 282 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"globals": "^17.4.0",
"husky": "^9.0.7",
"jest": "^30.1.2",
"lint-staged": "^17.0.7",
"lint-staged": "^17.3.0",
"prettier": "^3.0.3",
"release-it": "^21.0.1"
}
Expand Down
83 changes: 33 additions & 50 deletions src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,61 +222,44 @@ class Validator {
*/
_loadSettingsEditorconfig() {
if (typeof this._settings.editorconfig === 'string') {
let stat;
try {
stat = fs.statSync(this._settings.editorconfig);
} catch (_ /* Error */) {
this._fail(MESSAGES.EDITORCONFIG_NOTFOUND.message
.replace('{a}', this._settings.editorconfig));
}

if (stat.isFile()) {
// Load config for current path
//
// To work on windows, the config path should be relative to the
// current cwd. See: Issue #40
const relative = this._settings.editorconfig.replace(process.cwd(), '');
const config = editorconfig.parseSync(this._path, {
config: relative,
});
// Load config for current path
const config = editorconfig.parseSync(this._path, {
config: this._settings.editorconfig,
});

if (typeof config === 'object') {
// Merge editorconfig values into the correct settings names:
let key;
for (key in config) {
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;
}
if (typeof config === 'object') {
// Merge editorconfig values into the correct settings names:
let key;
for (key in config) {
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].name] = config[key] + 's';
break;
default:
this._settings[MAPPINGS[key].name] = config[key];
break;
}
switch (key) {
case 'indent_style':
// The 'indent_style' property value isn't
// equal to the expected setting value:
this._settings[MAPPINGS[key].name] = config[key] + 's';
break;
default:
this._settings[MAPPINGS[key].name] = config[key];
break;
}
}
}
} else {
this._fail(MESSAGES.PATH_ISNT_FILE.message
.replace('{a}', this._settings.editorconfig));
}
}
}
Expand Down
103 changes: 81 additions & 22 deletions src/Validator.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const extend = require('deep-extend');
const fs = require('fs');
const path = require('path');
const {
chdir,
cwd,
} = require('process');

const Defaults = require('./constants/defaults');
const Messages = require('./constants/messages');
Expand Down Expand Up @@ -144,7 +148,7 @@ describe('the validator', () => {
it('should override settings', () => {
// Fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig'),
editorconfig: '.editorconfig',

trailingspaces: true,
newline: true,
Expand Down Expand Up @@ -173,7 +177,7 @@ describe('the validator', () => {
it('should load specific settings by extension', () => {
// Fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig'),
editorconfig: '.editorconfig',

trailingspaces: true,
newline: true,
Expand Down Expand Up @@ -207,7 +211,7 @@ describe('the validator', () => {
// Fake loading:
const validator = new Validator({
rcconfig: __fromFixtures('.lintspacesrc'),
editorconfig: __fromFixtures('.editorconfig'),
editorconfig: '.editorconfig',
newline: 'foo',
});
validator._path = __fromFixtures('corer.other-fixture');
Expand All @@ -223,7 +227,7 @@ describe('the validator', () => {
it('should parse "unset" value as false', () => {
// Fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig.unset'),
editorconfig: '.editorconfig.unset',

trailingspaces: true,
newline: true,
Expand All @@ -249,7 +253,7 @@ describe('the validator', () => {
it('should parse invalid value as false', () => {
// Fake loading:
const validator = new Validator({
editorconfig: __fromFixtures('.editorconfig.invalid'),
editorconfig: '.editorconfig.invalid',

trailingspaces: true,
newline: true,
Expand All @@ -272,29 +276,84 @@ describe('the validator', () => {
}));
});

it('should throw if is not a file', () => {
const file = __fromFixtures('core.fixture');
[
'.',
__dirname,
].forEach(editorconfig => {
const message = Messages.PATH_ISNT_FILE.message.replace('{a}', editorconfig);
const error = new Error(message);
it('should not throw if file does not exist', () => {
// Fake loading:
const validator = new Validator({
editorconfig: '.editorconfig.notexists',

trailingspaces: true,
newline: true,

expect(() => new Validator({editorconfig}).validate(file)).toThrow(error);
indentation: 'spaces',
spaces: 2,
endOfLine: false,

newlineMaximum: false,
ignores: ['js-comments'],
});
validator._path = __fromFixtures('core.fixture');
validator._loadSettings();

expect(validator._settings.trailingspaces).toBeTruthy();
expect(validator._settings.newline).toBeTruthy();
expect(validator._settings.indentation).toBe('spaces');
expect(validator._settings.spaces).toBe(2);
expect(validator._settings.endOfLine).toBeFalsy();

// Unchanged:
expect(validator._settings.newlineMaximum).toBe(false);
expect(validator._settings.ignores).toEqual(['js-comments']);
});

it('should throw if file does not exist', () => {
const file = __fromFixtures('core.fixture');
[
path.join(__dirname, 'path', 'that', 'doesnt', 'existis', '.editorconfig'),
].forEach(editorconfig => {
const message = Messages.EDITORCONFIG_NOTFOUND.message.replace('{a}', editorconfig);
const error = new Error(message);
it('should allow working from another directory', () => {
const originalCwd = cwd();

chdir(path.resolve(__dirname, '__fixtures__/deep'));

Comment on lines +308 to 312

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I fix? I feel that it will not cause damage even if test fails.

// Fake loading:
const validator = new Validator({
editorconfig: '.editorconfig',

expect(() => new Validator({editorconfig}).validate(file)).toThrow(error);
trailingspaces: true,
newline: true,

indentation: 'spaces',
spaces: 2,
endOfLine: false,

newlineMaximum: false,
ignores: ['js-comments'],
});
validator._path = __fromFixtures('core.fixture');
validator._loadSettings();

expect(validator._settings.trailingspaces).toBeFalsy();
expect(validator._settings.newline).toBeFalsy();
expect(validator._settings.indentation).toBe('tabs');
expect(validator._settings.spaces).toBe(false);
expect(validator._settings.endOfLine).toBe('lf');

// Unchanged:
expect(validator._settings.newlineMaximum).toBe(false);
expect(validator._settings.ignores).toEqual(['js-comments']);

chdir(originalCwd);
});

it('will correctly read editorconfig hierarchically', () => {
// Fake loading:
const validator = new Validator({
editorconfig: '.editorconfig',
});
validator._path = __fromFixtures('deep/core.fixture');
validator._loadSettings();

expect(validator._settings.newline).toBeTruthy();

validator._path = __fromFixtures('core.fixture');
validator._loadSettings();

expect(validator._settings.newline).toBeFalsy();
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions src/__fixtures__/.editorconfig
Comment thread
Bartheleway marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; http://editorconfig.org

root = false
root = true

[*]
charset = utf-8
Expand All @@ -15,6 +15,5 @@ insert_final_newline = false
end_of_line = lf

[*.other-fixture]
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = crlf
20 changes: 20 additions & 0 deletions src/__fixtures__/deep/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; http://editorconfig.org

root = true

[*]
charset = utf-8
indent_style = tab
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.fixture]
trim_trailing_whitespace = false
insert_final_newline = true
end_of_line = lf

[*.other-fixture]
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = crlf
Empty file.