|
| 1 | +const rule = require('../../../lib/rules/template-require-input-type'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const ERROR_MISSING = 'All `<input>` elements should have a `type` attribute'; |
| 5 | +const errInvalid = (value) => `\`<input type="${value}">\` is not a valid input type`; |
| 6 | + |
| 7 | +const validHbs = [ |
| 8 | + '<input type="text" />', |
| 9 | + '<input type="email" />', |
| 10 | + '<input type="checkbox" />', |
| 11 | + '<input type="submit" />', |
| 12 | + '<input type="datetime-local" />', |
| 13 | + '<input type="{{this.inputType}}" />', |
| 14 | + '<input type={{this.inputType}} />', |
| 15 | + '<div />', |
| 16 | + '<div type="foo" />', |
| 17 | + '<MyInput type="unknown" />', |
| 18 | + // Default (requireExplicit=false): missing `type` is allowed. |
| 19 | + '<input />', |
| 20 | + '<input name="email" />', |
| 21 | +]; |
| 22 | + |
| 23 | +const invalidHbs = [ |
| 24 | + { |
| 25 | + code: '<input type="" />', |
| 26 | + output: '<input type="text" />', |
| 27 | + errors: [{ message: errInvalid('') }], |
| 28 | + }, |
| 29 | + { |
| 30 | + code: '<input type="foo" />', |
| 31 | + output: '<input type="text" />', |
| 32 | + errors: [{ message: errInvalid('foo') }], |
| 33 | + }, |
| 34 | + { |
| 35 | + code: '<input type="TEXTY" />', |
| 36 | + output: '<input type="text" />', |
| 37 | + errors: [{ message: errInvalid('TEXTY') }], |
| 38 | + }, |
| 39 | + // Valueless type attribute — per HTML spec resolves to the missing-value |
| 40 | + // default (Text state), same runtime result as `type=""`. Flag and autofix |
| 41 | + // to `type="text"`. (Output loses the pre-slash space because the |
| 42 | + // valueless attr range ends at `type`; prettier will re-insert if needed.) |
| 43 | + { |
| 44 | + code: '<input type />', |
| 45 | + output: '<input type="text"/>', |
| 46 | + errors: [{ message: errInvalid('') }], |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +const requireExplicitInvalid = [ |
| 51 | + { |
| 52 | + code: '<input />', |
| 53 | + options: [{ requireExplicit: true }], |
| 54 | + output: '<input type="text" />', |
| 55 | + errors: [{ message: ERROR_MISSING }], |
| 56 | + }, |
| 57 | + { |
| 58 | + code: '<input name="email" />', |
| 59 | + options: [{ requireExplicit: true }], |
| 60 | + output: '<input type="text" name="email" />', |
| 61 | + errors: [{ message: ERROR_MISSING }], |
| 62 | + }, |
| 63 | + { |
| 64 | + code: '<input name="email" />', |
| 65 | + options: [{ requireExplicit: true }], |
| 66 | + output: '<input type="text" name="email" />', |
| 67 | + errors: [{ message: ERROR_MISSING }], |
| 68 | + }, |
| 69 | +]; |
| 70 | + |
| 71 | +const requireExplicitValid = [ |
| 72 | + // With requireExplicit: an explicit known type satisfies the rule. |
| 73 | + { code: '<input type="text" />', options: [{ requireExplicit: true }] }, |
| 74 | + // Dynamic type also satisfies — we can't know the runtime value. |
| 75 | + { code: '<input type={{this.inputType}} />', options: [{ requireExplicit: true }] }, |
| 76 | +]; |
| 77 | + |
| 78 | +const gjsValid = validHbs.map((code) => `<template>${code}</template>`); |
| 79 | +const gjsInvalid = invalidHbs.map(({ code, output, errors }) => ({ |
| 80 | + code: `<template>${code}</template>`, |
| 81 | + output: `<template>${output}</template>`, |
| 82 | + errors, |
| 83 | +})); |
| 84 | + |
| 85 | +const gjsRuleTester = new RuleTester({ |
| 86 | + parser: require.resolve('ember-eslint-parser'), |
| 87 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 88 | +}); |
| 89 | + |
| 90 | +gjsRuleTester.run('template-require-input-type', rule, { |
| 91 | + valid: [ |
| 92 | + ...gjsValid, |
| 93 | + ...requireExplicitValid.map(({ code, options }) => ({ |
| 94 | + code: `<template>${code}</template>`, |
| 95 | + options, |
| 96 | + })), |
| 97 | + // Scope-shadowed `input` — the template's `<input>` refers to the local |
| 98 | + // const binding (a component), not the native HTML element. The rule |
| 99 | + // skips it via `isNativeElement`'s scope check. |
| 100 | + `const input = 'foo'; |
| 101 | +<template><input type="not-a-valid-type" /></template>`, |
| 102 | + `const input = 'foo'; |
| 103 | +<template><input /></template>`, |
| 104 | + // Block-param shadowing — `<Foo as |input|>` binds `input` inside the |
| 105 | + // yield block. The inner `<input>` should resolve to the block-param, |
| 106 | + // not the native tag. |
| 107 | + `import Foo from 'whatever'; |
| 108 | +<template><Foo as |input|><input type="not-a-valid-type" /></Foo></template>`, |
| 109 | + ], |
| 110 | + invalid: [ |
| 111 | + ...gjsInvalid, |
| 112 | + ...requireExplicitInvalid.map(({ code, options, output, errors }) => ({ |
| 113 | + code: `<template>${code}</template>`, |
| 114 | + options, |
| 115 | + output: `<template>${output}</template>`, |
| 116 | + errors, |
| 117 | + })), |
| 118 | + ], |
| 119 | +}); |
| 120 | + |
| 121 | +const hbsRuleTester = new RuleTester({ |
| 122 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 123 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 124 | +}); |
| 125 | + |
| 126 | +hbsRuleTester.run('template-require-input-type', rule, { |
| 127 | + valid: [...validHbs, ...requireExplicitValid], |
| 128 | + invalid: [...invalidHbs, ...requireExplicitInvalid], |
| 129 | +}); |
0 commit comments