Skip to content
97 changes: 90 additions & 7 deletions packages/eslint-plugin-svelte/src/rules/block-lang.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRule } from '../utils/index.js';
import { getLangValue } from '../utils/ast-utils.js';
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
import { getSourceCode } from '../utils/compat.js';

Expand Down Expand Up @@ -54,7 +54,8 @@ export default createRule('block-lang', {
}
],
messages: {},
type: 'suggestion'
type: 'suggestion',
hasSuggestions: true
},
create(context) {
if (!getSourceCode(context).parserServices.isSvelte) {
Expand Down Expand Up @@ -88,34 +89,109 @@ export default createRule('block-lang', {
loc: { line: 1, column: 1 },
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedScriptLangs
)}.`
)}.`,
suggest: allowedScriptLangs
.filter((lang) => lang != null && lang !== '')
.map((lang) => {
return {
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
return fixer.insertTextAfterRange(
[0, 0],
`<script${langAttributeText}>\n</script>\n\n`
);
}
};
})
});
}
for (const scriptNode of scriptNodes) {
if (!allowedScriptLangs.includes(getLangValue(scriptNode)?.toLowerCase() ?? null)) {
const langAttribute = findAttribute(scriptNode, 'lang');
context.report({
node: scriptNode,
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
allowedScriptLangs
)}.`
)}.`,
suggest: allowedScriptLangs
.filter((lang) => lang != null && lang !== '')
.map((lang) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
if (langAttribute) {
return {
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
return fixer.replaceText(langAttribute, langAttributeText.trim());
}
};
}
return {
desc: `Add a <script> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
return fixer.insertTextBeforeRange(
[scriptNode.startTag.range[0] + 7, 0],
langAttributeText
);
}
};
})
});
}
}
if (styleNodes.length === 0 && enforceStylePresent) {
const sourceCode = getSourceCode(context);
context.report({
loc: { line: 1, column: 1 },
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedStyleLangs
)}.`
)}.`,
suggest: allowedStyleLangs
.filter((lang) => lang != null && lang !== '')
.map((lang) => {
return {
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
return fixer.insertTextAfterRange(
[sourceCode.text.length, sourceCode.text.length],
`<style${langAttributeText}>\n</style>\n\n`
);
}
};
})
});
}
for (const styleNode of styleNodes) {
if (!allowedStyleLangs.includes(getLangValue(styleNode)?.toLowerCase() ?? null)) {
const langAttribute = findAttribute(styleNode, 'lang');
context.report({
node: styleNode,
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
allowedStyleLangs
)}.`
)}.`,
suggest: allowedStyleLangs
.filter((lang) => lang != null && lang !== '')
.map((lang) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
if (langAttribute) {
return {
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
return fixer.replaceText(langAttribute, langAttributeText.trim());
}
};
}
return {
desc: `Add a <style> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
return fixer.insertTextBeforeRange(
[styleNode.startTag.range[0] + 6, 0],
langAttributeText
);
}
};
})
});
}
}
Expand All @@ -131,11 +207,18 @@ function prettyPrintLangs(langs: (string | null)[]): string {
const hasNull = langs.includes(null);
const nonNullLangs = langs.filter((lang) => lang !== null).map((lang) => `"${lang}"`);
if (nonNullLangs.length === 0) {
// No special behaviour for `hasNull`, because that can never happen.
// No special behavior for `hasNull`, because that can never happen.
return 'omitted';
}
const hasNullText = hasNull ? 'either omitted or ' : '';
const nonNullText =
nonNullLangs.length === 1 ? nonNullLangs[0] : `one of ${nonNullLangs.join(', ')}`;
return hasNullText + nonNullText;
}

/**
* Returns the lang attribute text, with special handling of the `null` lang option with respect to the `prependWhitespace` argument.
*/
function getLangAttributeText(lang: string, prependWhitespace: boolean): string {
return `${prependWhitespace ? ' ' : ''}lang="${lang}"`;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "javascript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "javascript".
output: |
<script lang="javascript"></script>
<style lang="javascript"></style>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. I will fix this but I need to go outside now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon carefully reviewing the test cases, this is correct.😇

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "javascript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "javascript".
output: |
<script lang="javascript"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "javascript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "javascript".
output: |
<script lang="javascript"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "javascript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "javascript".
output: |
<script lang="javascript"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "javascript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "javascript".
output: |
<script lang="javascript"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "js".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "js".
output: |
<script lang="js"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "js".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "js".
output: |
<script lang="js"></script>

<style lang="js"></style>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "js".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "js".
output: |
<script lang="js"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "js".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "js".
output: |
<script lang="js"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "js".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "js".
output: |
<script lang="js"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script context="module" lang="ts"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script context="module" lang="ts"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts" context="module"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts" context="module"></script>

<script lang="ts"></script>

<style lang="ts"></style>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script context="module" lang="ts"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
one of "ts", "typescript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>
- desc: Add a <script> block with the lang attribute set to "typescript".
output: |
<script lang="typescript"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
one of "ts", "typescript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>
- desc: Add a <script> block with the lang attribute set to "typescript".
output: |
<script lang="typescript"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
one of "ts", "typescript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>

<style></style>
- desc: Add a <script> block with the lang attribute set to "typescript".
output: |
<script lang="typescript"></script>

<style></style>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
one of "ts", "typescript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>

<style lang="ts"></style>
- desc: Add a <script> block with the lang attribute set to "typescript".
output: |
<script lang="typescript"></script>

<style lang="ts"></style>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
one of "ts", "typescript".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>

<style lang="typescript"></style>
- desc: Add a <script> block with the lang attribute set to "typescript".
output: |
<script lang="typescript"></script>

<style lang="typescript"></style>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
suggestions:
- desc: Add a <script> block with the lang attribute set to "ts".
output: |
<script lang="ts"></script>
Loading
Loading