gherkin-lint-ts
Version:
Gherkin features linter written in Typescript
74 lines • 3.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.name = void 0;
const gherkinUtils = __importStar(require("./utils/gherkin"));
const chalk_1 = __importDefault(require("chalk"));
exports.name = "keywords-in-logical-order";
const keywordList = ["given", "when", "then"];
function run(feature) {
var _a;
if (!feature) {
return [];
}
const errors = [];
(_a = feature.children) === null || _a === void 0 ? void 0 : _a.forEach((child) => {
var _a;
if (child.rule) {
(_a = child.rule.children) === null || _a === void 0 ? void 0 : _a.forEach(ruleChild => checkNode(ruleChild, feature, errors));
}
else {
checkNode(child, feature, errors);
}
});
return errors;
}
exports.run = run;
function checkNode(child, feature, errors) {
var _a;
let maxKeywordPosition = 0;
const node = child.background || child.scenario;
(_a = node === null || node === void 0 ? void 0 : node.steps) === null || _a === void 0 ? void 0 : _a.forEach((step) => {
const keyword = gherkinUtils.getLanguageInsensitiveKeyword(step, feature.language);
let keywordPosition = keywordList.indexOf(keyword);
if (keywordPosition === -1) {
// not found
return;
}
if (keywordPosition < maxKeywordPosition) {
const maxKeyword = keywordList[maxKeywordPosition];
errors.push(createError(step, maxKeyword));
}
maxKeywordPosition = Math.max(maxKeywordPosition, keywordPosition) || keywordPosition;
});
}
function createError(step, maxKeyword) {
var _a;
return {
message: `Step "${chalk_1.default.yellow(`${step.keyword}${step.text}`)}" should not appear after step using keyword ${chalk_1.default.cyan(maxKeyword)}`,
rule: exports.name,
line: ((_a = step.location) === null || _a === void 0 ? void 0 : _a.line) || 0,
};
}
//# sourceMappingURL=keywords-in-logical-order.js.map