Skip to content
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

fix(no-uninstalled-addons): ignore local addons #98

Merged
merged 1 commit into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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