forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-quotes.js
More file actions
164 lines (154 loc) · 5.12 KB
/
template-quotes.js
File metadata and controls
164 lines (154 loc) · 5.12 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
function attrValueHasChar(node, ch) {
if (node.type === 'GlimmerTextNode') {
return node.chars.includes(ch);
}
if (node.type === 'GlimmerConcatStatement') {
return (node.parts || []).some((n) => n.type === 'GlimmerTextNode' && n.chars.includes(ch));
}
return false;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'layout',
docs: {
description: 'enforce consistent quote style in templates',
category: 'Stylistic Issues',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-quotes.md',
templateMode: 'both',
},
fixable: 'code',
schema: [
{
oneOf: [
{ enum: ['double', 'single'] },
{
type: 'object',
required: ['curlies', 'html'],
properties: {
curlies: { enum: ['double', 'single', false] },
html: { enum: ['double', 'single', false] },
},
additionalProperties: false,
},
],
},
],
messages: {
wrongQuotes: '{{message}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/quotes.js',
docs: 'docs/rule/quotes.md',
tests: 'test/unit/rules/quotes-test.js',
},
},
create(context) {
const rawOption = context.options[0];
if (rawOption === undefined) {
return {};
}
const config =
typeof rawOption === 'string' ? { curlies: rawOption, html: rawOption } : rawOption;
if (!config.curlies && !config.html) {
return {};
}
const chars = { single: "'", double: '"' };
const goodChars = {
curlies: config.curlies ? chars[config.curlies] : null,
html: config.html ? chars[config.html] : null,
};
const badChars = {
curlies: goodChars.curlies
? goodChars.curlies === chars.single
? chars.double
: chars.single
: null,
html: goodChars.html ? (goodChars.html === chars.single ? chars.double : chars.single) : null,
};
let message;
if (goodChars.curlies === goodChars.html && goodChars.curlies) {
message = `you must use ${config.curlies} quotes in templates`;
} else if (!goodChars.curlies || !goodChars.html) {
const active = goodChars.curlies || goodChars.html;
const activeType = active === chars.single ? 'single' : 'double';
const context_ = goodChars.curlies ? 'Handlebars syntax' : 'HTML attributes';
message = `you must use ${activeType} quotes in ${context_}`;
} else {
const doubleCtx =
goodChars.curlies === chars.double ? 'Handlebars syntax' : 'HTML attributes';
const singleCtx =
goodChars.curlies === chars.single ? 'Handlebars syntax' : 'HTML attributes';
message = `you must use double quotes for ${doubleCtx} and single quotes for ${singleCtx} in templates`;
}
const sourceCode = context.sourceCode;
return {
GlimmerAttrNode(node) {
if (!goodChars.html || !badChars.html) {
return;
}
// Skip valueless attributes
if (!node.value || node.isValueless) {
return;
}
const raw = sourceCode.getText(node);
// Extract quote char used: attr="..." or attr='...'
const eqIndex = raw.indexOf('=');
if (eqIndex === -1) {
return;
}
const afterEq = raw[eqIndex + 1];
if (afterEq !== badChars.html) {
return;
}
// If the value contains the desired quote char, we can't autofix
if (attrValueHasChar(node.value, goodChars.html)) {
context.report({ node, messageId: 'wrongQuotes', data: { message } });
return;
}
context.report({
node,
messageId: 'wrongQuotes',
data: { message },
fix(fixer) {
const nodeText = sourceCode.getText(node);
const eqIdx = nodeText.indexOf('=');
const valuePart = nodeText.slice(eqIdx + 1);
// Replace outer quotes
const newValuePart = goodChars.html + valuePart.slice(1, -1) + goodChars.html;
const newText = nodeText.slice(0, eqIdx + 1) + newValuePart;
return fixer.replaceText(node, newText);
},
});
},
GlimmerStringLiteral(node) {
if (!goodChars.curlies || !badChars.curlies) {
return;
}
const raw = sourceCode.getText(node);
if (!raw || raw.length < 2) {
return;
}
const usedQuote = raw[0];
if (usedQuote !== badChars.curlies) {
return;
}
// If the value contains the desired quote char, we can't autofix
if (node.value && node.value.includes(goodChars.curlies)) {
context.report({ node, messageId: 'wrongQuotes', data: { message } });
return;
}
context.report({
node,
messageId: 'wrongQuotes',
data: { message },
fix(fixer) {
const newText = goodChars.curlies + raw.slice(1, -1) + goodChars.curlies;
return fixer.replaceText(node, newText);
},
});
},
};
},
};