gpc-remove-duplicates
Version:
This GherKing Precompiler is responsible for having only a reasonable amount of tags and/or rows in each feature file.
103 lines • 4.66 kB
JavaScript
"use strict";
const gherkin_ast_1 = require("gherkin-ast");
const tagSet = require("./tag-set");
const rowSet = require("./row-set");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const debug = require("debug")("gpc:remove-duplicates");
const DEFAULT_CONFIG = {
processRows: false,
processTags: true,
};
class RemoveDuplicates {
constructor(options) {
this.options = Object.assign(Object.assign({}, DEFAULT_CONFIG), (options || {}));
}
handleElementTags(e, p, ...tagsToIgnore) {
/* istanbul ignore next */
debug('handleElementTags(e: %s, p: %s, toIgnore: %d)', e === null || e === void 0 ? void 0 : e.constructor.name, p === null || p === void 0 ? void 0 : p.constructor.name, tagsToIgnore.length);
if (!this.options.processTags) {
debug('...tags not processed');
return;
}
debug('...tags: %o', e.tags);
e.tags = tagSet.removeDuplicates(e.tags, ...this.featureTags);
debug('...tags after deduplicate with feature tags: %o', e.tags);
if (!!p && p instanceof gherkin_ast_1.Rule && this.ruleTags[p._id]) {
debug('...rule tags: %o', this.ruleTags[p._id]);
e.tags = tagSet.removeDuplicates(e.tags, ...this.ruleTags[p._id]);
debug('...tags after deduplicate with rule tags: %o', e.tags);
}
if (tagsToIgnore.length) {
debug('...ignored tags: %o', tagsToIgnore);
e.tags = tagSet.removeDuplicates(e.tags, ...tagsToIgnore);
debug('...tags after deduplicate with ignored tags: %o', e.tags);
}
}
handleTableRows(e) {
/* istanbul ignore next */
debug('handleTableRows(e: %s)', e === null || e === void 0 ? void 0 : e.constructor.name);
if (!this.options.processRows) {
debug('...rows not processed');
return;
}
if (e instanceof gherkin_ast_1.DataTable) {
debug('...data table rows: %o', e.rows);
e.rows = rowSet.removeDuplicates(e.rows);
debug('...data table rows after deduplicate: %o', e.rows);
}
else if (e instanceof gherkin_ast_1.Examples) {
debug('...example rows: %o', e.body);
e.body = rowSet.removeDuplicates(e.body);
debug('...example rows after deduplicate: %o', e.body);
}
}
onFeature(f, _1, _2) {
/* istanbul ignore next */
debug('onFeature(f: %s)', f === null || f === void 0 ? void 0 : f.constructor.name);
if (!this.options.processTags) {
debug('...tags not processed');
return;
}
debug('...tags: %o', f.tags);
f.tags = tagSet.removeDuplicates(f.tags);
debug('...tags after deduplicate: %o', f.tags);
this.featureTags = f.tags;
this.ruleTags = {};
debug('...{featureTags: %o, ruleTags: %o}', this.featureTags, this.ruleTags);
}
onRule(r, f, _2) {
/* istanbul ignore next */
debug('onRule(r: %s, f: %s)', r === null || r === void 0 ? void 0 : r.constructor.name, f === null || f === void 0 ? void 0 : f.constructor.name);
this.handleElementTags(r, f);
this.ruleTags[r._id] = r.tags;
debug('...{ruleTags: %o}', this.ruleTags);
}
onScenario(s, p, _1) {
/* istanbul ignore next */
debug('onScenario(s: %s, p: %s)', s === null || s === void 0 ? void 0 : s.constructor.name, p === null || p === void 0 ? void 0 : p.constructor.name);
this.handleElementTags(s, p);
}
onScenarioOutline(so, p, _1) {
/* istanbul ignore next */
debug('onScenarioOutline(so: %s, p: %s)', so === null || so === void 0 ? void 0 : so.constructor.name, p === null || p === void 0 ? void 0 : p.constructor.name);
this.handleElementTags(so, p);
for (const e of so.examples) {
/* istanbul ignore next */
debug('(pseudo)onExamples(e: %s, so: %s)', e === null || e === void 0 ? void 0 : e.constructor.name, so === null || so === void 0 ? void 0 : so.constructor.name);
this.handleElementTags(e, p);
this.handleElementTags(e, null, ...so.tags);
}
}
onDataTable(t, _1) {
/* istanbul ignore next */
debug('onDataTable(t: %s)', t === null || t === void 0 ? void 0 : t.constructor.name);
this.handleTableRows(t);
}
onExamples(e, _1, _2) {
/* istanbul ignore next */
debug('onExamples(e: %s)', e === null || e === void 0 ? void 0 : e.constructor.name);
this.handleTableRows(e);
}
}
module.exports = RemoveDuplicates;
//# sourceMappingURL=index.js.map