Skip to content

Commit a9c64a4

Browse files
committed
refactor
1 parent f67546e commit a9c64a4

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

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

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRule } from '../utils/index.js';
22
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
33
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
44
import { getSourceCode } from '../utils/compat.js';
5-
import type { SuggestionReportDescriptor } from '../types.js';
5+
import type { SuggestionReportDescriptor, SourceCode } from '../types.js';
66

77
export default createRule('block-lang', {
88
meta: {
@@ -91,20 +91,7 @@ export default createRule('block-lang', {
9191
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
9292
allowedScriptLangs
9393
)}.`,
94-
suggest: allowedScriptLangs
95-
.filter((lang) => lang != null && lang !== '')
96-
.map((lang) => {
97-
return {
98-
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
99-
fix: (fixer) => {
100-
const langAttributeText = getLangAttributeText(lang ?? '', true);
101-
return fixer.insertTextAfterRange(
102-
[0, 0],
103-
`<script${langAttributeText}>\n</script>\n\n`
104-
);
105-
}
106-
};
107-
})
94+
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', getSourceCode(context))
10895
});
10996
}
11097
for (const scriptNode of scriptNodes) {
@@ -114,7 +101,7 @@ export default createRule('block-lang', {
114101
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
115102
allowedScriptLangs
116103
)}.`,
117-
suggest: buildSuggestions(allowedScriptLangs, scriptNode)
104+
suggest: buildReplaceLangSuggestions(allowedScriptLangs, scriptNode)
118105
});
119106
}
120107
}
@@ -125,20 +112,7 @@ export default createRule('block-lang', {
125112
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
126113
allowedStyleLangs
127114
)}.`,
128-
suggest: allowedStyleLangs
129-
.filter((lang) => lang != null && lang !== '')
130-
.map((lang) => {
131-
return {
132-
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
133-
fix: (fixer) => {
134-
const langAttributeText = getLangAttributeText(lang ?? '', true);
135-
return fixer.insertTextAfterRange(
136-
[sourceCode.text.length, sourceCode.text.length],
137-
`<style${langAttributeText}>\n</style>\n\n`
138-
);
139-
}
140-
};
141-
})
115+
suggest: buildAddLangSuggestions(allowedStyleLangs, 'style', sourceCode)
142116
});
143117
}
144118
for (const styleNode of styleNodes) {
@@ -148,7 +122,7 @@ export default createRule('block-lang', {
148122
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
149123
allowedStyleLangs
150124
)}.`,
151-
suggest: buildSuggestions(allowedStyleLangs, styleNode)
125+
suggest: buildReplaceLangSuggestions(allowedStyleLangs, styleNode)
152126
});
153127
}
154128
}
@@ -157,7 +131,28 @@ export default createRule('block-lang', {
157131
}
158132
});
159133

160-
function buildSuggestions(
134+
function buildAddLangSuggestions(
135+
langs: (string | null)[],
136+
tagName: 'script' | 'style',
137+
sourceCode: SourceCode
138+
): SuggestionReportDescriptor[] {
139+
return langs
140+
.filter((lang) => lang != null && lang !== '')
141+
.map((lang) => {
142+
return {
143+
desc: `Add a lang attribute to a <${tagName}> block with the value "${lang}".`,
144+
fix: (fixer) => {
145+
const langAttributeText = getLangAttributeText(lang ?? '', true);
146+
return fixer.insertTextAfterRange(
147+
tagName === 'script' ? [0, 0] : [sourceCode.text.length, sourceCode.text.length],
148+
`<${tagName}${langAttributeText}>\n</${tagName}>\n\n`
149+
);
150+
}
151+
};
152+
});
153+
}
154+
155+
function buildReplaceLangSuggestions(
161156
langs: (string | null)[],
162157
node: SvelteScriptElement | SvelteStyleElement
163158
): SuggestionReportDescriptor[] {

0 commit comments

Comments
 (0)