|
| 1 | +// Form `method` attribute keywords: |
| 2 | +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method |
| 3 | +const VALID_FORM_METHODS = ['POST', 'GET', 'DIALOG']; |
| 4 | + |
| 5 | +const DEFAULT_CONFIG = { |
| 6 | + allowedMethods: VALID_FORM_METHODS, |
| 7 | +}; |
| 8 | + |
| 9 | +function parseConfig(config) { |
| 10 | + if (config === false || config === undefined) { |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | + if (config === true) { |
| 15 | + return DEFAULT_CONFIG; |
| 16 | + } |
| 17 | + |
| 18 | + if (typeof config === 'object' && Array.isArray(config.allowedMethods)) { |
| 19 | + const allowedMethods = config.allowedMethods.map((m) => String(m).toUpperCase()); |
| 20 | + |
| 21 | + // Check if all methods are valid |
| 22 | + const hasAllValid = allowedMethods.every((m) => VALID_FORM_METHODS.includes(m)); |
| 23 | + |
| 24 | + if (hasAllValid) { |
| 25 | + return { allowedMethods }; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return false; |
| 30 | +} |
| 31 | + |
| 32 | +function makeErrorMessage(methods) { |
| 33 | + return `All \`<form>\` elements should have \`method\` attribute with value of \`${methods.join(',')}\``; |
| 34 | +} |
| 35 | + |
| 36 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 37 | +module.exports = { |
| 38 | + meta: { |
| 39 | + type: 'suggestion', |
| 40 | + docs: { |
| 41 | + description: 'require form method attribute', |
| 42 | + category: 'Best Practices', |
| 43 | + recommended: false, |
| 44 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-form-method.md', |
| 45 | + templateMode: 'both', |
| 46 | + }, |
| 47 | + fixable: null, |
| 48 | + schema: [ |
| 49 | + { |
| 50 | + oneOf: [ |
| 51 | + { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + allowedMethods: { |
| 55 | + type: 'array', |
| 56 | + items: { |
| 57 | + type: 'string', |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + additionalProperties: false, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ], |
| 66 | + messages: {}, |
| 67 | + originallyFrom: { |
| 68 | + name: 'ember-template-lint', |
| 69 | + rule: 'lib/rules/require-form-method.js', |
| 70 | + docs: 'docs/rule/require-form-method.md', |
| 71 | + tests: 'test/unit/rules/require-form-method-test.js', |
| 72 | + }, |
| 73 | + }, |
| 74 | + |
| 75 | + create(context) { |
| 76 | + // If no options provided, use defaults |
| 77 | + let config = context.options[0]; |
| 78 | + config = config ? parseConfig(config) : DEFAULT_CONFIG; |
| 79 | + |
| 80 | + if (config === false) { |
| 81 | + return {}; |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + GlimmerElementNode(node) { |
| 86 | + if (node.tag !== 'form') { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const methodAttribute = node.attributes.find((attr) => attr.name === 'method'); |
| 91 | + |
| 92 | + if (!methodAttribute) { |
| 93 | + context.report({ |
| 94 | + node, |
| 95 | + message: makeErrorMessage(config.allowedMethods), |
| 96 | + }); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // Check if it's a text value |
| 101 | + if (methodAttribute.value && methodAttribute.value.type === 'GlimmerTextNode') { |
| 102 | + const methodValue = methodAttribute.value.chars.toUpperCase(); |
| 103 | + |
| 104 | + if (!config.allowedMethods.includes(methodValue)) { |
| 105 | + context.report({ |
| 106 | + node, |
| 107 | + message: makeErrorMessage(config.allowedMethods), |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + // If it's a dynamic value (like {{foo}}), don't report |
| 112 | + }, |
| 113 | + }; |
| 114 | + }, |
| 115 | +}; |
0 commit comments