Skip to content

Commit 50713d8

Browse files
committed
Use the editorconfig utils
1 parent 1496ad3 commit 50713d8

2 files changed

Lines changed: 186 additions & 97 deletions

File tree

lib/rules/template-linebreak-style.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
'use strict';
2+
3+
const os = require('node:os');
4+
const editorConfigUtil = require('../utils/editorconfig');
5+
16
function toDisplay(value) {
27
return value.replaceAll('\r', 'CR').replaceAll('\n', 'LF');
38
}
49

10+
const EOL_MAP = { lf: '\n', cr: '\r', crlf: '\r\n' };
11+
512
/** @type {import('eslint').Rule.RuleModule} */
613
module.exports = {
714
meta: {
@@ -30,10 +37,16 @@ module.exports = {
3037
},
3138

3239
create(context) {
33-
const os = require('node:os');
3440
const option = context.options[0] || 'unix';
41+
42+
// editorconfig end_of_line takes precedence over the rule option
43+
const editorConfig = editorConfigUtil.resolveEditorConfig(context.getFilename());
44+
const editorConfigEol = editorConfig['end_of_line'];
45+
3546
let expectedLinebreak;
36-
if (option === 'system') {
47+
if (editorConfigEol && EOL_MAP[editorConfigEol]) {
48+
expectedLinebreak = EOL_MAP[editorConfigEol];
49+
} else if (option === 'system') {
3750
expectedLinebreak = os.EOL;
3851
} else if (option === 'windows') {
3952
expectedLinebreak = '\r\n';

tests/lib/rules/template-linebreak-style.js

Lines changed: 171 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,186 @@
22
// Requirements
33
//------------------------------------------------------------------------------
44

5+
const editorConfigUtil = require('../../../lib/utils/editorconfig');
56
const rule = require('../../../lib/rules/template-linebreak-style');
67
const RuleTester = require('eslint').RuleTester;
78

89
//------------------------------------------------------------------------------
910
// Tests
11+
//
12+
// All tests are wrapped in a describe so beforeAll/afterAll can install a spy
13+
// on editorConfigUtil.resolveEditorConfig before any rule invocation. This
14+
// prevents the project's own .editorconfig from influencing test outcomes.
1015
//------------------------------------------------------------------------------
1116

12-
const ruleTester = new RuleTester({
13-
parser: require.resolve('ember-eslint-parser'),
14-
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15-
});
17+
describe('template-linebreak-style', () => {
18+
beforeAll(() => {
19+
vi.spyOn(editorConfigUtil, 'resolveEditorConfig').mockReturnValue({});
20+
});
1621

17-
ruleTester.run('template-linebreak-style', rule, {
18-
valid: [
19-
// default 'unix' — LF only
20-
'<template>\n<div>test</div>\n</template>',
21-
],
22-
23-
invalid: [
24-
{
25-
code: '<template>\r\n<div>test</div>\r\n</template>',
26-
output: '<template>\n<div>test</div>\n</template>',
27-
options: ['unix'],
28-
errors: [{ messageId: 'wrongLinebreak' }, { messageId: 'wrongLinebreak' }],
29-
},
30-
],
31-
});
22+
afterAll(() => {
23+
vi.restoreAllMocks();
24+
});
3225

33-
const hbsRuleTester = new RuleTester({
34-
parser: require.resolve('ember-eslint-parser/hbs'),
35-
parserOptions: {
36-
ecmaVersion: 2022,
37-
sourceType: 'module',
38-
},
39-
});
26+
const ruleTester = new RuleTester({
27+
parser: require.resolve('ember-eslint-parser'),
28+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
29+
});
4030

41-
hbsRuleTester.run('template-linebreak-style', rule, {
42-
valid: [
43-
// default 'unix' — all LF
44-
'testing this',
45-
'testing \n this',
46-
{ code: 'testing\nthis', options: ['unix'] },
47-
// windows — all CRLF
48-
{ code: 'testing\r\nthis', options: ['windows'] },
49-
// no linebreaks at all
50-
'single line no linebreaks',
51-
],
52-
53-
invalid: [
54-
// default 'unix' — mixed linebreaks, first is LF so CRLF is wrong
55-
{
56-
code: 'something\ngoes\r\n',
57-
output: 'something\ngoes\n',
58-
options: ['unix'],
59-
errors: [{ messageId: 'wrongLinebreak' }],
60-
},
61-
// unix — CRLF standalone
62-
{
63-
code: '\r\n',
64-
output: '\n',
65-
options: ['unix'],
66-
errors: [{ messageId: 'wrongLinebreak' }],
67-
},
68-
// unix — CRLF in block mustache
69-
{
70-
code: '{{#if test}}\r\n{{/if}}',
71-
output: '{{#if test}}\n{{/if}}',
72-
options: ['unix'],
73-
errors: [{ messageId: 'wrongLinebreak' }],
74-
},
75-
// unix — CRLF between mustaches
76-
{
77-
code: '{{blah}}\r\n{{blah}}',
78-
output: '{{blah}}\n{{blah}}',
79-
options: ['unix'],
80-
errors: [{ messageId: 'wrongLinebreak' }],
81-
},
82-
// unix — CRLF trailing
83-
{
84-
code: '{{blah}}\r\n',
85-
output: '{{blah}}\n',
86-
options: ['unix'],
87-
errors: [{ messageId: 'wrongLinebreak' }],
88-
},
89-
// unix — CRLF in attribute value
90-
{
91-
code: '{{blah arg="\r\n"}}',
92-
output: '{{blah arg="\n"}}',
93-
options: ['unix'],
94-
errors: [{ messageId: 'wrongLinebreak' }],
95-
},
96-
// unix — CRLF in element attribute
97-
{
98-
code: '<blah arg="\r\n" />',
99-
output: '<blah arg="\n" />',
100-
options: ['unix'],
101-
errors: [{ messageId: 'wrongLinebreak' }],
102-
},
103-
// windows — LF is wrong
104-
{
105-
code: '\n',
106-
output: '\r\n',
107-
options: ['windows'],
108-
errors: [{ messageId: 'wrongLinebreak' }],
31+
ruleTester.run('template-linebreak-style', rule, {
32+
valid: [
33+
// default 'unix' — LF only
34+
'<template>\n<div>test</div>\n</template>',
35+
],
36+
37+
invalid: [
38+
{
39+
code: '<template>\r\n<div>test</div>\r\n</template>',
40+
output: '<template>\n<div>test</div>\n</template>',
41+
options: ['unix'],
42+
errors: [{ messageId: 'wrongLinebreak' }, { messageId: 'wrongLinebreak' }],
43+
},
44+
],
45+
});
46+
47+
const hbsRuleTester = new RuleTester({
48+
parser: require.resolve('ember-eslint-parser/hbs'),
49+
parserOptions: {
50+
ecmaVersion: 2022,
51+
sourceType: 'module',
10952
},
110-
],
53+
});
54+
55+
hbsRuleTester.run('template-linebreak-style', rule, {
56+
valid: [
57+
// default 'unix' — all LF
58+
'testing this',
59+
'testing \n this',
60+
{ code: 'testing\nthis', options: ['unix'] },
61+
// windows — all CRLF
62+
{ code: 'testing\r\nthis', options: ['windows'] },
63+
// no linebreaks at all
64+
'single line no linebreaks',
65+
],
66+
67+
invalid: [
68+
// default 'unix' — mixed linebreaks, first is LF so CRLF is wrong
69+
{
70+
code: 'something\ngoes\r\n',
71+
output: 'something\ngoes\n',
72+
options: ['unix'],
73+
errors: [{ messageId: 'wrongLinebreak' }],
74+
},
75+
// unix — CRLF standalone
76+
{
77+
code: '\r\n',
78+
output: '\n',
79+
options: ['unix'],
80+
errors: [{ messageId: 'wrongLinebreak' }],
81+
},
82+
// unix — CRLF in block mustache
83+
{
84+
code: '{{#if test}}\r\n{{/if}}',
85+
output: '{{#if test}}\n{{/if}}',
86+
options: ['unix'],
87+
errors: [{ messageId: 'wrongLinebreak' }],
88+
},
89+
// unix — CRLF between mustaches
90+
{
91+
code: '{{blah}}\r\n{{blah}}',
92+
output: '{{blah}}\n{{blah}}',
93+
options: ['unix'],
94+
errors: [{ messageId: 'wrongLinebreak' }],
95+
},
96+
// unix — CRLF trailing
97+
{
98+
code: '{{blah}}\r\n',
99+
output: '{{blah}}\n',
100+
options: ['unix'],
101+
errors: [{ messageId: 'wrongLinebreak' }],
102+
},
103+
// unix — CRLF in attribute value
104+
{
105+
code: '{{blah arg="\r\n"}}',
106+
output: '{{blah arg="\n"}}',
107+
options: ['unix'],
108+
errors: [{ messageId: 'wrongLinebreak' }],
109+
},
110+
// unix — CRLF in element attribute
111+
{
112+
code: '<blah arg="\r\n" />',
113+
output: '<blah arg="\n" />',
114+
options: ['unix'],
115+
errors: [{ messageId: 'wrongLinebreak' }],
116+
},
117+
// windows — LF is wrong
118+
{
119+
code: '\n',
120+
output: '\r\n',
121+
options: ['windows'],
122+
errors: [{ messageId: 'wrongLinebreak' }],
123+
},
124+
],
125+
});
126+
127+
//------------------------------------------------------------------------------
128+
// EditorConfig integration tests
129+
//------------------------------------------------------------------------------
130+
131+
describe('editorconfig integration', () => {
132+
const hbsRuleTesterEditorConfig = new RuleTester({
133+
parser: require.resolve('ember-eslint-parser/hbs'),
134+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
135+
});
136+
137+
afterEach(() => {
138+
editorConfigUtil.resolveEditorConfig.mockReturnValue({});
139+
});
140+
141+
describe('end_of_line: crlf overrides rule option', () => {
142+
beforeEach(() => {
143+
editorConfigUtil.resolveEditorConfig.mockReturnValue({ end_of_line: 'crlf' });
144+
});
145+
146+
hbsRuleTesterEditorConfig.run('template-linebreak-style', rule, {
147+
valid: [
148+
// editorconfig crlf wins even when rule option says unix
149+
{ code: 'testing\r\nthis', options: ['unix'] },
150+
// editorconfig crlf matches windows option too
151+
{ code: 'testing\r\nthis', options: ['windows'] },
152+
],
153+
invalid: [
154+
// LF is wrong when editorconfig says crlf
155+
{
156+
code: 'testing\nthis',
157+
output: 'testing\r\nthis',
158+
options: ['unix'],
159+
errors: [{ messageId: 'wrongLinebreak' }],
160+
},
161+
],
162+
});
163+
});
164+
165+
describe('end_of_line: lf overrides rule option', () => {
166+
beforeEach(() => {
167+
editorConfigUtil.resolveEditorConfig.mockReturnValue({ end_of_line: 'lf' });
168+
});
169+
170+
hbsRuleTesterEditorConfig.run('template-linebreak-style', rule, {
171+
valid: [
172+
// editorconfig lf wins even when rule option says windows
173+
{ code: 'testing\nthis', options: ['windows'] },
174+
],
175+
invalid: [
176+
// CRLF is wrong when editorconfig says lf
177+
{
178+
code: 'testing\r\nthis',
179+
output: 'testing\nthis',
180+
options: ['windows'],
181+
errors: [{ messageId: 'wrongLinebreak' }],
182+
},
183+
],
184+
});
185+
});
186+
});
111187
});

0 commit comments

Comments
 (0)