Skip to content

Commit

Permalink
Split up function for checking different node types because they didn…
Browse files Browse the repository at this point in the history
…'t really share much code
  • Loading branch information
vsiakka committed May 4, 2019
1 parent 36e7d51 commit e4afbc1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
55 changes: 32 additions & 23 deletions src/rules/no-restricted-patterns.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var _ = require('lodash');
var rule = 'no-restricted-patterns';
var availableConfigs = {
'Global': [],
Expand All @@ -24,38 +25,46 @@ function noRestrictedPatterns(feature, fileName, configuration) {
restrictedPatterns[type] = restrictedPatterns[type].concat(globalPatterns);
});

checkNode(feature, restrictedPatterns, errors);
checkFeatureNode(feature, restrictedPatterns, errors);
return errors;
}


function checkNode(node, restrictedPatterns, errors, parentNodeType) {
// Steps use the configuration rules provided for their parents (eg Background)
// so allow the function to take an override for the node's type
var nodeType = parentNodeType || node.type;

if (node && restrictedPatterns.hasOwnProperty(nodeType)) {
restrictedPatterns[nodeType]
.forEach(function(pattern) {
check(node, 'name', pattern, errors);
check(node, 'description', pattern, errors, true);
check(node, 'text', pattern, errors);
});
function checkFeatureNode(node, restrictedPatterns, errors) {
if (_.isEmpty(node)) {
return;
}
checkNameAndDescription(node, restrictedPatterns, errors);
node.children.forEach(function(child) {
checkFeatureChildNode(child, restrictedPatterns, errors);
});
}

// Background, Scenarios and Scenario Outlines are children of a feature
if (node.children) {
node.children.forEach(function(child) {
checkNode(child, restrictedPatterns, errors);

function checkNameAndDescription(node, restrictedPatterns, errors) {
restrictedPatterns[node.type]
.forEach(function(pattern) {
check(node, 'name', pattern, errors);
check(node, 'description', pattern, errors, true);
});
}
}

if (node.steps) {
node.steps.forEach(function(step) {
// Use the node.type of the parent to determine which rule configuration to use
checkNode(step, restrictedPatterns, errors, node.type);

// Background, Scenarios and Scenario Outlines are children of a feature
function checkFeatureChildNode(node, restrictedPatterns, errors) {
checkNameAndDescription(node, restrictedPatterns, errors);
node.steps.forEach(function(step) {
// Use the node type of the parent to determine which rule configuration to use
checkStepNode(step, restrictedPatterns[node.type], errors);
});
}


function checkStepNode(node, restrictedPatterns, errors) {
restrictedPatterns
.forEach(function(pattern) {
check(node, 'text', pattern, errors);
});
}
}


Expand Down
21 changes: 0 additions & 21 deletions test/rules/no-restricted-patterns/FeatureViolations.feature
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
Feature: Feature with disallowed patterns
A bad description
A restricted global pattern

Background:
This is a good description
Given only allowed patterns are used

Scenario: Allowed steps only
This is a good description
Given I use one allowed step
When another allowed step is used
Then no errors should be reported

Scenario Outline: Allowed steps only
This is a good description
Given I use one allowed step
When another allowed step is used
Then no errors should be reported

Examples:
| example |
| one |
| two |

0 comments on commit e4afbc1

Please sign in to comment.