forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-require-form-method.js
More file actions
148 lines (135 loc) · 4.39 KB
/
template-require-form-method.js
File metadata and controls
148 lines (135 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const rule = require('../../../lib/rules/template-require-form-method');
const RuleTester = require('eslint').RuleTester;
const DEFAULT_ERROR =
'All `<form>` elements should have `method` attribute with value of `POST,GET,DIALOG`';
const validHbs = [
{
options: [{ allowedMethods: ['get'] }],
code: '<form method="GET"></form>',
},
// No options → default-enabled with POST,GET,DIALOG.
'<form method="POST"></form>',
'<form method="post"></form>',
'<form method="GET"></form>',
'<form method="get"></form>',
'<form method="DIALOG"></form>',
'<form method="dialog"></form>',
'<form method="{{formMethod}}"></form>',
'<form method={{formMethod}}></form>',
'<div/>',
'<div></div>',
'<div method="randomType"></div>',
// Explicit `true` behaves identically to no options.
{ options: [true], code: '<form method="POST"></form>' },
// Explicit `false` disables the rule.
{ options: [false], code: '<form></form>' },
];
const invalidHbs = [
// Default-enabled: no options → rule active with POST,GET,DIALOG.
{
code: '<form></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
code: '<form method="NOT_A_VALID_METHOD"></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
options: [{ allowedMethods: ['get'] }],
code: '<form method="POST"></form>',
output: '<form method="GET"></form>',
errors: [
{ message: 'All `<form>` elements should have `method` attribute with value of `GET`' },
],
},
{
options: [{ allowedMethods: ['POST'] }],
code: '<form method="GET"></form>',
output: '<form method="POST"></form>',
errors: [
{ message: 'All `<form>` elements should have `method` attribute with value of `POST`' },
],
},
{
options: [true],
code: '<form></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
options: [true],
code: '<form method=""></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
options: [true],
code: '<form method=42></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
options: [true],
code: '<form method=" ge t "></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
{
options: [true],
code: '<form method=" pos t "></form>',
output: '<form method="POST"></form>',
errors: [{ message: DEFAULT_ERROR }],
},
];
function wrapTemplate(entry) {
if (typeof entry === 'string') {
return `<template>${entry}</template>`;
}
return {
...entry,
code: `<template>${entry.code}</template>`,
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
};
}
const gjsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
gjsRuleTester.run('template-require-form-method', rule, {
valid: validHbs.map(wrapTemplate),
invalid: invalidHbs.map(wrapTemplate),
});
const hbsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser/hbs'),
parserOptions: {
ecmaVersion: 2022,
sourceType: 'module',
},
});
hbsRuleTester.run('template-require-form-method', rule, {
valid: validHbs,
invalid: invalidHbs,
});
// parseConfig should throw on an invalid `allowedMethods` entry so that
// misconfiguration is surfaced immediately rather than silently ignored.
describe('template-require-form-method invalid configuration', () => {
const { Linter } = require('eslint');
function lintWith(options) {
const linter = new Linter();
linter.defineParser('ember-eslint-parser/hbs', require('ember-eslint-parser/hbs'));
linter.defineRule('template-require-form-method', rule);
return linter.verify('<form method="POST"></form>', {
parser: 'ember-eslint-parser/hbs',
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
rules: { 'template-require-form-method': ['error', options] },
});
}
test('throws on unknown method in allowedMethods', () => {
expect(() => lintWith({ allowedMethods: ['PATCH'] })).toThrow(/invalid configuration/);
});
test('throws on mixed valid/invalid method list', () => {
expect(() => lintWith({ allowedMethods: ['GET', 'BOGUS'] })).toThrow(/invalid configuration/);
});
});