snowplow/snowplow-javascript-tracker

View on GitHub
libraries/tracker-core/src/contexts.ts

Summary

Maintainability
F
1 wk
Test Coverage

File contexts.ts has 496 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/*
 * Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
Severity: Minor
Found in libraries/tracker-core/src/contexts.ts - About 7 hrs to fix

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

    export function globalContexts(): GlobalContexts {
      let globalPrimitives: Array<ContextPrimitive> = [];
      let conditionalProviders: Array<ConditionalContextProvider> = [];
      let namedPrimitives: Record<string, ContextPrimitive> = {};
      let namedConditionalProviders: Record<string, ConditionalContextProvider> = {};
    Severity: Minor
    Found in libraries/tracker-core/src/contexts.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 globalContexts has 80 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    export function globalContexts(): GlobalContexts {
      let globalPrimitives: Array<ContextPrimitive> = [];
      let conditionalProviders: Array<ConditionalContextProvider> = [];
      let namedPrimitives: Record<string, ContextPrimitive> = {};
      let namedConditionalProviders: Record<string, ConditionalContextProvider> = {};
    Severity: Major
    Found in libraries/tracker-core/src/contexts.ts - About 3 hrs to fix

      Function compareProvider has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

      function compareProvider(
        a: ConditionalContextProvider | ContextPrimitive,
        b: ConditionalContextProvider | ContextPrimitive
      ): boolean {
        if (isConditionalContextProvider(a)) {
      Severity: Minor
      Found in libraries/tracker-core/src/contexts.ts - 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 matchSchemaAgainstRuleSet has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

      export function matchSchemaAgainstRuleSet(ruleSet: RuleSet, schema: string): boolean {
        let rejectCount = 0;
        let acceptCount = 0;
        const acceptRules = ruleSet['accept'];
        if (Array.isArray(acceptRules)) {
      Severity: Minor
      Found in libraries/tracker-core/src/contexts.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 isRuleSet has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

      export function isRuleSet(input: unknown): input is Record<string, unknown> {
        const ruleSet = input as Record<string, unknown>;
        let ruleCount = 0;
        if (input != null && typeof input === 'object' && !Array.isArray(input)) {
          if (Object.prototype.hasOwnProperty.call(ruleSet, 'accept')) {
      Severity: Minor
      Found in libraries/tracker-core/src/contexts.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 matchSchemaAgainstRuleSet has 28 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      export function matchSchemaAgainstRuleSet(ruleSet: RuleSet, schema: string): boolean {
        let rejectCount = 0;
        let acceptCount = 0;
        const acceptRules = ruleSet['accept'];
        if (Array.isArray(acceptRules)) {
      Severity: Minor
      Found in libraries/tracker-core/src/contexts.ts - About 1 hr to fix

        Function validateVendorParts has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

        export function validateVendorParts(parts: Array<string>): boolean {
          if (parts[0] === '*' || parts[1] === '*') {
            return false; // no wildcard in first or second part
          }
          if (parts.slice(2).length > 0) {
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.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 matchSchemaAgainstRule has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
        Open

        export function matchSchemaAgainstRule(rule: string, schema: string): boolean {
          if (!isValidRule(rule)) return false;
          const ruleParts = getRuleParts(rule);
          const schemaParts = getSchemaParts(schema);
          if (ruleParts && schemaParts) {
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.ts - About 55 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

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

        function evaluateProvider(
          provider: ConditionalContextProvider,
          event: PayloadBuilder,
          eventType: string,
          eventSchema: string
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.ts - 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

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

        function matchVendor(rule: string, vendor: string): boolean {
          // rule and vendor must have same number of elements
          const vendorParts = vendor.split('.');
          const ruleParts = rule.split('.');
          if (vendorParts && ruleParts) {
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.ts - 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

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

        function getUsefulSchema(sb: PayloadBuilder): string {
          let eventJson = sb.getJson();
          for (const json of eventJson) {
            if (json.keyIfEncoded === 'ue_px' && typeof json.json['data'] === 'object') {
              const schema = (json.json['data'] as Record<string, unknown>)['schema'];
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.ts - 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

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

        export function isFilterProvider(input: unknown): boolean {
          if (Array.isArray(input)) {
            if (input.length === 2) {
              if (Array.isArray(input[1])) {
                return isContextCallbackFunction(input[0]) && input[1].every(isContextPrimitive);
        Severity: Minor
        Found in libraries/tracker-core/src/contexts.ts - 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

        Avoid too many return statements within this function.
        Open

              if (Array.isArray(primitivesB)) return false;
        Severity: Major
        Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

            return false;
          Severity: Major
          Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                if (!isContextPrimitive(b)) return false;
            Severity: Major
            Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                    return compareProviderPart(primitivesA, primitivesB);
              Severity: Major
              Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                  return false;
                Severity: Major
                Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                      return compareProviderPart(a, b);
                  Severity: Major
                  Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                      return false;
                    Severity: Major
                    Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                            return primitivesA.reduce<boolean>((matches, a, i) => matches && compareProviderPart(a, primitivesB[i]), true);
                      Severity: Major
                      Found in libraries/tracker-core/src/contexts.ts - About 30 mins to fix

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

                        function generateConditionals(
                          providers: Array<ConditionalContextProvider> | ConditionalContextProvider,
                          event: PayloadBuilder,
                          eventType: string,
                          eventSchema: string
                        Severity: Major
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 1 day to fix
                        libraries/tracker-core/src/contexts.ts on lines 647..665

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

                        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

                        function generatePrimitives(
                          contextPrimitives: Array<ContextPrimitive> | ContextPrimitive,
                          event: PayloadBuilder,
                          eventType: string,
                          eventSchema: string
                        Severity: Major
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 1 day to fix
                        libraries/tracker-core/src/contexts.ts on lines 750..768

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

                        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 (Array.isArray(acceptRules)) {
                            if ((ruleSet.accept as Array<string>).some((rule) => matchSchemaAgainstRule(rule, schema))) {
                              acceptCount++;
                            }
                          } else if (typeof acceptRules === 'string') {
                        Severity: Major
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 2 hrs to fix
                        libraries/tracker-core/src/contexts.ts on lines 533..541

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

                        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 (Array.isArray(rejectRules)) {
                            if ((ruleSet.reject as Array<string>).some((rule) => matchSchemaAgainstRule(rule, schema))) {
                              rejectCount++;
                            }
                          } else if (typeof rejectRules === 'string') {
                        Severity: Major
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 2 hrs to fix
                        libraries/tracker-core/src/contexts.ts on lines 522..530

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

                        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 function isContextPrimitive(input: unknown): input is ContextPrimitive {
                          return isContextCallbackFunction(input) || isSelfDescribingJson(input);
                        }
                        Severity: Minor
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 35 mins to fix
                        libraries/tracker-core/src/contexts.ts on lines 509..511

                        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

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

                        export function isConditionalContextProvider(input: unknown): input is ConditionalContextProvider {
                          return isFilterProvider(input) || isRuleSetProvider(input);
                        }
                        Severity: Minor
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 35 mins to fix
                        libraries/tracker-core/src/contexts.ts on lines 471..473

                        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

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

                          addGlobalContexts(
                            contexts:
                              | Array<ConditionalContextProvider | ContextPrimitive>
                              | Record<string, ConditionalContextProvider | ContextPrimitive>
                          ): void;
                        Severity: Minor
                        Found in libraries/tracker-core/src/contexts.ts and 1 other location - About 35 mins to fix
                        libraries/tracker-core/src/core.ts on lines 276..280

                        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

                        There are no issues that match your filters.

                        Category
                        Status