-
Notifications
You must be signed in to change notification settings - Fork 3
Add a few rules to help with shortcuts #13
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
Open
mathieudutour
wants to merge
4
commits into
main
Choose a base branch
from
f/shortcuts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Warn when a shortcut is ambiguous in cross-platform extensions (`@raycast/no-ambiguous-platform-shortcut`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Warns when a single-form shortcut literal (one modifiers/key object) uses only `cmd` or only `ctrl` in an extension that targets multiple platforms. In multi-platform extensions, you should either provide platform-specific shortcuts (with `macOS`/`Windows` sections) or include both modifiers to ensure parity. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule checks JSX attributes named `shortcut`. It only inspects literal objects of the simple form: | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "s" }} /> | ||
| ``` | ||
|
|
||
| Platform-specific shortcut objects (those with `macOS`/`Windows`) and dynamic expressions are ignored. | ||
|
|
||
| The rule first locates the closest `package.json` to the file being linted. If the package declares a `platforms` array with more than one value, the rule activates and warns about ambiguous shortcuts. | ||
|
|
||
| ### Examples of incorrect code | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "s" }} /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["ctrl"], key: "s" }} /> | ||
| ``` | ||
|
|
||
| ### Examples of correct code | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd", "ctrl"], key: "s" }} /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action | ||
| shortcut={{ | ||
| macOS: { modifiers: ["cmd"], key: "s" }, | ||
| Windows: { modifiers: ["ctrl"], key: "s" }, | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| // Single-platform extension ⇒ rule does not apply | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "s" }} /> | ||
| ``` | ||
|
|
||
| > ℹ️ The rule only warns; it does not provide an autofix. Choose platform-specific shortcuts or include both modifiers manually. |
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,60 @@ | ||
| # Warn when a shortcut prop defines a reserved shortcut that Raycast uses (`@raycast/no-reserved-shortcut`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Warns when you define a literal shortcut that conflicts with one of Raycast's reserved shortcuts. Reserved shortcuts are used internally by the Raycast UI and reusing them in extensions may create confusing experiences. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule inspects JSX `shortcut` attributes with a direct object literal of the simple form: | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "w" }} /> | ||
| ``` | ||
|
|
||
| Platform form objects (with `macOS` / `Windows`) and dynamically built shortcuts are ignored. | ||
|
|
||
| ### Examples of incorrect code | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "w" }} /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: [], key: "delete" }} /> | ||
| ``` | ||
|
|
||
| ### Examples of correct code | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "x" }} /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action | ||
| shortcut={{ | ||
| macOS: { modifiers: ["cmd"], key: "w" }, | ||
| Windows: { modifiers: ["ctrl"], key: "w" }, | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| ## Reserved Shortcuts Checked | ||
|
|
||
| - CloseWindow: cmd + w | ||
| - Delete: delete | ||
| - DeleteForward: deleteForward | ||
| - DeleteLineBackward: cmd + delete | ||
| - DeleteWordBackward: opt + delete | ||
| - GoBack: escape | ||
| - OpenActionPanel: cmd + k | ||
| - OpenPreferences: cmd + , | ||
| - OpenSearchBarDropdown: cmd + p | ||
| - OpenSearchBarLink: shift + cmd + / | ||
| - PrimaryAction: enter | ||
| - Quit: cmd + q | ||
| - ReturnToRoot: cmd + escape | ||
| - SecondaryAction: cmd + enter | ||
| - SelectAll: cmd + a | ||
|
|
||
| Note: The rule does not attempt an autofix; choose an alternate shortcut instead. |
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 @@ | ||
| # Warn when a shortcut matches a common one; prefer Keyboard.Shortcut.Common.* from @raycast/api (`@raycast/prefer-common-shortcut`) | ||
|
|
||
| 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| When a React component uses a `shortcut` prop with a literal object that matches a | ||
| well-known shortcut, prefer using `Keyboard.Shortcut.Common.*` from `@raycast/api`. | ||
|
|
||
| This keeps shortcuts consistent across platforms and makes intent explicit. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule inspects JSX attributes named `shortcut` and looks for object literals in either of these forms: | ||
|
|
||
| - Single form: `{ modifiers: ["cmd"], key: "s" }` | ||
| - Platform form: `{ macOS: { modifiers: ["cmd"], key: "s" }, Windows: { modifiers: ["ctrl"], key: "s" } }` | ||
|
|
||
| If the value equals a known common shortcut, the rule suggests replacing it with the corresponding | ||
| `Keyboard.Shortcut.Common.Name` and will also add `Keyboard` to your `@raycast/api` import or create | ||
| one if missing. | ||
|
|
||
| ### Examples | ||
|
|
||
| Incorrect code: | ||
|
|
||
| ```tsx | ||
| <Action shortcut={{ modifiers: ["cmd"], key: "n" }} /> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action | ||
| shortcut={{ | ||
| macOS: { modifiers: ["cmd", "shift"], key: "arrowUp" }, | ||
| Windows: { modifiers: ["ctrl", "shift"], key: "arrowUp" }, | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| Correct code: | ||
|
|
||
| ```tsx | ||
| import { Keyboard } from "@raycast/api"; | ||
|
|
||
| <Action shortcut={Keyboard.Shortcut.Common.New} />; | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <Action shortcut={Keyboard.Shortcut.Common.MoveUp} /> | ||
| ``` | ||
|
|
||
| Note that the rule only flags "single" (non-platform) object literals when the common shortcut is exactly the same on macOS and Windows. For platform-specific differences, use the `platform` object form in your code or the corresponding `Keyboard.Shortcut.Common.*` reference. |
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,140 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| import { AST_NODE_TYPES, TSESLint, TSESTree } from "@typescript-eslint/utils"; | ||
|
|
||
| import { createRule } from "../utils"; | ||
|
|
||
| type SimpleShortcut = { modifiers: string[]; key: string }; | ||
|
|
||
| const packagePlatformsCache = new Map<string, boolean>(); | ||
|
|
||
| function hasMultiPlatformConfig(filename: string | undefined): boolean { | ||
| if (!filename || filename.startsWith("<")) { | ||
| return false; | ||
| } | ||
|
|
||
| let dir = path.dirname(filename); | ||
| while (true) { | ||
| const pkgPath = path.join(dir, "package.json"); | ||
| if (packagePlatformsCache.has(pkgPath)) { | ||
| return packagePlatformsCache.get(pkgPath)!; | ||
| } | ||
|
|
||
| if (fs.existsSync(pkgPath)) { | ||
| try { | ||
| const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); | ||
| const platforms = pkg?.platforms; | ||
| const multi = Array.isArray(platforms) && platforms.length > 1; | ||
| packagePlatformsCache.set(pkgPath, multi); | ||
| return multi; | ||
| } catch { | ||
mathieudutour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| packagePlatformsCache.set(pkgPath, false); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| const parent = path.dirname(dir); | ||
| if (parent === dir) { | ||
| break; | ||
| } | ||
| dir = parent; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function parseSimpleShortcut( | ||
| node: TSESTree.ObjectExpression | ||
| ): SimpleShortcut | null { | ||
| const props = new Map<string, TSESTree.Property>(); | ||
| for (const prop of node.properties) { | ||
| if ( | ||
| prop.type !== AST_NODE_TYPES.Property || | ||
| prop.key.type !== AST_NODE_TYPES.Identifier | ||
| ) { | ||
| return null; | ||
| } | ||
| if (prop.key.name !== "modifiers" && prop.key.name !== "key") { | ||
| return null; | ||
| } | ||
| props.set(prop.key.name, prop); | ||
| } | ||
|
|
||
| if (props.size !== 2) { | ||
| return null; | ||
| } | ||
|
|
||
| const modifiersProp = props.get("modifiers"); | ||
| const keyProp = props.get("key"); | ||
| if (!modifiersProp || !keyProp) return null; | ||
| if ( | ||
| modifiersProp.value.type !== AST_NODE_TYPES.ArrayExpression || | ||
| keyProp.value.type !== AST_NODE_TYPES.Literal || | ||
| typeof keyProp.value.value !== "string" | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const modifiers: string[] = []; | ||
| for (const el of modifiersProp.value.elements) { | ||
| if (!el) continue; | ||
| if (el.type !== AST_NODE_TYPES.Literal || typeof el.value !== "string") { | ||
| return null; | ||
| } | ||
| modifiers.push(el.value); | ||
| } | ||
|
|
||
| return { modifiers, key: keyProp.value.value }; | ||
| } | ||
|
|
||
| function isAmbiguous(modifiers: string[]) { | ||
| const hasCmd = modifiers.includes("cmd"); | ||
| const hasCtrl = modifiers.includes("ctrl"); | ||
| return (hasCmd || hasCtrl) && !(hasCmd && hasCtrl); | ||
| } | ||
|
|
||
| export default createRule({ | ||
| name: "no-ambiguous-platform-shortcut", | ||
| meta: { | ||
| type: "problem", | ||
| docs: { | ||
| description: | ||
| "Warn when a shortcut is ambiguous in cross-platform extensions.", | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| ambiguous: | ||
| "This shortcut is ambiguous across platforms. Provide platform-specific shortcuts.", | ||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const hasMultiPlatform = hasMultiPlatformConfig(context.filename); | ||
|
|
||
| if (!hasMultiPlatform) { | ||
| return {}; | ||
| } | ||
|
|
||
| return { | ||
| JSXAttribute(node) { | ||
| if ( | ||
| node.name.type === AST_NODE_TYPES.JSXIdentifier && | ||
| node.name.name === "shortcut" && | ||
| node.value && | ||
| node.value.type === AST_NODE_TYPES.JSXExpressionContainer && | ||
| node.value.expression.type === AST_NODE_TYPES.ObjectExpression | ||
| ) { | ||
| const simple = parseSimpleShortcut(node.value.expression); | ||
| if (!simple) return; | ||
| if (!isAmbiguous(simple.modifiers)) return; | ||
|
|
||
| context.report({ | ||
| node: node.value, | ||
| messageId: "ambiguous", | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
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.
The global
packagePlatformsCacheMap (line 10) persists across multiple linting runs. In a long-running ESLint process (like in an IDE), if a package.json file is modified, the cache will return stale data. Consider either clearing the cache between runs or using ESLint's built-in caching mechanisms, or documenting this limitation.