Skip to content

Commit

Permalink
Merge pull request #98 from storybookjs/fix/ignore-local-addons
Browse files Browse the repository at this point in the history
fix(no-uninstalled-addons): ignore local addons
  • Loading branch information
yannbf committed Jul 12, 2022
2 parents fa6290d + 29d633d commit 193c9b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
10 changes: 8 additions & 2 deletions lib/rules/no-uninstalled-addons.ts
Expand Up @@ -62,8 +62,12 @@ export = createStorybookRule({

type IsAddonInstalled = (addon: string, installedAddons: string[]) => boolean
const isAddonInstalled: IsAddonInstalled = (addon, installedAddons) => {
// cleanup /register or /preset from registered addon
const addonName = addon.replace(/\/register$/, '').replace(/\/preset$/, '')
// cleanup /register or /preset + file extension from registered addon
const addonName = addon
.replace(/\.[mc]?js$/, '')
.replace(/\/register$/, '')
.replace(/\/preset$/, '')

return installedAddons.includes(addonName)
}

Expand All @@ -73,6 +77,8 @@ export = createStorybookRule({
) => false | { name: string }[]
const areThereAddonsNotInstalled: AreThereAddonsNotInstalled = (addons, installedSbAddons) => {
const result = addons
// remove local addons (e.g. ./my-addon/register.js)
.filter((addon) => !addon.startsWith('.'))
.filter((addon) => !isAddonInstalled(addon, installedSbAddons))
.map((addon) => ({ name: addon }))
return result.length ? result : false
Expand Down
37 changes: 11 additions & 26 deletions tests/lib/rules/no-uninstalled-addons.test.ts
Expand Up @@ -16,32 +16,13 @@ jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: () => `
{
"name": "react-repro",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"packageManager": "yarn@3.2.1",
"devDependencies": {
"@babel/core": "^7.18.5",
"@mdx-js/react": "^1.6.22",
"@storybook/addon-actions": "^6.5.9",
"@storybook/addon-docs": "^6.5.9",
"@storybook/addon-essentials": "^6.5.9",
"@storybook/addon-interactions": "^6.5.9",
"@storybook/preset-create-react-app": "^6.5.9",
"@storybook/addon-links": "^6.5.9",
"@storybook/builder-webpack4": "^6.5.9",
"@storybook/manager-webpack4": "^6.5.9",
"@storybook/react": "^6.5.9",
"@storybook/testing-library": "^0.0.13",
"storybook-addon-valid-addon": "0.0.1",
"addon-without-the-prefix": "^0.0.1",
"babel-loader": "^8.2.5",
"prop-types": "^15.8.1"
"addon-without-the-prefix": "^0.0.1"
}
}
`,
Expand All @@ -52,11 +33,6 @@ jest.mock('fs', () => ({
//------------------------------------------------------------------------------

ruleTester.run('no-uninstalled-addons', rule, {
/**
* This is an example test for a rule that reports an error in case a named export is called 'wrong'
* Use https://eslint.org/docs/developer-guide/working-with-rules for Eslint API
* And delete this entire comment block
*/
valid: [
`
export default {
Expand Down Expand Up @@ -112,6 +88,14 @@ ruleTester.run('no-uninstalled-addons', rule, {
]
}
`,
`
module.exports = {
addons: [
"../my-local-addon",
"../my-local-addon/index.cjs",
]
}
`,
`
module.exports = {
addons: [
Expand All @@ -124,7 +108,7 @@ ruleTester.run('no-uninstalled-addons', rule, {
name: "addon-without-the-prefix",
},
{
name: "storybook-addon-valid-addon/register",
name: "storybook-addon-valid-addon/register.js",
},
]
}
Expand Down Expand Up @@ -279,6 +263,7 @@ ruleTester.run('no-uninstalled-addons', rule, {
{
code: `
export const addons = [
"../my-local-addon",
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
Expand Down

0 comments on commit 193c9b0

Please sign in to comment.