|
| 1 | +const rule = require('../../../lib/rules/template-no-yield-to-default'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const ERROR_MESSAGE = 'A block named "default" is not valid'; |
| 5 | + |
| 6 | +const validHbs = [ |
| 7 | + '{{yield}}', |
| 8 | + '{{yield to="title"}}', |
| 9 | + '{{has-block}}', |
| 10 | + '{{has-block "title"}}', |
| 11 | + '{{has-block-params}}', |
| 12 | + '{{has-block-params "title"}}', |
| 13 | + '{{hasBlock}}', |
| 14 | + '{{hasBlock "title"}}', |
| 15 | + '{{hasBlockParams}}', |
| 16 | + '{{hasBlockParams "title"}}', |
| 17 | +]; |
| 18 | + |
| 19 | +const invalidHbs = [ |
| 20 | + { |
| 21 | + code: '{{yield to="default"}}', |
| 22 | + output: null, |
| 23 | + errors: [{ message: ERROR_MESSAGE }], |
| 24 | + }, |
| 25 | + { |
| 26 | + code: '{{has-block "default"}}', |
| 27 | + output: null, |
| 28 | + errors: [{ message: ERROR_MESSAGE }], |
| 29 | + }, |
| 30 | + { |
| 31 | + code: '{{has-block-params "default"}}', |
| 32 | + output: null, |
| 33 | + errors: [{ message: ERROR_MESSAGE }], |
| 34 | + }, |
| 35 | + { |
| 36 | + code: '{{hasBlock "default"}}', |
| 37 | + output: null, |
| 38 | + errors: [{ message: ERROR_MESSAGE }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: '{{hasBlockParams "default"}}', |
| 42 | + output: null, |
| 43 | + errors: [{ message: ERROR_MESSAGE }], |
| 44 | + }, |
| 45 | + { |
| 46 | + code: '{{if (has-block "default")}}', |
| 47 | + output: null, |
| 48 | + errors: [{ message: ERROR_MESSAGE }], |
| 49 | + }, |
| 50 | + { |
| 51 | + code: '{{#if (has-block "default")}}{{/if}}', |
| 52 | + output: null, |
| 53 | + errors: [{ message: ERROR_MESSAGE }], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: '{{if (has-block-params "default")}}', |
| 57 | + output: null, |
| 58 | + errors: [{ message: ERROR_MESSAGE }], |
| 59 | + }, |
| 60 | + { |
| 61 | + code: '{{#if (has-block-params "default")}}{{/if}}', |
| 62 | + output: null, |
| 63 | + errors: [{ message: ERROR_MESSAGE }], |
| 64 | + }, |
| 65 | + { |
| 66 | + code: '{{if (hasBlock "default")}}', |
| 67 | + output: null, |
| 68 | + errors: [{ message: ERROR_MESSAGE }], |
| 69 | + }, |
| 70 | + { |
| 71 | + code: '{{#if (hasBlock "default")}}{{/if}}', |
| 72 | + output: null, |
| 73 | + errors: [{ message: ERROR_MESSAGE }], |
| 74 | + }, |
| 75 | + { |
| 76 | + code: '{{if (hasBlockParams "default")}}', |
| 77 | + output: null, |
| 78 | + errors: [{ message: ERROR_MESSAGE }], |
| 79 | + }, |
| 80 | + { |
| 81 | + code: '{{#if (hasBlockParams "default")}}{{/if}}', |
| 82 | + output: null, |
| 83 | + errors: [{ message: ERROR_MESSAGE }], |
| 84 | + }, |
| 85 | +]; |
| 86 | + |
| 87 | +function wrapTemplate(entry) { |
| 88 | + if (typeof entry === 'string') { |
| 89 | + return `<template>${entry}</template>`; |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + ...entry, |
| 94 | + code: `<template>${entry.code}</template>`, |
| 95 | + output: entry.output ? `<template>${entry.output}</template>` : entry.output, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +const gjsRuleTester = new RuleTester({ |
| 100 | + parser: require.resolve('ember-eslint-parser'), |
| 101 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 102 | +}); |
| 103 | + |
| 104 | +gjsRuleTester.run('template-no-yield-to-default', rule, { |
| 105 | + valid: validHbs.map(wrapTemplate), |
| 106 | + invalid: invalidHbs.map(wrapTemplate), |
| 107 | +}); |
| 108 | + |
| 109 | +const hbsRuleTester = new RuleTester({ |
| 110 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 111 | + parserOptions: { |
| 112 | + ecmaVersion: 2022, |
| 113 | + sourceType: 'module', |
| 114 | + }, |
| 115 | +}); |
| 116 | + |
| 117 | +hbsRuleTester.run('template-no-yield-to-default', rule, { |
| 118 | + valid: validHbs, |
| 119 | + invalid: invalidHbs, |
| 120 | +}); |
0 commit comments