forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-table-groups.js
More file actions
231 lines (214 loc) · 7.62 KB
/
template-table-groups.js
File metadata and controls
231 lines (214 loc) · 7.62 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const ALLOWED_TABLE_CHILDREN = ['caption', 'colgroup', 'thead', 'tbody', 'tfoot'];
const CONTROL_FLOW_START_MARK = 0;
const CONTROL_FLOW_END_MARK = 1;
function dasherize(str) {
return str
.replaceAll('::', '/')
.replaceAll(/([\da-z])([A-Z])/g, '$1-$2')
.toLowerCase();
}
function isControlFlowHelper(node) {
if (node.type !== 'GlimmerBlockStatement' && node.type !== 'GlimmerMustacheStatement') {
return false;
}
const name = node.path?.original;
return ['if', 'unless', 'each', 'each-in', 'let', 'with'].includes(name);
}
function isIfOrUnless(node) {
const name = node.path?.original;
return name === 'if' || name === 'unless';
}
function getEffectiveChildren(children) {
return (children || []).flatMap((child) => {
if (isControlFlowHelper(child)) {
if (isIfOrUnless(child) && child.program && child.inverse) {
return [
CONTROL_FLOW_START_MARK,
...getEffectiveChildren(child.program?.body || child.children || []),
CONTROL_FLOW_END_MARK,
CONTROL_FLOW_START_MARK,
...getEffectiveChildren(child.inverse?.body || []),
CONTROL_FLOW_END_MARK,
];
}
const body = child.program?.body || child.children || child.body?.body || [];
return getEffectiveChildren(body);
}
return [child];
});
}
function isAllowedTableChild(child, internalTags) {
switch (child.type) {
case 'GlimmerElementNode': {
const idx = ALLOWED_TABLE_CHILDREN.indexOf(child.tag);
if (idx > -1) {
return { allowed: true, indices: [idx] };
}
// Check @tagName attribute
const tagNameAttr = child.attributes?.find((a) => a.name === '@tagName');
if (tagNameAttr) {
const val = tagNameAttr.value?.type === 'GlimmerTextNode' ? tagNameAttr.value.chars : null;
const tIdx = ALLOWED_TABLE_CHILDREN.indexOf(val);
return { allowed: tIdx > -1, indices: tIdx > -1 ? [tIdx] : [] };
}
// Check custom component mapping
const dasherized = dasherize(child.tag);
const possibleIndices = internalTags.get(dasherized) || [];
if (possibleIndices.length > 0) {
return { allowed: true, indices: possibleIndices };
}
return { allowed: false };
}
case 'GlimmerBlockStatement':
case 'GlimmerMustacheStatement': {
// Check tagName hash pair
const tagNamePair = child.hash?.pairs?.find((p) => p.key === 'tagName');
if (tagNamePair) {
const val = tagNamePair.value?.value || tagNamePair.value?.chars;
const idx = ALLOWED_TABLE_CHILDREN.indexOf(val);
return { allowed: idx > -1, indices: idx > -1 ? [idx] : [] };
}
if (child.path?.original === 'yield') {
return { allowed: true, indices: [] };
}
const possibleIndices = internalTags.get(child.path?.original) || [];
if (possibleIndices.length > 0) {
return { allowed: true, indices: possibleIndices };
}
return { allowed: false };
}
case 'GlimmerCommentStatement':
case 'GlimmerMustacheCommentStatement': {
return { allowed: true, indices: [] };
}
case 'GlimmerTextNode': {
return { allowed: !/\S/.test(child.chars || ''), indices: [] };
}
default: {
return { allowed: false };
}
}
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require table elements to use table grouping elements',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-table-groups.md',
templateMode: 'both',
},
fixable: null,
schema: [
{
type: 'object',
properties: {
'allowed-table-components': { type: 'array', items: { type: 'string' } },
'allowed-caption-components': { type: 'array', items: { type: 'string' } },
'allowed-colgroup-components': { type: 'array', items: { type: 'string' } },
'allowed-thead-components': { type: 'array', items: { type: 'string' } },
'allowed-tbody-components': { type: 'array', items: { type: 'string' } },
'allowed-tfoot-components': { type: 'array', items: { type: 'string' } },
},
additionalProperties: false,
},
],
messages: {
missing: 'Tables must have a table group (thead, tbody or tfoot).',
ordering:
'Tables must have table groups in the correct order (caption, colgroup, thead, tbody then tfoot).',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/table-groups.js',
docs: 'docs/rule/table-groups.md',
tests: 'test/unit/rules/table-groups-test.js',
},
},
create(context) {
const options = context.options[0] || {};
const outerTags = new Set(options['allowed-table-components'] || []);
const internalTags = new Map();
const componentKeys = [
'allowed-caption-components',
'allowed-colgroup-components',
'allowed-thead-components',
'allowed-tbody-components',
'allowed-tfoot-components',
];
for (const [index, key] of componentKeys.entries()) {
if (options[key]) {
for (const comp of options[key]) {
if (!internalTags.has(comp)) {
internalTags.set(comp, []);
}
internalTags.get(comp).push(index);
}
}
}
function isTableElement(node) {
if (node.tag === 'table') {
return true;
}
if (outerTags.has(dasherize(node.tag))) {
return true;
}
const tagNameAttr = node.attributes?.find((a) => a.name === '@tagName');
if (tagNameAttr) {
const val = tagNameAttr.value?.type === 'GlimmerTextNode' ? tagNameAttr.value.chars : null;
return val === 'table';
}
return false;
}
return {
GlimmerElementNode(node) {
if (!isTableElement(node)) {
return;
}
// Truly empty table (no content at all between tags) must have table groups
if (!node.children || node.children.length === 0) {
const sourceCode = context.sourceCode;
const text = sourceCode.getText(node);
const openEnd = text.indexOf('>') + 1;
const closeStart = text.lastIndexOf('</');
if (closeStart >= 0 && closeStart <= openEnd) {
context.report({ node, messageId: 'missing' });
return;
}
}
const children = getEffectiveChildren(node.children);
let currentAllowedMinimumIndices = new Set([0]);
const scopedIndices = [];
for (const child of children) {
if (child === CONTROL_FLOW_START_MARK) {
scopedIndices.push(currentAllowedMinimumIndices);
currentAllowedMinimumIndices = new Set(currentAllowedMinimumIndices);
continue;
}
if (child === CONTROL_FLOW_END_MARK) {
currentAllowedMinimumIndices = scopedIndices.pop();
continue;
}
const { allowed, indices } = isAllowedTableChild(child, internalTags);
if (!allowed) {
context.report({ node, messageId: 'missing' });
return;
}
if (indices.length > 0) {
const newAllowedMinimumIndices = new Set(
[...currentAllowedMinimumIndices].flatMap((currentIndex) =>
indices.filter((newIndex) => newIndex >= currentIndex)
)
);
if (newAllowedMinimumIndices.size === 0) {
context.report({ node, messageId: 'ordering' });
return;
}
currentAllowedMinimumIndices = newAllowedMinimumIndices;
}
}
},
};
},
};