Skip to content

feat(block-lang): Added support for multiple modules of the same type #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-pillows-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

feat(block-lang): added support for multiple modules of the same type
50 changes: 27 additions & 23 deletions src/rules/block-lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,37 @@ export default createRule("block-lang", {
const allowedScriptLangs: (string | null)[] = Array.isArray(scriptOption)
? scriptOption
: [scriptOption]
let scriptLang: string | null = null
let scriptNode: SvelteScriptElement | undefined = undefined
const scriptNodes: SvelteScriptElement[] = []

const styleOption: string | null | (string | null)[] =
context.options[0]?.style ?? null
const allowedStyleLangs: (string | null)[] = Array.isArray(styleOption)
? styleOption
: [styleOption]
let styleLang: string | null = null
let styleNode: SvelteStyleElement | undefined = undefined
const styleNodes: SvelteStyleElement[] = []

return {
SvelteScriptElement(node) {
scriptNode = node
scriptLang = getLangValue(node)?.toLowerCase() ?? null
scriptNodes.push(node)
},
SvelteStyleElement(node) {
styleNode = node
styleLang = getLangValue(node)?.toLowerCase() ?? null
styleNodes.push(node)
},
"Program:exit"() {
if (!allowedScriptLangs.includes(scriptLang)) {
if (scriptNode !== undefined) {
if (scriptNodes.length === 0 && enforceScriptPresent) {
context.report({
loc: { line: 1, column: 1 },
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedScriptLangs,
)}.`,
})
}
for (const scriptNode of scriptNodes) {
if (
!allowedScriptLangs.includes(
getLangValue(scriptNode)?.toLowerCase() ?? null,
)
) {
context.report({
node: scriptNode,
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
Expand All @@ -103,16 +111,20 @@ export default createRule("block-lang", {
})
}
}
if (scriptNode === undefined && enforceScriptPresent) {
if (styleNodes.length === 0 && enforceStylePresent) {
context.report({
loc: { line: 1, column: 1 },
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedScriptLangs,
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedStyleLangs,
)}.`,
})
}
if (!allowedStyleLangs.includes(styleLang)) {
if (styleNode !== undefined) {
for (const styleNode of styleNodes) {
if (
!allowedStyleLangs.includes(
getLangValue(styleNode)?.toLowerCase() ?? null,
)
) {
context.report({
node: styleNode,
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
Expand All @@ -121,14 +133,6 @@ export default createRule("block-lang", {
})
}
}
if (styleNode === undefined && enforceStylePresent) {
context.report({
loc: { line: 1, column: 1 },
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedStyleLangs,
)}.`,
})
}
},
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "script": ["ts"], "style": ["ts", null] }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script context="module" lang="javascript"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script context="module" lang="js"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script context="module"></script>

<script lang="ts"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script context="module"></script>

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

<style lang="ts"></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: The lang attribute of the <script> block should be "ts".
line: 1
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script context="module" lang="typescript"></script>

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