From 4b53707d79cdc158ed73998031257e2ea2c903cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Tue, 7 Feb 2023 23:57:29 +0100 Subject: [PATCH 01/11] test(require-strict-events): Added rule tests --- .../invalid/no-strict-events01-input.svelte | 2 ++ .../valid/has-strict-events01-input.svelte | 2 ++ .../valid/no-typescript01-input.svelte | 2 ++ .../valid/script-module-context01-input.svelte | 5 +++++ tests/src/rules/require-strict-events.ts | 16 ++++++++++++++++ 5 files changed, 27 insertions(+) create mode 100644 tests/fixtures/rules/require-strict-events/invalid/no-strict-events01-input.svelte create mode 100644 tests/fixtures/rules/require-strict-events/valid/has-strict-events01-input.svelte create mode 100644 tests/fixtures/rules/require-strict-events/valid/no-typescript01-input.svelte create mode 100644 tests/fixtures/rules/require-strict-events/valid/script-module-context01-input.svelte create mode 100644 tests/src/rules/require-strict-events.ts diff --git a/tests/fixtures/rules/require-strict-events/invalid/no-strict-events01-input.svelte b/tests/fixtures/rules/require-strict-events/invalid/no-strict-events01-input.svelte new file mode 100644 index 000000000..0fbba9978 --- /dev/null +++ b/tests/fixtures/rules/require-strict-events/invalid/no-strict-events01-input.svelte @@ -0,0 +1,2 @@ + diff --git a/tests/fixtures/rules/require-strict-events/valid/has-strict-events01-input.svelte b/tests/fixtures/rules/require-strict-events/valid/has-strict-events01-input.svelte new file mode 100644 index 000000000..e96af6ef1 --- /dev/null +++ b/tests/fixtures/rules/require-strict-events/valid/has-strict-events01-input.svelte @@ -0,0 +1,2 @@ + diff --git a/tests/fixtures/rules/require-strict-events/valid/no-typescript01-input.svelte b/tests/fixtures/rules/require-strict-events/valid/no-typescript01-input.svelte new file mode 100644 index 000000000..49f5fcd1f --- /dev/null +++ b/tests/fixtures/rules/require-strict-events/valid/no-typescript01-input.svelte @@ -0,0 +1,2 @@ + diff --git a/tests/fixtures/rules/require-strict-events/valid/script-module-context01-input.svelte b/tests/fixtures/rules/require-strict-events/valid/script-module-context01-input.svelte new file mode 100644 index 000000000..5e40c34ee --- /dev/null +++ b/tests/fixtures/rules/require-strict-events/valid/script-module-context01-input.svelte @@ -0,0 +1,5 @@ + + + diff --git a/tests/src/rules/require-strict-events.ts b/tests/src/rules/require-strict-events.ts new file mode 100644 index 000000000..1de80a9d1 --- /dev/null +++ b/tests/src/rules/require-strict-events.ts @@ -0,0 +1,16 @@ +import { RuleTester } from "eslint" +import rule from "../../../src/rules/require-strict-events" +import { loadTestCases } from "../../utils/utils" + +const tester = new RuleTester({ + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, +}) + +tester.run( + "require-strict-events", + rule as any, + loadTestCases("require-strict-events"), +) From 3750438805864502fa075c0e48978b4de09e7410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 00:18:40 +0100 Subject: [PATCH 02/11] feat(require-strict-events): added the rule implementation --- src/rules/require-strict-events.ts | 31 +++++++++++++++++++ src/utils/rules.ts | 2 ++ .../invalid/no-strict-events01-errors.yaml | 4 +++ 3 files changed, 37 insertions(+) create mode 100644 src/rules/require-strict-events.ts create mode 100644 tests/fixtures/rules/require-strict-events/invalid/no-strict-events01-errors.yaml diff --git a/src/rules/require-strict-events.ts b/src/rules/require-strict-events.ts new file mode 100644 index 000000000..4cb9302a0 --- /dev/null +++ b/src/rules/require-strict-events.ts @@ -0,0 +1,31 @@ +import { createRule } from "../utils" +import { findAttribute, getLangValue } from "../utils/ast-utils" + +export default createRule("require-strict-events", { + meta: { + docs: { + description: "require the strictEvents attribute on + +/* ✗ BAD */ + +``` + + + +## :wrench: Options + +Nothing. + +## :rocket: Version + +This rule was introduced in eslint-plugin-svelte v2.17.0 + +## :mag: Implementation + +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/require-strict-events.ts) +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/require-strict-events.ts) From 745ce08ec9bec425d1efa9dafc6fd74403b650aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 01:01:37 +0100 Subject: [PATCH 05/11] feat: added the experimental rule section --- src/types.ts | 1 + tools/render-rules.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index accfd1514..038047a61 100644 --- a/src/types.ts +++ b/src/types.ts @@ -53,6 +53,7 @@ export type RuleCategory = | "Best Practices" | "Stylistic Issues" | "Extension Rules" + | "Experimental" | "System" export interface RuleMetaData { diff --git a/tools/render-rules.ts b/tools/render-rules.ts index 939351642..096b2dd78 100644 --- a/tools/render-rules.ts +++ b/tools/render-rules.ts @@ -7,10 +7,11 @@ const categories = [ "Best Practices", "Stylistic Issues", "Extension Rules", + "Experimental", "System", ] as const -const descriptions: Record<(typeof categories)[number], string> = { +const descriptions: Record = { "Possible Errors": "These rules relate to possible syntax or logic errors in Svelte code:", "Security Vulnerability": @@ -21,6 +22,8 @@ const descriptions: Record<(typeof categories)[number], string> = { "These rules relate to style guidelines, and are therefore quite subjective:", "Extension Rules": "These rules extend the rules provided by ESLint itself, or other plugins to work well in Svelte:", + Experimental: + ":warning: These rules are considered experimental and may change or be removed in the future:", System: "These rules relate to this plugin works:", } From 76068cae95ef0f702d4952d895af50d734b826e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 01:02:37 +0100 Subject: [PATCH 06/11] chore(require-strict-events): renamed rest of files to experimental-require-strict-events --- README.md | 4 ++-- docs/rules.md | 4 ++-- ...events.md => experimental-require-strict-events.md} | 10 ++++------ ...events.ts => experimental-require-strict-events.ts} | 2 +- src/utils/rules.ts | 4 ++-- ...events.ts => experimental-require-strict-events.ts} | 4 ++-- 6 files changed, 13 insertions(+), 15 deletions(-) rename docs/rules/{require-strict-events.md => experimental-require-strict-events.md} (80%) rename src/rules/{require-strict-events.ts => experimental-require-strict-events.ts} (96%) rename tests/src/rules/{require-strict-events.ts => experimental-require-strict-events.ts} (71%) diff --git a/README.md b/README.md index 1fc82e17b..fa50c9e85 100644 --- a/README.md +++ b/README.md @@ -375,11 +375,11 @@ These rules extend the rules provided by ESLint itself, or other plugins to work ## Experimental -:warning: These rules are considered experimental and may change or be removed in the future. +:warning: These rules are considered experimental and may change or be removed in the future: | Rule ID | Description | | |:--------|:------------|:---| -| [svelte/experimental-require-strict-events](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-strict-events/) | require the strictEvents attribute on + + /* ✗ BAD */ diff --git a/src/rules/experimental-require-strict-events.ts b/src/rules/experimental-require-strict-events.ts index 09108286f..76d620018 100644 --- a/src/rules/experimental-require-strict-events.ts +++ b/src/rules/experimental-require-strict-events.ts @@ -10,20 +10,34 @@ export default createRule("experimental-require-strict-events", { }, schema: [], messages: { - missingStrictEvents: `The From 19469e6b8dfd08530af5d2c6e03168493c0b1f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 01:51:45 +0100 Subject: [PATCH 08/11] chore(require-strict-events): added a changeset --- .changeset/tasty-houses-behave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tasty-houses-behave.md diff --git a/.changeset/tasty-houses-behave.md b/.changeset/tasty-houses-behave.md new file mode 100644 index 000000000..e3a2d861f --- /dev/null +++ b/.changeset/tasty-houses-behave.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-svelte": minor +--- + +Added the experimental-require-strict-events rule From 0908f32cdd6e2ab02337bc13399ca3d4311ca662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 11:20:36 +0100 Subject: [PATCH 09/11] docs(require-strict-events): fixed docs example --- docs/rules/experimental-require-strict-events.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/rules/experimental-require-strict-events.md b/docs/rules/experimental-require-strict-events.md index abedbf99b..4fd4bfd4a 100644 --- a/docs/rules/experimental-require-strict-events.md +++ b/docs/rules/experimental-require-strict-events.md @@ -20,9 +20,9 @@ This rule enforces the presence of the `strictEvents` attribute on the main ` ```svelte -/* eslint svelte/experimental-strict-events: "error" */ + -/* ✓ GOOD */ + @@ -30,7 +30,7 @@ This rule enforces the presence of the `strictEvents` attribute on the main ` -/* ✗ BAD */ + ``` From 67486eb8312d76089ceaee58feb31d3e34550d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 8 Feb 2023 12:58:28 +0100 Subject: [PATCH 10/11] fix: reverted spurious merge change --- tools/render-rules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/render-rules.ts b/tools/render-rules.ts index 096b2dd78..454c2406b 100644 --- a/tools/render-rules.ts +++ b/tools/render-rules.ts @@ -11,7 +11,7 @@ const categories = [ "System", ] as const -const descriptions: Record = { +const descriptions: Record<(typeof categories)[number], string> = { "Possible Errors": "These rules relate to possible syntax or logic errors in Svelte code:", "Security Vulnerability": From 99477b9b5aa2225afffdff68aee88093a50a4bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Thu, 9 Feb 2023 10:38:52 +0100 Subject: [PATCH 11/11] feat(require-strict-events): reporting lint issue on script tag --- src/rules/experimental-require-strict-events.ts | 9 +++++---- .../invalid/no-strict-events01-errors.yaml | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/rules/experimental-require-strict-events.ts b/src/rules/experimental-require-strict-events.ts index 76d620018..0451f8cb0 100644 --- a/src/rules/experimental-require-strict-events.ts +++ b/src/rules/experimental-require-strict-events.ts @@ -1,3 +1,5 @@ +import type { AST } from "svelte-eslint-parser" + import { createRule } from "../utils" import { findAttribute, getLangValue } from "../utils/ast-utils" @@ -18,11 +20,13 @@ export default createRule("experimental-require-strict-events", { let isTs = false let hasAttribute = false let hasInterface = false + let scriptNode: AST.SvelteScriptElement return { SvelteScriptElement(node) { const lang = getLangValue(node)?.toLowerCase() isTs = lang === "ts" || lang === "typescript" hasAttribute = findAttribute(node, "strictEvents") !== null + scriptNode = node }, TSInterfaceDeclaration(node) { if (node.id.name === "$$Events") { @@ -32,10 +36,7 @@ export default createRule("experimental-require-strict-events", { "Program:exit"() { if (isTs && !hasAttribute && !hasInterface) { context.report({ - loc: { - line: 1, - column: 1, - }, + node: scriptNode, messageId: "missingStrictEvents", }) } diff --git a/tests/fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-errors.yaml b/tests/fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-errors.yaml index 94486f515..058f144a8 100644 --- a/tests/fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-errors.yaml +++ b/tests/fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-errors.yaml @@ -2,5 +2,5 @@ The component must have the strictEvents attribute on its