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

chore: fix ESLint and TypeScript issues #113

Merged
merged 2 commits into from
Jan 10, 2023
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
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:eslint-plugin/recommended',
'plugin:node/recommended',
'plugin:prettier/recommended',
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
env: {
node: true,
},
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/await-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export = createStorybookRule({
*/

let isImportedFromStorybook = true
let invocationsThatShouldBeAwaited = [] as Array<{
const invocationsThatShouldBeAwaited = [] as Array<{
node: TSESTree.Node
method: TSESTree.Identifier
}>
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/context-in-play-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Yann Braga
*/

import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'

import { createStorybookRule } from '../utils/create-storybook-rule'
import { CategoryId } from '../utils/constants'
Expand Down Expand Up @@ -100,7 +100,7 @@ export = createStorybookRule({
// Public
//----------------------------------------------------------------------

let invocationsWithoutProperContext: TSESTree.Node[] = [];
const invocationsWithoutProperContext: TSESTree.Node[] = []

return {
CallExpression(node: TSESTree.CallExpression) {
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 @@ -3,7 +3,7 @@
* @author Yann Braga
*/

import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'
import { getMetaObjectExpression } from '../utils'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
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 @@ -3,7 +3,7 @@
* @author Yann Braga
*/

import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'
import { getMetaObjectExpression } from '../utils'
import { isLiteral, isSpreadElement } from '../utils/ast'
import { CategoryId } from '../utils/constants'
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 @@ -78,7 +78,7 @@ export = createStorybookRule({
}

const ruleProperties = ['title', 'args']
let dynamicProperties: TDynamicProperty[] = []
const dynamicProperties: TDynamicProperty[] = []

const metaNodes = meta.properties.filter(
(prop) => 'key' in prop && 'name' in prop.key && ruleProperties.includes(prop.key.name)
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 @@ -3,7 +3,7 @@
* @author Yann Braga
*/

import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'
import { getMetaObjectExpression } from '../utils'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
Expand Down
13 changes: 9 additions & 4 deletions lib/rules/no-uninstalled-addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
isVariableDeclarator,
isVariableDeclaration,
} from '../utils/ast'
import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -66,7 +66,7 @@ export = createStorybookRule({
return !!item
}

type MergeDepsWithDevDeps = (packageJson: Record<string, string>) => string[]
type MergeDepsWithDevDeps = (packageJson: PackageJsonDependencies) => string[]
const mergeDepsWithDevDeps: MergeDepsWithDevDeps = (packageJson) => {
const deps = Object.keys(packageJson.dependencies || {})
const devDeps = Object.keys(packageJson.devDependencies || {})
Expand Down Expand Up @@ -108,7 +108,12 @@ export = createStorybookRule({
return result.length ? result : false
}

type GetPackageJson = (path: string) => Record<string, any>
type PackageJsonDependencies = {
devDependencies: Record<string, string>
dependencies: Record<string, string>
}

type GetPackageJson = (path: string) => PackageJsonDependencies

const getPackageJson: GetPackageJson = (path) => {
const packageJson = {
Expand Down Expand Up @@ -168,7 +173,7 @@ export = createStorybookRule({

function reportUninstalledAddons(addonsProp: TSESTree.ArrayExpression) {
const packageJsonPath = resolve(packageJsonLocation || `./package.json`)
let packageJsonObject: Record<string, any>
let packageJsonObject: PackageJsonDependencies
try {
packageJsonObject = getPackageJson(packageJsonPath)
} catch (e) {
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/prefer-pascal-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Yann Braga
*/

import { ASTUtils, TSESTree } from "@typescript-eslint/utils";
import { ASTUtils, TSESTree } from '@typescript-eslint/utils'
import { IncludeExcludeOptions, isExportStory } from '@storybook/csf'

import { getDescriptor, getMetaObjectExpression } from '../utils'
Expand Down Expand Up @@ -83,7 +83,7 @@ export = createStorybookRule({
const referenceCount = variable?.references?.length || 0

for (let i = 0; i < referenceCount; i++) {
const ref = variable!.references[i]
const ref = variable?.references[i]
if (ref && !ref.init) {
yield fixer.replaceTextRange(ref.identifier.range, pascal)
}
Expand All @@ -102,7 +102,7 @@ export = createStorybookRule({

let meta
let nonStoryExportsConfig: IncludeExcludeOptions
let namedExports: TSESTree.Identifier[] = []
const namedExports: TSESTree.Identifier[] = []
let hasStoriesOfImport = false

return {
Expand All @@ -119,7 +119,9 @@ export = createStorybookRule({
excludeStories: getDescriptor(meta, 'excludeStories'),
includeStories: getDescriptor(meta, 'includeStories'),
}
} catch (err) {}
} catch (err) {
//
}
}
},
ExportNamedDeclaration: function (node: TSESTree.ExportNamedDeclaration) {
Expand Down
9 changes: 5 additions & 4 deletions lib/rules/story-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @author Yann Braga
*/


import { createStorybookRule } from '../utils/create-storybook-rule'
import { CategoryId } from '../utils/constants'
import {
Expand All @@ -14,7 +13,7 @@ import {
} from '../utils'
import { isImportDeclaration } from '../utils/ast'
import { IncludeExcludeOptions } from '@storybook/csf'
import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -54,7 +53,7 @@ export = createStorybookRule({
let hasStoriesOfImport = false
let nonStoryExportsConfig: IncludeExcludeOptions = {}
let meta: TSESTree.ObjectExpression | null
let namedExports: TSESTree.Identifier[] = []
const namedExports: TSESTree.Identifier[] = []

return {
ImportSpecifier(node) {
Expand All @@ -70,7 +69,9 @@ export = createStorybookRule({
excludeStories: getDescriptor(meta, 'excludeStories'),
includeStories: getDescriptor(meta, 'includeStories'),
}
} catch (err) {}
} catch (err) {
//
}
}
},
ExportNamedDeclaration: function (node) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/use-storybook-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CategoryId } from '../utils/constants'
import { isIdentifier, isImportSpecifier } from '../utils/ast'

import { createStorybookRule } from '../utils/create-storybook-rule'
import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -56,7 +56,7 @@ export = createStorybookRule<TDefaultOptions, string>({
//----------------------------------------------------------------------

let isImportingFromStorybookExpect = false
let expectInvocations: TSESTree.Identifier[] = []
const expectInvocations: TSESTree.Identifier[] = []

return {
ImportDeclaration(node) {
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 @@ -6,7 +6,7 @@
import { isImportDefaultSpecifier } from '../utils/ast'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
import { TSESTree } from "@typescript-eslint/utils";
import { TSESTree } from '@typescript-eslint/utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down
26 changes: 10 additions & 16 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ export type RuleModule = TSESLint.RuleModule<'', []> & {
meta: { hasSuggestions?: boolean; docs: { recommendedConfig?: 'error' | 'warn' } }
}

type RecommendedConfig<TOptions extends readonly unknown[]> =
| TSESLint.RuleMetaDataDocs['recommended']
| [TSESLint.RuleMetaDataDocs['recommended'], ...TOptions]

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

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

// const docs: StorybookRuleMetaDocs<any> = {
// recommended: true,
// Comment out for testing purposes:
// const docs: StorybookRuleMetaDocs = {
// description: 'bla',
// recommended: 'error',
// }

// const meta: StorybookRuleMeta<'someId', any> = {
// const meta: StorybookRuleMeta<'someId'> = {
// messages: {
// someId: 'yea',
// },
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, TOptions>
meta: StorybookRuleMeta<TMessageIds>
defaultOptions: Readonly<TOptions>
create: (
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>,
Expand Down
5 changes: 2 additions & 3 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ export const getDescriptor = (
if (!['StringLiteral', 'Literal'].includes(t.type)) {
throw new Error(`Unexpected descriptor element: ${t.type}`)
}
// @ts-ignore
// @ts-expect-error TODO: t should be only StringLiteral or Literal, and the type is not resolving correctly
return t.value
})
case 'Literal':
// TODO: Investigation needed. Type systems says, that "RegExpLiteral" does not exist
// @ts-ignore
// @ts-expect-error TODO: Investigation needed. Type systems says, that "RegExpLiteral" does not exist
case 'RegExpLiteral':
// @ts-ignore
return property.value.value
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@types/jest": "^27.0.2",
"@types/node": "^16.11.6",
"@types/requireindex": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"@typescript-eslint/parser": "^5.3.0",
"auto": "^10.32.2",
"eslint": "^7.1.0",
Expand Down
4 changes: 3 additions & 1 deletion tools/update-rules-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const rulesList: TRulesList[] = Object.entries(rules)
createRuleLink(rule.name),
rule.meta.docs.description,
rule.meta.fixable ? emojiKey.fixable : '',
`<ul>${rule.meta.docs.categories.map((c) => `<li>${c}</li>`).join('')}</ul>`,
rule.meta.docs.categories
? `<ul>${rule.meta.docs.categories.map((c) => `<li>${c}</li>`).join('')}</ul>`
: '',
]
})

Expand Down
12 changes: 6 additions & 6 deletions tools/utils/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ const categoriesConfig: TCategoriesConfig = {
},
}

export const categoryIds = Object.keys(categoriesConfig)
export const categoryIds = Object.keys(categoriesConfig) as CategoryId[]

for (const categoryId of categoryIds) {
categoriesConfig[categoryId].rules = []

for (const rule of rules) {
const ruleCategories = rule.meta.docs.categories
// Throw if rule does not have a category
if (!ruleCategories.length) {
if (!ruleCategories?.length) {
throw new Error(`Rule "${rule.ruleId}" does not have any category.`)
}

if (ruleCategories.includes(categoryId)) {
categoriesConfig[categoryId].rules.push(rule)
categoriesConfig[categoryId].rules?.push(rule)
}
}
}

export const categories = categoryIds
.map((categoryId) => {
if (!categoriesConfig[categoryId].rules.length) {
if (!categoriesConfig[categoryId].rules?.length) {
throw new Error(
`Category "${categoryId}" has no rules. Make sure that at least one rule is linked to this category.`
)
Expand All @@ -47,11 +47,11 @@ export const categories = categoryIds
return {
categoryId,
title: categoriesConfig[categoryId],
rules: categoriesConfig[categoryId].rules.filter((rule) => !rule.meta.deprecated),
rules: categoriesConfig[categoryId].rules?.filter((rule) => !rule.meta.deprecated) ?? [],
}
})
.filter((category) => {
return category.rules.length >= 1
return (category.rules?.length ?? 0) >= 1
})

export type TCategory = typeof categories extends (infer TCat)[] ? TCat : never