-
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathtemplate-self-closing-void-elements.js
More file actions
83 lines (72 loc) · 2.24 KB
/
template-self-closing-void-elements.js
File metadata and controls
83 lines (72 loc) · 2.24 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
'use strict';
const { htmlVoidElements } = require('html-void-elements');
const VOID_ELEMENTS = new Set(htmlVoidElements);
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require self-closing on void elements',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-self-closing-void-elements.md',
templateMode: 'both',
},
fixable: 'code',
schema: [
{
oneOf: [{ type: 'boolean' }, { type: 'string', enum: ['require'] }],
},
],
messages: {
redundantSelfClosing: 'Self-closing a void element is redundant',
requireSelfClosing: 'Self-closing a void element is required',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/self-closing-void-elements.js',
docs: 'docs/rule/self-closing-void-elements.md',
tests: 'test/unit/rules/self-closing-void-elements-test.js',
},
},
create(context) {
const sourceCode = context.sourceCode;
const config = context.options[0] ?? true;
if (config === false) {
return {};
}
const requireSelfClosing = config === 'require';
return {
GlimmerElementNode(node) {
if (!VOID_ELEMENTS.has(node.tag)) {
return;
}
if (requireSelfClosing) {
if (!node.selfClosing) {
const source = sourceCode.getText(node).trim();
context.report({
node,
messageId: 'requireSelfClosing',
fix(fixer) {
return fixer.replaceText(node, source.replace(/>$/, '/>'));
},
});
}
} else {
if (node.selfClosing) {
const source = sourceCode.getText(node).trim();
context.report({
node,
messageId: 'redundantSelfClosing',
fix(fixer) {
const replacement = node.blockParams?.length
? source.replace(/\/>$/, '>')
: source.replace(/\s*\/>$/, '>');
return fixer.replaceText(node, replacement);
},
});
}
}
},
};
},
};