Skip to content

Commit 22e440d

Browse files
committed
suggestion
1 parent 989922a commit 22e440d

File tree

1 file changed

+79
-52
lines changed

1 file changed

+79
-52
lines changed

packages/eslint-plugin-svelte/src/rules/block-lang.ts

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default createRule('block-lang', {
6666

6767
const scriptOption: string | null | (string | null)[] = context.options[0]?.script ?? null;
6868
const allowedScriptLangs: (string | null)[] = Array.isArray(scriptOption)
69-
? scriptOption
69+
? scriptOption.filter((lang) => lang != null && lang !== '')
7070
: [scriptOption];
7171
const scriptNodes: SvelteScriptElement[] = [];
7272

@@ -90,76 +90,108 @@ export default createRule('block-lang', {
9090
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
9191
allowedScriptLangs
9292
)}.`,
93-
*fix(fixer) {
94-
const langAttributeText = getLangAttributeText(allowedScriptLangs, true);
95-
96-
yield fixer.insertTextAfterRange(
97-
[0, 0],
98-
`<script${langAttributeText}>\n</script>\n\n`
99-
);
100-
}
93+
suggest: allowedScriptLangs
94+
.filter((lang) => lang != null && lang !== '')
95+
.map((lang) => {
96+
return {
97+
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
98+
fix: (fixer) => {
99+
const langAttributeText = getLangAttributeText(lang ?? '', true);
100+
return fixer.insertTextAfterRange(
101+
[0, 0],
102+
`<script${langAttributeText}>\n</script>\n\n`
103+
);
104+
}
105+
};
106+
})
101107
});
102108
}
103109
for (const scriptNode of scriptNodes) {
104110
if (!allowedScriptLangs.includes(getLangValue(scriptNode)?.toLowerCase() ?? null)) {
111+
const langAttribute = findAttribute(scriptNode, 'lang');
105112
context.report({
106113
node: scriptNode,
107114
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
108115
allowedScriptLangs
109116
)}.`,
110-
*fix(fixer) {
111-
const langAttribute = findAttribute(scriptNode, 'lang');
112-
const langAttributeText = getLangAttributeText(allowedScriptLangs, true);
113-
114-
if (langAttribute) {
115-
yield fixer.replaceText(langAttribute, langAttributeText.trim());
116-
} else {
117-
yield fixer.insertTextBeforeRange(
118-
[scriptNode.startTag.range[0] + 7, 0],
119-
langAttributeText
120-
);
121-
}
122-
}
117+
suggest: allowedScriptLangs
118+
.filter((lang) => lang != null && lang !== '')
119+
.map((lang) => {
120+
const langAttributeText = getLangAttributeText(lang ?? '', true);
121+
if (langAttribute) {
122+
return {
123+
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
124+
fix: (fixer) => {
125+
return fixer.replaceText(langAttribute, langAttributeText.trim());
126+
}
127+
};
128+
}
129+
return {
130+
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
131+
fix: (fixer) => {
132+
return fixer.insertTextBeforeRange(
133+
[scriptNode.startTag.range[0] + 7, 0],
134+
langAttributeText
135+
);
136+
}
137+
};
138+
})
123139
});
124140
}
125141
}
126142
if (styleNodes.length === 0 && enforceStylePresent) {
143+
const sourceCode = getSourceCode(context);
127144
context.report({
128145
loc: { line: 1, column: 1 },
129146
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
130147
allowedStyleLangs
131148
)}.`,
132-
*fix(fixer) {
133-
const sourceCode = getSourceCode(context);
134-
const langAttributeText = getLangAttributeText(allowedScriptLangs, true);
135-
136-
yield fixer.insertTextAfterRange(
137-
[sourceCode.text.length, sourceCode.text.length],
138-
`\n\n<style${langAttributeText}>\n</style>`
139-
);
140-
}
149+
suggest: allowedStyleLangs
150+
.filter((lang) => lang != null && lang !== '')
151+
.map((lang) => {
152+
return {
153+
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
154+
fix: (fixer) => {
155+
const langAttributeText = getLangAttributeText(lang ?? '', true);
156+
return fixer.insertTextAfterRange(
157+
[sourceCode.text.length, sourceCode.text.length],
158+
`<style${langAttributeText}>\n</style>\n\n`
159+
);
160+
}
161+
};
162+
})
141163
});
142164
}
143165
for (const styleNode of styleNodes) {
144166
if (!allowedStyleLangs.includes(getLangValue(styleNode)?.toLowerCase() ?? null)) {
167+
const langAttribute = findAttribute(styleNode, 'lang');
145168
context.report({
146169
node: styleNode,
147170
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
148171
allowedStyleLangs
149172
)}.`,
150-
*fix(fixer) {
151-
const langAttribute = findAttribute(styleNode, 'lang');
152-
const langAttributeText = getLangAttributeText(allowedStyleLangs, true);
153-
154-
if (langAttribute) {
155-
yield fixer.replaceText(langAttribute, langAttributeText.trim());
156-
} else {
157-
yield fixer.insertTextBeforeRange(
158-
[styleNode.startTag.range[0] + 6, 0],
159-
langAttributeText
160-
);
161-
}
162-
}
173+
suggest: allowedStyleLangs
174+
.filter((lang) => lang != null && lang !== '')
175+
.map((lang) => {
176+
const langAttributeText = getLangAttributeText(lang ?? '', true);
177+
if (langAttribute) {
178+
return {
179+
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
180+
fix: (fixer) => {
181+
return fixer.replaceText(langAttribute, langAttributeText.trim());
182+
}
183+
};
184+
}
185+
return {
186+
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
187+
fix: (fixer) => {
188+
return fixer.insertTextBeforeRange(
189+
[styleNode.startTag.range[0] + 6, 0],
190+
langAttributeText
191+
);
192+
}
193+
};
194+
})
163195
});
164196
}
165197
}
@@ -175,7 +207,7 @@ function prettyPrintLangs(langs: (string | null)[]): string {
175207
const hasNull = langs.includes(null);
176208
const nonNullLangs = langs.filter((lang) => lang !== null).map((lang) => `"${lang}"`);
177209
if (nonNullLangs.length === 0) {
178-
// No special behaviour for `hasNull`, because that can never happen.
210+
// No special behavior for `hasNull`, because that can never happen.
179211
return 'omitted';
180212
}
181213
const hasNullText = hasNull ? 'either omitted or ' : '';
@@ -187,11 +219,6 @@ function prettyPrintLangs(langs: (string | null)[]): string {
187219
/**
188220
* Returns the lang attribute text, with special handling of the `null` lang option with respect to the `prependWhitespace` argument.
189221
*/
190-
function getLangAttributeText(langs: (string | null)[], prependWhitespace: boolean): string {
191-
if (!langs.length || langs.includes(null)) return '';
192-
const [firstLang] = langs;
193-
if (langs.length === 1 && firstLang) {
194-
return `${prependWhitespace ? ' ' : ''}lang="${firstLang}"`;
195-
}
196-
return '';
222+
function getLangAttributeText(lang: string, prependWhitespace: boolean): string {
223+
return `${prependWhitespace ? ' ' : ''}lang="${lang}"`;
197224
}

0 commit comments

Comments
 (0)