Repository navigation
chore(lint): catch known tags with Effect.catchTags #16361
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| import { assert, describe } from "@effect/vitest"; | ||
|
|
||
| import { createOxlintRuleHarness } from "../test/utils.ts"; | ||
|
|
||
| const rule = createOxlintRuleHarness("t3code/prefer-catch-tags"); | ||
|
|
||
| describe("t3code/prefer-catch-tags", () => { | ||
| rule.valid( | ||
| "allows Effect.catchTags", | ||
| ` | ||
| import * as Effect from "effect/Effect"; | ||
|
|
||
| export const program = Effect.fail({ _tag: "A" } as const).pipe( | ||
| Effect.catchTags({ A: () => Effect.void }), | ||
| ); | ||
| `, | ||
| ); | ||
|
|
||
| rule.valid( | ||
| "ignores catchTag on other modules", | ||
| ` | ||
| import * as Stream from "effect/Stream"; | ||
|
|
||
| export const recover = Stream.catchTag("A", () => Stream.empty); | ||
| `, | ||
| ); | ||
|
|
||
| rule.invalid( | ||
| "reports Effect.catchTag", | ||
| ` | ||
| import * as Effect from "effect/Effect"; | ||
|
|
||
| export const program = Effect.fail({ _tag: "A" } as const).pipe( | ||
| Effect.catchTag("A", () => Effect.void), | ||
| ); | ||
| `, | ||
| (output) => { | ||
| assert.match(output, /Effect\.catchTags\(\{ Tag: handler \}\)/); | ||
| }, | ||
| ); | ||
|
|
||
| rule.invalid( | ||
| "reports catchTag through an aliased namespace", | ||
| ` | ||
| import * as Eff from "effect/Effect"; | ||
|
|
||
| export const recover = Eff.catchTag("A", () => Eff.void); | ||
| `, | ||
| ); | ||
|
|
||
| rule.invalid("reports a named catchTag import", `import { catchTag } from "effect/Effect";`); | ||
| }); |
This file contains hidden or 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,44 @@ | ||
| import { defineRule } from "@oxlint/plugins"; | ||
|
|
||
| const MESSAGE = "Catch known tags with `Effect.catchTags({ Tag: handler })`, even for one tag."; | ||
|
|
||
| /** Reports `Effect.catchTag`, through any namespace import of `effect/Effect` or a named import. */ | ||
| export default defineRule({ | ||
| meta: { | ||
| type: "suggestion", | ||
| docs: { | ||
| description: "Require `Effect.catchTags` over `Effect.catchTag`.", | ||
| }, | ||
| }, | ||
| create(context) { | ||
| const effectNamespaces = new Set<string>(); | ||
| return { | ||
| ImportDeclaration(node) { | ||
| if (node.source.value !== "effect/Effect") return; | ||
| for (const specifier of node.specifiers) { | ||
| if (specifier.type === "ImportNamespaceSpecifier") { | ||
| effectNamespaces.add(specifier.local.name); | ||
| } else if ( | ||
| specifier.type === "ImportSpecifier" && | ||
| (specifier.imported.type === "Identifier" | ||
| ? specifier.imported.name | ||
| : specifier.imported.value) === "catchTag" | ||
| ) { | ||
| context.report({ node: specifier, message: MESSAGE }); | ||
| } | ||
| } | ||
| }, | ||
| MemberExpression(node) { | ||
| if ( | ||
| !node.computed && | ||
| node.object.type === "Identifier" && | ||
| effectNamespaces.has(node.object.name) && | ||
| node.property.type === "Identifier" && | ||
| node.property.name === "catchTag" | ||
| ) { | ||
| context.report({ node, message: MESSAGE }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
🟡 Medium
rules/prefer-catch-tags.ts:34The rule reports
Effect.catchTag()on a shadowing parameter or local variable, so valid code such asfunction f(Effect) { Effect.catchTag(); }fails CI even though the access is not the imported namespace. The name-onlyeffectNamespacescheck must resolve theEffectidentifier to its import binding before reporting.🤖 Copy this AI Prompt to have your agent fix this:
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.
Good catch. Fixed in 46b9b3c: the rule now resolves the identifier through scope and reports only when it's bound by
import * as … from "effect/Effect", so a shadowing parameter or local isn't flagged. Added a test for it.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.
Sorry, I'm unable to act on this request because you do not have permissions within this repository.