Skip to content

Commit f67546e

Browse files
committed
support null
1 parent 2e96f17 commit f67546e

34 files changed

+114
-84
lines changed

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

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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';
56

67
export default createRule('block-lang', {
78
meta: {
@@ -108,34 +109,12 @@ export default createRule('block-lang', {
108109
}
109110
for (const scriptNode of scriptNodes) {
110111
if (!allowedScriptLangs.includes(getLangValue(scriptNode)?.toLowerCase() ?? null)) {
111-
const langAttribute = findAttribute(scriptNode, 'lang');
112112
context.report({
113113
node: scriptNode,
114114
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
115115
allowedScriptLangs
116116
)}.`,
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-
})
117+
suggest: buildSuggestions(allowedScriptLangs, scriptNode)
139118
});
140119
}
141120
}
@@ -164,34 +143,12 @@ export default createRule('block-lang', {
164143
}
165144
for (const styleNode of styleNodes) {
166145
if (!allowedStyleLangs.includes(getLangValue(styleNode)?.toLowerCase() ?? null)) {
167-
const langAttribute = findAttribute(styleNode, 'lang');
168146
context.report({
169147
node: styleNode,
170148
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
171149
allowedStyleLangs
172150
)}.`,
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-
})
151+
suggest: buildSuggestions(allowedStyleLangs, styleNode)
195152
});
196153
}
197154
}
@@ -200,6 +157,49 @@ export default createRule('block-lang', {
200157
}
201158
});
202159

