reactioncommerce/redoc

View on GitHub
packages/redoc-core/server/methods/repoData.js

Summary

Maintainability
D
2 days
Test Coverage

Function updateTOC has a Cognitive Complexity of 34 (exceeds 5 allowed). Consider refactoring.
Open

export function updateTOC() {
  const TOC = ReDoc.Collections.TOC.find();
  const repos = ReDoc.Collections.Repos.find({}).fetch();

  if (TOC.count() > 0) {
Severity: Minor
Found in packages/redoc-core/server/methods/repoData.js - About 5 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 updateRepoData has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring.
Open

export function updateRepoData() {
  if (hasExistingRepoData() === false) {
    const repoData = getRepoData();

    if (Array.isArray(repoData.repos)) {
Severity: Minor
Found in packages/redoc-core/server/methods/repoData.js - 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 cachePrimaryDocs has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Open

export function cachePrimaryDocs() {
  // Update repo data and TOC if they are empty;
  updateRepoData();
  updateTOC();

Severity: Minor
Found in packages/redoc-core/server/methods/repoData.js - About 2 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 updateRepoData has 57 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export function updateRepoData() {
  if (hasExistingRepoData() === false) {
    const repoData = getRepoData();

    if (Array.isArray(repoData.repos)) {
Severity: Major
Found in packages/redoc-core/server/methods/repoData.js - About 2 hrs to fix

    Function updateTOC has 49 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    export function updateTOC() {
      const TOC = ReDoc.Collections.TOC.find();
      const repos = ReDoc.Collections.Repos.find({}).fetch();
    
      if (TOC.count() > 0) {
    Severity: Minor
    Found in packages/redoc-core/server/methods/repoData.js - About 1 hr to fix

      Function initServiceConfiguration has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

      export function initServiceConfiguration() {
        // Import settinngs typically found in a Meteor settings file
        // settings file location <app-root>/settings/settings.json
        // or meteor --settings=path/to/settings.json
        if (hasServices()) {
      Severity: Minor
      Found in packages/redoc-core/server/methods/repoData.js - 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 initAdminUsers has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
      Open

      export function initAdminUsers() {
        if (hasServices()) {
          if (Meteor.settings.redoc && Meteor.settings.redoc.users) {
            for (const user of Meteor.settings.redoc.users) {
              const userData = Meteor.users.findOne({
      Severity: Minor
      Found in packages/redoc-core/server/methods/repoData.js - 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

      Avoid deeply nested control flow statements.
      Open

                if (toc) {
                  if (Array.isArray(toc.tocData)) {
                    // Loop through all entries and add them to the TOC collection
                    toc.tocData.forEach((tocItem, index) => {
                      const filename = tocItem.docPath.split("/").pop().replace(/\.[^/.]+$/, "");
      Severity: Major
      Found in packages/redoc-core/server/methods/repoData.js - About 45 mins to fix

        Avoid deeply nested control flow statements.
        Open

                  if (service === "github") {
                    authString = `?client_id=${settings.clientId}&client_secret=${settings.secret}`;
                  }
        Severity: Major
        Found in packages/redoc-core/server/methods/repoData.js - About 45 mins to fix

          Avoid deeply nested control flow statements.
          Open

                    if (Array.isArray(toc.tocData)) {
                      // Loop through all entries and add them to the TOC collection
                      toc.tocData.forEach((tocItem, index) => {
                        const filename = tocItem.docPath.split("/").pop().replace(/\.[^/.]+$/, "");
          
          
          Severity: Major
          Found in packages/redoc-core/server/methods/repoData.js - About 45 mins to fix

            Avoid deeply nested control flow statements.
            Open

                      if (_.contains(getPublicBranches(), branch.name)) {
                        const docCount = ReDoc.Collections.Docs.find({
                          repo: repo.repo,
                          branch: branch.name
                        }).count();
            Severity: Major
            Found in packages/redoc-core/server/methods/repoData.js - About 45 mins to fix

              Function getRepoData has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

              function getRepoData() {
                let repoData;
              
                if (Meteor.settings.redoc && Meteor.settings.redoc.initRepoData) {
                  // If the `initRepoData` key is a string, then fetch the repo data from that URL
              Severity: Minor
              Found in packages/redoc-core/server/methods/repoData.js - About 25 mins 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

              Don't make functions within a loop.
              Open

                            toc.tocData.forEach((tocItem, index) => {

              Disallow Functions in Loops (no-loop-func)

              Writing functions within loops tends to result in errors due to the way the function creates a closure around the loop. For example:

              for (var i = 0; i < 10; i++) {
                  funcs[i] = function() {
                      return i;
                  };
              }

              In this case, you would expect each function created within the loop to return a different number. In reality, each function returns 10, because that was the last value of i in the scope.

              let or const mitigate this problem.

              /*eslint-env es6*/
              
              for (let i = 0; i < 10; i++) {
                  funcs[i] = function() {
                      return i;
                  };
              }

              In this case, each function created within the loop returns a different number as expected.

              Rule Details

              This error is raised to highlight a piece of code that may not work as you expect it to and could also indicate a misunderstanding of how the language works. Your code may run without any problems if you do not fix this error, but in some situations it could behave unexpectedly.

              Examples of incorrect code for this rule:

              /*eslint no-loop-func: "error"*/
              /*eslint-env es6*/
              
              for (var i=10; i; i--) {
                  (function() { return i; })();
              }
              
              while(i) {
                  var a = function() { return i; };
                  a();
              }
              
              do {
                  function a() { return i; };
                  a();
              } while (i);
              
              let foo = 0;
              for (let i=10; i; i--) {
                  // Bad, function is referencing block scoped variable in the outer scope.
                  var a = function() { return foo; };
                  a();
              }

              Examples of correct code for this rule:

              /*eslint no-loop-func: "error"*/
              /*eslint-env es6*/
              
              var a = function() {};
              
              for (var i=10; i; i--) {
                  a();
              }
              
              for (var i=10; i; i--) {
                  var a = function() {}; // OK, no references to variables in the outer scopes.
                  a();
              }
              
              for (let i=10; i; i--) {
                  var a = function() { return i; }; // OK, all references are referring to block scoped variables in the loop.
                  a();
              }
              
              var foo = 100;
              for (let i=10; i; i--) {
                  var a = function() { return foo; }; // OK, all references are referring to never modified variables.
                  a();
              }
              //... no modifications of foo after this loop ...

              Source: http://eslint.org/docs/rules/

              Don't make functions within a loop.
              Open

                          toc.tocData.forEach((tocItem, index) => {

              Disallow Functions in Loops (no-loop-func)

              Writing functions within loops tends to result in errors due to the way the function creates a closure around the loop. For example:

              for (var i = 0; i < 10; i++) {
                  funcs[i] = function() {
                      return i;
                  };
              }

              In this case, you would expect each function created within the loop to return a different number. In reality, each function returns 10, because that was the last value of i in the scope.

              let or const mitigate this problem.

              /*eslint-env es6*/
              
              for (let i = 0; i < 10; i++) {
                  funcs[i] = function() {
                      return i;
                  };
              }

              In this case, each function created within the loop returns a different number as expected.

              Rule Details

              This error is raised to highlight a piece of code that may not work as you expect it to and could also indicate a misunderstanding of how the language works. Your code may run without any problems if you do not fix this error, but in some situations it could behave unexpectedly.

              Examples of incorrect code for this rule:

              /*eslint no-loop-func: "error"*/
              /*eslint-env es6*/
              
              for (var i=10; i; i--) {
                  (function() { return i; })();
              }
              
              while(i) {
                  var a = function() { return i; };
                  a();
              }
              
              do {
                  function a() { return i; };
                  a();
              } while (i);
              
              let foo = 0;
              for (let i=10; i; i--) {
                  // Bad, function is referencing block scoped variable in the outer scope.
                  var a = function() { return foo; };
                  a();
              }

              Examples of correct code for this rule:

              /*eslint no-loop-func: "error"*/
              /*eslint-env es6*/
              
              var a = function() {};
              
              for (var i=10; i; i--) {
                  a();
              }
              
              for (var i=10; i; i--) {
                  var a = function() {}; // OK, no references to variables in the outer scopes.
                  a();
              }
              
              for (let i=10; i; i--) {
                  var a = function() { return i; }; // OK, all references are referring to block scoped variables in the loop.
                  a();
              }
              
              var foo = 100;
              for (let i=10; i; i--) {
                  var a = function() { return foo; }; // OK, all references are referring to never modified variables.
                  a();
              }
              //... no modifications of foo after this loop ...

              Source: http://eslint.org/docs/rules/

              Unexpected console statement.
              Open

                          console.log("Error fetching", remoteRepoData.data.tags_url);

              disallow the use of console (no-console)

              In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console should be stripped before being pushed to production.

              console.log("Made it here.");
              console.error("That shouldn't have happened.");

              Rule Details

              This rule disallows calls to methods of the console object.

              Examples of incorrect code for this rule:

              /*eslint no-console: "error"*/
              
              console.log("Log a debug level message.");
              console.warn("Log a warn level message.");
              console.error("Log an error level message.");

              Examples of correct code for this rule:

              /*eslint no-console: "error"*/
              
              // custom console
              Console.log("Hello world!");

              Options

              This rule has an object option for exceptions:

              • "allow" has an array of strings which are allowed methods of the console object

              Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] } option:

              /*eslint no-console: ["error", { allow: ["warn", "error"] }] */
              
              console.warn("Log a warn level message.");
              console.error("Log an error level message.");

              When Not To Use It

              If you're using Node.js, however, console is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.

              Related Rules

              'remoteRepoData' is never reassigned. Use 'const' instead.
              Open

                      remoteRepoData = Meteor.http.get(apiUrl + authString, {

              Suggest using const (prefer-const)

              If a variable is never reassigned, using the const declaration is better.

              const declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.

              Rule Details

              This rule is aimed at flagging variables that are declared using let keyword, but never reassigned after the initial assignment.

              Examples of incorrect code for this rule:

              /*eslint prefer-const: "error"*/
              /*eslint-env es6*/
              
              // it's initialized and never reassigned.
              let a = 3;
              console.log(a);
              
              let a;
              a = 0;
              console.log(a);
              
              // `i` is redefined (not reassigned) on each loop step.
              for (let i in [1, 2, 3]) {
                  console.log(i);
              }
              
              // `a` is redefined (not reassigned) on each loop step.
              for (let a of [1, 2, 3]) {
                  console.log(a);
              }

              Examples of correct code for this rule:

              /*eslint prefer-const: "error"*/
              /*eslint-env es6*/
              
              // using const.
              const a = 0;
              
              // it's never initialized.
              let a;
              console.log(a);
              
              // it's reassigned after initialized.
              let a;
              a = 0;
              a = 1;
              console.log(a);
              
              // it's initialized in a different block from the declaration.
              let a;
              if (true) {
                  a = 0;
              }
              console.log(a);
              
              // it's initialized at a place that we cannot write a variable declaration.
              let a;
              if (true) a = 0;
              console.log(a);
              
              // `i` gets a new binding each iteration
              for (const i in [1, 2, 3]) {
                console.log(i);
              }
              
              // `a` gets a new binding each iteration
              for (const a of [1, 2, 3]) {
                console.log(a);
              }
              
              // `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
              for (let i = 0, end = 10; i < end; ++i) {
                  console.log(a);
              }
              
              // suggest to use `no-var` rule.
              var b = 3;
              console.log(b);

              Options

              {
                  "prefer-const": ["error", {
                      "destructuring": "any",
                      "ignoreReadBeforeAssign": false
                  }]
              }

              destructuring

              The kind of the way to address variables in destructuring. There are 2 values:

              • "any" (default) - If any variables in destructuring should be const, this rule warns for those variables.
              • "all" - If all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them.

              Examples of incorrect code for the default {"destructuring": "any"} option:

              /*eslint prefer-const: "error"*/
              /*eslint-env es6*/
              
              let {a, b} = obj;    /*error 'b' is never reassigned, use 'const' instead.*/
              a = a + 1;

              Examples of correct code for the default {"destructuring": "any"} option:

              /*eslint prefer-const: "error"*/
              /*eslint-env es6*/
              
              // using const.
              const {a: a0, b} = obj;
              const a = a0 + 1;
              
              // all variables are reassigned.
              let {a, b} = obj;
              a = a + 1;
              b = b + 1;

              Examples of incorrect code for the {"destructuring": "all"} option:

              /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
              /*eslint-env es6*/
              
              // all of `a` and `b` should be const, so those are warned.
              let {a, b} = obj;    /*error 'a' is never reassigned, use 'const' instead.
                                           'b' is never reassigned, use 'const' instead.*/

              Examples of correct code for the {"destructuring": "all"} option:

              /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
              /*eslint-env es6*/
              
              // 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
              let {a, b} = obj;
              a = a + 1;

              ignoreReadBeforeAssign

              This is an option to avoid conflicting with no-use-before-define rule (without "nofunc" option). If true is specified, this rule will ignore variables that are read between the declaration and the first assignment. Default is false.

              Examples of correct code for the {"ignoreReadBeforeAssign": true} option:

              /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
              /*eslint-env es6*/
              
              let timer;
              function initialize() {
                  if (foo()) {
                      clearInterval(timer);
                  }
              }
              timer = setInterval(initialize, 100);

              Examples of correct code for the default {"ignoreReadBeforeAssign": false} option:

              /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
              /*eslint-env es6*/
              
              const timer = setInterval(initialize, 100);
              function initialize() {
                  if (foo()) {
                      clearInterval(timer);
                  }
              }

              When Not To Use It

              If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.

              Related Rules

              There are no issues that match your filters.

              Category
              Status