|
| 1 | +/** |
| 2 | + * @fileoverview This rule identifies storybook addons that are invalid because they are either not installed or contain a typo in their name. |
| 3 | + * @author Andre "andrelas1" Santos |
| 4 | + */ |
| 5 | + |
| 6 | +import { readFileSync } from 'fs' |
| 7 | +import { resolve } from 'path' |
| 8 | + |
| 9 | +import { createStorybookRule } from '../utils/create-storybook-rule' |
| 10 | +import { CategoryId } from '../utils/constants' |
| 11 | +import { |
| 12 | + isObjectExpression, |
| 13 | + isProperty, |
| 14 | + isIdentifier, |
| 15 | + isArrayExpression, |
| 16 | + isLiteral, |
| 17 | + isVariableDeclarator, |
| 18 | + isVariableDeclaration, |
| 19 | +} from '../utils/ast' |
| 20 | +import { Property, ArrayExpression } from '@typescript-eslint/types/dist/ast-spec' |
| 21 | + |
| 22 | +//------------------------------------------------------------------------------ |
| 23 | +// Rule Definition |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | + |
| 26 | +export = createStorybookRule({ |
| 27 | + name: 'no-uninstalled-addons', |
| 28 | + defaultOptions: [], |
| 29 | + meta: { |
| 30 | + type: 'problem', |
| 31 | + docs: { |
| 32 | + description: |
| 33 | + 'This rule identifies storybook addons that are invalid because they are either not installed or contain a typo in their name.', |
| 34 | + categories: [CategoryId.RECOMMENDED], |
| 35 | + recommended: 'error', // or 'error' |
| 36 | + }, |
| 37 | + messages: { |
| 38 | + addonIsNotInstalled: `The {{ addonName }} is not installed. Did you forget to install it?`, |
| 39 | + }, |
| 40 | + |
| 41 | + schema: [], // Add a schema if the rule has options. Otherwise remove this |
| 42 | + }, |
| 43 | + |
| 44 | + create(context) { |
| 45 | + // variables should be defined here |
| 46 | + |
| 47 | + //---------------------------------------------------------------------- |
| 48 | + // Helpers |
| 49 | + //---------------------------------------------------------------------- |
| 50 | + |
| 51 | + // this will not only exclude the nullables but it will also exclude the type undefined from them, so that TS does not complain |
| 52 | + function excludeNullable<T>(item: T | undefined): item is T { |
| 53 | + return !!item |
| 54 | + } |
| 55 | + |
| 56 | + type MergeDepsWithDevDeps = (packageJson: Record<string, string>) => string[] |
| 57 | + const mergeDepsWithDevDeps: MergeDepsWithDevDeps = (packageJson) => { |
| 58 | + const deps = Object.keys(packageJson.dependencies || {}) |
| 59 | + const devDeps = Object.keys(packageJson.devDependencies || {}) |
| 60 | + return [...deps, ...devDeps] |
| 61 | + } |
| 62 | + |
| 63 | + type IsAddonInstalled = (addon: string, installedAddons: string[]) => boolean |
| 64 | + const isAddonInstalled: IsAddonInstalled = (addon, installedAddons) => { |
| 65 | + // cleanup /register or /preset from registered addon |
| 66 | + const addonName = addon.replace(/\/register$/, '').replace(/\/preset$/, '') |
| 67 | + return installedAddons.includes(addonName) |
| 68 | + } |
| 69 | + |
| 70 | + type AreThereAddonsNotInstalled = ( |
| 71 | + addons: string[], |
| 72 | + installedSbAddons: string[] |
| 73 | + ) => false | { name: string }[] |
| 74 | + const areThereAddonsNotInstalled: AreThereAddonsNotInstalled = (addons, installedSbAddons) => { |
| 75 | + const result = addons |
| 76 | + .filter((addon) => !isAddonInstalled(addon, installedSbAddons)) |
| 77 | + .map((addon) => ({ name: addon })) |
| 78 | + return result.length ? result : false |
| 79 | + } |
| 80 | + |
| 81 | + type GetPackageJson = (path: string) => Record<string, any> |
| 82 | + |
| 83 | + const getPackageJson: GetPackageJson = (path) => { |
| 84 | + const packageJson = { |
| 85 | + devDependencies: {}, |
| 86 | + dependencies: {}, |
| 87 | + } |
| 88 | + try { |
| 89 | + const file = readFileSync(path, 'utf8') |
| 90 | + const parsedFile = JSON.parse(file) |
| 91 | + packageJson.dependencies = parsedFile.dependencies || {} |
| 92 | + packageJson.devDependencies = parsedFile.devDependencies || {} |
| 93 | + } catch (e) { |
| 94 | + throw new Error( |
| 95 | + 'Could not fetch package.json - it is probably not in the same directory as the .storybook folder' |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + return packageJson |
| 100 | + } |
| 101 | + |
| 102 | + const extractAllAddonsFromTheStorybookConfig = ( |
| 103 | + addonsExpression: ArrayExpression | undefined |
| 104 | + ) => { |
| 105 | + if (addonsExpression?.elements) { |
| 106 | + // extract all nodes taht are a string inside the addons array |
| 107 | + const nodesWithAddons = addonsExpression.elements |
| 108 | + .map((elem) => (isLiteral(elem) ? { value: elem.value, node: elem } : undefined)) |
| 109 | + .filter(excludeNullable) |
| 110 | + |
| 111 | + const listOfAddonsInString = nodesWithAddons.map((elem) => elem.value) as string[] |
| 112 | + |
| 113 | + // extract all nodes that are an object inside the addons array |
| 114 | + const nodesWithAddonsInObj = addonsExpression.elements |
| 115 | + .map((elem) => (isObjectExpression(elem) ? elem : { properties: [] })) |
| 116 | + .map((elem) => { |
| 117 | + const property: Property = elem.properties.find( |
| 118 | + (prop) => isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'name' |
| 119 | + ) as Property |
| 120 | + return isLiteral(property?.value) |
| 121 | + ? { value: property.value.value, node: property.value } |
| 122 | + : undefined |
| 123 | + }) |
| 124 | + .filter(excludeNullable) |
| 125 | + |
| 126 | + const listOfAddonsInObj = nodesWithAddonsInObj.map((elem) => elem.value) as string[] |
| 127 | + |
| 128 | + const listOfAddons = [...listOfAddonsInString, ...listOfAddonsInObj] |
| 129 | + const listOfAddonElements = [...nodesWithAddons, ...nodesWithAddonsInObj] |
| 130 | + return { listOfAddons, listOfAddonElements } |
| 131 | + } |
| 132 | + |
| 133 | + return { listOfAddons: [], listOfAddonElements: [] } |
| 134 | + } |
| 135 | + |
| 136 | + function reportUninstalledAddons(addonsProp: ArrayExpression) { |
| 137 | + // when this is running for .storybook/main.js, we get the path to the folder which contains the package.json of the |
| 138 | + // project. This will be handy for monorepos that may be running ESLint in a node process in another folder. |
| 139 | + const projectRoot = context.getPhysicalFilename |
| 140 | + ? resolve(context.getPhysicalFilename(), '../../') |
| 141 | + : './' |
| 142 | + let packageJsonObject: Record<string, any> |
| 143 | + try { |
| 144 | + packageJsonObject = getPackageJson(`${projectRoot}/package.json`) |
| 145 | + } catch (e) { |
| 146 | + // if we cannot find the package.json, we cannot check if the addons are installed |
| 147 | + console.error(e) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + const depsAndDevDeps = mergeDepsWithDevDeps(packageJsonObject) |
| 152 | + |
| 153 | + const { listOfAddons, listOfAddonElements } = |
| 154 | + extractAllAddonsFromTheStorybookConfig(addonsProp) |
| 155 | + const result = areThereAddonsNotInstalled(listOfAddons, depsAndDevDeps) |
| 156 | + |
| 157 | + if (result) { |
| 158 | + const elemsWithErrors = listOfAddonElements.filter( |
| 159 | + (elem) => !!result.find((addon) => addon.name === elem.value) |
| 160 | + ) |
| 161 | + elemsWithErrors.forEach((elem) => { |
| 162 | + context.report({ |
| 163 | + node: elem.node, |
| 164 | + messageId: 'addonIsNotInstalled', |
| 165 | + data: { addonName: elem.value }, |
| 166 | + }) |
| 167 | + }) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + //---------------------------------------------------------------------- |
| 172 | + // Public |
| 173 | + //---------------------------------------------------------------------- |
| 174 | + |
| 175 | + return { |
| 176 | + AssignmentExpression: function (node) { |
| 177 | + if (isObjectExpression(node.right)) { |
| 178 | + const addonsProp = node.right.properties.find( |
| 179 | + (prop): prop is Property => |
| 180 | + isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'addons' |
| 181 | + ) |
| 182 | + |
| 183 | + if (addonsProp && addonsProp.value && isArrayExpression(addonsProp.value)) { |
| 184 | + reportUninstalledAddons(addonsProp.value) |
| 185 | + } |
| 186 | + } |
| 187 | + }, |
| 188 | + ExportDefaultDeclaration: function (node) { |
| 189 | + if (isObjectExpression(node.declaration)) { |
| 190 | + const addonsProp = node.declaration.properties.find( |
| 191 | + (prop): prop is Property => |
| 192 | + isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'addons' |
| 193 | + ) |
| 194 | + |
| 195 | + if (addonsProp && addonsProp.value && isArrayExpression(addonsProp.value)) { |
| 196 | + reportUninstalledAddons(addonsProp.value) |
| 197 | + } |
| 198 | + } |
| 199 | + }, |
| 200 | + ExportNamedDeclaration: function (node) { |
| 201 | + const addonsProp = |
| 202 | + isVariableDeclaration(node.declaration) && |
| 203 | + node.declaration.declarations.find( |
| 204 | + (decl) => |
| 205 | + isVariableDeclarator(decl) && isIdentifier(decl.id) && decl.id.name === 'addons' |
| 206 | + ) |
| 207 | + |
| 208 | + if (addonsProp && isArrayExpression(addonsProp.init)) { |
| 209 | + reportUninstalledAddons(addonsProp.init) |
| 210 | + } |
| 211 | + }, |
| 212 | + } |
| 213 | + }, |
| 214 | +}) |
0 commit comments