Skip to content
Merged
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
17 changes: 17 additions & 0 deletions lib/utils/editorconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const editorconfig = require('editorconfig');

/**
* Resolve editorconfig properties for a given file path using the official
* editorconfig library.
*
* Returns an object like `{ indent_size: 4, indent_style: 'space', ... }`
* with only the properties that matched. Returns an empty object if no
* .editorconfig is found or no sections match.
*/
function resolveEditorConfig(filePath) {
return editorconfig.parseSync(filePath);
}

module.exports = { resolveEditorConfig };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"dependencies": {
"@ember-data/rfc395-data": "^0.0.4",
"css-tree": "^3.0.1",
"editorconfig": "^3.0.2",
"ember-eslint-parser": "^0.6.0",
"ember-rfc176-data": "^0.3.18",
"eslint-utils": "^3.0.0",
Expand Down
55 changes: 55 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions tests/lib/utils/editorconfig-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

const fs = require('fs');
const path = require('path');
const os = require('os');
const { resolveEditorConfig } = require('../../../lib/utils/editorconfig');

describe('resolveEditorConfig', () => {
let tmpDir;

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'editorconfig-test-'));
});

afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});

it('returns empty object when no .editorconfig exists', () => {
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result).toEqual({});
});

it('reads indent_size from .editorconfig', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});

it('matches *.hbs sections', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.hbs]', 'indent_size = 3'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(3);
});

it('does not match non-matching glob', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.js]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBeUndefined();
});

it('handles brace expansion *.{hbs,gjs}', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.{hbs,gjs}]', 'indent_size = 6'].join('\n')
);
expect(resolveEditorConfig(path.join(tmpDir, 'test.hbs')).indent_size).toBe(6);
expect(resolveEditorConfig(path.join(tmpDir, 'test.gjs')).indent_size).toBe(6);
expect(resolveEditorConfig(path.join(tmpDir, 'test.js')).indent_size).toBeUndefined();
});

it('later sections override earlier ones', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 2', '', '[*.hbs]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});

it('inner .editorconfig overrides outer', () => {
// outer
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 2'].join('\n')
);
// inner dir
const innerDir = path.join(tmpDir, 'app');
fs.mkdirSync(innerDir);
fs.writeFileSync(path.join(innerDir, '.editorconfig'), ['[*]', 'indent_size = 4'].join('\n'));
const filePath = path.join(innerDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});

it('sets indent_size to tab when indent_style is tab and indent_size unset', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_style = tab'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_style).toBe('tab');
expect(result.indent_size).toBe('tab');
});

it('ignores comments', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '# a comment', '; another comment', '[*]', 'indent_size = 5'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(5);
});
});
Loading