|
| 1 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; |
| 2 | + |
| 3 | +import { createRule } from './utils/create-rule'; |
| 4 | + |
| 5 | +export const RULE_NAME = 'prefer-fluentui-v9'; |
| 6 | + |
| 7 | +type Options = Array<{}>; |
| 8 | + |
| 9 | +type MessageIds = 'replaceFluent8With9' | 'replaceIconWithJsx' | 'replaceStackWithFlex' | 'replaceFocusZoneWithTabster'; |
| 10 | + |
| 11 | +export const rule = createRule<Options, MessageIds>({ |
| 12 | + name: RULE_NAME, |
| 13 | + meta: { |
| 14 | + type: 'problem', |
| 15 | + docs: { |
| 16 | + description: 'This rule ensures the use of Fluent UI v9 counterparts for Fluent UI v8 components.', |
| 17 | + }, |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + replaceFluent8With9: `Avoid importing {{ fluent8 }} from '@fluentui/react', as this package has started migration to Fluent UI 9. Import {{ fluent9 }} from '{{ package }}' instead.`, |
| 21 | + replaceIconWithJsx: `Avoid using Icon from '@fluentui/react', as this package has already migrated to Fluent UI 9. Use a JSX SVG icon from '@fluentui/react-icons' instead.`, |
| 22 | + replaceStackWithFlex: `Avoid using Stack from '@fluentui/react', as this package has already migrated to Fluent UI 9. Use native CSS flexbox instead. More details are available at https://react.fluentui.dev/?path=/docs/concepts-migration-from-v8-components-flex-stack--docs`, |
| 23 | + replaceFocusZoneWithTabster: `Avoid using {{ fluent8 }} from '@fluentui/react', as this package has already migrated to Fluent UI 9. Use the equivalent [Tabster](https://tabster.io/) hook instead.`, |
| 24 | + }, |
| 25 | + }, |
| 26 | + defaultOptions: [], |
| 27 | + create(context) { |
| 28 | + return { |
| 29 | + ImportDeclaration(node) { |
| 30 | + if (node.source.value !== '@fluentui/react') { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + for (const specifier of node.specifiers) { |
| 35 | + if ( |
| 36 | + specifier.type === AST_NODE_TYPES.ImportSpecifier && |
| 37 | + specifier.imported.type === AST_NODE_TYPES.Identifier |
| 38 | + ) { |
| 39 | + const name = specifier.imported.name; |
| 40 | + |
| 41 | + switch (name) { |
| 42 | + case 'Icon': |
| 43 | + context.report({ node, messageId: 'replaceIconWithJsx' }); |
| 44 | + break; |
| 45 | + case 'Stack': |
| 46 | + context.report({ node, messageId: 'replaceStackWithFlex' }); |
| 47 | + break; |
| 48 | + case 'FocusTrapZone': |
| 49 | + case 'FocusZone': |
| 50 | + context.report({ node, messageId: 'replaceFocusZoneWithTabster', data: { fluent8: name } }); |
| 51 | + break; |
| 52 | + default: |
| 53 | + if (isMigration(name)) { |
| 54 | + const migration = MIGRATIONS[name]; |
| 55 | + |
| 56 | + context.report({ |
| 57 | + node, |
| 58 | + messageId: 'replaceFluent8With9', |
| 59 | + data: { |
| 60 | + fluent8: name, |
| 61 | + fluent9: migration.import, |
| 62 | + package: migration.package, |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + }; |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +/** |
| 75 | + * Migrations from Fluent 8 components to Fluent 9 components. |
| 76 | + * @see https://react.fluentui.dev/?path=/docs/concepts-migration-from-v8-component-mapping--docs |
| 77 | + */ |
| 78 | +const MIGRATIONS = { |
| 79 | + makeStyles: { import: 'makeStyles', package: '@fluentui/react-components' }, |
| 80 | + ActionButton: { import: 'Button', package: '@fluentui/react-components' }, |
| 81 | + Announced: { import: 'useAnnounce', package: '@fluentui/react-components' }, |
| 82 | + Breadcrumb: { import: 'Breadcrumb', package: '@fluentui/react-components' }, |
| 83 | + Button: { import: 'Button', package: '@fluentui/react-components' }, |
| 84 | + Callout: { import: 'Popover', package: '@fluentui/react-components' }, |
| 85 | + Calendar: { import: 'Calendar', package: '@fluentui/react-calendar-compat' }, |
| 86 | + CommandBar: { import: 'Toolbar', package: '@fluentui/react-components' }, |
| 87 | + CommandBarButton: { import: 'Toolbar', package: '@fluentui/react-components' }, |
| 88 | + CommandButton: { import: 'MenuButton', package: '@fluentui/react-components' }, |
| 89 | + CompoundButton: { import: 'CompoundButton', package: '@fluentui/react-components' }, |
| 90 | + Checkbox: { import: 'Checkbox', package: '@fluentui/react-components' }, |
| 91 | + ChoiceGroup: { import: 'RadioGroup', package: '@fluentui/react-components' }, |
| 92 | + Coachmark: { import: 'TeachingPopover', package: '@fluentui/react-components' }, |
| 93 | + ComboBox: { import: 'Combobox', package: '@fluentui/react-components' }, |
| 94 | + ContextualMenu: { import: 'Menu', package: '@fluentui/react-components' }, |
| 95 | + DefaultButton: { import: 'Button', package: '@fluentui/react-components' }, |
| 96 | + DatePicker: { import: 'DatePicker', package: '@fluentui/react-datepicker-compat' }, |
| 97 | + DetailsList: { import: 'DataGrid', package: '@fluentui/react-components' }, |
| 98 | + Dialog: { import: 'Dialog', package: '@fluentui/react-components' }, |
| 99 | + DocumentCard: { import: 'Card', package: '@fluentui/react-components' }, |
| 100 | + Dropdown: { import: 'Dropdown', package: '@fluentui/react-components' }, |
| 101 | + Fabric: { import: 'FluentProvider', package: '@fluentui/react-components' }, |
| 102 | + Facepile: { import: 'AvatarGroup', package: '@fluentui/react-components' }, |
| 103 | + FocusTrapZone: { import: 'Tabster', package: '@fluentui/react-components' }, |
| 104 | + FocusZone: { import: 'Tabster', package: '@fluentui/react-components' }, |
| 105 | + GroupedList: { import: 'Tree', package: '@fluentui/react-components' }, |
| 106 | + HoverCard: { import: 'Popover', package: '@fluentui/react-components' }, // Not a direct equivalent; but could be used with custom behavior. |
| 107 | + IconButton: { import: 'Button', package: '@fluentui/react-components' }, |
| 108 | + Image: { import: 'Image', package: '@fluentui/react-components' }, |
| 109 | + Keytips: { import: 'Keytips', package: '@fluentui-contrib/react-keytips' }, |
| 110 | + Label: { import: 'Label', package: '@fluentui/react-components' }, |
| 111 | + Layer: { import: 'Portal', package: '@fluentui/react-components' }, |
| 112 | + Link: { import: 'Link', package: '@fluentui/react-components' }, |
| 113 | + MessageBar: { import: 'MessageBar', package: '@fluentui/react-components' }, |
| 114 | + Modal: { import: 'Dialog', package: '@fluentui/react-components' }, |
| 115 | + OverflowSet: { import: 'Overflow', package: '@fluentui/react-components' }, |
| 116 | + Overlay: { import: 'Portal', package: '@fluentui/react-components' }, |
| 117 | + Panel: { import: 'Drawer', package: '@fluentui/react-components' }, |
| 118 | + PeoplePicker: { import: 'TagPicker', package: '@fluentui/react-components' }, |
| 119 | + Persona: { import: 'Persona', package: '@fluentui/react-components' }, |
| 120 | + Pivot: { import: 'TabList', package: '@fluentui/react-components' }, |
| 121 | + PivotItem: { import: 'Tab', package: '@fluentui/react-components' }, |
| 122 | + ProgressIndicator: { import: 'ProgressBar', package: '@fluentui/react-components' }, |
| 123 | + Rating: { import: 'Rating', package: '@fluentui/react-components' }, |
| 124 | + SearchBox: { import: 'SearchBox', package: '@fluentui/react-components' }, |
| 125 | + Separator: { import: 'Divider', package: '@fluentui/react-components' }, |
| 126 | + Shimmer: { import: 'Skeleton', package: '@fluentui/react-components' }, |
| 127 | + Slider: { import: 'Slider', package: '@fluentui/react-components' }, |
| 128 | + SplitButton: { import: 'SplitButton', package: '@fluentui/react-components' }, |
| 129 | + SpinButton: { import: 'SpinButton', package: '@fluentui/react-components' }, |
| 130 | + Spinner: { import: 'Spinner', package: '@fluentui/react-components' }, |
| 131 | + Stack: { import: 'StackShim', package: '@fluentui/react-components' }, |
| 132 | + SwatchColorPicker: { import: 'SwatchPicker', package: '@fluentui/react-components' }, |
| 133 | + TagPicker: { import: 'TagPicker', package: '@fluentui/react-components' }, |
| 134 | + TeachingBubble: { import: 'TeachingPopover', package: '@fluentui/react-components' }, |
| 135 | + Text: { import: 'Text', package: '@fluentui/react-components' }, |
| 136 | + TextField: { import: 'Input', package: '@fluentui/react-components' }, |
| 137 | + TimePicker: { import: 'TimePicker', package: '@fluentui/react-timepicker-compat' }, |
| 138 | + ToggleButton: { import: 'ToggleButton', package: '@fluentui/react-components' }, |
| 139 | + Toggle: { import: 'Switch', package: '@fluentui/react-components' }, |
| 140 | + Tooltip: { import: 'Tooltip', package: '@fluentui/react-components' }, |
| 141 | +}; |
| 142 | + |
| 143 | +/** |
| 144 | + * Checks if a component name is in the MIGRATIONS list. |
| 145 | + * @param name - The name of the component. |
| 146 | + * @returns True if the component is in the MIGRATIONS list, false otherwise. |
| 147 | + */ |
| 148 | +const isMigration = (name: string): name is keyof typeof MIGRATIONS => name in MIGRATIONS; |
0 commit comments