fbredius/storybook

View on GitHub
lib/instrumenter/src/instrumenter.ts

Summary

Maintainability
F
3 days
Test Coverage

File instrumenter.ts has 435 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/* eslint-disable no-underscore-dangle */
import { addons, Channel, StoryId } from '@storybook/addons';
import { once } from '@storybook/client-logger';
import {
  FORCE_REMOUNT,
Severity: Minor
Found in lib/instrumenter/src/instrumenter.ts - About 6 hrs to fix

    Function constructor has 105 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

      constructor() {
        this.channel = addons.getChannel();
    
        // Restore state from the parent window in case the iframe was reloaded.
        this.state = global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__ || {};
    Severity: Major
    Found in lib/instrumenter/src/instrumenter.ts - About 4 hrs to fix

      Function constructor has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

        constructor() {
          this.channel = addons.getChannel();
      
          // Restore state from the parent window in case the iframe was reloaded.
          this.state = global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__ || {};
      Severity: Minor
      Found in lib/instrumenter/src/instrumenter.ts - 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 invoke has 88 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        invoke(fn: Function, call: Call, options: Options) {
          // TODO this doesnt work because the abortSignal we have here is the newly created one
          // const { abortSignal } = global.window.__STORYBOOK_PREVIEW__ || {};
          // if (abortSignal && abortSignal.aborted) throw IGNORED_EXCEPTION;
      
      
      Severity: Major
      Found in lib/instrumenter/src/instrumenter.ts - About 3 hrs to fix

        Function invoke has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
        Open

          invoke(fn: Function, call: Call, options: Options) {
            // TODO this doesnt work because the abortSignal we have here is the newly created one
            // const { abortSignal } = global.window.__STORYBOOK_PREVIEW__ || {};
            // if (abortSignal && abortSignal.aborted) throw IGNORED_EXCEPTION;
        
        
        Severity: Minor
        Found in lib/instrumenter/src/instrumenter.ts - 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 instrument has 26 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          instrument<TObj extends { [x: string]: any }>(obj: TObj, options: Options): PatchedObj<TObj> {
            if (!isInstrumentable(obj)) return obj;
        
            const { mutate = false, path = [] } = options;
            return Object.keys(obj).reduce(
        Severity: Minor
        Found in lib/instrumenter/src/instrumenter.ts - About 1 hr to fix

          Avoid too many return statements within this function.
          Open

            return true;
          Severity: Major
          Found in lib/instrumenter/src/instrumenter.ts - About 30 mins to fix

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

                    this.setState(call.storyId, (state) => ({
                      callRefsByResult: new Map([
                        ...Array.from(state.callRefsByResult.entries()),
                        [result, { __callId__: call.id, retain: call.retain }],
                      ]),
            Severity: Major
            Found in lib/instrumenter/src/instrumenter.ts and 1 other location - About 2 hrs to fix
            lib/instrumenter/src/instrumenter.ts on lines 407..412

            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 75.

            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

                    this.setState(call.storyId, (state) => ({
                      callRefsByResult: new Map([
                        ...Array.from(state.callRefsByResult.entries()),
                        [e, { __callId__: call.id, retain: call.retain }],
                      ]),
            Severity: Major
            Found in lib/instrumenter/src/instrumenter.ts and 1 other location - About 2 hrs to fix
            lib/instrumenter/src/instrumenter.ts on lines 462..467

            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 75.

            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

                const call: Call = { id, parentId, storyId, cursor, path, method, args, interceptable, retain };
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts and 1 other location - About 35 mins to fix
            lib/channel-postmessage/src/index.ts on lines 79..89

            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 47.

            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

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

                }, {} as Record<StoryId, State>);
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts 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.

            Don't use 'Function' as a type. Avoid using the Function type. Prefer a specific function type, like () => void.
            Open

              invoke(fn: Function, call: Call, options: Options) {
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts by tslint

            Rule: ban-types

            Bans specific types from being used. Does not ban the corresponding runtime objects from being used.

            Notes
            • TypeScript Only

            Config

            A list of ["regex", "optional explanation here"], which bans types that match regex

            Examples
            "ban-types": true,Object,Use {} instead.,String
            Schema
            {
              "type": "list",
              "listType": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minLength": 1,
                "maxLength": 2
              }
            }

            For more information see this page.

            'Object.assign' returns the first argument. Prefer object spread if you want a new object.
            Open

                    .reduce<Record<Call['id'], Call>>((a, c) => Object.assign(a, { [c.id]: c }), {});
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts by tslint

            Rule: prefer-object-spread

            Enforces the use of the ES2018 object spread operator over Object.assign() where appropriate.

            Rationale

            Object spread allows for better type checking and inference.

            Notes
            • Has Fix

            Config

            Not configurable.

            Examples
            "prefer-object-spread": true

            For more information see this page.

            Don't use 'Function' as a type. Avoid using the Function type. Prefer a specific function type, like () => void.
            Open

              intercept(fn: Function, call: Call, options: Options) {
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts by tslint

            Rule: ban-types

            Bans specific types from being used. Does not ban the corresponding runtime objects from being used.

            Notes
            • TypeScript Only

            Config

            A list of ["regex", "optional explanation here"], which bans types that match regex

            Examples
            "ban-types": true,Object,Use {} instead.,String
            Schema
            {
              "type": "list",
              "listType": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minLength": 1,
                "maxLength": 2
              }
            }

            For more information see this page.

            Don't use 'Function' as a type. Avoid using the Function type. Prefer a specific function type, like () => void.
            Open

              track(method: string, fn: Function, args: any[], options: Options) {
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts by tslint

            Rule: ban-types

            Bans specific types from being used. Does not ban the corresponding runtime objects from being used.

            Notes
            • TypeScript Only

            Config

            A list of ["regex", "optional explanation here"], which bans types that match regex

            Examples
            "ban-types": true,Object,Use {} instead.,String
            Schema
            {
              "type": "list",
              "listType": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minLength": 1,
                "maxLength": 2
              }
            }

            For more information see this page.

            Shadowed name: 'next'
            Open

                  const next = isDebugging
            Severity: Minor
            Found in lib/instrumenter/src/instrumenter.ts by tslint

            Rule: no-shadowed-variable

            Disallows shadowing variable declarations.

            Rationale

            When a variable in a local scope and a variable in the containing scope have the same name, shadowing occurs. Shadowing makes it impossible to access the variable in the containing scope and obscures to what value an identifier actually refers. Compare the following snippets:

            const a = 'no shadow';
            function print() {
                console.log(a);
            }
            print(); // logs 'no shadow'.
            const a = 'no shadow';
            function print() {
                const a = 'shadow'; // TSLint will complain here.
                console.log(a);
            }
            print(); // logs 'shadow'.

            ESLint has an equivalent rule. For more background information, refer to this MDN closure doc.

            Config

            You can optionally pass an object to disable checking for certain kinds of declarations. Possible keys are "class", "enum", "function", "import", "interface", "namespace", "typeAlias" and "typeParameter". You can also pass "underscore" to ignore variable names that begin with _. Just set the value to false for the check you want to disable. All checks default to true, i.e. are enabled by default. Note that you cannot disable variables and parameters.

            The option "temporalDeadZone" defaults to true which shows errors when shadowing block scoped declarations in their temporal dead zone. When set to false parameters, classes, enums and variables declared with let or const are not considered shadowed if the shadowing occurs within their temporal dead zone.

            The following example shows how the "temporalDeadZone" option changes the linting result:

            function fn(value) {
                if (value) {
                    const tmp = value; // no error on this line if "temporalDeadZone" is false
                    return tmp;
                }
                let tmp = undefined;
                if (!value) {
                    const tmp = value; // this line always contains an error
                    return tmp;
                }
            }
            Examples
            "no-shadowed-variable": true
            "no-shadowed-variable": true,[object Object]
            Schema
            {
              "type": "object",
              "properties": {
                "class": {
                  "type": "boolean"
                },
                "enum": {
                  "type": "boolean"
                },
                "function": {
                  "type": "boolean"
                },
                "import": {
                  "type": "boolean"
                },
                "interface": {
                  "type": "boolean"
                },
                "namespace": {
                  "type": "boolean"
                },
                "typeAlias": {
                  "type": "boolean"
                },
                "typeParameter": {
                  "type": "boolean"
                },
                "temporalDeadZone": {
                  "type": "boolean"
                },
                "underscore": {
                  "type": "boolean"
                }
              }
            }

            For more information see this page.

            There are no issues that match your filters.

            Category
            Status