Skip to content

Commit

Permalink
upgrade to typescript eslint utils v7
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jul 2, 2024
1 parent 92c47e4 commit 38fde17
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 153 deletions.
2 changes: 1 addition & 1 deletion lib/rules/await-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export = createStorybookRule({
docs: {
description: 'Interactions should be awaited',
categories: [CategoryId.ADDON_INTERACTIONS, CategoryId.RECOMMENDED],
recommended: 'error', // or 'warn'
recommended: 'strict',
},
messages: {
interactionShouldBeAwaited: 'Interaction should be awaited: {{method}}',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/context-in-play-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export = createStorybookRule({
docs: {
description: 'Pass a context when invoking play function of another story',
categories: [CategoryId.RECOMMENDED, CategoryId.ADDON_INTERACTIONS],
recommended: 'error',
recommended: 'strict',
},
messages: {
passContextToPlayFunction: 'Pass a context when invoking play function of another story',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/csf-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export = createStorybookRule({
docs: {
description: 'The component property should be set',
categories: [CategoryId.CSF],
recommended: 'warn',
recommended: 'recommended',
},
messages: {
missingComponentProperty: 'Missing component property.',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/default-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export = createStorybookRule({
docs: {
description: 'Story files should have a default export',
categories: [CategoryId.CSF, CategoryId.RECOMMENDED],
recommended: 'error',
recommended: 'strict',
},
messages: {
shouldHaveDefaultExport: 'The file should have a default export.',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/hierarchy-separator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export = createStorybookRule({
docs: {
description: 'Deprecated hierarchy separator in title property',
categories: [CategoryId.CSF, CategoryId.RECOMMENDED],
recommended: 'warn',
recommended: 'recommended',
},
messages: {
useCorrectSeparators: 'Use correct separators',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/meta-inline-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export = createStorybookRule({
description: 'Meta should only have inline properties',
categories: [CategoryId.CSF, CategoryId.RECOMMENDED],
excludeFromConfig: true,
recommended: 'error',
recommended: 'strict',
},
messages: {
metaShouldHaveInlineProperties: 'Meta should only have inline properties: {{property}}',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-redundant-story-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export = createStorybookRule({
docs: {
description: 'A story should not have a redundant name property',
categories: [CategoryId.CSF, CategoryId.RECOMMENDED],
recommended: 'warn',
recommended: 'recommended',
},
messages: {
removeRedundantName: 'Remove redundant name',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-stories-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export = createStorybookRule({
docs: {
description: 'storiesOf is deprecated and should not be used',
categories: [CategoryId.CSF_STRICT],
recommended: 'error',
recommended: 'strict',
},
messages: {
doNotUseStoriesOf: 'storiesOf is deprecated and should not be used',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-title-property-in-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export = createStorybookRule({
docs: {
description: 'Do not define a title in meta',
categories: [CategoryId.CSF_STRICT],
recommended: 'error',
recommended: 'strict',
},
messages: {
removeTitleInMeta: 'Remove title property from meta',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-uninstalled-addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export = createStorybookRule({
description:
'This rule identifies storybook addons that are invalid because they are either not installed or contain a typo in their name.',
categories: [CategoryId.RECOMMENDED],
recommended: 'error', // or 'error'
recommended: 'strict',
},
messages: {
addonIsNotInstalled: `The {{ addonName }} is not installed in {{packageJsonPath}}. Did you forget to install it or is your package.json in a different location?`,
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/prefer-pascal-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { getDescriptor, getMetaObjectExpression } from '../utils'
import { isIdentifier, isVariableDeclaration } from '../utils/ast'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
import { Scope } from '@typescript-eslint/utils/dist/ts-eslint-scope'

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -26,7 +25,7 @@ export = createStorybookRule({
docs: {
description: 'Stories should use PascalCase',
categories: [CategoryId.RECOMMENDED],
recommended: 'warn',
recommended: 'stylistic',
},
messages: {
convertToPascalCase: 'Use pascal case',
Expand Down Expand Up @@ -55,15 +54,16 @@ export = createStorybookRule({
.replace(new RegExp(/\w/), (s) => s.toUpperCase())
}
const getModuleScope = (): TSESLint.Scope.Scope | undefined => {
// @ts-expect-error TODO: when we will upgrade `@typescript-eslint/utils` v7.x from v5.x on this package, we should resolve type definion with latest version.
// In `@typescript-eslint/utils` v5.x, cannot resolve of `sourceCode`, because type definition is not still provide from that version.
const { sourceCode } = context

// Compatibility implementation for eslint v8.x and v9.x or later
// see https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/#context.getscope()
// @ts-expect-error keep it for compatibility with eslint v8.x
if (sourceCode.getScope) {
// for eslint v9.x or later
return sourceCode.scopeManager.scopes.find((scope: Scope) => scope.type === 'module')
return sourceCode.scopeManager?.scopes?.find(
(scope: TSESLint.Scope.Scope) => scope.type === 'module'
)
} else {
// for eslint v8.x
return context
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/story-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export = createStorybookRule({
docs: {
description: 'A story file must contain at least one story export',
categories: [CategoryId.RECOMMENDED, CategoryId.CSF],
recommended: 'error',
recommended: 'strict',
},
messages: {
shouldHaveStoryExport: 'The file should have at least one story export',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/use-storybook-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export = createStorybookRule<TDefaultOptions, string>({
docs: {
description: 'Use expect from `@storybook/jest`',
categories: [CategoryId.ADDON_INTERACTIONS, CategoryId.RECOMMENDED],
recommended: 'error',
recommended: 'strict',
},
messages: {
updateImports: 'Update imports',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/use-storybook-testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export = createStorybookRule({
docs: {
description: 'Do not use testing-library directly on stories',
categories: [CategoryId.ADDON_INTERACTIONS, CategoryId.RECOMMENDED],
recommended: 'error',
recommended: 'strict',
},
schema: [],
messages: {
Expand Down
17 changes: 10 additions & 7 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export type RuleModule = TSESLint.RuleModule<'', []> & {

// These 2 types are copied from @typescript-eslint/experimental-utils' CreateRuleMeta
// and modified to our needs
export type StorybookRuleMetaDocs = Omit<TSESLint.RuleMetaDataDocs, 'url'> & {
export type StorybookRuleMetaDocs<TOptions extends readonly unknown[]> = Omit<
TSESLint.RuleMetaDataDocs<TOptions>,
'url'
> & {
/**
* Whether or not this rule should be excluded from linter config
*/
Expand All @@ -18,17 +21,17 @@ export type StorybookRuleMetaDocs = Omit<TSESLint.RuleMetaDataDocs, 'url'> & {
categories?: CategoryId[]
}

export type StorybookRuleMeta<TMessageIds extends string> = Omit<
TSESLint.RuleMetaData<TMessageIds>,
'docs'
> & {
docs: StorybookRuleMetaDocs
export type StorybookRuleMeta<
TMessageIds extends string,
TOptions extends readonly unknown[],
> = Omit<TSESLint.RuleMetaData<TMessageIds, TOptions>, 'docs'> & {
docs: StorybookRuleMetaDocs<TOptions>
}

// Comment out for testing purposes:
// const docs: StorybookRuleMetaDocs = {
// description: 'bla',
// recommended: 'error',
// recommended: 'strict',
// }

// const meta: StorybookRuleMeta<'someId'> = {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/create-storybook-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function createStorybookRule<
...remainingConfig
}: Readonly<{
name: string
meta: StorybookRuleMeta<TMessageIds>
meta: StorybookRuleMeta<TMessageIds, TOptions>
defaultOptions: Readonly<TOptions>
create: (
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>,
Expand Down
2 changes: 0 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const getMetaObjectExpression = (
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>
) => {
let meta: TSESTree.ExportDefaultDeclaration['declaration'] | null = node.declaration
// @ts-expect-error TODO: when we will upgrade `@typescript-eslint/utils` v7.x from v5.x on this package, we should resolve type definion with latest version.
// In `@typescript-eslint/utils` v5.x, cannot resolve of `sourceCode`, because type definition is not still provide from that version.
const { sourceCode } = context
if (isIdentifier(meta)) {
// Compatibility implementation for eslint v8.x and v9.x or later
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"dependencies": {
"@storybook/csf": "^0.0.1",
"@typescript-eslint/utils": "^5.62.0",
"@typescript-eslint/utils": "^7.15.0",
"ts-dedent": "^2.2.0"
},
"devDependencies": {
Expand All @@ -63,8 +63,8 @@
"@types/node": "^18.19.17",
"@types/semver": "^7.5.8",
"@types/requireindex": "^1.2.4",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"auto": "^11.1.6",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
Loading

0 comments on commit 38fde17

Please sign in to comment.