fbredius/storybook

View on GitHub
lib/csf-tools/src/CsfFile.ts

Summary

Maintainability
F
5 days
Test Coverage

Function parse has a Cognitive Complexity of 44 (exceeds 5 allowed). Consider refactoring.
Open

  parse() {
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const self = this;
    traverse(this._ast, {
      ExportDefaultDeclaration: {
Severity: Minor
Found in lib/csf-tools/src/CsfFile.ts - About 6 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 parse has 164 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  parse() {
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const self = this;
    traverse(this._ast, {
      ExportDefaultDeclaration: {
Severity: Major
Found in lib/csf-tools/src/CsfFile.ts - About 6 hrs to fix

    File CsfFile.ts has 362 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    /* eslint-disable no-underscore-dangle */
    import fs from 'fs-extra';
    import dedent from 'ts-dedent';
    import * as t from '@babel/types';
    import generate from '@babel/generator';
    Severity: Minor
    Found in lib/csf-tools/src/CsfFile.ts - About 4 hrs to fix

      Function enter has 59 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

              enter({ node, parent }) {
                let declarations;
                if (t.isVariableDeclaration(node.declaration)) {
                  declarations = node.declaration.declarations.filter((d) => t.isVariableDeclarator(d));
                } else if (t.isFunctionDeclaration(node.declaration)) {
      Severity: Major
      Found in lib/csf-tools/src/CsfFile.ts - About 2 hrs to fix

        Consider simplifying this complex logical expression.
        Open

            if (
              t.isProgram(parent) &&
              t.isMemberExpression(callee) &&
              t.isIdentifier(callee.object) &&
              t.isIdentifier(callee.property) &&
        Severity: Critical
        Found in lib/csf-tools/src/CsfFile.ts - About 1 hr to fix

          Function isArgsStory has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
          Open

          const isArgsStory = (init: t.Node, parent: t.Node, csf: CsfFile) => {
            let storyFn: t.Node = init;
            // export const Foo = Bar.bind({})
            if (t.isCallExpression(init)) {
              const { callee, arguments: bindArguments } = init;
          Severity: Minor
          Found in lib/csf-tools/src/CsfFile.ts - 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 isArgsStory has 29 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          const isArgsStory = (init: t.Node, parent: t.Node, csf: CsfFile) => {
            let storyFn: t.Node = init;
            // export const Foo = Bar.bind({})
            if (t.isCallExpression(init)) {
              const { callee, arguments: bindArguments } = init;
          Severity: Minor
          Found in lib/csf-tools/src/CsfFile.ts - About 1 hr to fix

            Function enter has 29 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                    enter({ node, parent }) {
                      const { expression } = node;
                      // B.storyName = 'some string';
                      if (
                        t.isProgram(parent) &&
            Severity: Minor
            Found in lib/csf-tools/src/CsfFile.ts - About 1 hr to fix

              Avoid deeply nested control flow statements.
              Open

                                    if (p.key.name === 'render') {
                                      __isArgsStory = isArgsStory(p.value as t.Expression, parent, self);
                                    } else if (p.key.name === 'name' && t.isStringLiteral(p.value)) {
                                      name = p.value.value;
                                    }
              Severity: Major
              Found in lib/csf-tools/src/CsfFile.ts - About 45 mins to fix

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

                const findVarInitialization = (identifier: string, program: t.Program) => {
                  let init: t.Expression = null;
                  let declarations: t.VariableDeclarator[] = null;
                  program.body.find((node: t.Node) => {
                    if (t.isVariableDeclaration(node)) {
                Severity: Major
                Found in lib/csf-tools/src/CsfFile.ts and 1 other location - About 1 day to fix
                lib/csf-tools/src/ConfigFile.ts on lines 30..56

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

                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

                export const writeCsf = async (csf: CsfFile, fileName?: string) => {
                  const fname = fileName || csf._fileName;
                  if (!fname) throw new Error('Please specify a fileName for writeCsf');
                  await fs.writeFile(fileName, await formatCsf(csf));
                };
                Severity: Major
                Found in lib/csf-tools/src/CsfFile.ts and 1 other location - About 2 hrs to fix
                lib/csf-tools/src/ConfigFile.ts on lines 233..237

                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

                export const formatCsf = (csf: CsfFile) => {
                  const { code } = generate(csf._ast, {});
                  return code;
                };
                Severity: Minor
                Found in lib/csf-tools/src/CsfFile.ts and 1 other location - About 40 mins to fix
                lib/csf-tools/src/ConfigFile.ts on lines 223..226

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

                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<string, Story>);
                Severity: Minor
                Found in lib/csf-tools/src/CsfFile.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.

                Assigning this reference to local variable not allowed: self.
                Open

                    const self = this;
                Severity: Minor
                Found in lib/csf-tools/src/CsfFile.ts by tslint

                Rule: no-this-assignment

                Disallows unnecessary references to this.

                Rationale

                Assigning a variable to this instead of properly using arrow lambdas may be a symptom of pre-ES6 practices or not managing scope well.

                Instead of storing a reference to this and using it inside a function () {:

                const self = this;
                
                setTimeout(function () {
                    self.doWork();
                });

                Use () => arrow lambdas, as they preserve this scope for you:

                setTimeout(() => {
                    this.doWork();
                });

                Config

                Two options may be provided on an object:

                • allow-destructuring allows using destructuring to access members of this (e.g. { foo, bar } = this;).
                • allowed-names may be specified as a list of regular expressions to match allowed variable names.
                Examples
                "no-this-assignment": true
                "no-this-assignment": true,[object Object]
                Schema
                {
                  "additionalProperties": false,
                  "properties": {
                    "allow-destructuring": {
                      "type": "boolean"
                    },
                    "allowed-names": {
                      "listType": "string",
                      "type": "list"
                    }
                  },
                  "type": "object"
                }

                For more information see this page.

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

                  }, {} as Record<string, any>);
                Severity: Minor
                Found in lib/csf-tools/src/CsfFile.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.

                There are no issues that match your filters.

                Category
                Status