-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat: Added the experimental-require-strict-events
rule
#365
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4b53707
test(require-strict-events): Added rule tests
marekdedic 3750438
feat(require-strict-events): added the rule implementation
marekdedic 85d990c
chore(require-strict-events): renamed to experimental-require-strict-…
marekdedic d9627f1
docs(require-strict-events): added rule docs
marekdedic 745ce08
feat: added the experimental rule section
marekdedic 76068ca
chore(require-strict-events): renamed rest of files to experimental-r…
marekdedic 4666c27
feat(require-strict-events): added support for components defining th…
marekdedic 19469e6
chore(require-strict-events): added a changeset
marekdedic 94baeb5
Merge branch 'main' into require-strict-events
marekdedic 0908f32
docs(require-strict-events): fixed docs example
marekdedic 67486eb
fix: reverted spurious merge change
marekdedic 99477b9
feat(require-strict-events): reporting lint issue on script tag
marekdedic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-svelte": minor | ||
--- | ||
|
||
Added the experimental-require-strict-events rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/experimental-require-strict-events" | ||
description: "require the strictEvents attribute on <script> tags" | ||
--- | ||
|
||
# svelte/experimental-require-strict-events | ||
|
||
> require the strictEvents attribute on <script> tags | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces the presence of the `strictEvents` attribute on the main `<script>` tag of all components. This attributes enforces typechecking of events dispatched by the component, e.g. making it a typescript error to listen to any non-existent events. Alternatively, the event types may be defined manually by declaring the `$$Events` interface. The `strictEvents` attribute and the `$$Events` interface are experimental and are documented in [svelte RFC #38](https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md#typing-events). | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- eslint svelte/experimental-require-strict-events: "error" --> | ||
<!-- ✓ GOOD --> | ||
<script lang="ts" strictEvents> | ||
</script> | ||
<script lang="ts"> | ||
interface $$Events {} | ||
</script> | ||
<!-- ✗ BAD --> | ||
<script lang="ts"> | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/experimental-require-strict-events.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/experimental-require-strict-events.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createRule } from "../utils" | ||
import { findAttribute, getLangValue } from "../utils/ast-utils" | ||
|
||
export default createRule("experimental-require-strict-events", { | ||
meta: { | ||
docs: { | ||
description: "require the strictEvents attribute on <script> tags", | ||
category: "Experimental", | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
missingStrictEvents: `The component must have the strictEvents attribute on its <script> tag or it must define the $$Events interface.`, | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
let isTs = false | ||
let hasAttribute = false | ||
let hasInterface = false | ||
return { | ||
SvelteScriptElement(node) { | ||
const lang = getLangValue(node)?.toLowerCase() | ||
isTs = lang === "ts" || lang === "typescript" | ||
hasAttribute = findAttribute(node, "strictEvents") !== null | ||
}, | ||
TSInterfaceDeclaration(node) { | ||
if (node.id.name === "$$Events") { | ||
hasInterface = true | ||
} | ||
}, | ||
"Program:exit"() { | ||
if (isTs && !hasAttribute && !hasInterface) { | ||
context.report({ | ||
loc: { | ||
marekdedic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
line: 1, | ||
column: 1, | ||
}, | ||
messageId: "missingStrictEvents", | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
.../fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- message: | ||
The component must have the strictEvents attribute on its <script> tag | ||
or it must define the $$Events interface. | ||
line: 1 | ||
column: 2 | ||
suggestions: null |
2 changes: 2 additions & 0 deletions
2
...fixtures/rules/experimental-require-strict-events/invalid/no-strict-events01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script lang="ts"> | ||
</script> |
3 changes: 3 additions & 0 deletions
3
...xtures/rules/experimental-require-strict-events/valid/has-events-interface01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script lang="ts"> | ||
interface $$Events {} | ||
</script> |
2 changes: 2 additions & 0 deletions
2
.../fixtures/rules/experimental-require-strict-events/valid/has-strict-events01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script lang="ts" strictEvents> | ||
</script> |
2 changes: 2 additions & 0 deletions
2
tests/fixtures/rules/experimental-require-strict-events/valid/no-typescript01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script> | ||
</script> |
5 changes: 5 additions & 0 deletions
5
...tures/rules/experimental-require-strict-events/valid/script-module-context01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script lang="ts" context="module"> | ||
</script> | ||
|
||
<script lang="ts" strictEvents> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/experimental-require-strict-events" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"experimental-require-strict-events", | ||
rule as any, | ||
loadTestCases("experimental-require-strict-events"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think the
strictEvents
attribute should be given even ifcreateEventDispatcher
oron:click
are not used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do - the
strictEvents
attributes makes all "callers" of the component (i.e. components that contain it) have theiron:something
handlers checked as well - so even if a component has no events, thestrictEvents
attribute brings a benefit in that the callers will error if they try to add a handler to a non-existent event.