Skip to content

Commit

Permalink
Merge pull request #139 from hjoelh/additional-'no-uninstalled-addons…
Browse files Browse the repository at this point in the history
…'-support

no-uninstalled-addons: support satisfies operator and default reexport
  • Loading branch information
yannbf authored Oct 8, 2023
2 parents de1edff + 73883f4 commit 0fb2407
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/rules/no-uninstalled-addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isVariableDeclaration,
} from '../utils/ast'
import { TSESTree } from '@typescript-eslint/utils'
import { getMetaObjectExpression } from '../utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -228,34 +229,32 @@ export = createStorybookRule({
}
}

function findAddonsPropAndReport(node: TSESTree.ObjectExpression) {
const addonsProp = node.properties.find(
(prop): prop is TSESTree.Property =>
isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'addons'
)

if (addonsProp?.value && isArrayExpression(addonsProp.value)) {
reportUninstalledAddons(addonsProp.value)
}
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------

return {
AssignmentExpression: function (node) {
if (isObjectExpression(node.right)) {
const addonsProp = node.right.properties.find(
(prop): prop is TSESTree.Property =>
isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'addons'
)

if (addonsProp && addonsProp.value && isArrayExpression(addonsProp.value)) {
reportUninstalledAddons(addonsProp.value)
}
findAddonsPropAndReport(node.right)
}
},
ExportDefaultDeclaration: function (node) {
if (isObjectExpression(node.declaration)) {
const addonsProp = node.declaration.properties.find(
(prop): prop is TSESTree.Property =>
isProperty(prop) && isIdentifier(prop.key) && prop.key.name === 'addons'
)
const meta = getMetaObjectExpression(node, context)
if (!meta) return null

if (addonsProp && addonsProp.value && isArrayExpression(addonsProp.value)) {
reportUninstalledAddons(addonsProp.value)
}
}
findAddonsPropAndReport(meta)
},
ExportNamedDeclaration: function (node) {
const addonsProp =
Expand Down
84 changes: 84 additions & 0 deletions tests/lib/rules/no-uninstalled-addons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ ruleTester.run('no-uninstalled-addons', rule, {
"@storybook/preset-create-react-app"
]
}
`,
`
export default {
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/preset-create-react-app"
]
} satisfies StorybookConfig
`,
`
const config: StorybookConfig = {
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/preset-create-react-app"
]
}
export default config
`,
`
export const addons = [
Expand Down Expand Up @@ -291,6 +312,69 @@ ruleTester.run('no-uninstalled-addons', rule, {
},
],
},
{
code: `
export default {
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"addon-withut-the-prefix",
"@storybook/addon-esentials",
]
} satisfies StorybookConfig
`,
errors: [
{
messageId: 'addonIsNotInstalled', // comes from the rule file
type: AST_NODE_TYPES.Literal,
data: {
addonName: 'addon-withut-the-prefix',
packageJsonPath: `eslint-plugin-storybook${sep}`,
},
},
{
messageId: 'addonIsNotInstalled', // comes from the rule file
type: AST_NODE_TYPES.Literal,
data: {
addonName: '@storybook/addon-esentials',
packageJsonPath: `eslint-plugin-storybook${sep}`,
},
},
],
},
{
code: `
const config: StorybookConfig = {
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"addon-withut-the-prefix",
"@storybook/addon-esentials",
]
}
export default config
`,
errors: [
{
messageId: 'addonIsNotInstalled', // comes from the rule file
type: AST_NODE_TYPES.Literal,
data: {
addonName: 'addon-withut-the-prefix',
packageJsonPath: `eslint-plugin-storybook${sep}`,
},
},
{
messageId: 'addonIsNotInstalled', // comes from the rule file
type: AST_NODE_TYPES.Literal,
data: {
addonName: '@storybook/addon-esentials',
packageJsonPath: `eslint-plugin-storybook${sep}`,
},
},
],
},
{
code: `
export const addons = [
Expand Down

0 comments on commit 0fb2407

Please sign in to comment.