Skip to content

Commit

Permalink
Type configs using literal types
Browse files Browse the repository at this point in the history
By using `as const` on the string values for each of the rule configs,
we ensure that the generated rules have string literal types matching
their configured values.

Fixes #181
  • Loading branch information
ethanwu10 committed Nov 15, 2024
1 parent 3e31c5b commit ad49a03
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/configs/addon-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export = {
'storybook/context-in-play-function': 'error',
'storybook/use-storybook-expect': 'error',
'storybook/use-storybook-testing-library': 'error',
},
} as const,
},
{
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
],
}
2 changes: 1 addition & 1 deletion lib/configs/csf-strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export = {
'import/no-anonymous-default-export': 'off',
'storybook/no-stories-of': 'error',
'storybook/no-title-property-in-meta': 'error',
},
} as const,
}
4 changes: 2 additions & 2 deletions lib/configs/csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export = {
'storybook/hierarchy-separator': 'warn',
'storybook/no-redundant-story-name': 'warn',
'storybook/story-exports': 'error',
},
} as const,
},
{
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
],
}
4 changes: 2 additions & 2 deletions lib/configs/flat/addon-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export = [
'storybook/context-in-play-function': 'error',
'storybook/use-storybook-expect': 'error',
'storybook/use-storybook-testing-library': 'error',
},
} as const,
},
{
name: 'storybook:addon-interactions:main-rules',
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
]
2 changes: 1 addition & 1 deletion lib/configs/flat/csf-strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export = [
'import/no-anonymous-default-export': 'off',
'storybook/no-stories-of': 'error',
'storybook/no-title-property-in-meta': 'error',
},
} as const,
},
]
4 changes: 2 additions & 2 deletions lib/configs/flat/csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export = [
'storybook/hierarchy-separator': 'warn',
'storybook/no-redundant-story-name': 'warn',
'storybook/story-exports': 'error',
},
} as const,
},
{
name: 'storybook:csf:main-rules',
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
]
4 changes: 2 additions & 2 deletions lib/configs/flat/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export = [
'storybook/story-exports': 'error',
'storybook/use-storybook-expect': 'error',
'storybook/use-storybook-testing-library': 'error',
},
} as const,
},
{
name: 'storybook:recommended:main-rules',
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
]
4 changes: 2 additions & 2 deletions lib/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export = {
'storybook/story-exports': 'error',
'storybook/use-storybook-expect': 'error',
'storybook/use-storybook-testing-library': 'error',
},
} as const,
},
{
files: ['.storybook/main.@(js|cjs|mjs|ts)'],
rules: {
'storybook/no-uninstalled-addons': 'error',
},
} as const,
},
],
}
4 changes: 2 additions & 2 deletions tools/utils/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export function formatRules(rules: TCategory['rules'], exclude?: string[]) {
{ ...externalRuleOverrides }
)

return JSON.stringify(obj, null, 2)
return JSON.stringify(obj, null, 2) + ' as const'
}

export function formatSingleRule(rules: TCategory['rules'], ruleId: string) {
const ruleOpt = rules.find((rule) => rule.ruleId === ruleId)?.meta.severity || 'error'

return JSON.stringify({ [ruleId]: ruleOpt }, null, 2)
return JSON.stringify({ [ruleId]: ruleOpt }, null, 2) + ' as const'
}

export const SUPPORTED_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs']
Expand Down

0 comments on commit ad49a03

Please sign in to comment.