From 311785fdaf1384a7b9a35340da414737395fc1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Mon, 11 Jul 2022 12:41:36 +0200 Subject: [PATCH 01/20] feat: add `svelte/html-self-closing` rule --- src/rules/html-self-closing.ts | 136 ++++++++++++++++++ src/utils/rules.ts | 2 + src/utils/template-utils.ts | 23 +++ src/utils/void-elements.json | 1 + .../html-end-tags/valid/test01-input.svelte | 1 + .../invalid/component-never/_config.json | 7 + .../component-never-errors.json | 7 + .../component-never-input.svelte | 4 + .../component-never-output.svelte | 4 + .../invalid/normal-never/_config.json | 7 + .../normal-never/component-never-errors.json | 7 + .../normal-never/component-never-input.svelte | 4 + .../component-never-output.svelte | 4 + .../invalid/test01-errors.json | 12 ++ .../invalid/test01-input.svelte | 5 + .../invalid/test01-output.svelte | 5 + .../valid/test01-input.svelte | 3 + tests/src/rules/html-self-closing.ts | 12 ++ tsconfig.json | 1 + 19 files changed, 245 insertions(+) create mode 100644 src/rules/html-self-closing.ts create mode 100644 src/utils/template-utils.ts create mode 100644 src/utils/void-elements.json create mode 100644 tests/fixtures/rules/html-end-tags/valid/test01-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-errors.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte create mode 100644 tests/fixtures/rules/html-self-closing/valid/test01-input.svelte create mode 100644 tests/src/rules/html-self-closing.ts diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts new file mode 100644 index 000000000..338616e1b --- /dev/null +++ b/src/rules/html-self-closing.ts @@ -0,0 +1,136 @@ +import type { AST } from "svelte-eslint-parser" +import { createRule } from "../utils" +import { + getNodeName, + isCustomComponent, + isVoidHtmlElement, +} from "../utils/template-utils" + +enum TypeMessages { + normal = "HTML elements", + void = "HTML void elements", + component = "Svelte custom components", + unknown = "unknown elements", +} + +export default createRule("html-self-closing", { + meta: { + docs: { + description: "Enforce self-closing style", + category: "Stylistic Issues", + recommended: false, + conflictWithPrettier: true, + }, + type: "layout", + fixable: "code", + messages: { + requireClosing: "Require self-closing on {{type}}", + disallowClosing: "Disallow self-closing on {{type}}", + }, + schema: [ + { + type: "object", + properties: { + html: { + type: "object", + properties: { + normal: { + enum: ["never", "always"], + }, + component: { + enum: ["never", "always"], + }, + }, + additionalProperties: false, + }, + }, + additionalProperties: false, + }, + ], + }, + create(ctx) { + const options: { [key: string]: "never" | "always" } = { + normal: ctx.options?.[0]?.html?.normal ?? "always", + component: ctx.options?.[0]?.html?.component ?? "always", + } + + /** + * + */ + function getElementType( + node: AST.SvelteElement, + ): "component" | "void" | "normal" { + if (isCustomComponent(node)) return "component" + if (isVoidHtmlElement(node)) return "void" + return "normal" + } + + /** + * + */ + function elementTypeMessages( + type: "component" | "void" | "normal", + ): TypeMessages { + switch (type) { + case "component": + return TypeMessages.component + case "normal": + return TypeMessages.normal + case "void": + return TypeMessages.void + default: + return TypeMessages.unknown + } + } + + /** + * + */ + function report(node: AST.SvelteElement, close: boolean) { + const elementType = getElementType(node) + + ctx.report({ + node, + messageId: close ? "requireClosing" : "disallowClosing", + data: { + type: elementTypeMessages(elementType), + }, + *fix(fixer) { + if (close) { + yield fixer.insertTextBeforeRange( + [node.startTag.range[1] - 1, node.startTag.range[1]], + "/", + ) + + if (node.endTag) yield fixer.removeRange(node.endTag.range) + } else { + yield fixer.removeRange([ + node.startTag.range[1] - 2, + node.startTag.range[1] - 1, + ]) + + yield fixer.insertTextAfter(node, ``) + } + }, + }) + } + + return { + SvelteElement(node: AST.SvelteElement) { + if (node.children.length > 0) return + + const elementType = getElementType(node) + + if (elementType === "void") return + const shouldBeClosed = options[elementType] === "always" + // const hasEndTag = Boolean(node.endTag) + + if (shouldBeClosed && !node.startTag.selfClosing) { + report(node, true) + } else if (!shouldBeClosed && node.startTag.selfClosing) { + report(node, false) + } + }, + } + }, +}) diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 19fe075fd..21a52c3f9 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -3,6 +3,7 @@ import buttonHasType from "../rules/button-has-type" import commentDirective from "../rules/comment-directive" import firstAttributeLinebreak from "../rules/first-attribute-linebreak" import htmlQuotes from "../rules/html-quotes" +import htmlSelfClosing from "../rules/html-self-closing" import indent from "../rules/indent" import maxAttributesPerLine from "../rules/max-attributes-per-line" import mustacheSpacing from "../rules/mustache-spacing" @@ -33,6 +34,7 @@ export const rules = [ commentDirective, firstAttributeLinebreak, htmlQuotes, + htmlSelfClosing, indent, maxAttributesPerLine, mustacheSpacing, diff --git a/src/utils/template-utils.ts b/src/utils/template-utils.ts new file mode 100644 index 000000000..7a2d84a5c --- /dev/null +++ b/src/utils/template-utils.ts @@ -0,0 +1,23 @@ +import type { AST } from "svelte-eslint-parser" +import voidElements from "./void-elements.json" + +/** + * + */ +export function getNodeName(node: AST.SvelteElement): string { + return "name" in node.name ? node.name.name : node.name.property.name +} + +/** + * + */ +export function isCustomComponent(node: AST.SvelteElement): boolean { + return node.kind === "component" +} + +/** + * + */ +export function isVoidHtmlElement(node: AST.SvelteElement): boolean { + return voidElements.includes(getNodeName(node)) +} diff --git a/src/utils/void-elements.json b/src/utils/void-elements.json new file mode 100644 index 000000000..b737cc683 --- /dev/null +++ b/src/utils/void-elements.json @@ -0,0 +1 @@ +["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"] diff --git a/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte b/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte new file mode 100644 index 000000000..8b60f7965 --- /dev/null +++ b/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte @@ -0,0 +1 @@ +

This should be valid

diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json new file mode 100644 index 000000000..90d2b8ac1 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json @@ -0,0 +1,7 @@ +{ + "options": [{ + "html": { + "component": "never" + } + }] +} diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json new file mode 100644 index 000000000..0693ceb6b --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "Disallow self-closing on Svelte custom components", + "line": 3, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte new file mode 100644 index 000000000..6c888d0bc --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte @@ -0,0 +1,4 @@ + +
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte new file mode 100644 index 000000000..f90849ac6 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte @@ -0,0 +1,4 @@ + +
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json new file mode 100644 index 000000000..b1bde9a13 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json @@ -0,0 +1,7 @@ +{ + "options": [{ + "html": { + "normal": "never" + } + }] +} diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json new file mode 100644 index 000000000..01fdd8191 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "Disallow self-closing on HTML elements", + "line": 3, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte new file mode 100644 index 000000000..5c8a2cf01 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte @@ -0,0 +1,4 @@ + +
+
+
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte new file mode 100644 index 000000000..a45145e01 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte @@ -0,0 +1,4 @@ + +
+
+
diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json b/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json new file mode 100644 index 000000000..4a251aeaa --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json @@ -0,0 +1,12 @@ +[ + { + "message": "Require self-closing on HTML elements", + "line": 3, + "column": 3 + }, + { + "message": "Require self-closing on Svelte custom components", + "line": 4, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte new file mode 100644 index 000000000..38285e508 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte @@ -0,0 +1,5 @@ + +
+
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte new file mode 100644 index 000000000..51af2156b --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte @@ -0,0 +1,5 @@ + +
+
+ +
diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte new file mode 100644 index 000000000..0e85f49ad --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte @@ -0,0 +1,3 @@ +
+
+
diff --git a/tests/src/rules/html-self-closing.ts b/tests/src/rules/html-self-closing.ts new file mode 100644 index 000000000..6890e9087 --- /dev/null +++ b/tests/src/rules/html-self-closing.ts @@ -0,0 +1,12 @@ +import { RuleTester } from "eslint" +import rule from "../../../src/rules/html-self-closing" +import { loadTestCases } from "../../utils/utils" + +const tester = new RuleTester({ + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, +}) + +tester.run("html-self-closing", rule as any, loadTestCases("html-self-closing")) diff --git a/tsconfig.json b/tsconfig.json index 9c5f25268..03e0f0b76 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,7 @@ // "*": ["typings/*"] // }, "esModuleInterop": true, + "resolveJsonModule": true, "outDir": "lib", "paths": { "*": ["typings/*"] From be14399ab50bf40617360c843509e192997e3304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Mon, 11 Jul 2022 22:04:42 +0200 Subject: [PATCH 02/20] chore: add .idea to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 14f844ab3..5909fbfca 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,6 @@ dist /svelte.config-dist.js /docs-svelte-kit/shim/eslint.mjs /docs-svelte-kit/shim/assert.mjs + +# Intellij IDES +.idea From b5765cbaa09ff2e4f08f9ad489a44510bb5eea9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Mon, 11 Jul 2022 22:08:46 +0200 Subject: [PATCH 03/20] chore: increase coverage --- tests/fixtures/rules/html-self-closing/valid/test01-input.svelte | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte index 0e85f49ad..aea3e7646 100644 --- a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte +++ b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte @@ -1,3 +1,4 @@
+
From a57d224a2d628f3197e478e826e2258c07781178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Mon, 11 Jul 2022 22:25:01 +0200 Subject: [PATCH 04/20] fix(html-self-closing): ignore if element children is whitespace text, fixer remove child whitespace --- src/rules/html-self-closing.ts | 22 +++++++++++++++++-- .../invalid/test01-input.svelte | 2 +- .../valid/test01-input.svelte | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index 338616e1b..23fa8a481 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -13,6 +13,7 @@ enum TypeMessages { unknown = "unknown elements", } +// TODO: add / remove closing from void elements export default createRule("html-self-closing", { meta: { docs: { @@ -83,6 +84,20 @@ export default createRule("html-self-closing", { } } + /** + * + */ + function isElementEmpty(node: AST.SvelteElement): boolean { + if (node.children.length <= 0) return true + + for (const child of node.children) { + if (child.type !== "SvelteText") return false + if (!/^\s*$/.test(child.value)) return false + } + + return true + } + /** * */ @@ -97,6 +112,10 @@ export default createRule("html-self-closing", { }, *fix(fixer) { if (close) { + for (const child of node.children) { + yield fixer.removeRange(child.range) + } + yield fixer.insertTextBeforeRange( [node.startTag.range[1] - 1, node.startTag.range[1]], "/", @@ -117,13 +136,12 @@ export default createRule("html-self-closing", { return { SvelteElement(node: AST.SvelteElement) { - if (node.children.length > 0) return + if (!isElementEmpty(node)) return const elementType = getElementType(node) if (elementType === "void") return const shouldBeClosed = options[elementType] === "always" - // const hasEndTag = Boolean(node.endTag) if (shouldBeClosed && !node.startTag.selfClosing) { report(node, true) diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte index 38285e508..a541d4d55 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte +++ b/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte @@ -1,5 +1,5 @@
- +
diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte index aea3e7646..104b5c286 100644 --- a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte +++ b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte @@ -1,4 +1,5 @@
+
hello
From 0f5c58dc25612287c7569d3311c6b16e4426d10b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Mon, 11 Jul 2022 22:40:56 +0200 Subject: [PATCH 05/20] fix(html-self-closing): add / remove closing from void elements --- src/rules/html-self-closing.ts | 23 ++++++++++++++----- .../invalid/void-always/_config.json | 7 ++++++ .../void-always/void-always-errors.json | 7 ++++++ .../void-always/void-always-input.svelte | 4 ++++ .../void-always/void-always-output.svelte | 4 ++++ .../valid/test01-input.svelte | 3 ++- 6 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index 23fa8a481..2ddb3b7b5 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -13,7 +13,6 @@ enum TypeMessages { unknown = "unknown elements", } -// TODO: add / remove closing from void elements export default createRule("html-self-closing", { meta: { docs: { @@ -35,6 +34,9 @@ export default createRule("html-self-closing", { html: { type: "object", properties: { + void: { + enum: ["never", "always"], + }, normal: { enum: ["never", "always"], }, @@ -50,7 +52,9 @@ export default createRule("html-self-closing", { ], }, create(ctx) { + const source = ctx.getSourceCode() const options: { [key: string]: "never" | "always" } = { + void: ctx.options?.[0]?.html?.void ?? "never", normal: ctx.options?.[0]?.html?.normal ?? "always", component: ctx.options?.[0]?.html?.component ?? "always", } @@ -128,7 +132,8 @@ export default createRule("html-self-closing", { node.startTag.range[1] - 1, ]) - yield fixer.insertTextAfter(node, ``) + if (!isVoidHtmlElement(node)) + yield fixer.insertTextAfter(node, ``) } }, }) @@ -140,12 +145,18 @@ export default createRule("html-self-closing", { const elementType = getElementType(node) - if (elementType === "void") return + // if (elementType === "void") return const shouldBeClosed = options[elementType] === "always" - - if (shouldBeClosed && !node.startTag.selfClosing) { + const startTagSrc = source.getText(node.startTag) + const selfClosing = + startTagSrc.slice( + Math.max(startTagSrc.length - 2, 0), + Math.max(startTagSrc.length - 1, 0), + ) === "/" + + if (shouldBeClosed && !selfClosing) { report(node, true) - } else if (!shouldBeClosed && node.startTag.selfClosing) { + } else if (!shouldBeClosed && selfClosing) { report(node, false) } }, diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json b/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json new file mode 100644 index 000000000..71ed391d4 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json @@ -0,0 +1,7 @@ +{ + "options": [{ + "html": { + "void": "always" + } + }] +} diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json new file mode 100644 index 000000000..863ebe381 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "Require self-closing on HTML void elements", + "line": 3, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte new file mode 100644 index 000000000..e7f7af063 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte @@ -0,0 +1,4 @@ + +
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte new file mode 100644 index 000000000..931bf6a39 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte @@ -0,0 +1,4 @@ + +
+ +
diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte index 104b5c286..b05316224 100644 --- a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte +++ b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte @@ -1,5 +1,6 @@ +
hello
- +
From d446688a69f2e5b799a0f8d4e4df210ced20b6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Tue, 12 Jul 2022 23:25:58 +0200 Subject: [PATCH 06/20] feat: add `svelte/no-spaces-around-equal-signs-in-attribute` rule --- src/rules/html-self-closing.ts | 11 ++- ...-spaces-around-equal-signs-in-attribute.ts | 67 +++++++++++++++++++ src/utils/rules.ts | 2 + .../component-never-errors.json | 2 +- .../normal-never/component-never-errors.json | 2 +- .../invalid/test01-errors.json | 4 +- .../void-always/void-always-errors.json | 2 +- .../invalid/test01-errors.json | 27 ++++++++ .../invalid/test01-input.svelte | 10 +++ .../invalid/test01-output.svelte | 8 +++ .../valid/test-01-input.svelte | 2 + ...-spaces-around-equal-signs-in-attribute.ts | 16 +++++ 12 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/rules/no-spaces-around-equal-signs-in-attribute.ts create mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json create mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte create mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte create mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte create mode 100644 tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index 2ddb3b7b5..65ae715f2 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -10,7 +10,7 @@ enum TypeMessages { normal = "HTML elements", void = "HTML void elements", component = "Svelte custom components", - unknown = "unknown elements", + // unknown = "unknown elements", } export default createRule("html-self-closing", { @@ -24,8 +24,8 @@ export default createRule("html-self-closing", { type: "layout", fixable: "code", messages: { - requireClosing: "Require self-closing on {{type}}", - disallowClosing: "Disallow self-closing on {{type}}", + requireClosing: "Require self-closing on {{type}}.", + disallowClosing: "Disallow self-closing on {{type}}.", }, schema: [ { @@ -79,12 +79,10 @@ export default createRule("html-self-closing", { switch (type) { case "component": return TypeMessages.component - case "normal": - return TypeMessages.normal case "void": return TypeMessages.void default: - return TypeMessages.unknown + return TypeMessages.normal } } @@ -145,7 +143,6 @@ export default createRule("html-self-closing", { const elementType = getElementType(node) - // if (elementType === "void") return const shouldBeClosed = options[elementType] === "always" const startTagSrc = source.getText(node.startTag) const selfClosing = diff --git a/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/src/rules/no-spaces-around-equal-signs-in-attribute.ts new file mode 100644 index 000000000..7579484df --- /dev/null +++ b/src/rules/no-spaces-around-equal-signs-in-attribute.ts @@ -0,0 +1,67 @@ +import { createRule } from "../utils" +import type { AST } from "svelte-eslint-parser" + +export default createRule("no-spaces-around-equal-signs-in-attribute", { + meta: { + docs: { + description: "Disallow spaces around equal signs in attribute", + category: "Stylistic Issues", + recommended: false, + conflictWithPrettier: true, + }, + schema: {}, + fixable: "code", + messages: { + noSpaces: "Unexpected spaces found around equal signs.", + }, + type: "layout", + }, + create(ctx) { + const source = ctx.getSourceCode() + + /** + * + */ + function getAttrEq(node: AST.SvelteAttribute): [string, AST.Range] { + const attrSource = source.getText(node) + const keyRange = node.key.range + const valueStart = node.value?.[0]?.range?.[0] ?? keyRange[1] + let eqSource = attrSource.slice( + keyRange[1] - keyRange[0], + valueStart - keyRange[0], + ) + + if (['"', "'"].includes(eqSource[eqSource.length - 1])) + eqSource = eqSource.slice(0, eqSource.length - 1) + return [eqSource, [keyRange[1], keyRange[1] + eqSource.length]] + } + + /** + * + */ + function containsSpaces(string: string): boolean { + return /.*\s.*/s.test(string) + } + + return { + SvelteAttribute(node: AST.SvelteAttribute) { + const [eqSource, range] = getAttrEq(node) + + if (!containsSpaces(eqSource)) return + + const loc = { + start: source.getLocFromIndex(range[0]), + end: source.getLocFromIndex(range[1] - 1), + } + + ctx.report({ + loc, + messageId: "noSpaces", + *fix(fixer) { + yield fixer.replaceTextRange(range, "=") + }, + }) + }, + } + }, +}) diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 21a52c3f9..89a38b752 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -18,6 +18,7 @@ import noObjectInTextMustaches from "../rules/no-object-in-text-mustaches" import noShorthandStylePropertyOverrides from "../rules/no-shorthand-style-property-overrides" import noTargetBlank from "../rules/no-target-blank" import noUnknownStyleDirectiveProperty from "../rules/no-unknown-style-directive-property" +import noSpacesAroundEqualSignsInAttribute from "../rules/no-spaces-around-equal-signs-in-attribute" import noUnusedSvelteIgnore from "../rules/no-unused-svelte-ignore" import noUselessMustaches from "../rules/no-useless-mustaches" import preferClassDirective from "../rules/prefer-class-directive" @@ -47,6 +48,7 @@ export const rules = [ noNotFunctionHandler, noObjectInTextMustaches, noShorthandStylePropertyOverrides, + noSpacesAroundEqualSignsInAttribute, noTargetBlank, noUnknownStyleDirectiveProperty, noUnusedSvelteIgnore, diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json index 0693ceb6b..c7692bcc9 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json @@ -1,6 +1,6 @@ [ { - "message": "Disallow self-closing on Svelte custom components", + "message": "Disallow self-closing on Svelte custom components.", "line": 3, "column": 3 } diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json index 01fdd8191..1d62f7723 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json @@ -1,6 +1,6 @@ [ { - "message": "Disallow self-closing on HTML elements", + "message": "Disallow self-closing on HTML elements.", "line": 3, "column": 3 } diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json b/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json index 4a251aeaa..0a02955f3 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json +++ b/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json @@ -1,11 +1,11 @@ [ { - "message": "Require self-closing on HTML elements", + "message": "Require self-closing on HTML elements.", "line": 3, "column": 3 }, { - "message": "Require self-closing on Svelte custom components", + "message": "Require self-closing on Svelte custom components.", "line": 4, "column": 3 } diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json index 863ebe381..b0280769a 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json @@ -1,6 +1,6 @@ [ { - "message": "Require self-closing on HTML void elements", + "message": "Require self-closing on HTML void elements.", "line": 3, "column": 3 } diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json new file mode 100644 index 000000000..cf020908a --- /dev/null +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json @@ -0,0 +1,27 @@ +[ + { + "message": "Unexpected spaces found around equal signs.", + "line": 3, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 4, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 5, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 6, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 8, + "column": 11 + } +] diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte new file mode 100644 index 000000000..c86a336d3 --- /dev/null +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte @@ -0,0 +1,10 @@ + +
+

+

+

+

+

+
diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte new file mode 100644 index 000000000..2377ac148 --- /dev/null +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte @@ -0,0 +1,8 @@ + +
+

+

+

+

+

+
diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte new file mode 100644 index 000000000..138bbb761 --- /dev/null +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte @@ -0,0 +1,2 @@ +

+

diff --git a/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts new file mode 100644 index 000000000..edd7fe8bf --- /dev/null +++ b/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts @@ -0,0 +1,16 @@ +import { RuleTester } from "eslint" +import rule from "../../../src/rules/no-spaces-around-equal-signs-in-attribute" +import { loadTestCases } from "../../utils/utils" + +const tester = new RuleTester({ + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, +}) + +tester.run( + "no-spaces-around-equal-signs-in-attribute", + rule as any, + loadTestCases("no-spaces-around-equal-signs-in-attribute"), +) From 9648598454356c7e0296c0101139c2fdfd96c29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Tue, 12 Jul 2022 23:59:21 +0200 Subject: [PATCH 07/20] fix(no-spaces-around-equal-signs-in-attribute): throw error even if there is no value (key="") --- .../no-spaces-around-equal-signs-in-attribute.ts | 10 +++++----- .../invalid/test01-errors.json | 15 +++++++++++++++ .../invalid/test01-input.svelte | 13 ++++++++----- .../invalid/test01-output.svelte | 13 ++++++++----- .../valid/test-01-input.svelte | 4 +++- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/src/rules/no-spaces-around-equal-signs-in-attribute.ts index 7579484df..60770dc9f 100644 --- a/src/rules/no-spaces-around-equal-signs-in-attribute.ts +++ b/src/rules/no-spaces-around-equal-signs-in-attribute.ts @@ -25,14 +25,14 @@ export default createRule("no-spaces-around-equal-signs-in-attribute", { function getAttrEq(node: AST.SvelteAttribute): [string, AST.Range] { const attrSource = source.getText(node) const keyRange = node.key.range - const valueStart = node.value?.[0]?.range?.[0] ?? keyRange[1] - let eqSource = attrSource.slice( + const index = + /[^\s=]/.exec(attrSource.slice(keyRange[1] - keyRange[0]))?.index ?? 0 + const valueStart = keyRange[1] + index + const eqSource = attrSource.slice( keyRange[1] - keyRange[0], valueStart - keyRange[0], ) - if (['"', "'"].includes(eqSource[eqSource.length - 1])) - eqSource = eqSource.slice(0, eqSource.length - 1) return [eqSource, [keyRange[1], keyRange[1] + eqSource.length]] } @@ -51,7 +51,7 @@ export default createRule("no-spaces-around-equal-signs-in-attribute", { const loc = { start: source.getLocFromIndex(range[0]), - end: source.getLocFromIndex(range[1] - 1), + end: source.getLocFromIndex(range[1]), } ctx.report({ diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json index cf020908a..ec98b89ad 100644 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json @@ -23,5 +23,20 @@ "message": "Unexpected spaces found around equal signs.", "line": 8, "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 10, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 11, + "column": 11 + }, + { + "message": "Unexpected spaces found around equal signs.", + "line": 12, + "column": 11 } ] diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte index c86a336d3..eabc42f5d 100644 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte @@ -1,10 +1,13 @@

-

-

-

+

+

+

+ "l">

+ = "o">

+

+

+

diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte index 2377ac148..c16cfc843 100644 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte @@ -1,8 +1,11 @@
-

-

-

-

-

+

+

+

+

+

+

+

+

diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte index 138bbb761..86dd60bf0 100644 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte +++ b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte @@ -1,2 +1,4 @@ -

+

+ +

From c0952e32025d5e520a13071e016398cd3023556c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Wed, 13 Jul 2022 00:07:27 +0200 Subject: [PATCH 08/20] chore(html-self-closing): naming changes --- src/rules/html-self-closing.ts | 41 ++++++++++++++-------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index 65ae715f2..fc4ed9ae0 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -6,13 +6,14 @@ import { isVoidHtmlElement, } from "../utils/template-utils" -enum TypeMessages { - normal = "HTML elements", - void = "HTML void elements", - component = "Svelte custom components", - // unknown = "unknown elements", +const TYPE_MESSAGES = { + normal: "HTML elements", + void: "HTML void elements", + component: "Svelte custom components", } +type ElementTypes = "normal" | "void" | "component" + export default createRule("html-self-closing", { meta: { docs: { @@ -53,18 +54,19 @@ export default createRule("html-self-closing", { }, create(ctx) { const source = ctx.getSourceCode() - const options: { [key: string]: "never" | "always" } = { - void: ctx.options?.[0]?.html?.void ?? "never", - normal: ctx.options?.[0]?.html?.normal ?? "always", - component: ctx.options?.[0]?.html?.component ?? "always", + const options = { + html: { + void: "never", + normal: "always", + component: "always", + }, + ...ctx.options?.[0], } /** * */ - function getElementType( - node: AST.SvelteElement, - ): "component" | "void" | "normal" { + function getElementType(node: AST.SvelteElement): ElementTypes { if (isCustomComponent(node)) return "component" if (isVoidHtmlElement(node)) return "void" return "normal" @@ -73,17 +75,8 @@ export default createRule("html-self-closing", { /** * */ - function elementTypeMessages( - type: "component" | "void" | "normal", - ): TypeMessages { - switch (type) { - case "component": - return TypeMessages.component - case "void": - return TypeMessages.void - default: - return TypeMessages.normal - } + function elementTypeMessages(type: ElementTypes): string { + return TYPE_MESSAGES[type] } /** @@ -143,7 +136,7 @@ export default createRule("html-self-closing", { const elementType = getElementType(node) - const shouldBeClosed = options[elementType] === "always" + const shouldBeClosed = options.html[elementType] === "always" const startTagSrc = source.getText(node.startTag) const selfClosing = startTagSrc.slice( From b51e80865b02c23fab698db799fecb619af1d0d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Wed, 13 Jul 2022 00:19:28 +0200 Subject: [PATCH 09/20] docs: add missing jsdoc content --- src/rules/html-self-closing.ts | 18 +++++++----------- ...o-spaces-around-equal-signs-in-attribute.ts | 8 ++++---- src/utils/template-utils.ts | 7 ++++--- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index fc4ed9ae0..2e4802d49 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -64,7 +64,10 @@ export default createRule("html-self-closing", { } /** - * + * Get SvelteElement type. + * If element is custom component "component" is returned + * If element is void element "void" is returned + * otherwise "normal" is returned */ function getElementType(node: AST.SvelteElement): ElementTypes { if (isCustomComponent(node)) return "component" @@ -73,14 +76,7 @@ export default createRule("html-self-closing", { } /** - * - */ - function elementTypeMessages(type: ElementTypes): string { - return TYPE_MESSAGES[type] - } - - /** - * + * Returns true if element has no children, or has only whitespace text */ function isElementEmpty(node: AST.SvelteElement): boolean { if (node.children.length <= 0) return true @@ -94,7 +90,7 @@ export default createRule("html-self-closing", { } /** - * + * Report */ function report(node: AST.SvelteElement, close: boolean) { const elementType = getElementType(node) @@ -103,7 +99,7 @@ export default createRule("html-self-closing", { node, messageId: close ? "requireClosing" : "disallowClosing", data: { - type: elementTypeMessages(elementType), + type: TYPE_MESSAGES[elementType], }, *fix(fixer) { if (close) { diff --git a/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/src/rules/no-spaces-around-equal-signs-in-attribute.ts index 60770dc9f..5cae34d8e 100644 --- a/src/rules/no-spaces-around-equal-signs-in-attribute.ts +++ b/src/rules/no-spaces-around-equal-signs-in-attribute.ts @@ -20,7 +20,7 @@ export default createRule("no-spaces-around-equal-signs-in-attribute", { const source = ctx.getSourceCode() /** - * + * Returns source text between attribute key and value, and range of that source */ function getAttrEq(node: AST.SvelteAttribute): [string, AST.Range] { const attrSource = source.getText(node) @@ -37,9 +37,9 @@ export default createRule("no-spaces-around-equal-signs-in-attribute", { } /** - * + * Returns true if string contains whitespace characters */ - function containsSpaces(string: string): boolean { + function containsWhitespace(string: string): boolean { return /.*\s.*/s.test(string) } @@ -47,7 +47,7 @@ export default createRule("no-spaces-around-equal-signs-in-attribute", { SvelteAttribute(node: AST.SvelteAttribute) { const [eqSource, range] = getAttrEq(node) - if (!containsSpaces(eqSource)) return + if (!containsWhitespace(eqSource)) return const loc = { start: source.getLocFromIndex(range[0]), diff --git a/src/utils/template-utils.ts b/src/utils/template-utils.ts index 7a2d84a5c..6b4b7f712 100644 --- a/src/utils/template-utils.ts +++ b/src/utils/template-utils.ts @@ -2,21 +2,22 @@ import type { AST } from "svelte-eslint-parser" import voidElements from "./void-elements.json" /** - * + * Returns name of SvelteElement */ export function getNodeName(node: AST.SvelteElement): string { return "name" in node.name ? node.name.name : node.name.property.name } /** - * + * Returns true if Element is custom component */ export function isCustomComponent(node: AST.SvelteElement): boolean { return node.kind === "component" } /** - * + * Returns true if element is known void element + * {@link https://developer.mozilla.org/en-US/docs/Glossary/Empty_element} */ export function isVoidHtmlElement(node: AST.SvelteElement): boolean { return voidElements.includes(getNodeName(node)) From 31667c2dc1f85deae9fbad6badc87b7b85705dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Wed, 13 Jul 2022 10:39:38 +0200 Subject: [PATCH 10/20] chore: add "any" option to html-self-closing, add rules documentation --- README.md | 2 + docs-svelte-kit/.eslintrc.cjs | 1 - docs-svelte-kit/build-system/src/eslint.mjs | 2 +- docs-svelte-kit/tools/highlight.mjs | 2 +- docs/rules.md | 2 + docs/rules/html-self-closing.md | 84 +++++++++++++++++++ ...-spaces-around-equal-signs-in-attribute.md | 62 ++++++++++++++ src/configs/prettier.ts | 2 + src/rules/html-self-closing.ts | 10 ++- src/utils/rules.ts | 2 +- src/utils/void-elements.json | 19 ++++- .../invalid/component-never/_config.json | 10 ++- .../invalid/normal-any/_config.json | 9 ++ .../invalid/normal-any/normal-any-errors.json | 7 ++ .../normal-any/normal-any-input.svelte | 6 ++ .../normal-any/normal-any-output.svelte | 6 ++ .../invalid/normal-never/_config.json | 10 ++- .../invalid/void-always/_config.json | 10 ++- vite.config.mjs | 2 +- 19 files changed, 226 insertions(+), 22 deletions(-) create mode 100644 docs/rules/html-self-closing.md create mode 100644 docs/rules/no-spaces-around-equal-signs-in-attribute.md create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte create mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte diff --git a/README.md b/README.md index b2e746eeb..6e26ab682 100644 --- a/README.md +++ b/README.md @@ -293,9 +293,11 @@ These rules relate to style guidelines, and are therefore quite subjective: |:--------|:------------|:---| | [svelte/first-attribute-linebreak](https://ota-meshi.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: | | [svelte/html-quotes](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-quotes/) | enforce quotes style of HTML attributes | :wrench: | +| [svelte/html-self-closing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-self-closing/) | Enforce self-closing style | :wrench: | | [svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent/) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](https://ota-meshi.github.io/eslint-plugin-svelte/rules/max-attributes-per-line/) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/mustache-spacing/) | enforce unified spacing in mustache | :wrench: | +| [svelte/no-spaces-around-equal-signs-in-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/) | Disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-class-directive/) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-style-directive/) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/shorthand-attribute/) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs-svelte-kit/.eslintrc.cjs b/docs-svelte-kit/.eslintrc.cjs index bddda3031..ae0a82f84 100644 --- a/docs-svelte-kit/.eslintrc.cjs +++ b/docs-svelte-kit/.eslintrc.cjs @@ -1,4 +1,3 @@ -// eslint-disable-next-line no-undef -- ignore module.exports = { extends: ["plugin:svelte/recommended"], env: { diff --git a/docs-svelte-kit/build-system/src/eslint.mjs b/docs-svelte-kit/build-system/src/eslint.mjs index 636e4ebae..11887b201 100644 --- a/docs-svelte-kit/build-system/src/eslint.mjs +++ b/docs-svelte-kit/build-system/src/eslint.mjs @@ -1,6 +1,6 @@ /* eslint require-jsdoc:0 -- shim */ -import * as all from "../../../node_modules/eslint/lib/linter/linter.js" +import * as all from "../../../node_modules/eslint/lib/linter/linter" const Linter = all.Linter export { Linter } export default { Linter } diff --git a/docs-svelte-kit/tools/highlight.mjs b/docs-svelte-kit/tools/highlight.mjs index 0734c43ce..a2ee4755e 100644 --- a/docs-svelte-kit/tools/highlight.mjs +++ b/docs-svelte-kit/tools/highlight.mjs @@ -1,5 +1,5 @@ import prism from "prismjs" -import loadLanguages from "prismjs/components/index.js" +import loadLanguages from "prismjs/components/index" import escapeHtml from "escape-html" import "prism-svelte" diff --git a/docs/rules.md b/docs/rules.md index 70a940c90..12194ca80 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -53,9 +53,11 @@ These rules relate to style guidelines, and are therefore quite subjective: |:--------|:------------|:---| | [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: | | [svelte/html-quotes](./rules/html-quotes.md) | enforce quotes style of HTML attributes | :wrench: | +| [svelte/html-self-closing](./rules/html-self-closing.md) | Enforce self-closing style | :wrench: | | [svelte/indent](./rules/indent.md) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](./rules/max-attributes-per-line.md) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](./rules/mustache-spacing.md) | enforce unified spacing in mustache | :wrench: | +| [svelte/no-spaces-around-equal-signs-in-attribute](./rules/no-spaces-around-equal-signs-in-attribute.md) | Disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](./rules/prefer-class-directive.md) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](./rules/prefer-style-directive.md) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](./rules/shorthand-attribute.md) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md new file mode 100644 index 000000000..7352ea7c2 --- /dev/null +++ b/docs/rules/html-self-closing.md @@ -0,0 +1,84 @@ +--- +pageClass: "rule-details" +sidebarDepth: 0 +title: "svelte/html-self-closing" +description: "Enforce self-closing style" +since: "v2.2.0" +--- + +# svelte/html-self-closing + +> Enforce self-closing style + +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. + +## :book: Rule Details + +You can choose either two styles for elements without content + +- always: `` +- never: `
` + +This rule enforces the quotes style of HTML attributes. + + + + + + +```svelte + + + +
+

Hello

+
+ + + +
+

+
+ +``` + + + + + +## :wrench: Options + +```json +{ + "svelte/html-self-closing": [ + "error", + { + "html": { + "void": "never", // or "always" or "any" + "normal": "always", // or "never" or "any" + "custom": "always" // or "never" or "any" + } + } + ] +} +``` + +- `html.void` (`"never"` by default)... Style of HTML void elements +- `html.component` (`"always"` by default)... Style of svelte components +- `html.normal` (`"always"` by default)... Style of other elements + +Every option can be set to +- "always" (`
`) +- "never" (`
`) +- "any" (either `
` or `
`) + +## :rocket: Version + +This rule was introduced in eslint-plugin-svelte v2.2.0 + +## :mag: Implementation + +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/html-self-closing.ts) +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/html-self-closing.ts) diff --git a/docs/rules/no-spaces-around-equal-signs-in-attribute.md b/docs/rules/no-spaces-around-equal-signs-in-attribute.md new file mode 100644 index 000000000..7f7b10ff0 --- /dev/null +++ b/docs/rules/no-spaces-around-equal-signs-in-attribute.md @@ -0,0 +1,62 @@ +--- +pageClass: "rule-details" +sidebarDepth: 0 +title: "svelte/no-spaces-around-equal-signs-in-attribute" +description: "Disallow spaces around equal signs in attribute" +since: "v2.2.0" +--- + +# svelte/no-spaces-around-equal-signs-in-attribute + +> Disallow spaces around equal signs in attribute + +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. + +## :book: Rule Details + +This rule disallows spaces around equal signs in attributes + + + + + + +```svelte + + + +
+

hi

+A photo of a very cute {animal} + + +
+

hi

+A photo of a very cute {animal} + + + +## :wrench: Options + +```json +{ + "svelte/no-spaces-around-equal-signs-in-attribute": ["error"] +} +``` + +## :rocket: Version + +This rule was introduced in eslint-plugin-svelte v2.2.0 + +## :mag: Implementation + +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-spaces-around-equal-signs-in-attribute.ts) +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts) diff --git a/src/configs/prettier.ts b/src/configs/prettier.ts index 6c0eba387..8380da4a4 100644 --- a/src/configs/prettier.ts +++ b/src/configs/prettier.ts @@ -8,9 +8,11 @@ export = { // eslint-plugin-svelte rules "svelte/first-attribute-linebreak": "off", "svelte/html-quotes": "off", + "svelte/html-self-closing": "off", "svelte/indent": "off", "svelte/max-attributes-per-line": "off", "svelte/mustache-spacing": "off", + "svelte/no-spaces-around-equal-signs-in-attribute": "off", "svelte/shorthand-attribute": "off", "svelte/shorthand-directive": "off", }, diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index 2e4802d49..c97fda778 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -36,13 +36,13 @@ export default createRule("html-self-closing", { type: "object", properties: { void: { - enum: ["never", "always"], + enum: ["never", "always", "any"], }, normal: { - enum: ["never", "always"], + enum: ["never", "always", "any"], }, component: { - enum: ["never", "always"], + enum: ["never", "always", "any"], }, }, additionalProperties: false, @@ -132,7 +132,9 @@ export default createRule("html-self-closing", { const elementType = getElementType(node) - const shouldBeClosed = options.html[elementType] === "always" + const elementTypeOptions = options.html[elementType] + if (elementTypeOptions === "any") return + const shouldBeClosed = elementTypeOptions === "always" const startTagSrc = source.getText(node.startTag) const selfClosing = startTagSrc.slice( diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 89a38b752..a45470ab3 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -16,9 +16,9 @@ import noInnerDeclarations from "../rules/no-inner-declarations" import noNotFunctionHandler from "../rules/no-not-function-handler" import noObjectInTextMustaches from "../rules/no-object-in-text-mustaches" import noShorthandStylePropertyOverrides from "../rules/no-shorthand-style-property-overrides" +import noSpacesAroundEqualSignsInAttribute from "../rules/no-spaces-around-equal-signs-in-attribute" import noTargetBlank from "../rules/no-target-blank" import noUnknownStyleDirectiveProperty from "../rules/no-unknown-style-directive-property" -import noSpacesAroundEqualSignsInAttribute from "../rules/no-spaces-around-equal-signs-in-attribute" import noUnusedSvelteIgnore from "../rules/no-unused-svelte-ignore" import noUselessMustaches from "../rules/no-useless-mustaches" import preferClassDirective from "../rules/prefer-class-directive" diff --git a/src/utils/void-elements.json b/src/utils/void-elements.json index b737cc683..a24e32f3d 100644 --- a/src/utils/void-elements.json +++ b/src/utils/void-elements.json @@ -1 +1,18 @@ -["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"] +[ + "area", + "base", + "br", + "col", + "embed", + "hr", + "img", + "input", + "keygen", + "link", + "menuitem", + "meta", + "param", + "source", + "track", + "wbr" +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json index 90d2b8ac1..5c198116d 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json +++ b/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json @@ -1,7 +1,9 @@ { - "options": [{ - "html": { - "component": "never" + "options": [ + { + "html": { + "component": "never" + } } - }] + ] } diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json new file mode 100644 index 000000000..62c435eb0 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json @@ -0,0 +1,9 @@ +{ + "options": [ + { + "html": { + "normal": "any" + } + } + ] +} diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json new file mode 100644 index 000000000..95f4dfb19 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "Disallow self-closing on HTML void elements.", + "line": 5, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte new file mode 100644 index 000000000..c195a36c1 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte @@ -0,0 +1,6 @@ + +
+
+
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte new file mode 100644 index 000000000..66e0cd246 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte @@ -0,0 +1,6 @@ + +
+
+
+ +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json index b1bde9a13..53bea34aa 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json @@ -1,7 +1,9 @@ { - "options": [{ - "html": { - "normal": "never" + "options": [ + { + "html": { + "normal": "never" + } } - }] + ] } diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json b/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json index 71ed391d4..7107f6f25 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json +++ b/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json @@ -1,7 +1,9 @@ { - "options": [{ - "html": { - "void": "always" + "options": [ + { + "html": { + "void": "always" + } } - }] + ] } diff --git a/vite.config.mjs b/vite.config.mjs index a9c730e15..bf9d1048a 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -4,7 +4,7 @@ import path from "path" import svelteMd from "vite-plugin-svelte-md" import svelteMdOption from "./docs-svelte-kit/tools/vite-plugin-svelte-md-option.mjs" -import "./docs-svelte-kit/build-system/build.js" +import "./docs-svelte-kit/build-system/build" const dirname = path.dirname(new URL(import.meta.url).pathname) From f230ff9fffa184f30e6b969dfe3e57807ca8887f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Fri, 15 Jul 2022 10:08:05 +0200 Subject: [PATCH 11/20] fix: since version in `html-self-closing` and `no-spaces-around-equal-signs-in-attribute` --- docs/rules/html-self-closing.md | 4 ++-- docs/rules/no-spaces-around-equal-signs-in-attribute.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md index 7352ea7c2..5df2c1de8 100644 --- a/docs/rules/html-self-closing.md +++ b/docs/rules/html-self-closing.md @@ -3,7 +3,7 @@ pageClass: "rule-details" sidebarDepth: 0 title: "svelte/html-self-closing" description: "Enforce self-closing style" -since: "v2.2.0" +since: "v2.3.0" --- # svelte/html-self-closing @@ -76,7 +76,7 @@ Every option can be set to ## :rocket: Version -This rule was introduced in eslint-plugin-svelte v2.2.0 +This rule was introduced in eslint-plugin-svelte v2.3.0 ## :mag: Implementation diff --git a/docs/rules/no-spaces-around-equal-signs-in-attribute.md b/docs/rules/no-spaces-around-equal-signs-in-attribute.md index 001247eb6..d332d61e2 100644 --- a/docs/rules/no-spaces-around-equal-signs-in-attribute.md +++ b/docs/rules/no-spaces-around-equal-signs-in-attribute.md @@ -3,7 +3,7 @@ pageClass: "rule-details" sidebarDepth: 0 title: "svelte/no-spaces-around-equal-signs-in-attribute" description: "Disallow spaces around equal signs in attribute" -since: "v2.2.0" +since: "v2.3.0" --- # svelte/no-spaces-around-equal-signs-in-attribute @@ -53,7 +53,7 @@ This rule disallows spaces around equal signs in attributes ## :rocket: Version -This rule was introduced in eslint-plugin-svelte v2.2.0 +This rule was introduced in eslint-plugin-svelte v2.3.0 ## :mag: Implementation From aae3c93e849276f4104cfe42eacfbcd14b2ce32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Fri, 15 Jul 2022 11:14:54 +0200 Subject: [PATCH 12/20] feat: add `svelte/html-closing-bracket-spacing` rule --- src/rules/html-closing-bracket-spacing.ts | 127 ++++++++++++++++++ src/utils/rules.ts | 2 + .../invalid/closing-any/_config.json | 5 + .../closing-any/closing-any-errors.json | 12 ++ .../closing-any/closing-any-input.svelte | 5 + .../closing-any/closing-any-output.svelte | 5 + .../invalid/end-any/_config.json | 5 + .../invalid/end-any/end-any-errors.json | 12 ++ .../invalid/end-any/end-any-input.svelte | 5 + .../invalid/end-any/end-any-output.svelte | 5 + .../invalid/start-any/_config.json | 5 + .../invalid/start-any/start-any-errors.json | 12 ++ .../invalid/start-any/start-any-input.svelte | 5 + .../invalid/start-any/start-any-output.svelte | 5 + .../invalid/test-01-errors.json | 17 +++ .../invalid/test-01-input.svelte | 10 ++ .../invalid/test-01-output.svelte | 10 ++ .../valid/test-01-input.svelte | 11 ++ .../src/rules/html-closing-bracket-spacing.ts | 16 +++ 19 files changed, 274 insertions(+) create mode 100644 src/rules/html-closing-bracket-spacing.ts create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-errors.json create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-input.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-output.svelte create mode 100644 tests/fixtures/rules/html-closing-bracket-spacing/valid/test-01-input.svelte create mode 100644 tests/src/rules/html-closing-bracket-spacing.ts diff --git a/src/rules/html-closing-bracket-spacing.ts b/src/rules/html-closing-bracket-spacing.ts new file mode 100644 index 000000000..5d24c8db9 --- /dev/null +++ b/src/rules/html-closing-bracket-spacing.ts @@ -0,0 +1,127 @@ +import { createRule } from "../utils" +import type { AST } from "svelte-eslint-parser" + +export default createRule("html-closing-bracket-spacing", { + meta: { + docs: { + description: "Require or disallow a space before tag's closing brackets", + category: "Stylistic Issues", + conflictWithPrettier: true, + recommended: false, + }, + schema: [ + { + type: "object", + properties: { + startTag: { + enum: ["always", "never", "any"], + }, + endTag: { + enum: ["always", "never", "any"], + }, + selfClosingTag: { + enum: ["always", "never", "any"], + }, + }, + additionalProperties: false, + }, + ], + messages: { + expectedSpace: "Expected space before '>', but not found.", + unexpectedSpace: "Expected no space before '>', but found.", + }, + fixable: "code", + type: "layout", + }, + create(ctx) { + const options = { + startTag: "never", + endTag: "never", + selfClosingTag: "always", + ...ctx.options[0], + } + const src = ctx.getSourceCode() + + /** + * Returns true if string contains newline characters + */ + function containsNewline(string: string): boolean { + return /.*\n.*/s.test(string) + } + + /** + * Returns true if string contains whitespace characters + */ + function containsWhitespace(string: string): boolean { + return /.*\s.*/s.test(string) + } + + /** + * Report + */ + function report( + node: AST.SvelteStartTag | AST.SvelteEndTag, + shouldHave: boolean, + ) { + const tagSrc = src.getText(node) + const match = /(\s*)\/?>$/m.exec(tagSrc) + + const end = node.range[1] + const start = node.range[1] - match![0].length + const loc = { + start: src.getLocFromIndex(start), + end: src.getLocFromIndex(end), + } + + ctx.report({ + loc, + messageId: shouldHave ? "expectedSpace" : "unexpectedSpace", + *fix(fixer) { + if (shouldHave) { + yield fixer.insertTextBeforeRange([start, end], " ") + } else { + const spaces = match![1] + + yield fixer.removeRange([start, start + spaces.length]) + } + }, + }) + } + + return { + "SvelteStartTag, SvelteEndTag"( + node: AST.SvelteStartTag | AST.SvelteEndTag, + ) { + if (node.type === "SvelteEndTag" && options.endTag === "any") return + + const tagSrc = src.getText(node) + const selfClosing = + tagSrc.slice( + Math.max(tagSrc.length - 2, 0), + Math.max(tagSrc.length - 1, 0), + ) === "/" + + const tagType = + node.type === "SvelteEndTag" + ? "endTag" + : selfClosing + ? "selfClosingTag" + : "startTag" + + if (node.type === "SvelteStartTag" && options[tagType] === "any") return + + const match = /(\s*)\/?>$/m.exec(tagSrc) + if (containsNewline(match![1])) return + + if (options[tagType] === "always" && !containsWhitespace(match![1])) { + report(node, true) + } else if ( + options[tagType] === "never" && + containsWhitespace(match![1]) + ) { + report(node, false) + } + }, + } + }, +}) diff --git a/src/utils/rules.ts b/src/utils/rules.ts index a45470ab3..41351bfc3 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -2,6 +2,7 @@ import type { RuleModule } from "../types" import buttonHasType from "../rules/button-has-type" import commentDirective from "../rules/comment-directive" import firstAttributeLinebreak from "../rules/first-attribute-linebreak" +import htmlClosingBracketSpacing from "../rules/html-closing-bracket-spacing" import htmlQuotes from "../rules/html-quotes" import htmlSelfClosing from "../rules/html-self-closing" import indent from "../rules/indent" @@ -34,6 +35,7 @@ export const rules = [ buttonHasType, commentDirective, firstAttributeLinebreak, + htmlClosingBracketSpacing, htmlQuotes, htmlSelfClosing, indent, diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json new file mode 100644 index 000000000..e8d542d0b --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json @@ -0,0 +1,5 @@ +{ + "options": [{ + "selfClosingTag": "any" + }] +} diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json new file mode 100644 index 000000000..8e0d94be0 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json @@ -0,0 +1,12 @@ +[ + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 3 + }, + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 14 + } +] diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte new file mode 100644 index 000000000..0aa1e17af --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte @@ -0,0 +1,5 @@ + +

Hello

+ +
+
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte new file mode 100644 index 000000000..ee4523394 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte @@ -0,0 +1,5 @@ + +

Hello

+ +
+
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json new file mode 100644 index 000000000..4b250246c --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json @@ -0,0 +1,5 @@ +{ + "options": [{ + "endTag": "any" + }] +} diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json new file mode 100644 index 000000000..130ab5d28 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json @@ -0,0 +1,12 @@ +[ + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 3 + }, + { + "message": "Expected space before '>', but not found.", + "line": 5, + "column": 5 + } +] diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte new file mode 100644 index 000000000..97408e4d3 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte @@ -0,0 +1,5 @@ + +

Hello

+

Hi

+ +
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte new file mode 100644 index 000000000..8b8916c1b --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte @@ -0,0 +1,5 @@ + +

Hello

+

Hi

+ +
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json new file mode 100644 index 000000000..ac2bda1a9 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json @@ -0,0 +1,5 @@ +{ + "options": [{ + "startTag": "any" + }] +} diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json new file mode 100644 index 000000000..bb1c24ac3 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json @@ -0,0 +1,12 @@ +[ + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 14 + }, + { + "message": "Expected space before '>', but not found.", + "line": 5, + "column": 5 + } +] diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte new file mode 100644 index 000000000..97408e4d3 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte @@ -0,0 +1,5 @@ + +

Hello

+

Hi

+ +
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte new file mode 100644 index 000000000..644df5de0 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte @@ -0,0 +1,5 @@ + +

Hello

+

Hi

+ +
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-errors.json new file mode 100644 index 000000000..bababc39e --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-errors.json @@ -0,0 +1,17 @@ +[ + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 3 + }, + { + "message": "Expected no space before '>', but found.", + "line": 2, + "column": 14 + }, + { + "message": "Expected space before '>', but not found.", + "line": 4, + "column": 5 + } +] diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-input.svelte new file mode 100644 index 000000000..7b0b7117d --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-input.svelte @@ -0,0 +1,10 @@ + +

Hello

+ +
+ +
+
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-output.svelte new file mode 100644 index 000000000..2cbd69667 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/test-01-output.svelte @@ -0,0 +1,10 @@ + +

Hello

+ +
+ +
+
diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/valid/test-01-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/valid/test-01-input.svelte new file mode 100644 index 000000000..e40adb0e0 --- /dev/null +++ b/tests/fixtures/rules/html-closing-bracket-spacing/valid/test-01-input.svelte @@ -0,0 +1,11 @@ +

Hello

+
+ +
+ +
+
diff --git a/tests/src/rules/html-closing-bracket-spacing.ts b/tests/src/rules/html-closing-bracket-spacing.ts new file mode 100644 index 000000000..80cbe4562 --- /dev/null +++ b/tests/src/rules/html-closing-bracket-spacing.ts @@ -0,0 +1,16 @@ +import { RuleTester } from "eslint" +import rule from "../../../src/rules/html-closing-bracket-spacing" +import { loadTestCases } from "../../utils/utils" + +const tester = new RuleTester({ + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, +}) + +tester.run( + "html-closing-bracket-spacing", + rule as any, + loadTestCases("html-closing-bracket-spacing"), +) From 4ba5ffa3ccfaaf36f63eda55e16695d74b2f58ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Fri, 15 Jul 2022 11:24:17 +0200 Subject: [PATCH 13/20] docs: add `html-closing-bracket-spacing` docs --- README.md | 1 + docs/rules.md | 1 + docs/rules/html-closing-bracket-spacing.md | 81 ++++++++++++++++++++++ docs/rules/html-self-closing.md | 4 +- src/configs/prettier.ts | 1 + 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 docs/rules/html-closing-bracket-spacing.md diff --git a/README.md b/README.md index 6e26ab682..67af1c5e6 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,7 @@ These rules relate to style guidelines, and are therefore quite subjective: | Rule ID | Description | | |:--------|:------------|:---| | [svelte/first-attribute-linebreak](https://ota-meshi.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: | +| [svelte/html-closing-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | Require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-quotes/) | enforce quotes style of HTML attributes | :wrench: | | [svelte/html-self-closing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-self-closing/) | Enforce self-closing style | :wrench: | | [svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent/) | enforce consistent indentation | :wrench: | diff --git a/docs/rules.md b/docs/rules.md index 12194ca80..5dced2976 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -52,6 +52,7 @@ These rules relate to style guidelines, and are therefore quite subjective: | Rule ID | Description | | |:--------|:------------|:---| | [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: | +| [svelte/html-closing-bracket-spacing](./rules/html-closing-bracket-spacing.md) | Require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](./rules/html-quotes.md) | enforce quotes style of HTML attributes | :wrench: | | [svelte/html-self-closing](./rules/html-self-closing.md) | Enforce self-closing style | :wrench: | | [svelte/indent](./rules/indent.md) | enforce consistent indentation | :wrench: | diff --git a/docs/rules/html-closing-bracket-spacing.md b/docs/rules/html-closing-bracket-spacing.md new file mode 100644 index 000000000..8333134fa --- /dev/null +++ b/docs/rules/html-closing-bracket-spacing.md @@ -0,0 +1,81 @@ +--- +pageClass: "rule-details" +sidebarDepth: 0 +title: "svelte/html-closing-bracket-spacing" +description: "Require or disallow a space before tag's closing brackets" +since: "v2.3.0" +--- + +# svelte/html-closing-bracket-spacing + +> Require or disallow a space before tag's closing brackets + +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. + +## :book: Rule Details + +You can choose either two styles for spacing before closing bracket + +- always: `
` +- never: `
` + + + + + + +```svelte + + + +
+

Hello

+
+
+ + +
+

Hello

+
+
+``` + + + + + +## :wrench: Options + +```json +{ + "svelte/html-closing-bracket-spacing": [ + "error", + { + "startTag": "never", // or "always" or "any" + "endTag": "never", // or "always" or "any" + "selfClosingTag": "always" // or "never" or "any" + } + ] +} +``` + +- `startTag` (`"never"` by default)... Spacing in start tags +- `endTag` (`"never"` by default)... Spacing in end tags +- `selfClosingTag` (`"always"` by default)... Spacing in self closing tags + +Every option can be set to +- "always" (`
`) +- "never" (`
`) +- "any" (either `
` or `
`) + +## :rocket: Version + +This rule was introduced in eslint-plugin-svelte v2.3.0 + +## :mag: Implementation + +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/html-closing-bracket-spacing.ts) +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/html-closing-bracket-spacing.ts) diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md index 5df2c1de8..dc2dfdef7 100644 --- a/docs/rules/html-self-closing.md +++ b/docs/rules/html-self-closing.md @@ -16,11 +16,9 @@ since: "v2.3.0" You can choose either two styles for elements without content -- always: `` +- always: `
` - never: `
` -This rule enforces the quotes style of HTML attributes. - diff --git a/src/configs/prettier.ts b/src/configs/prettier.ts index 8380da4a4..20ea0c6a7 100644 --- a/src/configs/prettier.ts +++ b/src/configs/prettier.ts @@ -7,6 +7,7 @@ export = { rules: { // eslint-plugin-svelte rules "svelte/first-attribute-linebreak": "off", + "svelte/html-closing-bracket-spacing": "off", "svelte/html-quotes": "off", "svelte/html-self-closing": "off", "svelte/indent": "off", From bfb519cdedeaee52f676b6e881996261f488e069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Sat, 16 Jul 2022 10:42:06 +0200 Subject: [PATCH 14/20] chore: lint --- .../invalid/closing-any/_config.json | 8 +++++--- .../invalid/end-any/_config.json | 8 +++++--- .../invalid/start-any/_config.json | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json index e8d542d0b..b592e46e2 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json @@ -1,5 +1,7 @@ { - "options": [{ - "selfClosingTag": "any" - }] + "options": [ + { + "selfClosingTag": "any" + } + ] } diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json index 4b250246c..3f5b8fc4b 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json @@ -1,5 +1,7 @@ { - "options": [{ - "endTag": "any" - }] + "options": [ + { + "endTag": "any" + } + ] } diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json index ac2bda1a9..a0d989410 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json @@ -1,5 +1,7 @@ { - "options": [{ - "startTag": "any" - }] + "options": [ + { + "startTag": "any" + } + ] } From 6eebfd76b57e66f2e55c2a201fb6cf284a46f78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 10:25:59 +0200 Subject: [PATCH 15/20] chore: some requested changes --- README.md | 6 ++--- docs-svelte-kit/build-system/src/eslint.mjs | 2 +- docs/rules.md | 6 ++--- docs/rules/html-closing-bracket-spacing.md | 18 ++++++--------- docs/rules/html-self-closing.md | 18 ++++++--------- ...-spaces-around-equal-signs-in-attribute.md | 10 +++------ src/rules/html-closing-bracket-spacing.ts | 22 +++++-------------- src/rules/html-self-closing.ts | 12 +++++----- ...-spaces-around-equal-signs-in-attribute.ts | 2 +- .../html-end-tags/valid/test01-input.svelte | 1 - .../_config.json | 2 +- .../normal-any-errors.json | 0 .../normal-any-input.svelte | 0 .../normal-any-output.svelte | 0 .../void-always/void-always-errors.json | 7 ------ .../{normal-any => void-never}/_config.json | 2 +- .../invalid/void-never/void-never-errors.json | 7 ++++++ .../void-never-input.svelte} | 2 +- .../void-never-output.svelte} | 2 +- .../valid/test01-input.svelte | 3 +-- 20 files changed, 49 insertions(+), 73 deletions(-) delete mode 100644 tests/fixtures/rules/html-end-tags/valid/test01-input.svelte rename tests/fixtures/rules/html-self-closing/invalid/{void-always => normal-ignore}/_config.json (68%) rename tests/fixtures/rules/html-self-closing/invalid/{normal-any => normal-ignore}/normal-any-errors.json (100%) rename tests/fixtures/rules/html-self-closing/invalid/{normal-any => normal-ignore}/normal-any-input.svelte (100%) rename tests/fixtures/rules/html-self-closing/invalid/{normal-any => normal-ignore}/normal-any-output.svelte (100%) delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json rename tests/fixtures/rules/html-self-closing/invalid/{normal-any => void-never}/_config.json (71%) create mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json rename tests/fixtures/rules/html-self-closing/invalid/{void-always/void-always-output.svelte => void-never/void-never-input.svelte} (79%) rename tests/fixtures/rules/html-self-closing/invalid/{void-always/void-always-input.svelte => void-never/void-never-output.svelte} (80%) diff --git a/README.md b/README.md index 67af1c5e6..452dc1880 100644 --- a/README.md +++ b/README.md @@ -292,13 +292,13 @@ These rules relate to style guidelines, and are therefore quite subjective: | Rule ID | Description | | |:--------|:------------|:---| | [svelte/first-attribute-linebreak](https://ota-meshi.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: | -| [svelte/html-closing-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | Require or disallow a space before tag's closing brackets | :wrench: | +| [svelte/html-closing-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-quotes/) | enforce quotes style of HTML attributes | :wrench: | -| [svelte/html-self-closing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-self-closing/) | Enforce self-closing style | :wrench: | +| [svelte/html-self-closing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-self-closing/) | enforce self-closing style | :wrench: | | [svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent/) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](https://ota-meshi.github.io/eslint-plugin-svelte/rules/max-attributes-per-line/) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/mustache-spacing/) | enforce unified spacing in mustache | :wrench: | -| [svelte/no-spaces-around-equal-signs-in-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/) | Disallow spaces around equal signs in attribute | :wrench: | +| [svelte/no-spaces-around-equal-signs-in-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/) | disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-class-directive/) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-style-directive/) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/shorthand-attribute/) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs-svelte-kit/build-system/src/eslint.mjs b/docs-svelte-kit/build-system/src/eslint.mjs index 11887b201..636e4ebae 100644 --- a/docs-svelte-kit/build-system/src/eslint.mjs +++ b/docs-svelte-kit/build-system/src/eslint.mjs @@ -1,6 +1,6 @@ /* eslint require-jsdoc:0 -- shim */ -import * as all from "../../../node_modules/eslint/lib/linter/linter" +import * as all from "../../../node_modules/eslint/lib/linter/linter.js" const Linter = all.Linter export { Linter } export default { Linter } diff --git a/docs/rules.md b/docs/rules.md index 5dced2976..a80220047 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -52,13 +52,13 @@ These rules relate to style guidelines, and are therefore quite subjective: | Rule ID | Description | | |:--------|:------------|:---| | [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: | -| [svelte/html-closing-bracket-spacing](./rules/html-closing-bracket-spacing.md) | Require or disallow a space before tag's closing brackets | :wrench: | +| [svelte/html-closing-bracket-spacing](./rules/html-closing-bracket-spacing.md) | require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](./rules/html-quotes.md) | enforce quotes style of HTML attributes | :wrench: | -| [svelte/html-self-closing](./rules/html-self-closing.md) | Enforce self-closing style | :wrench: | +| [svelte/html-self-closing](./rules/html-self-closing.md) | enforce self-closing style | :wrench: | | [svelte/indent](./rules/indent.md) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](./rules/max-attributes-per-line.md) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](./rules/mustache-spacing.md) | enforce unified spacing in mustache | :wrench: | -| [svelte/no-spaces-around-equal-signs-in-attribute](./rules/no-spaces-around-equal-signs-in-attribute.md) | Disallow spaces around equal signs in attribute | :wrench: | +| [svelte/no-spaces-around-equal-signs-in-attribute](./rules/no-spaces-around-equal-signs-in-attribute.md) | disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](./rules/prefer-class-directive.md) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](./rules/prefer-style-directive.md) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](./rules/shorthand-attribute.md) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs/rules/html-closing-bracket-spacing.md b/docs/rules/html-closing-bracket-spacing.md index 8333134fa..b514cdc05 100644 --- a/docs/rules/html-closing-bracket-spacing.md +++ b/docs/rules/html-closing-bracket-spacing.md @@ -2,14 +2,14 @@ pageClass: "rule-details" sidebarDepth: 0 title: "svelte/html-closing-bracket-spacing" -description: "Require or disallow a space before tag's closing brackets" -since: "v2.3.0" +description: "require or disallow a space before tag's closing brackets" --- # svelte/html-closing-bracket-spacing -> Require or disallow a space before tag's closing brackets +> require or disallow a space before tag's closing brackets +- :exclamation: **_This rule has not been released yet._** - :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. ## :book: Rule Details @@ -54,9 +54,9 @@ You can choose either two styles for spacing before closing bracket "svelte/html-closing-bracket-spacing": [ "error", { - "startTag": "never", // or "always" or "any" - "endTag": "never", // or "always" or "any" - "selfClosingTag": "always" // or "never" or "any" + "startTag": "never", // or "always" or "ignore" + "endTag": "never", // or "always" or "ignore" + "selfClosingTag": "always" // or "never" or "ignore" } ] } @@ -69,11 +69,7 @@ You can choose either two styles for spacing before closing bracket Every option can be set to - "always" (`
`) - "never" (`
`) -- "any" (either `
` or `
`) - -## :rocket: Version - -This rule was introduced in eslint-plugin-svelte v2.3.0 +- "ignore" (either `
` or `
`) ## :mag: Implementation diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md index dc2dfdef7..2771265b3 100644 --- a/docs/rules/html-self-closing.md +++ b/docs/rules/html-self-closing.md @@ -2,14 +2,14 @@ pageClass: "rule-details" sidebarDepth: 0 title: "svelte/html-self-closing" -description: "Enforce self-closing style" -since: "v2.3.0" +description: "enforce self-closing style" --- # svelte/html-self-closing -> Enforce self-closing style +> enforce self-closing style +- :exclamation: **_This rule has not been released yet._** - :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. ## :book: Rule Details @@ -54,9 +54,9 @@ You can choose either two styles for elements without content "error", { "html": { - "void": "never", // or "always" or "any" - "normal": "always", // or "never" or "any" - "custom": "always" // or "never" or "any" + "void": "always", // or "always" or "ignore" + "normal": "always", // or "never" or "ignore" + "component": "always" // or "never" or "ignore" } } ] @@ -70,11 +70,7 @@ You can choose either two styles for elements without content Every option can be set to - "always" (`
`) - "never" (`
`) -- "any" (either `
` or `
`) - -## :rocket: Version - -This rule was introduced in eslint-plugin-svelte v2.3.0 +- "ignore" (either `
` or `
`) ## :mag: Implementation diff --git a/docs/rules/no-spaces-around-equal-signs-in-attribute.md b/docs/rules/no-spaces-around-equal-signs-in-attribute.md index d332d61e2..f577ce873 100644 --- a/docs/rules/no-spaces-around-equal-signs-in-attribute.md +++ b/docs/rules/no-spaces-around-equal-signs-in-attribute.md @@ -2,14 +2,14 @@ pageClass: "rule-details" sidebarDepth: 0 title: "svelte/no-spaces-around-equal-signs-in-attribute" -description: "Disallow spaces around equal signs in attribute" -since: "v2.3.0" +description: "disallow spaces around equal signs in attribute" --- # svelte/no-spaces-around-equal-signs-in-attribute -> Disallow spaces around equal signs in attribute +> disallow spaces around equal signs in attribute +- :exclamation: **_This rule has not been released yet._** - :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. ## :book: Rule Details @@ -51,10 +51,6 @@ This rule disallows spaces around equal signs in attributes } ``` -## :rocket: Version - -This rule was introduced in eslint-plugin-svelte v2.3.0 - ## :mag: Implementation - [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-spaces-around-equal-signs-in-attribute.ts) diff --git a/src/rules/html-closing-bracket-spacing.ts b/src/rules/html-closing-bracket-spacing.ts index 5d24c8db9..8c823397a 100644 --- a/src/rules/html-closing-bracket-spacing.ts +++ b/src/rules/html-closing-bracket-spacing.ts @@ -4,7 +4,7 @@ import type { AST } from "svelte-eslint-parser" export default createRule("html-closing-bracket-spacing", { meta: { docs: { - description: "Require or disallow a space before tag's closing brackets", + description: "require or disallow a space before tag's closing brackets", category: "Stylistic Issues", conflictWithPrettier: true, recommended: false, @@ -46,14 +46,7 @@ export default createRule("html-closing-bracket-spacing", { * Returns true if string contains newline characters */ function containsNewline(string: string): boolean { - return /.*\n.*/s.test(string) - } - - /** - * Returns true if string contains whitespace characters - */ - function containsWhitespace(string: string): boolean { - return /.*\s.*/s.test(string) + return string.includes("\n") } /** @@ -64,7 +57,7 @@ export default createRule("html-closing-bracket-spacing", { shouldHave: boolean, ) { const tagSrc = src.getText(node) - const match = /(\s*)\/?>$/m.exec(tagSrc) + const match = /(\s*)\/?>$/.exec(tagSrc) const end = node.range[1] const start = node.range[1] - match![0].length @@ -110,15 +103,12 @@ export default createRule("html-closing-bracket-spacing", { if (node.type === "SvelteStartTag" && options[tagType] === "any") return - const match = /(\s*)\/?>$/m.exec(tagSrc) + const match = /(\s*)\/?>$/.exec(tagSrc) if (containsNewline(match![1])) return - if (options[tagType] === "always" && !containsWhitespace(match![1])) { + if (options[tagType] === "always" && !match![1]) { report(node, true) - } else if ( - options[tagType] === "never" && - containsWhitespace(match![1]) - ) { + } else if (options[tagType] === "never" && match![1]) { report(node, false) } }, diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index c97fda778..f3da83c6f 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -17,7 +17,7 @@ type ElementTypes = "normal" | "void" | "component" export default createRule("html-self-closing", { meta: { docs: { - description: "Enforce self-closing style", + description: "enforce self-closing style", category: "Stylistic Issues", recommended: false, conflictWithPrettier: true, @@ -36,13 +36,13 @@ export default createRule("html-self-closing", { type: "object", properties: { void: { - enum: ["never", "always", "any"], + enum: ["never", "always", "ignore"], }, normal: { - enum: ["never", "always", "any"], + enum: ["never", "always", "ignore"], }, component: { - enum: ["never", "always", "any"], + enum: ["never", "always", "ignore"], }, }, additionalProperties: false, @@ -56,7 +56,7 @@ export default createRule("html-self-closing", { const source = ctx.getSourceCode() const options = { html: { - void: "never", + void: "always", normal: "always", component: "always", }, @@ -133,7 +133,7 @@ export default createRule("html-self-closing", { const elementType = getElementType(node) const elementTypeOptions = options.html[elementType] - if (elementTypeOptions === "any") return + if (elementTypeOptions === "ignore") return const shouldBeClosed = elementTypeOptions === "always" const startTagSrc = source.getText(node.startTag) const selfClosing = diff --git a/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/src/rules/no-spaces-around-equal-signs-in-attribute.ts index 5cae34d8e..dcc9a06f2 100644 --- a/src/rules/no-spaces-around-equal-signs-in-attribute.ts +++ b/src/rules/no-spaces-around-equal-signs-in-attribute.ts @@ -4,7 +4,7 @@ import type { AST } from "svelte-eslint-parser" export default createRule("no-spaces-around-equal-signs-in-attribute", { meta: { docs: { - description: "Disallow spaces around equal signs in attribute", + description: "disallow spaces around equal signs in attribute", category: "Stylistic Issues", recommended: false, conflictWithPrettier: true, diff --git a/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte b/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte deleted file mode 100644 index 8b60f7965..000000000 --- a/tests/fixtures/rules/html-end-tags/valid/test01-input.svelte +++ /dev/null @@ -1 +0,0 @@ -

This should be valid

diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json similarity index 68% rename from tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json rename to tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json index 7107f6f25..8a13060cb 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/_config.json +++ b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json @@ -2,7 +2,7 @@ "options": [ { "html": { - "void": "always" + "normal": "ignore" } } ] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json similarity index 100% rename from tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-errors.json rename to tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte similarity index 100% rename from tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-input.svelte rename to tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte similarity index 100% rename from tests/fixtures/rules/html-self-closing/invalid/normal-any/normal-any-output.svelte rename to tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json b/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json deleted file mode 100644 index b0280769a..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-errors.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Require self-closing on HTML void elements.", - "line": 3, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json b/tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json similarity index 71% rename from tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json rename to tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json index 62c435eb0..4e7d9b767 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-any/_config.json +++ b/tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json @@ -2,7 +2,7 @@ "options": [ { "html": { - "normal": "any" + "void": "never" } } ] diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json new file mode 100644 index 000000000..3b61e0b15 --- /dev/null +++ b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json @@ -0,0 +1,7 @@ +[ + { + "message": "Disallow self-closing on HTML void elements.", + "line": 3, + "column": 3 + } +] diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte similarity index 79% rename from tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte rename to tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte index 931bf6a39..42ea6dbf7 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-output.svelte +++ b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte @@ -1,4 +1,4 @@
- +
diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte similarity index 80% rename from tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte rename to tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte index e7f7af063..713cf9f72 100644 --- a/tests/fixtures/rules/html-self-closing/invalid/void-always/void-always-input.svelte +++ b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte @@ -1,4 +1,4 @@
- +
diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte index b05316224..104b5c286 100644 --- a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte +++ b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte @@ -1,6 +1,5 @@ -
hello
- +
From db17e62f2d641f6e8f1c584315a285725f7cf019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 10:37:34 +0200 Subject: [PATCH 16/20] docs: fix incorrect default info --- docs/rules/html-self-closing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md index 2771265b3..d5350720f 100644 --- a/docs/rules/html-self-closing.md +++ b/docs/rules/html-self-closing.md @@ -63,7 +63,7 @@ You can choose either two styles for elements without content } ``` -- `html.void` (`"never"` by default)... Style of HTML void elements +- `html.void` (`"always"` by default)... Style of HTML void elements - `html.component` (`"always"` by default)... Style of svelte components - `html.normal` (`"always"` by default)... Style of other elements From 77e36eb0713b2d273853ca40b89e1e829e5635f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 10:57:41 +0200 Subject: [PATCH 17/20] chore: use startTag.selfClosing instead of checking for "/". Actually rename any to ignore in html-closing-bracket-spacing --- src/rules/html-closing-bracket-spacing.ts | 20 ++++++------------- src/rules/html-self-closing.ts | 13 +++--------- .../_config.json | 2 +- .../closing-ignore-errors.json} | 0 .../closing-ignore-input.svelte} | 0 .../closing-ignore-output.svelte} | 0 .../{end-any => end-ignore}/_config.json | 2 +- .../end-ignore-errors.json} | 0 .../end-ignore-input.svelte} | 0 .../end-ignore-output.svelte} | 0 .../{start-any => start-ignore}/_config.json | 2 +- .../start-ignore-errors.json} | 0 .../start-ignore-input.svelte} | 0 .../start-ignore-output.svelte} | 0 14 files changed, 12 insertions(+), 27 deletions(-) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{closing-any => closing-ignore}/_config.json (51%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{closing-any/closing-any-errors.json => closing-ignore/closing-ignore-errors.json} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{closing-any/closing-any-input.svelte => closing-ignore/closing-ignore-input.svelte} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{closing-any/closing-any-output.svelte => closing-ignore/closing-ignore-output.svelte} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{end-any => end-ignore}/_config.json (58%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{end-any/end-any-errors.json => end-ignore/end-ignore-errors.json} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{end-any/end-any-input.svelte => end-ignore/end-ignore-input.svelte} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{end-any/end-any-output.svelte => end-ignore/end-ignore-output.svelte} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{start-any => start-ignore}/_config.json (56%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{start-any/start-any-errors.json => start-ignore/start-ignore-errors.json} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{start-any/start-any-input.svelte => start-ignore/start-ignore-input.svelte} (100%) rename tests/fixtures/rules/html-closing-bracket-spacing/invalid/{start-any/start-any-output.svelte => start-ignore/start-ignore-output.svelte} (100%) diff --git a/src/rules/html-closing-bracket-spacing.ts b/src/rules/html-closing-bracket-spacing.ts index 8c823397a..82037b38c 100644 --- a/src/rules/html-closing-bracket-spacing.ts +++ b/src/rules/html-closing-bracket-spacing.ts @@ -14,13 +14,13 @@ export default createRule("html-closing-bracket-spacing", { type: "object", properties: { startTag: { - enum: ["always", "never", "any"], + enum: ["always", "never", "ignore"], }, endTag: { - enum: ["always", "never", "any"], + enum: ["always", "never", "ignore"], }, selfClosingTag: { - enum: ["always", "never", "any"], + enum: ["always", "never", "ignore"], }, }, additionalProperties: false, @@ -85,24 +85,16 @@ export default createRule("html-closing-bracket-spacing", { "SvelteStartTag, SvelteEndTag"( node: AST.SvelteStartTag | AST.SvelteEndTag, ) { - if (node.type === "SvelteEndTag" && options.endTag === "any") return - - const tagSrc = src.getText(node) - const selfClosing = - tagSrc.slice( - Math.max(tagSrc.length - 2, 0), - Math.max(tagSrc.length - 1, 0), - ) === "/" - const tagType = node.type === "SvelteEndTag" ? "endTag" - : selfClosing + : node.selfClosing ? "selfClosingTag" : "startTag" - if (node.type === "SvelteStartTag" && options[tagType] === "any") return + if (options[tagType] === "ignore") return + const tagSrc = src.getText(node) const match = /(\s*)\/?>$/.exec(tagSrc) if (containsNewline(match![1])) return diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts index f3da83c6f..af3d19c09 100644 --- a/src/rules/html-self-closing.ts +++ b/src/rules/html-self-closing.ts @@ -53,7 +53,6 @@ export default createRule("html-self-closing", { ], }, create(ctx) { - const source = ctx.getSourceCode() const options = { html: { void: "always", @@ -135,16 +134,10 @@ export default createRule("html-self-closing", { const elementTypeOptions = options.html[elementType] if (elementTypeOptions === "ignore") return const shouldBeClosed = elementTypeOptions === "always" - const startTagSrc = source.getText(node.startTag) - const selfClosing = - startTagSrc.slice( - Math.max(startTagSrc.length - 2, 0), - Math.max(startTagSrc.length - 1, 0), - ) === "/" - - if (shouldBeClosed && !selfClosing) { + + if (shouldBeClosed && !node.startTag.selfClosing) { report(node, true) - } else if (!shouldBeClosed && selfClosing) { + } else if (!shouldBeClosed && node.startTag.selfClosing) { report(node, false) } }, diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/_config.json similarity index 51% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/_config.json index b592e46e2..e5e1e4d84 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/_config.json @@ -1,7 +1,7 @@ { "options": [ { - "selfClosingTag": "any" + "selfClosingTag": "ignore" } ] } diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-errors.json similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-errors.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-errors.json diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-input.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-input.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-input.svelte diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-output.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-any/closing-any-output.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/closing-ignore/closing-ignore-output.svelte diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/_config.json similarity index 58% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/_config.json index 3f5b8fc4b..c2b607b39 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/_config.json @@ -1,7 +1,7 @@ { "options": [ { - "endTag": "any" + "endTag": "ignore" } ] } diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-errors.json similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-errors.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-errors.json diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-input.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-input.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-input.svelte diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-output.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-any/end-any-output.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/end-ignore/end-ignore-output.svelte diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/_config.json similarity index 56% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/_config.json index a0d989410..e0a9668b5 100644 --- a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/_config.json +++ b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/_config.json @@ -1,7 +1,7 @@ { "options": [ { - "startTag": "any" + "startTag": "ignore" } ] } diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-errors.json similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-errors.json rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-errors.json diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-input.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-input.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-input.svelte diff --git a/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte b/tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-output.svelte similarity index 100% rename from tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-any/start-any-output.svelte rename to tests/fixtures/rules/html-closing-bracket-spacing/invalid/start-ignore/start-ignore-output.svelte From 6fd94c3493760dcf249a0afe9df262faf9571338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 11:01:43 +0200 Subject: [PATCH 18/20] chore: remove .idea from gitignore, disable resolveJsonModule --- .gitignore | 3 --- src/utils/template-utils.ts | 2 +- src/utils/{void-elements.json => void-elements.ts} | 6 ++++-- tsconfig.json | 1 - 4 files changed, 5 insertions(+), 7 deletions(-) rename src/utils/{void-elements.json => void-elements.ts} (72%) diff --git a/.gitignore b/.gitignore index 5909fbfca..14f844ab3 100644 --- a/.gitignore +++ b/.gitignore @@ -108,6 +108,3 @@ dist /svelte.config-dist.js /docs-svelte-kit/shim/eslint.mjs /docs-svelte-kit/shim/assert.mjs - -# Intellij IDES -.idea diff --git a/src/utils/template-utils.ts b/src/utils/template-utils.ts index 6b4b7f712..2dbe1a8b6 100644 --- a/src/utils/template-utils.ts +++ b/src/utils/template-utils.ts @@ -1,5 +1,5 @@ import type { AST } from "svelte-eslint-parser" -import voidElements from "./void-elements.json" +import voidElements from "./void-elements" /** * Returns name of SvelteElement diff --git a/src/utils/void-elements.json b/src/utils/void-elements.ts similarity index 72% rename from src/utils/void-elements.json rename to src/utils/void-elements.ts index a24e32f3d..4df980dc6 100644 --- a/src/utils/void-elements.json +++ b/src/utils/void-elements.ts @@ -1,4 +1,4 @@ -[ +const voidElements = [ "area", "base", "br", @@ -14,5 +14,7 @@ "param", "source", "track", - "wbr" + "wbr", ] + +export default voidElements diff --git a/tsconfig.json b/tsconfig.json index edf35c8c3..f498baa9c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,7 +18,6 @@ // "*": ["typings/*"] // }, "esModuleInterop": true, - "resolveJsonModule": true, "outDir": "lib", "paths": { "*": ["typings/*"] From eacf05b0b928b501b61c1d07ed07cd28d9e08169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 11:58:21 +0200 Subject: [PATCH 19/20] chore: move each rule into separate PR --- README.md | 2 - docs/rules.md | 2 - docs/rules/html-self-closing.md | 78 ---------- ...-spaces-around-equal-signs-in-attribute.md | 57 ------- src/configs/prettier.ts | 2 - src/rules/html-self-closing.ts | 146 ------------------ ...-spaces-around-equal-signs-in-attribute.ts | 67 -------- src/utils/rules.ts | 4 - src/utils/template-utils.ts | 24 --- src/utils/void-elements.ts | 20 --- .../invalid/component-never/_config.json | 9 -- .../component-never-errors.json | 7 - .../component-never-input.svelte | 4 - .../component-never-output.svelte | 4 - .../invalid/normal-ignore/_config.json | 9 -- .../normal-ignore/normal-any-errors.json | 7 - .../normal-ignore/normal-any-input.svelte | 6 - .../normal-ignore/normal-any-output.svelte | 6 - .../invalid/normal-never/_config.json | 9 -- .../normal-never/component-never-errors.json | 7 - .../normal-never/component-never-input.svelte | 4 - .../component-never-output.svelte | 4 - .../invalid/test01-errors.json | 12 -- .../invalid/test01-input.svelte | 5 - .../invalid/test01-output.svelte | 5 - .../invalid/void-never/_config.json | 9 -- .../invalid/void-never/void-never-errors.json | 7 - .../void-never/void-never-input.svelte | 4 - .../void-never/void-never-output.svelte | 4 - .../valid/test01-input.svelte | 5 - .../invalid/test01-errors.json | 42 ----- .../invalid/test01-input.svelte | 13 -- .../invalid/test01-output.svelte | 11 -- .../valid/test-01-input.svelte | 4 - tests/src/rules/html-self-closing.ts | 12 -- ...-spaces-around-equal-signs-in-attribute.ts | 16 -- 36 files changed, 627 deletions(-) delete mode 100644 docs/rules/html-self-closing.md delete mode 100644 docs/rules/no-spaces-around-equal-signs-in-attribute.md delete mode 100644 src/rules/html-self-closing.ts delete mode 100644 src/rules/no-spaces-around-equal-signs-in-attribute.ts delete mode 100644 src/utils/template-utils.ts delete mode 100644 src/utils/void-elements.ts delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-errors.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte delete mode 100644 tests/fixtures/rules/html-self-closing/valid/test01-input.svelte delete mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json delete mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte delete mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte delete mode 100644 tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte delete mode 100644 tests/src/rules/html-self-closing.ts delete mode 100644 tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts diff --git a/README.md b/README.md index 452dc1880..f92b2633e 100644 --- a/README.md +++ b/README.md @@ -294,11 +294,9 @@ These rules relate to style guidelines, and are therefore quite subjective: | [svelte/first-attribute-linebreak](https://ota-meshi.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: | | [svelte/html-closing-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-quotes/) | enforce quotes style of HTML attributes | :wrench: | -| [svelte/html-self-closing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/html-self-closing/) | enforce self-closing style | :wrench: | | [svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent/) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](https://ota-meshi.github.io/eslint-plugin-svelte/rules/max-attributes-per-line/) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](https://ota-meshi.github.io/eslint-plugin-svelte/rules/mustache-spacing/) | enforce unified spacing in mustache | :wrench: | -| [svelte/no-spaces-around-equal-signs-in-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/) | disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-class-directive/) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-style-directive/) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/shorthand-attribute/) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs/rules.md b/docs/rules.md index a80220047..85077d75e 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -54,11 +54,9 @@ These rules relate to style guidelines, and are therefore quite subjective: | [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: | | [svelte/html-closing-bracket-spacing](./rules/html-closing-bracket-spacing.md) | require or disallow a space before tag's closing brackets | :wrench: | | [svelte/html-quotes](./rules/html-quotes.md) | enforce quotes style of HTML attributes | :wrench: | -| [svelte/html-self-closing](./rules/html-self-closing.md) | enforce self-closing style | :wrench: | | [svelte/indent](./rules/indent.md) | enforce consistent indentation | :wrench: | | [svelte/max-attributes-per-line](./rules/max-attributes-per-line.md) | enforce the maximum number of attributes per line | :wrench: | | [svelte/mustache-spacing](./rules/mustache-spacing.md) | enforce unified spacing in mustache | :wrench: | -| [svelte/no-spaces-around-equal-signs-in-attribute](./rules/no-spaces-around-equal-signs-in-attribute.md) | disallow spaces around equal signs in attribute | :wrench: | | [svelte/prefer-class-directive](./rules/prefer-class-directive.md) | require class directives instead of ternary expressions | :wrench: | | [svelte/prefer-style-directive](./rules/prefer-style-directive.md) | require style directives instead of style attribute | :wrench: | | [svelte/shorthand-attribute](./rules/shorthand-attribute.md) | enforce use of shorthand syntax in attribute | :wrench: | diff --git a/docs/rules/html-self-closing.md b/docs/rules/html-self-closing.md deleted file mode 100644 index d5350720f..000000000 --- a/docs/rules/html-self-closing.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -pageClass: "rule-details" -sidebarDepth: 0 -title: "svelte/html-self-closing" -description: "enforce self-closing style" ---- - -# svelte/html-self-closing - -> enforce self-closing style - -- :exclamation: **_This rule has not been released yet._** -- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. - -## :book: Rule Details - -You can choose either two styles for elements without content - -- always: `
` -- never: `
` - - - - - - -```svelte - - - -
-

Hello

-
- - - -
-

-
- -``` - - - - - -## :wrench: Options - -```json -{ - "svelte/html-self-closing": [ - "error", - { - "html": { - "void": "always", // or "always" or "ignore" - "normal": "always", // or "never" or "ignore" - "component": "always" // or "never" or "ignore" - } - } - ] -} -``` - -- `html.void` (`"always"` by default)... Style of HTML void elements -- `html.component` (`"always"` by default)... Style of svelte components -- `html.normal` (`"always"` by default)... Style of other elements - -Every option can be set to -- "always" (`
`) -- "never" (`
`) -- "ignore" (either `
` or `
`) - -## :mag: Implementation - -- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/html-self-closing.ts) -- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/html-self-closing.ts) diff --git a/docs/rules/no-spaces-around-equal-signs-in-attribute.md b/docs/rules/no-spaces-around-equal-signs-in-attribute.md deleted file mode 100644 index f577ce873..000000000 --- a/docs/rules/no-spaces-around-equal-signs-in-attribute.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -pageClass: "rule-details" -sidebarDepth: 0 -title: "svelte/no-spaces-around-equal-signs-in-attribute" -description: "disallow spaces around equal signs in attribute" ---- - -# svelte/no-spaces-around-equal-signs-in-attribute - -> disallow spaces around equal signs in attribute - -- :exclamation: **_This rule has not been released yet._** -- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. - -## :book: Rule Details - -This rule disallows spaces around equal signs in attributes - - - - - - -```svelte - - - -
-

hi

-A photo of a very cute {animal} - - -
-

hi

-A photo of a very cute {animal} -``` - - - - - -## :wrench: Options - -```json -{ - "svelte/no-spaces-around-equal-signs-in-attribute": ["error"] -} -``` - -## :mag: Implementation - -- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-spaces-around-equal-signs-in-attribute.ts) -- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts) diff --git a/src/configs/prettier.ts b/src/configs/prettier.ts index 20ea0c6a7..fa92608da 100644 --- a/src/configs/prettier.ts +++ b/src/configs/prettier.ts @@ -9,11 +9,9 @@ export = { "svelte/first-attribute-linebreak": "off", "svelte/html-closing-bracket-spacing": "off", "svelte/html-quotes": "off", - "svelte/html-self-closing": "off", "svelte/indent": "off", "svelte/max-attributes-per-line": "off", "svelte/mustache-spacing": "off", - "svelte/no-spaces-around-equal-signs-in-attribute": "off", "svelte/shorthand-attribute": "off", "svelte/shorthand-directive": "off", }, diff --git a/src/rules/html-self-closing.ts b/src/rules/html-self-closing.ts deleted file mode 100644 index af3d19c09..000000000 --- a/src/rules/html-self-closing.ts +++ /dev/null @@ -1,146 +0,0 @@ -import type { AST } from "svelte-eslint-parser" -import { createRule } from "../utils" -import { - getNodeName, - isCustomComponent, - isVoidHtmlElement, -} from "../utils/template-utils" - -const TYPE_MESSAGES = { - normal: "HTML elements", - void: "HTML void elements", - component: "Svelte custom components", -} - -type ElementTypes = "normal" | "void" | "component" - -export default createRule("html-self-closing", { - meta: { - docs: { - description: "enforce self-closing style", - category: "Stylistic Issues", - recommended: false, - conflictWithPrettier: true, - }, - type: "layout", - fixable: "code", - messages: { - requireClosing: "Require self-closing on {{type}}.", - disallowClosing: "Disallow self-closing on {{type}}.", - }, - schema: [ - { - type: "object", - properties: { - html: { - type: "object", - properties: { - void: { - enum: ["never", "always", "ignore"], - }, - normal: { - enum: ["never", "always", "ignore"], - }, - component: { - enum: ["never", "always", "ignore"], - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - }, - create(ctx) { - const options = { - html: { - void: "always", - normal: "always", - component: "always", - }, - ...ctx.options?.[0], - } - - /** - * Get SvelteElement type. - * If element is custom component "component" is returned - * If element is void element "void" is returned - * otherwise "normal" is returned - */ - function getElementType(node: AST.SvelteElement): ElementTypes { - if (isCustomComponent(node)) return "component" - if (isVoidHtmlElement(node)) return "void" - return "normal" - } - - /** - * Returns true if element has no children, or has only whitespace text - */ - function isElementEmpty(node: AST.SvelteElement): boolean { - if (node.children.length <= 0) return true - - for (const child of node.children) { - if (child.type !== "SvelteText") return false - if (!/^\s*$/.test(child.value)) return false - } - - return true - } - - /** - * Report - */ - function report(node: AST.SvelteElement, close: boolean) { - const elementType = getElementType(node) - - ctx.report({ - node, - messageId: close ? "requireClosing" : "disallowClosing", - data: { - type: TYPE_MESSAGES[elementType], - }, - *fix(fixer) { - if (close) { - for (const child of node.children) { - yield fixer.removeRange(child.range) - } - - yield fixer.insertTextBeforeRange( - [node.startTag.range[1] - 1, node.startTag.range[1]], - "/", - ) - - if (node.endTag) yield fixer.removeRange(node.endTag.range) - } else { - yield fixer.removeRange([ - node.startTag.range[1] - 2, - node.startTag.range[1] - 1, - ]) - - if (!isVoidHtmlElement(node)) - yield fixer.insertTextAfter(node, ``) - } - }, - }) - } - - return { - SvelteElement(node: AST.SvelteElement) { - if (!isElementEmpty(node)) return - - const elementType = getElementType(node) - - const elementTypeOptions = options.html[elementType] - if (elementTypeOptions === "ignore") return - const shouldBeClosed = elementTypeOptions === "always" - - if (shouldBeClosed && !node.startTag.selfClosing) { - report(node, true) - } else if (!shouldBeClosed && node.startTag.selfClosing) { - report(node, false) - } - }, - } - }, -}) diff --git a/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/src/rules/no-spaces-around-equal-signs-in-attribute.ts deleted file mode 100644 index dcc9a06f2..000000000 --- a/src/rules/no-spaces-around-equal-signs-in-attribute.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { createRule } from "../utils" -import type { AST } from "svelte-eslint-parser" - -export default createRule("no-spaces-around-equal-signs-in-attribute", { - meta: { - docs: { - description: "disallow spaces around equal signs in attribute", - category: "Stylistic Issues", - recommended: false, - conflictWithPrettier: true, - }, - schema: {}, - fixable: "code", - messages: { - noSpaces: "Unexpected spaces found around equal signs.", - }, - type: "layout", - }, - create(ctx) { - const source = ctx.getSourceCode() - - /** - * Returns source text between attribute key and value, and range of that source - */ - function getAttrEq(node: AST.SvelteAttribute): [string, AST.Range] { - const attrSource = source.getText(node) - const keyRange = node.key.range - const index = - /[^\s=]/.exec(attrSource.slice(keyRange[1] - keyRange[0]))?.index ?? 0 - const valueStart = keyRange[1] + index - const eqSource = attrSource.slice( - keyRange[1] - keyRange[0], - valueStart - keyRange[0], - ) - - return [eqSource, [keyRange[1], keyRange[1] + eqSource.length]] - } - - /** - * Returns true if string contains whitespace characters - */ - function containsWhitespace(string: string): boolean { - return /.*\s.*/s.test(string) - } - - return { - SvelteAttribute(node: AST.SvelteAttribute) { - const [eqSource, range] = getAttrEq(node) - - if (!containsWhitespace(eqSource)) return - - const loc = { - start: source.getLocFromIndex(range[0]), - end: source.getLocFromIndex(range[1]), - } - - ctx.report({ - loc, - messageId: "noSpaces", - *fix(fixer) { - yield fixer.replaceTextRange(range, "=") - }, - }) - }, - } - }, -}) diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 41351bfc3..5bff9ab49 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -4,7 +4,6 @@ import commentDirective from "../rules/comment-directive" import firstAttributeLinebreak from "../rules/first-attribute-linebreak" import htmlClosingBracketSpacing from "../rules/html-closing-bracket-spacing" import htmlQuotes from "../rules/html-quotes" -import htmlSelfClosing from "../rules/html-self-closing" import indent from "../rules/indent" import maxAttributesPerLine from "../rules/max-attributes-per-line" import mustacheSpacing from "../rules/mustache-spacing" @@ -17,7 +16,6 @@ import noInnerDeclarations from "../rules/no-inner-declarations" import noNotFunctionHandler from "../rules/no-not-function-handler" import noObjectInTextMustaches from "../rules/no-object-in-text-mustaches" import noShorthandStylePropertyOverrides from "../rules/no-shorthand-style-property-overrides" -import noSpacesAroundEqualSignsInAttribute from "../rules/no-spaces-around-equal-signs-in-attribute" import noTargetBlank from "../rules/no-target-blank" import noUnknownStyleDirectiveProperty from "../rules/no-unknown-style-directive-property" import noUnusedSvelteIgnore from "../rules/no-unused-svelte-ignore" @@ -37,7 +35,6 @@ export const rules = [ firstAttributeLinebreak, htmlClosingBracketSpacing, htmlQuotes, - htmlSelfClosing, indent, maxAttributesPerLine, mustacheSpacing, @@ -50,7 +47,6 @@ export const rules = [ noNotFunctionHandler, noObjectInTextMustaches, noShorthandStylePropertyOverrides, - noSpacesAroundEqualSignsInAttribute, noTargetBlank, noUnknownStyleDirectiveProperty, noUnusedSvelteIgnore, diff --git a/src/utils/template-utils.ts b/src/utils/template-utils.ts deleted file mode 100644 index 2dbe1a8b6..000000000 --- a/src/utils/template-utils.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { AST } from "svelte-eslint-parser" -import voidElements from "./void-elements" - -/** - * Returns name of SvelteElement - */ -export function getNodeName(node: AST.SvelteElement): string { - return "name" in node.name ? node.name.name : node.name.property.name -} - -/** - * Returns true if Element is custom component - */ -export function isCustomComponent(node: AST.SvelteElement): boolean { - return node.kind === "component" -} - -/** - * Returns true if element is known void element - * {@link https://developer.mozilla.org/en-US/docs/Glossary/Empty_element} - */ -export function isVoidHtmlElement(node: AST.SvelteElement): boolean { - return voidElements.includes(getNodeName(node)) -} diff --git a/src/utils/void-elements.ts b/src/utils/void-elements.ts deleted file mode 100644 index 4df980dc6..000000000 --- a/src/utils/void-elements.ts +++ /dev/null @@ -1,20 +0,0 @@ -const voidElements = [ - "area", - "base", - "br", - "col", - "embed", - "hr", - "img", - "input", - "keygen", - "link", - "menuitem", - "meta", - "param", - "source", - "track", - "wbr", -] - -export default voidElements diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json deleted file mode 100644 index 5c198116d..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "options": [ - { - "html": { - "component": "never" - } - } - ] -} diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json deleted file mode 100644 index c7692bcc9..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Disallow self-closing on Svelte custom components.", - "line": 3, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte deleted file mode 100644 index 6c888d0bc..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte deleted file mode 100644 index f90849ac6..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json deleted file mode 100644 index 8a13060cb..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "options": [ - { - "html": { - "normal": "ignore" - } - } - ] -} diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json deleted file mode 100644 index 95f4dfb19..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Disallow self-closing on HTML void elements.", - "line": 5, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte deleted file mode 100644 index c195a36c1..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte +++ /dev/null @@ -1,6 +0,0 @@ - -
-
-
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte deleted file mode 100644 index 66e0cd246..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte +++ /dev/null @@ -1,6 +0,0 @@ - -
-
-
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json deleted file mode 100644 index 53bea34aa..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "options": [ - { - "html": { - "normal": "never" - } - } - ] -} diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json deleted file mode 100644 index 1d62f7723..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Disallow self-closing on HTML elements.", - "line": 3, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte deleted file mode 100644 index 5c8a2cf01..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
-
-
diff --git a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte deleted file mode 100644 index a45145e01..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
-
-
diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json b/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json deleted file mode 100644 index 0a02955f3..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/test01-errors.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "message": "Require self-closing on HTML elements.", - "line": 3, - "column": 3 - }, - { - "message": "Require self-closing on Svelte custom components.", - "line": 4, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte deleted file mode 100644 index a541d4d55..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/test01-input.svelte +++ /dev/null @@ -1,5 +0,0 @@ - -
-
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte deleted file mode 100644 index 51af2156b..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/test01-output.svelte +++ /dev/null @@ -1,5 +0,0 @@ - -
-
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json b/tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json deleted file mode 100644 index 4e7d9b767..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/void-never/_config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "options": [ - { - "html": { - "void": "never" - } - } - ] -} diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json deleted file mode 100644 index 3b61e0b15..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-errors.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Disallow self-closing on HTML void elements.", - "line": 3, - "column": 3 - } -] diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte deleted file mode 100644 index 42ea6dbf7..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-input.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
- -
diff --git a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte b/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte deleted file mode 100644 index 713cf9f72..000000000 --- a/tests/fixtures/rules/html-self-closing/invalid/void-never/void-never-output.svelte +++ /dev/null @@ -1,4 +0,0 @@ - -
- -
diff --git a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte b/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte deleted file mode 100644 index 104b5c286..000000000 --- a/tests/fixtures/rules/html-self-closing/valid/test01-input.svelte +++ /dev/null @@ -1,5 +0,0 @@ -
-
-
hello
- -
diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json deleted file mode 100644 index ec98b89ad..000000000 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-errors.json +++ /dev/null @@ -1,42 +0,0 @@ -[ - { - "message": "Unexpected spaces found around equal signs.", - "line": 3, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 4, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 5, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 6, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 8, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 10, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 11, - "column": 11 - }, - { - "message": "Unexpected spaces found around equal signs.", - "line": 12, - "column": 11 - } -] diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte deleted file mode 100644 index eabc42f5d..000000000 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-input.svelte +++ /dev/null @@ -1,13 +0,0 @@ - -
-

-

-

-

-

-

-

-

-
diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte deleted file mode 100644 index c16cfc843..000000000 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/invalid/test01-output.svelte +++ /dev/null @@ -1,11 +0,0 @@ - -
-

-

-

-

-

-

-

-

-
diff --git a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte b/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte deleted file mode 100644 index 86dd60bf0..000000000 --- a/tests/fixtures/rules/no-spaces-around-equal-signs-in-attribute/valid/test-01-input.svelte +++ /dev/null @@ -1,4 +0,0 @@ -

-

- -

diff --git a/tests/src/rules/html-self-closing.ts b/tests/src/rules/html-self-closing.ts deleted file mode 100644 index 6890e9087..000000000 --- a/tests/src/rules/html-self-closing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { RuleTester } from "eslint" -import rule from "../../../src/rules/html-self-closing" -import { loadTestCases } from "../../utils/utils" - -const tester = new RuleTester({ - parserOptions: { - ecmaVersion: 2020, - sourceType: "module", - }, -}) - -tester.run("html-self-closing", rule as any, loadTestCases("html-self-closing")) diff --git a/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts b/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts deleted file mode 100644 index edd7fe8bf..000000000 --- a/tests/src/rules/no-spaces-around-equal-signs-in-attribute.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { RuleTester } from "eslint" -import rule from "../../../src/rules/no-spaces-around-equal-signs-in-attribute" -import { loadTestCases } from "../../utils/utils" - -const tester = new RuleTester({ - parserOptions: { - ecmaVersion: 2020, - sourceType: "module", - }, -}) - -tester.run( - "no-spaces-around-equal-signs-in-attribute", - rule as any, - loadTestCases("no-spaces-around-equal-signs-in-attribute"), -) From 34df3a3fc6e3a4fd19de8482bf0236cbb26c7bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vosp=C4=9Bl?= Date: Thu, 21 Jul 2022 19:39:04 +0200 Subject: [PATCH 20/20] chore: change fixable to whitespace --- src/rules/html-closing-bracket-spacing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/html-closing-bracket-spacing.ts b/src/rules/html-closing-bracket-spacing.ts index 82037b38c..a69b16334 100644 --- a/src/rules/html-closing-bracket-spacing.ts +++ b/src/rules/html-closing-bracket-spacing.ts @@ -30,7 +30,7 @@ export default createRule("html-closing-bracket-spacing", { expectedSpace: "Expected space before '>', but not found.", unexpectedSpace: "Expected no space before '>', but found.", }, - fixable: "code", + fixable: "whitespace", type: "layout", }, create(ctx) {