|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const url = require('../url') |
| 4 | + |
| 5 | +const wildcardImports = new Map([ |
| 6 | + [ |
| 7 | + '@primer/react/lib-esm/sx', |
| 8 | + [ |
| 9 | + { |
| 10 | + type: 'type', |
| 11 | + name: 'BetterSystemStyleObject', |
| 12 | + from: '@primer/react', |
| 13 | + }, |
| 14 | + { |
| 15 | + type: 'type', |
| 16 | + name: 'SxProp', |
| 17 | + from: '@primer/react', |
| 18 | + }, |
| 19 | + { |
| 20 | + type: 'type', |
| 21 | + name: 'BetterCssProperties', |
| 22 | + from: '@primer/react', |
| 23 | + }, |
| 24 | + ], |
| 25 | + ], |
| 26 | + [ |
| 27 | + '@primer/react/lib-esm/useIsomorphicLayoutEffect', |
| 28 | + [ |
| 29 | + { |
| 30 | + name: 'default', |
| 31 | + from: '@primer/react', |
| 32 | + as: 'useIsomorphicLayoutEffect', |
| 33 | + }, |
| 34 | + ], |
| 35 | + ], |
| 36 | + [ |
| 37 | + '@primer/react/lib-esm/Token/IssueLabelToken', |
| 38 | + [ |
| 39 | + { |
| 40 | + type: 'type', |
| 41 | + name: 'IssueLabelTokenProps', |
| 42 | + from: '@primer/react', |
| 43 | + }, |
| 44 | + ], |
| 45 | + ], |
| 46 | + [ |
| 47 | + '@primer/react/lib-esm/deprecated/ActionList', |
| 48 | + [ |
| 49 | + { |
| 50 | + type: 'type', |
| 51 | + name: 'ItemProps', |
| 52 | + from: '@primer/react/deprecated', |
| 53 | + }, |
| 54 | + ], |
| 55 | + ], |
| 56 | + [ |
| 57 | + '@primer/react/lib-esm/deprecated/ActionList/List', |
| 58 | + [ |
| 59 | + { |
| 60 | + type: 'type', |
| 61 | + name: 'GroupedListProps', |
| 62 | + from: '@primer/react/deprecated', |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'ItemInput', |
| 66 | + from: '@primer/react/deprecated', |
| 67 | + }, |
| 68 | + ], |
| 69 | + ], |
| 70 | + [ |
| 71 | + '@primer/react/lib-esm/SelectPanel/SelectPanel', |
| 72 | + [ |
| 73 | + { |
| 74 | + name: 'SelectPanel', |
| 75 | + from: '@primer/react/experimental', |
| 76 | + }, |
| 77 | + { |
| 78 | + type: 'type', |
| 79 | + name: 'SelectPanelProps', |
| 80 | + from: '@primer/react/experimental', |
| 81 | + }, |
| 82 | + ], |
| 83 | + ], |
| 84 | + [ |
| 85 | + '@primer/react/lib-esm/_VisuallyHidden', |
| 86 | + [ |
| 87 | + { |
| 88 | + name: 'default', |
| 89 | + from: '@primer/react', |
| 90 | + as: 'VisuallyHidden', |
| 91 | + }, |
| 92 | + ], |
| 93 | + ], |
| 94 | + [ |
| 95 | + '@primer/react/lib-esm/hooks/useResizeObserver', |
| 96 | + [ |
| 97 | + { |
| 98 | + name: 'default', |
| 99 | + from: '@primer/react', |
| 100 | + as: 'useResizeObserver', |
| 101 | + }, |
| 102 | + ], |
| 103 | + ], |
| 104 | + [ |
| 105 | + '@primer/react/lib-esm/hooks/useProvidedRefOrCreate', |
| 106 | + [ |
| 107 | + { |
| 108 | + name: 'default', |
| 109 | + from: '@primer/react', |
| 110 | + as: 'useProvidedRefOrCreate', |
| 111 | + }, |
| 112 | + ], |
| 113 | + ], |
| 114 | + [ |
| 115 | + '@primer/react/lib-esm/Button/types', |
| 116 | + [ |
| 117 | + { |
| 118 | + type: 'type', |
| 119 | + name: 'ButtonBaseProps', |
| 120 | + from: '@primer/react', |
| 121 | + }, |
| 122 | + { |
| 123 | + name: 'ButtonBase', |
| 124 | + from: '@primer/react', |
| 125 | + }, |
| 126 | + ], |
| 127 | + ], |
| 128 | + [ |
| 129 | + '@primer/react/lib-esm/utils/polymorphic', |
| 130 | + [ |
| 131 | + { |
| 132 | + type: 'type', |
| 133 | + name: 'ForwardRefComponent', |
| 134 | + from: '@primer/react', |
| 135 | + }, |
| 136 | + ], |
| 137 | + ], |
| 138 | + [ |
| 139 | + '@primer/react/lib-esm/hooks/useResponsiveValue', |
| 140 | + [ |
| 141 | + { |
| 142 | + type: 'type', |
| 143 | + name: 'useResponsiveValue', |
| 144 | + from: '@primer/react', |
| 145 | + as: 'useResponsiveValue', |
| 146 | + }, |
| 147 | + ], |
| 148 | + ], |
| 149 | + [ |
| 150 | + '@primer/react/lib-esm/Dialog/Dialog', |
| 151 | + [ |
| 152 | + { |
| 153 | + name: 'Dialog', |
| 154 | + from: '@primer/react/experimental', |
| 155 | + }, |
| 156 | + ], |
| 157 | + ], |
| 158 | +]) |
| 159 | + |
| 160 | +/** |
| 161 | + * @type {import('eslint').Rule.RuleModule} |
| 162 | + */ |
| 163 | +module.exports = { |
| 164 | + meta: { |
| 165 | + type: 'problem', |
| 166 | + docs: { |
| 167 | + description: 'Wildcard imports are discouraged. Import from a main entrypoint instead', |
| 168 | + recommended: true, |
| 169 | + url: url(module), |
| 170 | + }, |
| 171 | + fixable: true, |
| 172 | + schema: [], |
| 173 | + }, |
| 174 | + create(context) { |
| 175 | + return { |
| 176 | + ImportDeclaration(node) { |
| 177 | + if (!node.source.value.startsWith('@primer/react/lib-esm')) { |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + const wildcardImportMigrations = wildcardImports.get(node.source.value) |
| 182 | + if (!wildcardImportMigrations) { |
| 183 | + context.report({ |
| 184 | + node, |
| 185 | + message: 'Wildcard imports from @primer/react are not allowed. Import from an entrypoint instead', |
| 186 | + }) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Maps entrypoint to array of changes. This tuple contains the new |
| 192 | + * imported name from the entrypoint along with the existing local name |
| 193 | + * @type {Map<string, Array<[string, string]>>} |
| 194 | + */ |
| 195 | + const changes = new Map() |
| 196 | + |
| 197 | + for (const specifier of node.specifiers) { |
| 198 | + const migration = wildcardImportMigrations.find(migration => { |
| 199 | + if (specifier.type === 'ImportDefaultSpecifier') { |
| 200 | + return migration.name === 'default' |
| 201 | + } |
| 202 | + return specifier.imported.name === migration.name |
| 203 | + }) |
| 204 | + |
| 205 | + // If we do not have a migration, we should report an error even if we |
| 206 | + // cannot autofix it |
| 207 | + if (!migration) { |
| 208 | + context.report({ |
| 209 | + node, |
| 210 | + message: `Wildcard import ${specifier.imported.name} from ${node.source.value} is not allowed. Import from an entrypoint instead`, |
| 211 | + }) |
| 212 | + break |
| 213 | + } |
| 214 | + |
| 215 | + if (!changes.has(migration.from)) { |
| 216 | + changes.set(migration.from, []) |
| 217 | + } |
| 218 | + |
| 219 | + if (migration.as) { |
| 220 | + changes.get(migration.from).push([migration.as, migration.as, migration.type]) |
| 221 | + } else { |
| 222 | + changes.get(migration.from).push([migration.name, specifier.local.name, migration.type]) |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if (changes.length !== 0) { |
| 227 | + context.report({ |
| 228 | + node, |
| 229 | + message: `Wildcard imports from ${node.source.value} are not allowed. Import from an entrypoint instead`, |
| 230 | + *fix(fixer) { |
| 231 | + for (const [entrypoint, importSpecifiers] of changes) { |
| 232 | + const allTypeImports = importSpecifiers.every(([_, __, type]) => type === 'type') |
| 233 | + const importStatement = allTypeImports ? 'import type' : 'import' |
| 234 | + const specifiers = importSpecifiers |
| 235 | + .map(([imported, local, type]) => { |
| 236 | + const prefix = allTypeImports ? '' : type === 'type' ? 'type ' : '' |
| 237 | + if (imported === local) { |
| 238 | + return `${prefix}${imported}` |
| 239 | + } |
| 240 | + |
| 241 | + return `${prefix}${imported} as ${local}` |
| 242 | + }) |
| 243 | + .join(', ') |
| 244 | + yield fixer.replaceText(node, `${importStatement} {${specifiers}} from '${entrypoint}'`) |
| 245 | + } |
| 246 | + }, |
| 247 | + }) |
| 248 | + } |
| 249 | + }, |
| 250 | + } |
| 251 | + }, |
| 252 | +} |
0 commit comments