|
| 1 | +import { createRule } from "../utils" |
| 2 | +import { getLangValue } from "../utils/ast-utils" |
| 3 | +import type { |
| 4 | + SvelteScriptElement, |
| 5 | + SvelteStyleElement, |
| 6 | +} from "svelte-eslint-parser/lib/ast" |
| 7 | + |
| 8 | +export default createRule("block-lang", { |
| 9 | + meta: { |
| 10 | + docs: { |
| 11 | + description: |
| 12 | + "disallows the use of languages other than those specified in the configuration for the lang attribute of `<script>` and `<style>` blocks.", |
| 13 | + category: "Best Practices", |
| 14 | + recommended: false, |
| 15 | + }, |
| 16 | + schema: [ |
| 17 | + { |
| 18 | + type: "object", |
| 19 | + properties: { |
| 20 | + enforceScriptPresent: { |
| 21 | + type: "boolean", |
| 22 | + }, |
| 23 | + enforceStylePresent: { |
| 24 | + type: "boolean", |
| 25 | + }, |
| 26 | + script: { |
| 27 | + oneOf: [ |
| 28 | + { |
| 29 | + type: ["string", "null"], |
| 30 | + }, |
| 31 | + { |
| 32 | + type: "array", |
| 33 | + items: { |
| 34 | + type: ["string", "null"], |
| 35 | + }, |
| 36 | + minItems: 1, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + style: { |
| 41 | + oneOf: [ |
| 42 | + { |
| 43 | + type: ["string", "null"], |
| 44 | + }, |
| 45 | + { |
| 46 | + type: "array", |
| 47 | + items: { |
| 48 | + type: ["string", "null"], |
| 49 | + }, |
| 50 | + minItems: 1, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }, |
| 55 | + additionalProperties: false, |
| 56 | + }, |
| 57 | + ], |
| 58 | + messages: {}, |
| 59 | + type: "suggestion", |
| 60 | + }, |
| 61 | + create(context) { |
| 62 | + const enforceScriptPresent: boolean = |
| 63 | + context.options[0]?.enforceScriptPresent ?? false |
| 64 | + const enforceStylePresent: boolean = |
| 65 | + context.options[0]?.enforceStylePresent ?? false |
| 66 | + |
| 67 | + const scriptOption: string | null | (string | null)[] = |
| 68 | + context.options[0]?.script ?? null |
| 69 | + const allowedScriptLangs: (string | null)[] = Array.isArray(scriptOption) |
| 70 | + ? scriptOption |
| 71 | + : [scriptOption] |
| 72 | + let scriptLang: string | null = null |
| 73 | + let scriptNode: SvelteScriptElement | undefined = undefined |
| 74 | + |
| 75 | + const styleOption: string | null | (string | null)[] = |
| 76 | + context.options[0]?.style ?? null |
| 77 | + const allowedStyleLangs: (string | null)[] = Array.isArray(styleOption) |
| 78 | + ? styleOption |
| 79 | + : [styleOption] |
| 80 | + let styleLang: string | null = null |
| 81 | + let styleNode: SvelteStyleElement | undefined = undefined |
| 82 | + |
| 83 | + return { |
| 84 | + SvelteScriptElement(node) { |
| 85 | + scriptNode = node |
| 86 | + scriptLang = getLangValue(node)?.toLowerCase() ?? null |
| 87 | + }, |
| 88 | + SvelteStyleElement(node) { |
| 89 | + styleNode = node |
| 90 | + styleLang = getLangValue(node)?.toLowerCase() ?? null |
| 91 | + }, |
| 92 | + "Program:exit"() { |
| 93 | + if (!allowedScriptLangs.includes(scriptLang)) { |
| 94 | + if (scriptNode !== undefined) { |
| 95 | + context.report({ |
| 96 | + node: scriptNode, |
| 97 | + message: `The lang attribute of the <script> block should be ${prettyPrintLangs( |
| 98 | + allowedScriptLangs, |
| 99 | + )}.`, |
| 100 | + }) |
| 101 | + } |
| 102 | + } |
| 103 | + if (scriptNode === undefined && enforceScriptPresent) { |
| 104 | + context.report({ |
| 105 | + loc: { line: 1, column: 1 }, |
| 106 | + message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs( |
| 107 | + allowedScriptLangs, |
| 108 | + )}.`, |
| 109 | + }) |
| 110 | + } |
| 111 | + if (!allowedStyleLangs.includes(styleLang)) { |
| 112 | + if (styleNode !== undefined) { |
| 113 | + context.report({ |
| 114 | + node: styleNode, |
| 115 | + message: `The lang attribute of the <style> block should be ${prettyPrintLangs( |
| 116 | + allowedStyleLangs, |
| 117 | + )}.`, |
| 118 | + }) |
| 119 | + } |
| 120 | + } |
| 121 | + if (styleNode === undefined && enforceStylePresent) { |
| 122 | + context.report({ |
| 123 | + loc: { line: 1, column: 1 }, |
| 124 | + message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs( |
| 125 | + allowedStyleLangs, |
| 126 | + )}.`, |
| 127 | + }) |
| 128 | + } |
| 129 | + }, |
| 130 | + } |
| 131 | + }, |
| 132 | +}) |
| 133 | + |
| 134 | +/** |
| 135 | + * Prints the list of allowed languages, with special handling of the `null` option. |
| 136 | + */ |
| 137 | +function prettyPrintLangs(langs: (string | null)[]): string { |
| 138 | + const hasNull = langs.includes(null) |
| 139 | + const nonNullLangs = langs |
| 140 | + .filter((lang) => lang !== null) |
| 141 | + .map((lang) => `"${lang}"`) |
| 142 | + if (nonNullLangs.length === 0) { |
| 143 | + // No special behaviour for `hasNull`, because that can never happen. |
| 144 | + return "omitted" |
| 145 | + } |
| 146 | + const hasNullText = hasNull ? "either omitted or " : "" |
| 147 | + const nonNullText = |
| 148 | + nonNullLangs.length === 1 |
| 149 | + ? nonNullLangs[0] |
| 150 | + : `one of ${nonNullLangs.join(", ")}` |
| 151 | + return hasNullText + nonNullText |
| 152 | +} |
0 commit comments