|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const editorConfigUtil = require('../../../lib/utils/editorconfig'); |
| 6 | +const rule = require('../../../lib/rules/template-eol-last'); |
| 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 when |
| 15 | +// the 'editorconfig' option is used. |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +describe('template-eol-last', () => { |
| 19 | + beforeAll(() => { |
| 20 | + vi.spyOn(editorConfigUtil, 'resolveEditorConfig').mockReturnValue({}); |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(() => { |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + const ruleTester = new RuleTester({ |
| 28 | + parser: require.resolve('ember-eslint-parser'), |
| 29 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 30 | + }); |
| 31 | + |
| 32 | + ruleTester.run('template-eol-last', rule, { |
| 33 | + valid: [ |
| 34 | + // In gjs/gts mode, eol-last is a no-op (file-level eol-last is handled by eslint core) |
| 35 | + `<template> |
| 36 | +<div>test</div> |
| 37 | +</template>`, |
| 38 | + '<template><div>test</div></template>', |
| 39 | + ], |
| 40 | + |
| 41 | + invalid: [], |
| 42 | + }); |
| 43 | + |
| 44 | + const hbsRuleTester = new RuleTester({ |
| 45 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 46 | + parserOptions: { |
| 47 | + ecmaVersion: 2022, |
| 48 | + sourceType: 'module', |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + hbsRuleTester.run('template-eol-last', rule, { |
| 53 | + valid: [ |
| 54 | + // default 'always' — ends with newline |
| 55 | + 'test\n', |
| 56 | + '<img>\n', |
| 57 | + '<div>test</div>\n', |
| 58 | + '{{#my-component}}\n test\n{{/my-component}}\n', |
| 59 | + // config 'never' — does not end with newline |
| 60 | + { code: 'test', options: ['never'] }, |
| 61 | + { code: '<img>', options: ['never'] }, |
| 62 | + { code: '<div>test</div>', options: ['never'] }, |
| 63 | + { code: '{{#my-component}}\n test\n{{/my-component}}', options: ['never'] }, |
| 64 | + ], |
| 65 | + |
| 66 | + invalid: [ |
| 67 | + // default 'always' — missing newline |
| 68 | + { |
| 69 | + code: 'test', |
| 70 | + output: 'test\n', |
| 71 | + options: ['always'], |
| 72 | + errors: [{ messageId: 'mustEnd' }], |
| 73 | + }, |
| 74 | + { |
| 75 | + code: '<img>', |
| 76 | + output: '<img>\n', |
| 77 | + options: ['always'], |
| 78 | + errors: [{ messageId: 'mustEnd' }], |
| 79 | + }, |
| 80 | + { |
| 81 | + code: '<div>test</div>', |
| 82 | + output: '<div>test</div>\n', |
| 83 | + options: ['always'], |
| 84 | + errors: [{ messageId: 'mustEnd' }], |
| 85 | + }, |
| 86 | + // config 'never' — has trailing newline |
| 87 | + { |
| 88 | + code: 'test\n', |
| 89 | + output: 'test', |
| 90 | + options: ['never'], |
| 91 | + errors: [{ messageId: 'mustNotEnd' }], |
| 92 | + }, |
| 93 | + { |
| 94 | + code: '<img>\n', |
| 95 | + output: '<img>', |
| 96 | + options: ['never'], |
| 97 | + errors: [{ messageId: 'mustNotEnd' }], |
| 98 | + }, |
| 99 | + { |
| 100 | + code: '{{#my-component}}\n test\n{{/my-component}}\n', |
| 101 | + output: '{{#my-component}}\n test\n{{/my-component}}', |
| 102 | + options: ['never'], |
| 103 | + errors: [{ messageId: 'mustNotEnd' }], |
| 104 | + }, |
| 105 | + ], |
| 106 | + }); |
| 107 | + |
| 108 | + //------------------------------------------------------------------------------ |
| 109 | + // EditorConfig integration tests |
| 110 | + //------------------------------------------------------------------------------ |
| 111 | + |
| 112 | + describe('editorconfig option', () => { |
| 113 | + const hbsRuleTesterEditorConfig = new RuleTester({ |
| 114 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 115 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 116 | + }); |
| 117 | + |
| 118 | + afterEach(() => { |
| 119 | + editorConfigUtil.resolveEditorConfig.mockReturnValue({}); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('insert_final_newline: true behaves like always', () => { |
| 123 | + beforeEach(() => { |
| 124 | + editorConfigUtil.resolveEditorConfig.mockReturnValue({ insert_final_newline: true }); |
| 125 | + }); |
| 126 | + |
| 127 | + hbsRuleTesterEditorConfig.run('template-eol-last', rule, { |
| 128 | + valid: [{ code: 'test\n', options: ['editorconfig'] }], |
| 129 | + invalid: [ |
| 130 | + { |
| 131 | + code: 'test', |
| 132 | + output: 'test\n', |
| 133 | + options: ['editorconfig'], |
| 134 | + errors: [{ messageId: 'mustEnd' }], |
| 135 | + }, |
| 136 | + ], |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('insert_final_newline: false behaves like never', () => { |
| 141 | + beforeEach(() => { |
| 142 | + editorConfigUtil.resolveEditorConfig.mockReturnValue({ insert_final_newline: false }); |
| 143 | + }); |
| 144 | + |
| 145 | + hbsRuleTesterEditorConfig.run('template-eol-last', rule, { |
| 146 | + valid: [{ code: 'test', options: ['editorconfig'] }], |
| 147 | + invalid: [ |
| 148 | + { |
| 149 | + code: 'test\n', |
| 150 | + output: 'test', |
| 151 | + options: ['editorconfig'], |
| 152 | + errors: [{ messageId: 'mustNotEnd' }], |
| 153 | + }, |
| 154 | + ], |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments