Skip to content

fix: handle type aliases for $$Slots and $$Events related rules #530

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

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

fix: handle type aliases for $$Events and $$Slots declarations
15 changes: 11 additions & 4 deletions src/rules/experimental-require-slot-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createRule } from "../utils"
import { getLangValue } from "../utils/ast-utils"

const SLOTS_TYPE_NAME = "$$Slots"

export default createRule("experimental-require-slot-types", {
meta: {
docs: {
Expand All @@ -18,7 +20,7 @@ export default createRule("experimental-require-slot-types", {
create(context) {
let isTs = false
let hasSlot = false
let hasInterface = false
let hasDeclaredSlots = false
return {
SvelteScriptElement(node) {
const lang = getLangValue(node)?.toLowerCase()
Expand All @@ -30,12 +32,17 @@ export default createRule("experimental-require-slot-types", {
}
},
TSInterfaceDeclaration(node) {
if (node.id.name === "$$Slots") {
hasInterface = true
if (node.id.name === SLOTS_TYPE_NAME) {
hasDeclaredSlots = true
}
},
TSTypeAliasDeclaration(node) {
if (node.id.name === SLOTS_TYPE_NAME) {
hasDeclaredSlots = true
}
},
"Program:exit"() {
if (isTs && hasSlot && !hasInterface) {
if (isTs && hasSlot && !hasDeclaredSlots) {
context.report({
loc: {
line: 1,
Expand Down
15 changes: 11 additions & 4 deletions src/rules/experimental-require-strict-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"
import { findAttribute, getLangValue } from "../utils/ast-utils"

const EVENTS_TYPE_NAME = "$$Events"

export default createRule("experimental-require-strict-events", {
meta: {
docs: {
Expand All @@ -19,7 +21,7 @@ export default createRule("experimental-require-strict-events", {
create(context) {
let isTs = false
let hasAttribute = false
let hasInterface = false
let hasDeclaredEvents = false
let scriptNode: AST.SvelteScriptElement
return {
SvelteScriptElement(node) {
Expand All @@ -29,12 +31,17 @@ export default createRule("experimental-require-strict-events", {
scriptNode = node
},
TSInterfaceDeclaration(node) {
if (node.id.name === "$$Events") {
hasInterface = true
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true
}
},
TSTypeAliasDeclaration(node) {
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true
}
},
"Program:exit"() {
if (isTs && !hasAttribute && !hasInterface) {
if (isTs && !hasAttribute && !hasDeclaredEvents) {
context.report({
node: scriptNode,
messageId: "missingStrictEvents",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
type $$Slots = {
defalt: Record<string, never>
}
</script>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script lang="ts">
type $$Events = {}
</script>