160+
function buildSuggestions(
161+
langs: (string | null)[],
162+
node: SvelteScriptElement | SvelteStyleElement
163+
): SuggestionReportDescriptor[] {
164+
const tagName = node.name.name;
165+
const langAttribute = findAttribute(node, 'lang');
166+
const filteredLangs = langs.filter((lang) => lang != null && lang !== '');
167+
168+
if (filteredLangs.length === 0 && langs.includes(null) && langAttribute !== null) {
169+
return [
170+
{
171+
desc: `Replace a <${tagName}> block with the lang attribute omitted.`,
172+
fix: (fixer) => {
173+
return fixer.remove({
174+
type: langAttribute.type,
175+
range: [langAttribute.range[0] - 1, langAttribute.range[1]]
176+
});
177+
}
178+
}
179+
];
180+
}
181+
return filteredLangs.map((lang) => {
182+
const langAttributeText = getLangAttributeText(lang ?? '', true);
183+
if (langAttribute) {
184+
return {
185+
desc: `Replace a <${tagName}> block with the lang attribute set to "${lang}".`,
186+
fix: (fixer) => {
187+
return fixer.replaceText(langAttribute, langAttributeText.trim());
188+
}
189+
};
190+
}
191+
return {
192+
desc: `Add a <${tagName}> block with the lang attribute set to "${lang}".`,
193+
fix: (fixer) => {
194+
return fixer.insertTextBeforeRange(
195+
[node.startTag.range[0] + tagName.length + 1, 0],
196+
langAttributeText
197+
);
198+
}
199+
};
200+
});
201+
}
202+
203203
/**
204204
* Prints the list of allowed languages, with special handling of the `null` option.
205205
*/

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/javascript/js01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "javascript".
5+
- desc: Replace a <script> block with the lang attribute set to "javascript".
66
output: |
77
<script lang="javascript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/javascript/ts01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "javascript".
5+
- desc: Replace a <script> block with the lang attribute set to "javascript".
66
output: |
77
<script lang="javascript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/javascript/typescript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "javascript".
5+
- desc: Replace a <script> block with the lang attribute set to "javascript".
66
output: |
77
<script lang="javascript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/js/javascript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "js".
5+
- desc: Replace a <script> block with the lang attribute set to "js".
66
output: |
77
<script lang="js"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/js/ts01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "js".
5+
- desc: Replace a <script> block with the lang attribute set to "js".
66
output: |
77
<script lang="js"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/js/typescript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "js".
5+
- desc: Replace a <script> block with the lang attribute set to "js".
66
output: |
77
<script lang="js"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/module-context/javascript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script context="module" lang="ts"></script>
88

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/module-context/js01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script context="module" lang="ts"></script>
88

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/module-context/typescript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script context="module" lang="ts"></script>
88

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/multiple/javascript01-errors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
line: 1
44
column: 1
55
suggestions:
6-
- desc: Add a <script> block with the lang attribute set to "ts".
6+
- desc: Replace a <script> block with the lang attribute set to "ts".
77
output: |
88
<script lang="ts"></script>
9-
- desc: Add a <script> block with the lang attribute set to "typescript".
9+
- desc: Replace a <script> block with the lang attribute set to "typescript".
1010
output: |
1111
<script lang="typescript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/multiple/js01-errors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
line: 1
44
column: 1
55
suggestions:
6-
- desc: Add a <script> block with the lang attribute set to "ts".
6+
- desc: Replace a <script> block with the lang attribute set to "ts".
77
output: |
88
<script lang="ts"></script>
9-
- desc: Add a <script> block with the lang attribute set to "typescript".
9+
- desc: Replace a <script> block with the lang attribute set to "typescript".
1010
output: |
1111
<script lang="typescript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/multiple/null-as-style-lang01-errors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
line: 1
44
column: 1
55
suggestions:
6-
- desc: Add a <script> block with the lang attribute set to "ts".
6+
- desc: Replace a <script> block with the lang attribute set to "ts".
77
output: |
88
<script lang="ts"></script>
99
1010
<style></style>
11-
- desc: Add a <script> block with the lang attribute set to "typescript".
11+
- desc: Replace a <script> block with the lang attribute set to "typescript".
1212
output: |
1313
<script lang="typescript"></script>
1414

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/multiple/ts-as-style-lang01-errors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
line: 1
44
column: 1
55
suggestions:
6-
- desc: Add a <script> block with the lang attribute set to "ts".
6+
- desc: Replace a <script> block with the lang attribute set to "ts".
77
output: |
88
<script lang="ts"></script>
99
1010
<style lang="ts"></style>
11-
- desc: Add a <script> block with the lang attribute set to "typescript".
11+
- desc: Replace a <script> block with the lang attribute set to "typescript".
1212
output: |
1313
<script lang="typescript"></script>
1414

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/multiple/typescript-as-style-lang01-errors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
line: 1
44
column: 1
55
suggestions:
6-
- desc: Add a <script> block with the lang attribute set to "ts".
6+
- desc: Replace a <script> block with the lang attribute set to "ts".
77
output: |
88
<script lang="ts"></script>
99
1010
<style lang="typescript"></style>
11-
- desc: Add a <script> block with the lang attribute set to "typescript".
11+
- desc: Replace a <script> block with the lang attribute set to "typescript".
1212
output: |
1313
<script lang="typescript"></script>
1414
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
- message: The lang attribute of the <script> block should be omitted.
22
line: 1
33
column: 1
4-
suggestions: null
4+
suggestions:
5+
- desc: Replace a <script> block with the lang attribute omitted.
6+
output: |
7+
<script></script>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
- message: The lang attribute of the <script> block should be omitted.
22
line: 1
33
column: 1
4-
suggestions: null
4+
suggestions:
5+
- desc: Replace a <script> block with the lang attribute omitted.
6+
output: |
7+
<script></script>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
- message: The lang attribute of the <script> block should be omitted.
22
line: 1
33
column: 1
4-
suggestions: null
4+
suggestions:
5+
- desc: Replace a <script> block with the lang attribute omitted.
6+
output: |
7+
<script></script>
8+
9+
<style></style>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
- message: The lang attribute of the <script> block should be omitted.
22
line: 1
33
column: 1
4-
suggestions: null
4+
suggestions:
5+
- desc: Replace a <script> block with the lang attribute omitted.
6+
output: |
7+
<script></script>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
- message: The lang attribute of the <script> block should be omitted.
22
line: 1
33
column: 1
4-
suggestions: null
4+
suggestions:
5+
- desc: Replace a <script> block with the lang attribute omitted.
6+
output: |
7+
<script></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/shorthand/javascript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/shorthand/js01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/shorthand/typescript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/ts/javascript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/ts/js01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/ts/typescript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "ts".
5+
- desc: Replace a <script> block with the lang attribute set to "ts".
66
output: |
77
<script lang="ts"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/typescript/javascript01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "typescript".
5+
- desc: Replace a <script> block with the lang attribute set to "typescript".
66
output: |
77
<script lang="typescript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/typescript/js01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "typescript".
5+
- desc: Replace a <script> block with the lang attribute set to "typescript".
66
output: |
77
<script lang="typescript"></script>

packages/eslint-plugin-svelte/tests/fixtures/rules/block-lang/invalid/script/typescript/ts01-errors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
line: 1
33
column: 1
44
suggestions:
5-
- desc: Add a <script> block with the lang attribute set to "typescript".
5+
- desc: Replace a <script> block with the lang attribute set to "typescript".
66
output: |
77
<script lang="typescript"></script>

0 commit comments

Comments
 (0)