|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const editorConfigUtil = require('../../../lib/utils/editorconfig'); |
| 6 | +const rule = require('../../../lib/rules/template-linebreak-style'); |
| 7 | +const RuleTester = require('eslint').RuleTester; |
| 8 | + |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | +// 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. |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +describe('template-linebreak-style', () => { |
| 18 | + beforeAll(() => { |
| 19 | + vi.spyOn(editorConfigUtil, 'resolveEditorConfig').mockReturnValue({}); |
| 20 | + }); |
| 21 | + |
| 22 | + afterAll(() => { |
| 23 | + vi.restoreAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + const ruleTester = new RuleTester({ |
| 27 | + parser: require.resolve('ember-eslint-parser'), |
| 28 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 29 | + }); |
| 30 | + |
| 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', |
| 52 | + }, |
| 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 | + }); |
| 187 | +}); |
0 commit comments