zacanger/koa-modern-router

View on GitHub

Showing 22 of 22 total issues

Function allowedMethods has a Cognitive Complexity of 42 (exceeds 5 allowed). Consider refactoring.
Open

Router.prototype.allowedMethods = function (options) {
  options = options || {}
  const implemented = this.methods

  return function allowedMethods (ctx, next) {
Severity: Minor
Found in lib/router.js - 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

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

          } else if (!allowed[ctx.method]) {
            if (options.throw) {
              let notAllowedThrowable
              if (typeof options.methodNotAllowed === 'function') {
                notAllowedThrowable = options.methodNotAllowed() // set whatever the user returns from their function
Severity: Major
Found in lib/router.js and 1 other location - About 3 hrs to fix
lib/router.js on lines 448..480

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

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 (!~implemented.indexOf(ctx.method)) {
          if (options.throw) {
            let notImplementedThrowable
            if (typeof options.notImplemented === 'function') {
              notImplementedThrowable = options.notImplemented() // set whatever the user returns from their function
Severity: Major
Found in lib/router.js and 1 other location - About 3 hrs to fix
lib/router.js on lines 466..479

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

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

File router.js has 300 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/* eslint-disable max-lines, func-style */

/**
 * RESTful resource routing middleware for koa.
 *
Severity: Minor
Found in lib/router.js - About 3 hrs to fix

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

        if (typeof path === 'string' || path instanceof RegExp || Array.isArray(path)) {
          middleware = Array.prototype.slice.call(arguments, 2)
        } else {
          middleware = Array.prototype.slice.call(arguments, 1)
          path = name
    Severity: Major
    Found in lib/router.js and 1 other location - About 3 hrs to fix
    lib/router.js on lines 500..506

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

    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

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

      if (typeof path === 'string' || path instanceof RegExp || Array.isArray(path)) {
        middleware = Array.prototype.slice.call(arguments, 2)
      } else {
        middleware = Array.prototype.slice.call(arguments, 1)
        path = name
    Severity: Major
    Found in lib/router.js and 1 other location - About 3 hrs to fix
    lib/router.js on lines 195..201

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

    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

    Function match has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

    Router.prototype.match = function (path, method) {
      const layers = this.stack
      let layer
      let maxMatchLength = -1
      let maxMatchPathLength = -1
    Severity: Minor
    Found in lib/router.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 allowedMethods has 48 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    Router.prototype.allowedMethods = function (options) {
      options = options || {}
      const implemented = this.methods
    
      return function allowedMethods (ctx, next) {
    Severity: Minor
    Found in lib/router.js - About 1 hr to fix

      Function allowedMethods has 44 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        return function allowedMethods (ctx, next) {
          return next().then(function () {
            const allowed = {}
      
            if (!ctx.status || ctx.status === 404) {
      Severity: Minor
      Found in lib/router.js - About 1 hr to fix

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

          url (params, options) {
            const toPath = pathToRegExp.compile(this.plainPath)
        
            let args = params
        
        
        Severity: Minor
        Found in lib/layer.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 use has 37 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        Router.prototype.use = function () {
          const router = this
          const middleware = Array.prototype.slice.call(arguments)
          let path
        
        
        Severity: Minor
        Found in lib/router.js - About 1 hr to fix

          Function match has 34 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          Router.prototype.match = function (path, method) {
            const layers = this.stack
            let layer
            let maxMatchLength = -1
            let maxMatchPathLength = -1
          Severity: Minor
          Found in lib/router.js - About 1 hr to fix

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

            const sortByMostSpecificLayer = (a, b) => {
              const wildA = a.path.endsWith('(.*)')
              const wildB = b.path.endsWith('(.*)')
              if (wildA && wildB) return a.path.length - b.path.length
              const pathA = wildA ? a.path.slice(0, -4) : a.path
            Severity: Minor
            Found in lib/router.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 constructor has 31 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              constructor (path, methods, middleware, opts = {}) {
                this.opts = opts
                this.name = this.opts.name || null
                this.methods = []
                this.paramNames = []
            Severity: Minor
            Found in lib/layer.js - About 1 hr to fix

              Function middleware has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

              Router.prototype.routes = Router.prototype.middleware = function () {
                const router = this
              
                const dispatch = function dispatch (ctx, next) {
                  const path = router.opts.routerPath || ctx.routerPath || ctx.path
              Severity: Minor
              Found in lib/router.js - About 1 hr to fix

                Function url has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                  url (params, options) {
                    const toPath = pathToRegExp.compile(this.plainPath)
                
                    let args = params
                
                
                Severity: Minor
                Found in lib/layer.js - About 1 hr to fix

                  Function dispatch has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                    const dispatch = function dispatch (ctx, next) {
                      const path = router.opts.routerPath || ctx.routerPath || ctx.path
                      const matched = router.match(path, ctx.method)
                  
                      if (ctx.matched) {
                  Severity: Minor
                  Found in lib/router.js - About 1 hr to fix

                    Function middleware has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                    Open

                    Router.prototype.routes = Router.prototype.middleware = function () {
                      const router = this
                    
                      const dispatch = function dispatch (ctx, next) {
                        const path = router.opts.routerPath || ctx.routerPath || ctx.path
                    Severity: Minor
                    Found in lib/router.js - About 45 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

                    Avoid deeply nested control flow statements.
                    Open

                                if (options.throw) {
                                  let notAllowedThrowable
                                  if (typeof options.methodNotAllowed === 'function') {
                                    notAllowedThrowable = options.methodNotAllowed() // set whatever the user returns from their function
                                  } else {
                    Severity: Major
                    Found in lib/router.js - About 45 mins to fix

                      Function params has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                      Open

                        params (path, captures, existingParams) {
                          const params = existingParams || {}
                      
                          for (let len = captures.length, i = 0; i < len; i++) {
                            if (this.paramNames[i]) {
                      Severity: Minor
                      Found in lib/layer.js - About 35 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

                      Severity
                      Category
                      Status
                      Source
                      Language