TypeError: Cannot read properties of undefined (reading 'find') in gherkin lint rule

14 Views Asked by At

I am creating a custom gherkin lint rule to make sure that any ignored scenario has a comment with a Jira ticket id on the line before the tag. I am getting the title error in this function

function run(feature) {
  if (!feature) return [];

  let errors = [];

  feature.children.forEach((child) => {
    if (child.scenario) {
      let scenario = child.scenario;

      scenario.tags.forEach((tag, index) => {
        if (tag.name === "@Ignore") {
          let previousLine = scenario.location.line - 2;
          let comment = feature.comments.find(
            (comment) => comment.location.line === previousLine
          );

          if (!comment || !comment.text.includes("FD-")) {
            errors.push({
              message:
                "If the @Ignore tag is used, there must be a comment on the line before it that includes a Jira ticket number.",
              rule,
              line: scenario.location.line,
            });
          }
        }
      });
    }
  });

  return errors;
}

This code should just let me get the line that the comment should be on and I have tested with some different conditonal statements, but the conditional statements wouldnt seem to be correct and I still got the same errorsuch as this below:

let comment = feature.comments.find(
            (comment) => comment.location && comment.location.line === previousLine
          );

I also tried making sure that the comment exists first by having an if statement inside the find block as I saw in this question:

let comment = feature.comments.find(
            (comment) => {if(comment.location.line) comment.location.line === previousLine;}
          );

But this also did not work(I tried having both comment and comment.location inside the if statement as well and that did not work either) What is failing here and how should I make it work correctly?

0

There are 0 best solutions below