gherkin-lint-ts
Version:
Gherkin features linter written in Typescript
118 lines • 4.96 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.availableConfigs = exports.name = void 0;
const gherkinUtils = __importStar(require("./utils/gherkin"));
const chalk_1 = __importDefault(require("chalk"));
const _ = require("lodash");
exports.name = "indentation";
const defaultConfig = {
"Feature": 0,
"Background": 0,
"Rule": 0,
"Scenario": 0,
"Step": 2,
"Examples": 0,
"example": 2,
"given": 2,
"when": 2,
"then": 2,
"and": 2,
"but": 2,
};
exports.availableConfigs = _.merge({}, defaultConfig, {
// The values here are unused by the config parsing logic.
"feature tag": -1,
"scenario tag": -1,
});
function mergeConfiguration(configuration) {
let mergedConfiguration = _.merge({}, defaultConfig, configuration);
if (!Object.prototype.hasOwnProperty.call(mergedConfiguration, "feature tag")) {
mergedConfiguration["feature tag"] = mergedConfiguration["Feature"];
}
if (!Object.prototype.hasOwnProperty.call(mergedConfiguration, "scenario tag")) {
mergedConfiguration["scenario tag"] = mergedConfiguration["Scenario"];
}
return mergedConfiguration;
}
function run(feature, unused, configuration) {
var _a;
if (!feature) {
return [];
}
const errors = [];
const mergedConfiguration = mergeConfiguration(configuration);
function test(parsedLocation, type) {
// location.column is 1 index based so, when we compare with the expected
// indentation we need to subtract 1
if (parsedLocation.column - 1 !== mergedConfiguration[type]) {
errors.push({
message: `Wrong indentation for "${chalk_1.default.cyan(type)}", expected indentation level of ${mergedConfiguration[type]}, but got ${parsedLocation.column - 1}`,
rule: exports.name,
line: parsedLocation.line,
});
}
}
function testStep(step) {
let stepType = gherkinUtils.getLanguageInsensitiveKeyword(step, feature.language);
stepType = stepType in configuration ? stepType : "Step";
test(step.location, stepType);
}
function testTags(tags, type) {
_(tags).groupBy("location.line").forEach(tagLocationGroup => {
const firstTag = _(tagLocationGroup).sortBy("location.column").head();
test(firstTag.location, type);
});
}
test(feature.location, "Feature");
testTags(feature.tags, "feature tag");
(_a = feature.children) === null || _a === void 0 ? void 0 : _a.forEach(child => {
var _a, _b, _c, _d, _e, _f, _g;
if (child.rule) {
test(child.rule.location, "Rule");
}
else if (child.background) {
test(child.background.location, "Background");
(_a = child.background.steps) === null || _a === void 0 ? void 0 : _a.forEach(testStep);
}
else {
test((_b = child.scenario) === null || _b === void 0 ? void 0 : _b.location, "Scenario");
testTags((_c = child.scenario) === null || _c === void 0 ? void 0 : _c.tags, "scenario tag");
(_e = (_d = child.scenario) === null || _d === void 0 ? void 0 : _d.steps) === null || _e === void 0 ? void 0 : _e.forEach(testStep);
(_g = (_f = child.scenario) === null || _f === void 0 ? void 0 : _f.examples) === null || _g === void 0 ? void 0 : _g.forEach(examples => {
var _a;
test(examples.location, "Examples");
if (examples.tableHeader) {
test(examples.tableHeader.location, "example");
(_a = examples.tableBody) === null || _a === void 0 ? void 0 : _a.forEach(row => {
test(row.location, "example");
});
}
});
}
});
return errors;
}
exports.run = run;
//# sourceMappingURL=indentation.js.map