|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-no-trailing-spaces'); |
| 6 | +const RuleTester = require('eslint').RuleTester; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Tests |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const ruleTester = new RuleTester({ |
| 13 | + parser: require.resolve('ember-eslint-parser'), |
| 14 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 15 | +}); |
| 16 | + |
| 17 | +ruleTester.run('template-no-trailing-spaces', rule, { |
| 18 | + valid: [ |
| 19 | + `<template> |
| 20 | + <div>Hello World</div> |
| 21 | + </template>`, |
| 22 | + `<template> |
| 23 | + <div> |
| 24 | + Content |
| 25 | + </div> |
| 26 | + </template>`, |
| 27 | + |
| 28 | + '<template>test</template>', |
| 29 | + '<template> test</template>', |
| 30 | + `<template>test |
| 31 | +</template>`, |
| 32 | + `<template>{{#my-component}} |
| 33 | +{{/my-component}}</template>`, |
| 34 | + `<template> test |
| 35 | +</template>`, |
| 36 | + |
| 37 | + // JS code with trailing spaces outside <template> must NOT be flagged |
| 38 | + 'const foo = "bar"; \n\n<template><div>Hello</div></template>', |
| 39 | + ], |
| 40 | + |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + code: `<template> |
| 44 | + <div>Hello</div> |
| 45 | + </template>`, |
| 46 | + output: `<template> |
| 47 | + <div>Hello</div> |
| 48 | + </template>`, |
| 49 | + errors: [ |
| 50 | + { |
| 51 | + message: 'Trailing whitespace detected.', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: `<template> |
| 57 | + <div>Hello</div> |
| 58 | + </template>`, |
| 59 | + output: `<template> |
| 60 | + <div>Hello</div> |
| 61 | + </template>`, |
| 62 | + errors: [ |
| 63 | + { |
| 64 | + message: 'Trailing whitespace detected.', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + |
| 69 | + { |
| 70 | + code: `<template>test |
| 71 | +</template>`, |
| 72 | + output: `<template>test |
| 73 | +</template>`, |
| 74 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 75 | + }, |
| 76 | + { |
| 77 | + code: `<template>import { hbs } from 'ember-cli-htmlbars'; |
| 78 | +
|
| 79 | +test('it renders', async (assert) => { |
| 80 | + await render(hbs\` |
| 81 | + <div class="parent"> |
| 82 | + <div class="child"></div> |
| 83 | + </div> |
| 84 | + \`); |
| 85 | +});</template>`, |
| 86 | + output: `<template>import { hbs } from 'ember-cli-htmlbars'; |
| 87 | +
|
| 88 | +test('it renders', async (assert) => { |
| 89 | + await render(hbs\` |
| 90 | + <div class="parent"> |
| 91 | + <div class="child"></div> |
| 92 | + </div> |
| 93 | + \`); |
| 94 | +});</template>`, |
| 95 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 96 | + }, |
| 97 | + { |
| 98 | + code: `<template>import { hbs } from 'ember-cli-htmlbars'; |
| 99 | +
|
| 100 | +test('it renders', async (assert) => { |
| 101 | + await render(hbs\` |
| 102 | + <div></div> |
| 103 | + |
| 104 | + <div></div> |
| 105 | + \`); |
| 106 | +});</template>`, |
| 107 | + output: `<template>import { hbs } from 'ember-cli-htmlbars'; |
| 108 | +
|
| 109 | +test('it renders', async (assert) => { |
| 110 | + await render(hbs\` |
| 111 | + <div></div> |
| 112 | +
|
| 113 | + <div></div> |
| 114 | + \`); |
| 115 | +});</template>`, |
| 116 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 117 | + }, |
| 118 | + { |
| 119 | + code: '<template>test\n \n</template>', |
| 120 | + output: '<template>test\n\n</template>', |
| 121 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 122 | + }, |
| 123 | + { |
| 124 | + code: `<template>{{#my-component}} |
| 125 | + test |
| 126 | +{{/my-component}}</template>`, |
| 127 | + output: `<template>{{#my-component}} |
| 128 | + test |
| 129 | +{{/my-component}}</template>`, |
| 130 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 131 | + }, |
| 132 | + ], |
| 133 | +}); |
| 134 | + |
| 135 | +const hbsRuleTester = new RuleTester({ |
| 136 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 137 | + parserOptions: { |
| 138 | + ecmaVersion: 2022, |
| 139 | + sourceType: 'module', |
| 140 | + }, |
| 141 | +}); |
| 142 | + |
| 143 | +hbsRuleTester.run('template-no-trailing-spaces', rule, { |
| 144 | + valid: [ |
| 145 | + 'test', |
| 146 | + ' test', |
| 147 | + `test |
| 148 | +`, |
| 149 | + ` |
| 150 | +`, |
| 151 | + ` test |
| 152 | +`, |
| 153 | + `{{#my-component}} |
| 154 | + test |
| 155 | +{{/my-component}}`, |
| 156 | + `import { hbs } from 'ember-cli-htmlbars'; |
| 157 | +
|
| 158 | +test('it renders', async (assert) => { |
| 159 | + await render(hbs\` |
| 160 | + <div class="parent"> |
| 161 | + <div class="child"></div> |
| 162 | + </div> |
| 163 | + \`); |
| 164 | +});`, |
| 165 | + ], |
| 166 | + invalid: [ |
| 167 | + { |
| 168 | + code: 'test ', |
| 169 | + output: 'test', |
| 170 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 171 | + }, |
| 172 | + { |
| 173 | + code: `test |
| 174 | +`, |
| 175 | + output: `test |
| 176 | +`, |
| 177 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 178 | + }, |
| 179 | + { |
| 180 | + code: `import { hbs } from 'ember-cli-htmlbars'; |
| 181 | +
|
| 182 | +test('it renders', async (assert) => { |
| 183 | + await render(hbs\` |
| 184 | + <div class="parent"> |
| 185 | + <div class="child"></div> |
| 186 | + </div> |
| 187 | + \`); |
| 188 | +});`, |
| 189 | + output: `import { hbs } from 'ember-cli-htmlbars'; |
| 190 | +
|
| 191 | +test('it renders', async (assert) => { |
| 192 | + await render(hbs\` |
| 193 | + <div class="parent"> |
| 194 | + <div class="child"></div> |
| 195 | + </div> |
| 196 | + \`); |
| 197 | +});`, |
| 198 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 199 | + }, |
| 200 | + { |
| 201 | + code: `import { hbs } from 'ember-cli-htmlbars'; |
| 202 | +
|
| 203 | +test('it renders', async (assert) => { |
| 204 | + await render(hbs\` |
| 205 | + <div></div> |
| 206 | + |
| 207 | + <div></div> |
| 208 | + \`); |
| 209 | +});`, |
| 210 | + output: `import { hbs } from 'ember-cli-htmlbars'; |
| 211 | +
|
| 212 | +test('it renders', async (assert) => { |
| 213 | + await render(hbs\` |
| 214 | + <div></div> |
| 215 | +
|
| 216 | + <div></div> |
| 217 | + \`); |
| 218 | +});`, |
| 219 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 220 | + }, |
| 221 | + { |
| 222 | + code: 'test\n \n', |
| 223 | + output: 'test\n\n', |
| 224 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 225 | + }, |
| 226 | + { |
| 227 | + code: '{{#my-component}}\n test \n{{/my-component}}', |
| 228 | + output: '{{#my-component}}\n test\n{{/my-component}}', |
| 229 | + errors: [{ message: 'Trailing whitespace detected.' }], |
| 230 | + }, |
| 231 | + ], |
| 232 | +}); |
0 commit comments