fbredius/storybook

View on GitHub
lib/preview-web/src/PreviewWeb.tsx

Summary

Maintainability
F
3 days
Test Coverage

File PreviewWeb.tsx has 586 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';
import global from 'global';
import { SynchronousPromise } from 'synchronous-promise';
import Events, { IGNORED_EXCEPTION } from '@storybook/core-events';
Severity: Major
Found in lib/preview-web/src/PreviewWeb.tsx - About 1 day to fix

    PreviewWeb has 30 functions (exceeds 20 allowed). Consider refactoring.
    Open

    export class PreviewWeb<TFramework extends AnyFramework> {
      channel: Channel;
    
      serverChannel?: Channel;
    
    
    Severity: Minor
    Found in lib/preview-web/src/PreviewWeb.tsx - About 3 hrs to fix

      Function renderStoryToElement has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
      Open

        renderStoryToElement({
          story,
          renderContext: renderContextWithoutStoryContext,
          element: canvasElement,
          viewMode,
      Severity: Minor
      Found in lib/preview-web/src/PreviewWeb.tsx - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function renderStoryToElement has 87 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        renderStoryToElement({
          story,
          renderContext: renderContextWithoutStoryContext,
          element: canvasElement,
          viewMode,
      Severity: Major
      Found in lib/preview-web/src/PreviewWeb.tsx - About 3 hrs to fix

        Function renderSelection has 55 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          async renderSelection({ persistedArgs }: { persistedArgs?: Args } = {}) {
            const { selection } = this.urlStore;
            if (!selection) {
              throw new Error('Cannot render story as no selection was made');
            }
        Severity: Major
        Found in lib/preview-web/src/PreviewWeb.tsx - About 2 hrs to fix

          Function renderDocs has 40 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            async renderDocs({ story }: { story: Story<TFramework> }) {
              const { id, title, name } = story;
              const csfFile: CSFFile<TFramework> = await this.storyStore.loadCSFFileByStoryId(id);
              const docsContext = {
                id,
          Severity: Minor
          Found in lib/preview-web/src/PreviewWeb.tsx - About 1 hr to fix

            Function renderSelection has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

              async renderSelection({ persistedArgs }: { persistedArgs?: Args } = {}) {
                const { selection } = this.urlStore;
                if (!selection) {
                  throw new Error('Cannot render story as no selection was made');
                }
            Severity: Minor
            Found in lib/preview-web/src/PreviewWeb.tsx - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function selectSpecifiedStory has 33 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              async selectSpecifiedStory() {
                if (!this.urlStore.selectionSpecifier) {
                  this.renderMissingStory();
                  return;
                }
            Severity: Minor
            Found in lib/preview-web/src/PreviewWeb.tsx - About 1 hr to fix

              Similar blocks of code found in 2 locations. Consider refactoring.
              Open

                    if (!global.FEATURES?.modernInlineRender) {
                      this.channel.off(Events.UPDATE_GLOBALS, render);
                      this.channel.off(Events.UPDATE_STORY_ARGS, render);
                      this.channel.off(Events.RESET_STORY_ARGS, render);
                    }
              Severity: Major
              Found in lib/preview-web/src/PreviewWeb.tsx and 1 other location - About 1 hr to fix
              lib/preview-web/src/PreviewWeb.tsx on lines 541..545

              Duplicated Code

              Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

              Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

              When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

              Tuning

              This issue has a mass of 64.

              We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

              The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

              If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

              See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

              Refactorings

              Further Reading

              Similar blocks of code found in 2 locations. Consider refactoring.
              Open

                  if (!global.FEATURES?.modernInlineRender) {
                    this.channel.on(Events.UPDATE_GLOBALS, render);
                    this.channel.on(Events.UPDATE_STORY_ARGS, render);
                    this.channel.on(Events.RESET_STORY_ARGS, render);
                  }
              Severity: Major
              Found in lib/preview-web/src/PreviewWeb.tsx and 1 other location - About 1 hr to fix
              lib/preview-web/src/PreviewWeb.tsx on lines 548..552

              Duplicated Code

              Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

              Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

              When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

              Tuning

              This issue has a mass of 64.

              We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

              The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

              If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

              See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

              Refactorings

              Further Reading

              block is empty
              Open

                    await new Promise(() => {});
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: no-empty

              Disallows empty blocks.

              Blocks with a comment inside are not considered empty.

              Rationale

              Empty blocks are often indicators of missing code.

              Config

              If allow-empty-catch is specified, then catch blocks are allowed to be empty. If allow-empty-functions is specified, then function definitions are allowed to be empty.

              Examples
              "no-empty": true
              "no-empty": true,allow-empty-catch
              "no-empty": true,allow-empty-functions
              "no-empty": true,allow-empty-catch,allow-empty-functions
              Schema
              {
                "type": "array",
                "items": {
                  "anyOf": [
                    {
                      "type": "string",
                      "enum": [
                        "allow-empty-catch"
                      ]
                    },
                    {
                      "type": "string",
                      "enum": [
                        "allow-empty-functions"
                      ]
                    }
                  ]
                }
              }

              For more information see this page.

              Type assertion on object literals is forbidden, use a type annotation instead.
              Open

                        loadedContext = await applyLoaders({
                          ...this.storyStore.getStoryContext(story),
                          viewMode,
                        } as StoryContextForLoaders<TFramework>);
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: no-object-literal-type-assertion

              Forbids an object literal to appear in a type assertion expression. Casting to any or to unknown is still allowed.

              Rationale

              Always prefer const x: T = { ... }; to const x = { ... } as T;. The type assertion in the latter case is either unnecessary or hides an error. The compiler will warn for excess properties with this syntax, but not missing required fields. For example: const x: { foo: number } = {} will fail to compile, but const x = {} as { foo: number } will succeed. Additionally, the const assertion const x = { foo: 1 } as const, introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.

              Notes
              • TypeScript Only

              Config

              One option may be configured:

              • allow-arguments allows type assertions to be used on object literals inside call expressions.
              Examples
              "no-object-literal-type-assertion": true
              "no-object-literal-type-assertion": true,[object Object]
              Schema
              {
                "type": "object",
                "properties": {
                  "allow-arguments": {
                    "type": "boolean"
                  }
                },
                "additionalProperties": false
              }

              For more information see this page.

              Type assertion on object literals is forbidden, use a type annotation instead.
              Open

                  }, {} as Partial<Args>);
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: no-object-literal-type-assertion

              Forbids an object literal to appear in a type assertion expression. Casting to any or to unknown is still allowed.

              Rationale

              Always prefer const x: T = { ... }; to const x = { ... } as T;. The type assertion in the latter case is either unnecessary or hides an error. The compiler will warn for excess properties with this syntax, but not missing required fields. For example: const x: { foo: number } = {} will fail to compile, but const x = {} as { foo: number } will succeed. Additionally, the const assertion const x = { foo: 1 } as const, introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.

              Notes
              • TypeScript Only

              Config

              One option may be configured:

              • allow-arguments allows type assertions to be used on object literals inside call expressions.
              Examples
              "no-object-literal-type-assertion": true
              "no-object-literal-type-assertion": true,[object Object]
              Schema
              {
                "type": "object",
                "properties": {
                  "allow-arguments": {
                    "type": "boolean"
                  }
                },
                "additionalProperties": false
              }

              For more information see this page.

              Type assertion on object literals is forbidden, use a type annotation instead.
              Open

                      ({
                        ...this.storyStore.getStoryContext(renderedStory),
                        viewMode: 'docs' as ViewMode,
                      } as StoryContextForLoaders<TFramework>),
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: no-object-literal-type-assertion

              Forbids an object literal to appear in a type assertion expression. Casting to any or to unknown is still allowed.

              Rationale

              Always prefer const x: T = { ... }; to const x = { ... } as T;. The type assertion in the latter case is either unnecessary or hides an error. The compiler will warn for excess properties with this syntax, but not missing required fields. For example: const x: { foo: number } = {} will fail to compile, but const x = {} as { foo: number } will succeed. Additionally, the const assertion const x = { foo: 1 } as const, introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.

              Notes
              • TypeScript Only

              Config

              One option may be configured:

              • allow-arguments allows type assertions to be used on object literals inside call expressions.
              Examples
              "no-object-literal-type-assertion": true
              "no-object-literal-type-assertion": true,[object Object]
              Schema
              {
                "type": "object",
                "properties": {
                  "allow-arguments": {
                    "type": "boolean"
                  }
                },
                "additionalProperties": false
              }

              For more information see this page.

              Type assertion on object literals is forbidden, use a type annotation instead.
              Open

                return {
                  signal: { aborted: false },
                  abort() {
                    this.signal.aborted = true;
                  },
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: no-object-literal-type-assertion

              Forbids an object literal to appear in a type assertion expression. Casting to any or to unknown is still allowed.

              Rationale

              Always prefer const x: T = { ... }; to const x = { ... } as T;. The type assertion in the latter case is either unnecessary or hides an error. The compiler will warn for excess properties with this syntax, but not missing required fields. For example: const x: { foo: number } = {} will fail to compile, but const x = {} as { foo: number } will succeed. Additionally, the const assertion const x = { foo: 1 } as const, introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.

              Notes
              • TypeScript Only

              Config

              One option may be configured:

              • allow-arguments allows type assertions to be used on object literals inside call expressions.
              Examples
              "no-object-literal-type-assertion": true
              "no-object-literal-type-assertion": true,[object Object]
              Schema
              {
                "type": "object",
                "properties": {
                  "allow-arguments": {
                    "type": "boolean"
                  }
                },
                "additionalProperties": false
              }

              For more information see this page.

              Use a conditional expression instead of assigning to 'this.previousCleanup' in multiple places.
              Open

                  if (selection.viewMode === 'docs' || story.parameters.docsOnly) {
              Severity: Minor
              Found in lib/preview-web/src/PreviewWeb.tsx by tslint

              Rule: prefer-conditional-expression

              Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement.

              Rationale

              This reduces duplication and can eliminate an unnecessary variable declaration.

              Config

              If check-else-if is specified, the rule also checks nested if-else-if statements.

              Examples
              "prefer-conditional-expression": true
              "prefer-conditional-expression": true,check-else-if
              Schema
              {
                "type": "string",
                "enum": [
                  "check-else-if"
                ]
              }

              For more information see this page.

              There are no issues that match your filters.

              Category
              Status