gplint
Version:
A Gherkin linter/validator written in Javascript.
71 lines • 2.64 kB
JavaScript
import _ from 'lodash';
export const name = 'no-homogenous-tags';
export function run({ feature }) {
if (!feature) {
return [];
}
const errors = [];
checkHomogenousContainer(feature, errors, ['Feature', 'Scenarios and Rules']);
return errors;
}
function getTagNames(node) {
return _.map(node.tags, tag => tag.name);
}
/**
* Tags that exist in every scenario and scenario outline
* should be applied on a feature/rule level
* @param container
* @param errors
* @param errorMessage
*/
function checkHomogenousContainer(container, errors, [containerName, childrenName]) {
const childrenTags = [];
if (container.children.length < 2) {
// Feature/Rule with only one child, skipping
return;
}
container.children.forEach(child => {
if (child.scenario) {
const { scenario } = child;
childrenTags.push(getTagNames(scenario));
const exampleTags = [];
scenario.examples.forEach(example => {
exampleTags.push(getTagNames(example));
});
const homogenousExampleTags = _.intersection(...exampleTags);
if (homogenousExampleTags.length) {
errors.push({
message: `All Examples of a Scenario Outline have the same tag(s), they should be defined on the Scenario Outline instead: ${homogenousExampleTags.join(', ')}`,
rule: name,
line: scenario.location.line,
column: scenario.location.column,
});
}
}
else if (child.rule) {
const { rule } = child;
childrenTags.push(getTagNames(rule));
checkHomogenousContainer(rule, errors, ['Rule', 'Scenarios']);
}
});
const homogenousTags = _.intersection(...childrenTags);
if (homogenousTags.length) {
errors.push({
message: `All ${childrenName} on this ${containerName} have the same tag(s), they should be defined on the ${containerName} instead: ${homogenousTags.join(', ')}`,
rule: name,
line: container.location.line,
column: container.location.column,
});
}
}
export const documentation = {
description: 'Disallows tags present on every Scenario/Rule in a Feature or Rule, rather than on the Feature/Rule itself. Skips if contains a single scenario.',
examples: [{
title: 'Example',
description: 'Enable rule',
config: {
[name]: 'error',
}
}],
};
//# sourceMappingURL=no-homogenous-tags.js.map