diff --git a/src/rules/no-duplicate-tags.js b/src/rules/no-duplicate-tags.js index b2d19e16..bea95107 100644 --- a/src/rules/no-duplicate-tags.js +++ b/src/rules/no-duplicate-tags.js @@ -1,33 +1,27 @@ const rule = 'no-duplicate-tags'; -const {compose, intoArray} = require('../utils/generic'); -const {filter, map} = require('../utils/transducers'); const {flatMapNodeTags} = require('../utils/gherkin'); -const collectTagsInfo = (tags, {name, location}) => { - const info = tags[name]; - if (info) { - info.count++; +const appendErrors = (track, tag) => { + if (track.names.has(tag.name)) { + track.errors.push(createError(tag)); } else { - tags[name] = { - count: 1, - location, - name, - }; + track.names.add(tag.name); } - return tags; + return track; }; +const createError = (tag) => ({ + type: 'rule', + message: `Duplicate tags are not allowed: ${tag.name}`, + rule: rule, + location: tag.location, +}); + const verifyTags = ({tags, location}) => { - const tagsInfo = tags.reduce(collectTagsInfo, {}); - return intoArray(compose( - filter(({count}) => count > 1), - map((tag) => ({ - type: 'rule', - message: `Duplicate tags are not allowed: ${tag.name}`, - rule: rule, - location: tag.location, - })) - ))(tagsInfo); + return tags.reduce(appendErrors, { + names: new Set(), + errors: [], + }).errors; }; module.exports = { diff --git a/test/rules/no-duplicate-tags/no-duplicate-tags.js b/test/rules/no-duplicate-tags/no-duplicate-tags.js index 6bd88fe8..2f7befd4 100644 --- a/test/rules/no-duplicate-tags/no-duplicate-tags.js +++ b/test/rules/no-duplicate-tags/no-duplicate-tags.js @@ -19,7 +19,7 @@ describe('No Duplicate Tags Rule', () => { rule: ruleName, location: { line: 1, - column: 1, + column: 13, }, }, { @@ -27,7 +27,15 @@ describe('No Duplicate Tags Rule', () => { rule: ruleName, location: { line: 7, - column: 1, + column: 14, + }, + }, + { + messageElements: {tags: '@scenariotag'}, + rule: ruleName, + location: { + line: 7, + column: 27, }, }, { @@ -35,7 +43,15 @@ describe('No Duplicate Tags Rule', () => { rule: ruleName, location: { line: 11, - column: 1, + column: 14, + }, + }, + { + messageElements: {tags: '@exampletag'}, + rule: ruleName, + location: { + line: 14, + column: 15, }, }, { @@ -43,7 +59,7 @@ describe('No Duplicate Tags Rule', () => { rule: ruleName, location: { line: 14, - column: 3, + column: 27, }, }]); });