-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(eslint-rules): add consistent-base-hook rule for v9 base hooks #36236
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
Draft
Hotell
wants to merge
8
commits into
microsoft:master
Choose a base branch
from
Hotell:tools/ws-lint/consistent-base-hook
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
89f79fb
feat(eslint-rules): add consistent-base-hook rule
Hotell 870b274
chore(eslint-plugin): enable consistent-base-hook for v9 sources
Hotell 1cf197c
chore(react-components): suppress consistent-base-hook violations in …
Hotell 881af30
feat(eslint-rules): allow useFocusWithin by default in consistent-bas…
Hotell 2c9c23d
chore(react-components): drop now-unneeded consistent-base-hook suppr…
Hotell 4331ed8
feat(eslint-rules): allow useFocusVisible by default in consistent-ba…
Hotell 5cbbb5a
feat(eslint-rules): allow keyborg-only react-tabster APIs and drop ke…
Hotell a023f05
chore(react-tooltip): drop now-unneeded consistent-base-hook suppress…
Hotell 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import { rule, RULE_NAME } from './consistent-base-hook'; | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run(RULE_NAME, rule, { | ||
| valid: [ | ||
| // Valid base hook: 2 Identifier params, no forbidden imports. | ||
| { | ||
| code: ` | ||
| import * as React from 'react'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| return { props, ref }; | ||
| }; | ||
| `, | ||
| }, | ||
| // Valid base hook declared as FunctionDeclaration. | ||
| { | ||
| code: ` | ||
| import { Ref } from 'react'; | ||
| export function useThingBase_unstable(props, ref: Ref<HTMLElement>) { | ||
| return { props, ref }; | ||
| } | ||
| `, | ||
| }, | ||
| // Valid base hook with only `props` (ref is optional). | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (props) => { | ||
| return { props }; | ||
| }; | ||
| `, | ||
| }, | ||
| // Forbidden import exists but is only used by a non-base hook in the same file. | ||
| { | ||
| code: ` | ||
| import { useArrowNavigationGroup } from '@fluentui/react-tabster'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| return { props, ref }; | ||
| }; | ||
| export const useThing_unstable = (props, ref) => { | ||
| const nav = useArrowNavigationGroup({}); | ||
| return { ...useThingBase_unstable(props, ref), nav }; | ||
| }; | ||
| `, | ||
| }, | ||
| // Non-base hook is not subject to the param-shape constraint. | ||
| { | ||
| code: ` | ||
| export const useThing_unstable = (props, ref, extra) => { | ||
| return { props, ref, extra }; | ||
| }; | ||
| `, | ||
| }, | ||
| // Allowlist opt-out: a specific imported name is permitted inside base hooks. | ||
| { | ||
| code: ` | ||
| import { useFocusFinders } from '@fluentui/react-tabster'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| const finders = useFocusFinders(); | ||
| return { props, ref, finders }; | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| forbiddenPackages: [{ name: '@fluentui/react-tabster', allow: ['useFocusFinders'] }], | ||
| }, | ||
| ], | ||
| }, | ||
| // Default allowlist: `useFocusWithin` and `useFocusVisible` from `@fluentui/react-tabster` are permitted inside base hooks. | ||
| { | ||
| code: ` | ||
| import { useFocusWithin, useFocusVisible } from '@fluentui/react-tabster'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLSpanElement>) => { | ||
| useFocusVisible(); | ||
| return { props, ref: useFocusWithin<HTMLSpanElement>() }; | ||
| }; | ||
| `, | ||
| }, | ||
| // `keyborg` is not forbidden by default — bindings imported from it are allowed inside base hooks. | ||
| { | ||
| code: ` | ||
| import { createKeyborg, KEYBORG_FOCUSIN } from 'keyborg'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| return { kb: createKeyborg(window), evt: KEYBORG_FOCUSIN }; | ||
| }; | ||
| `, | ||
| }, | ||
| // Identifier with the same local name as a forbidden import alias does not collide via scope analysis. | ||
| { | ||
| code: ` | ||
| import { useArrowNavigationGroup } from '@fluentui/react-tabster'; | ||
| export const useThing_unstable = (props, ref) => { | ||
| return useArrowNavigationGroup({}); | ||
| }; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| const useArrowNavigationGroup = () => 1; | ||
| return { value: useArrowNavigationGroup() }; | ||
| }; | ||
| `, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| // Too few params (0). | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = () => ({}); | ||
| `, | ||
| errors: [{ messageId: 'invalidParamCount', data: { hookName: 'useThingBase_unstable', actual: 0 } }], | ||
| }, | ||
| // Too many params. | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (props, ref, extra) => ({ props, ref, extra }); | ||
| `, | ||
| errors: [{ messageId: 'invalidParamCount', data: { hookName: 'useThingBase_unstable', actual: 3 } }], | ||
| }, | ||
| // Wrong param names. | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (p, r) => ({ p, r }); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidParamName', | ||
| data: { hookName: 'useThingBase_unstable', index: 1, expected: 'props', actual: 'p' }, | ||
| }, | ||
| { | ||
| messageId: 'invalidParamName', | ||
| data: { hookName: 'useThingBase_unstable', index: 2, expected: 'ref', actual: 'r' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // ObjectPattern for `props` is not allowed. | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = ({ a }, ref: React.Ref<HTMLElement>) => ({ a, ref }); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidParamName', | ||
| data: { hookName: 'useThingBase_unstable', index: 1, expected: 'props', actual: '{ ... }' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // `ref` parameter without a type annotation. | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (props, ref) => ({ props, ref }); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidRefType', | ||
| data: { hookName: 'useThingBase_unstable', actual: '<missing type annotation>' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // `ref` parameter typed as something other than React.Ref. | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (props, ref: HTMLElement) => ({ props, ref }); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidRefType', | ||
| data: { hookName: 'useThingBase_unstable', actual: 'HTMLElement' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // `ref` parameter typed as React.ForwardedRef (must be React.Ref). | ||
| { | ||
| code: ` | ||
| export const useThingBase_unstable = (props, ref: React.ForwardedRef<HTMLElement>) => ({ props, ref }); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidRefType', | ||
| data: { hookName: 'useThingBase_unstable', actual: 'React.ForwardedRef' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Body uses a binding imported from @fluentui/react-tabster. | ||
| { | ||
| code: ` | ||
| import { useArrowNavigationGroup } from '@fluentui/react-tabster'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| const nav = useArrowNavigationGroup({}); | ||
| return { props, ref, nav }; | ||
| }; | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'forbiddenPackageUsage', | ||
| data: { | ||
| hookName: 'useThingBase_unstable', | ||
| importedName: 'useArrowNavigationGroup', | ||
| package: '@fluentui/react-tabster', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Body uses a binding imported from tabster, even when aliased. | ||
| { | ||
| code: ` | ||
| import { getTabsterAttribute as gta } from 'tabster'; | ||
| export function useThingBase_unstable(props, ref: React.Ref<HTMLElement>) { | ||
| return { attr: gta({}) }; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'forbiddenPackageUsage', | ||
| data: { | ||
| hookName: 'useThingBase_unstable', | ||
| importedName: 'getTabsterAttribute', | ||
| package: 'tabster', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Allowlist excludes one name; siblings still error. | ||
| { | ||
| code: ` | ||
| import { useFocusFinders, useArrowNavigationGroup } from '@fluentui/react-tabster'; | ||
| export const useThingBase_unstable = (props, ref: React.Ref<HTMLElement>) => { | ||
| const finders = useFocusFinders(); | ||
| const nav = useArrowNavigationGroup({}); | ||
| return { props, ref, finders, nav }; | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| forbiddenPackages: [{ name: '@fluentui/react-tabster', allow: ['useFocusFinders'] }], | ||
| }, | ||
| ], | ||
| errors: [ | ||
| { | ||
| messageId: 'forbiddenPackageUsage', | ||
| data: { | ||
| hookName: 'useThingBase_unstable', | ||
| importedName: 'useArrowNavigationGroup', | ||
| package: '@fluentui/react-tabster', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
🕵🏾♀️ visual changes to review in the Visual Change Report
vr-tests-react-components/Charts-DonutChart 2 screenshots
vr-tests-react-components/Menu Converged - submenuIndicator slotted content 2 screenshots
vr-tests-react-components/Positioning 2 screenshots
vr-tests-react-components/ProgressBar converged 3 screenshots
vr-tests-react-components/SwatchPicker Converged 1 screenshots
vr-tests-react-components/TagPicker 1 screenshots
vr-tests-web-components/Accordion 1 screenshots
vr-tests-web-components/MenuList 1 screenshots
vr-tests-web-components/RadioGroup 1 screenshots
vr-tests/Callout 2 screenshots
vr-tests/react-charting-LineChart 4 screenshots
vr-tests/react-charting-VerticalBarChart 1 screenshots
There were 5 duplicate changes discarded. Check the build logs for more information.