NaturalIntelligence/fast-xml-parser

View on GitHub
src/validator.js

Summary

Maintainability
F
1 wk
Test Coverage
A
99%

Function validate has a Cognitive Complexity of 133 (exceeds 5 allowed). Consider refactoring.
Open

exports.validate = function (xmlData, options) {
  options = Object.assign({}, defaultOptions, options);

  //xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
  //xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag
Severity: Minor
Found in src/validator.js - About 2 days 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 validate has 139 lines of code (exceeds 25 allowed). Consider refactoring.
Open

exports.validate = function (xmlData, options) {
  options = Object.assign({}, defaultOptions, options);

  //xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
  //xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag
Severity: Major
Found in src/validator.js - About 5 hrs to fix

    File validator.js has 327 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    'use strict';
    
    const util = require('./util');
    
    const defaultOptions = {
    Severity: Minor
    Found in src/validator.js - About 3 hrs to fix

      Function readCommentAndCDATA has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring.
      Open

      function readCommentAndCDATA(xmlData, i) {
        if (xmlData.length > i + 5 && xmlData[i + 1] === '-' && xmlData[i + 2] === '-') {
          //comment
          for (i += 3; i < xmlData.length; i++) {
            if (xmlData[i] === '-' && xmlData[i + 1] === '-' && xmlData[i + 2] === '>') {
      Severity: Minor
      Found in src/validator.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 readCommentAndCDATA has 46 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      function readCommentAndCDATA(xmlData, i) {
        if (xmlData.length > i + 5 && xmlData[i + 1] === '-' && xmlData[i + 2] === '-') {
          //comment
          for (i += 3; i < xmlData.length; i++) {
            if (xmlData[i] === '-' && xmlData[i + 1] === '-' && xmlData[i + 2] === '>') {
      Severity: Minor
      Found in src/validator.js - About 1 hr to fix

        Function readAttributeStr has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

        function readAttributeStr(xmlData, i) {
          let attrStr = '';
          let startChar = '';
          let tagClosed = false;
          for (; i < xmlData.length; i++) {
        Severity: Minor
        Found in src/validator.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

        Consider simplifying this complex logical expression.
        Open

          } else if (
            xmlData.length > i + 9 &&
            xmlData[i + 1] === '[' &&
            xmlData[i + 2] === 'C' &&
            xmlData[i + 3] === 'D' &&
        Severity: Critical
        Found in src/validator.js - About 1 hr to fix

          Consider simplifying this complex logical expression.
          Open

            } else if (
              xmlData.length > i + 8 &&
              xmlData[i + 1] === 'D' &&
              xmlData[i + 2] === 'O' &&
              xmlData[i + 3] === 'C' &&
          Severity: Critical
          Found in src/validator.js - About 1 hr to fix

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

            function validateAttributeString(attrStr, options) {
              //console.log("start:"+attrStr+":end");
            
              //if(attrStr.trim().length === 0) return true; //empty string
            
            
            Severity: Minor
            Found in src/validator.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 readAttributeStr has 27 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function readAttributeStr(xmlData, i) {
              let attrStr = '';
              let startChar = '';
              let tagClosed = false;
              for (; i < xmlData.length; i++) {
            Severity: Minor
            Found in src/validator.js - About 1 hr to fix

              Function readPI has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
              Open

              function readPI(xmlData, i) {
                const start = i;
                for (; i < xmlData.length; i++) {
                  if (xmlData[i] == '?' || xmlData[i] == ' ') {
                    //tagname
              Severity: Minor
              Found in src/validator.js - 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

              Avoid deeply nested control flow statements.
              Open

                      if (!validateTagName(tagName)) {
                        let msg;
                        if (tagName.trim().length === 0) {
                          msg = "Invalid space after '<'.";
                        } else {
              Severity: Major
              Found in src/validator.js - About 45 mins to fix

                Avoid deeply nested control flow statements.
                Open

                        if (xmlData[i] === '/') {
                          //closing tag
                          closingTag = true;
                          i++;
                        }
                Severity: Major
                Found in src/validator.js - About 45 mins to fix

                  Avoid deeply nested control flow statements.
                  Open

                          for (i++; i < xmlData.length; i++) {
                            if (xmlData[i] === '<') {
                              if (xmlData[i + 1] === '!') {
                                //comment or CADATA
                                i++;
                  Severity: Major
                  Found in src/validator.js - About 45 mins to fix

                    Avoid deeply nested control flow statements.
                    Open

                            if (result === false) {
                              return getErrorObject('InvalidAttr', "Attributes for '"+tagName+"' have open quote.", getLineNumberForPosition(xmlData, i));
                            }
                    Severity: Major
                    Found in src/validator.js - About 45 mins to fix

                      Avoid deeply nested control flow statements.
                      Open

                              for (; i < xmlData.length &&
                                xmlData[i] !== '>' &&
                                xmlData[i] !== ' ' &&
                                xmlData[i] !== '\t' &&
                                xmlData[i] !== '\n' &&
                      Severity: Major
                      Found in src/validator.js - About 45 mins to fix

                        Avoid deeply nested control flow statements.
                        Open

                                if (tagName[tagName.length - 1] === '/') {
                                  //self closing tag without attributes
                                  tagName = tagName.substring(0, tagName.length - 1);
                                  //continue;
                                  i--;
                        Severity: Major
                        Found in src/validator.js - About 45 mins to fix

                          Avoid deeply nested control flow statements.
                          Open

                                  if (xmlData[i] === '<') {
                                    i--;
                                  }
                          Severity: Major
                          Found in src/validator.js - About 45 mins to fix

                            Avoid deeply nested control flow statements.
                            Open

                                    if (attrStr[attrStr.length - 1] === '/') {
                                      //self closing tag
                                      const attrStrStart = i - attrStr.length;
                                      attrStr = attrStr.substring(0, attrStr.length - 1);
                                      const isValid = validateAttributeString(attrStr, options);
                            Severity: Major
                            Found in src/validator.js - About 45 mins to fix

                              Avoid deeply nested control flow statements.
                              Open

                                      if (angleBracketsCount === 0) {
                                        break;
                                      }
                              Severity: Major
                              Found in src/validator.js - About 45 mins to fix

                                Avoid deeply nested control flow statements.
                                Open

                                      if (xmlData[i] === ']' && xmlData[i + 1] === ']' && xmlData[i + 2] === '>') {
                                        i += 2;
                                        break;
                                      }
                                Severity: Major
                                Found in src/validator.js - About 45 mins to fix

                                  Consider simplifying this complex logical expression.
                                  Open

                                        if (xmlData[i] === '!') {
                                          i = readCommentAndCDATA(xmlData, i);
                                          continue;
                                        } else {
                                          let closingTag = false;
                                  Severity: Major
                                  Found in src/validator.js - About 40 mins to fix

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

                                    function validateAmpersand(xmlData, i) {
                                      // https://www.w3.org/TR/xml/#dt-charref
                                      i++;
                                      if (xmlData[i] === ';')
                                        return -1;
                                    Severity: Minor
                                    Found in src/validator.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

                                    Avoid too many return statements within this function.
                                    Open

                                                  return getErrorObject('InvalidXml', "Extra text at the end", getLineNumberForPosition(xmlData, i));
                                    Severity: Major
                                    Found in src/validator.js - About 30 mins to fix

                                      Avoid too many return statements within this function.
                                      Open

                                                    return getErrorObject('InvalidChar', "char '&' is not expected.", getLineNumberForPosition(xmlData, i));
                                      Severity: Major
                                      Found in src/validator.js - About 30 mins to fix

                                        Avoid too many return statements within this function.
                                        Open

                                              return getErrorObject('InvalidXml', "Invalid '"+
                                                  JSON.stringify(tags.map(t => t.tagName), null, 4).replace(/\r?\n/g, '')+
                                                  "' found.", {line: 1, col: 1});
                                        Severity: Major
                                        Found in src/validator.js - About 30 mins to fix

                                          Avoid too many return statements within this function.
                                          Open

                                                      return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
                                          Severity: Major
                                          Found in src/validator.js - About 30 mins to fix

                                            Avoid too many return statements within this function.
                                            Open

                                                        return getErrorObject(isValid.err.code, isValid.err.msg, getLineNumberForPosition(xmlData, i - attrStr.length + isValid.err.line));
                                            Severity: Major
                                            Found in src/validator.js - About 30 mins to fix

                                              Avoid too many return statements within this function.
                                              Open

                                                          return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, i));
                                              Severity: Major
                                              Found in src/validator.js - About 30 mins to fix

                                                Avoid too many return statements within this function.
                                                Open

                                                            return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
                                                Severity: Major
                                                Found in src/validator.js - About 30 mins to fix

                                                  Avoid too many return statements within this function.
                                                  Open

                                                        return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is repeated.", getPositionFromMatch(matches[i]));
                                                  Severity: Major
                                                  Found in src/validator.js - About 30 mins to fix

                                                    Avoid too many return statements within this function.
                                                    Open

                                                          return getErrorObject('InvalidTag', "Unclosed tag '"+tags[0].tagName+"'.", getLineNumberForPosition(xmlData, tags[0].tagStartPos));
                                                    Severity: Major
                                                    Found in src/validator.js - About 30 mins to fix

                                                      Avoid too many return statements within this function.
                                                      Open

                                                            return getErrorObject('InvalidChar', "char '"+xmlData[i]+"' is not expected.", getLineNumberForPosition(xmlData, i));
                                                      Severity: Major
                                                      Found in src/validator.js - About 30 mins to fix

                                                        Avoid too many return statements within this function.
                                                        Open

                                                                    return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
                                                        Severity: Major
                                                        Found in src/validator.js - About 30 mins to fix

                                                          Avoid too many return statements within this function.
                                                          Open

                                                                        if (i.err) return i;
                                                          Severity: Major
                                                          Found in src/validator.js - About 30 mins to fix

                                                            Avoid too many return statements within this function.
                                                            Open

                                                              return true;
                                                            Severity: Major
                                                            Found in src/validator.js - About 30 mins to fix

                                                              Avoid too many return statements within this function.
                                                              Open

                                                                            return getErrorObject('InvalidTag',
                                                                              "Expected closing tag '"+otg.tagName+"' (opened in line "+openPos.line+", col "+openPos.col+") instead of closing tag '"+tagName+"'.",
                                                                              getLineNumberForPosition(xmlData, tagStartPos));
                                                              Severity: Major
                                                              Found in src/validator.js - About 30 mins to fix

                                                                Avoid too many return statements within this function.
                                                                Open

                                                                    return getErrorObject('InvalidXml', 'Start tag expected.', 1);
                                                                Severity: Major
                                                                Found in src/validator.js - About 30 mins to fix

                                                                  Avoid too many return statements within this function.
                                                                  Open

                                                                    return true;
                                                                  Severity: Major
                                                                  Found in src/validator.js - About 30 mins to fix

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

                                                                    function validateNumberAmpersand(xmlData, i) {
                                                                      let re = /\d/;
                                                                      if (xmlData[i] === 'x') {
                                                                        i++;
                                                                        re = /[\da-fA-F]/;
                                                                    Severity: Minor
                                                                    Found in src/validator.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

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

                                                                      } else if (
                                                                        xmlData.length > i + 8 &&
                                                                        xmlData[i + 1] === 'D' &&
                                                                        xmlData[i + 2] === 'O' &&
                                                                        xmlData[i + 3] === 'C' &&
                                                                    Severity: Major
                                                                    Found in src/validator.js and 1 other location - About 4 hrs to fix
                                                                    src/validator.js on lines 251..267

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

                                                                    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

                                                                      } else if (
                                                                        xmlData.length > i + 9 &&
                                                                        xmlData[i + 1] === '[' &&
                                                                        xmlData[i + 2] === 'C' &&
                                                                        xmlData[i + 3] === 'D' &&
                                                                    Severity: Major
                                                                    Found in src/validator.js and 1 other location - About 4 hrs to fix
                                                                    src/validator.js on lines 230..267

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

                                                                    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

                                                                        for (i += 8; i < xmlData.length; i++) {
                                                                          if (xmlData[i] === ']' && xmlData[i + 1] === ']' && xmlData[i + 2] === '>') {
                                                                            i += 2;
                                                                            break;
                                                                          }
                                                                    Severity: Major
                                                                    Found in src/validator.js and 1 other location - About 1 hr to fix
                                                                    src/validator.js on lines 224..229

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

                                                                    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

                                                                        for (i += 3; i < xmlData.length; i++) {
                                                                          if (xmlData[i] === '-' && xmlData[i + 1] === '-' && xmlData[i + 2] === '>') {
                                                                            i += 2;
                                                                            break;
                                                                          }
                                                                    Severity: Major
                                                                    Found in src/validator.js and 1 other location - About 1 hr to fix
                                                                    src/validator.js on lines 261..266

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

                                                                    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