UNPKG

gherkin

Version:
137 lines 5.41 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var Parser_1 = __importDefault(require("../Parser")); var crypto_1 = __importDefault(require("crypto")); var TokenMatcher_1 = __importDefault(require("../TokenMatcher")); var cucumber_messages_1 = require("cucumber-messages"); var types_1 = require("../types"); var compile_1 = __importDefault(require("../pickles/compile")); function numberToInt32LE(value) { var buffer = Buffer.allocUnsafe(4); buffer.writeInt32LE(value, 0); return buffer; } function makeId(data, locations) { var e_1, _a; var shasum = crypto_1.default.createHash('sha1'); shasum.update(Buffer.from(data)); try { for (var locations_1 = __values(locations), locations_1_1 = locations_1.next(); !locations_1_1.done; locations_1_1 = locations_1.next()) { var loc = locations_1_1.value; shasum.update(numberToInt32LE(loc.line)); shasum.update(numberToInt32LE(loc.column)); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (locations_1_1 && !locations_1_1.done && (_a = locations_1.return)) _a.call(locations_1); } finally { if (e_1) throw e_1.error; } } return shasum.digest('hex'); } function generateMessages(data, uri, options) { var e_2, _a, e_3, _b; var result = []; var opts = types_1.gherkinOptions(options); try { if (opts.includeSource) { result.push(new cucumber_messages_1.messages.Envelope({ source: { uri: uri, data: data, media: { encoding: cucumber_messages_1.messages.Media.Encoding.UTF8, contentType: 'text/x.cucumber.gherkin+plain', }, }, })); } if (!opts.includeGherkinDocument && !opts.includePickles) { return result; } var parser = new Parser_1.default(); parser.stopAtFirstError = false; var gherkinDocument = parser.parse(data, new TokenMatcher_1.default(opts.defaultDialect)); if (opts.includeGherkinDocument) { result.push(cucumber_messages_1.messages.Envelope.fromObject({ gherkinDocument: __assign(__assign({}, gherkinDocument), { uri: uri }), })); } if (opts.includePickles) { var pickles = compile_1.default(gherkinDocument); try { for (var pickles_1 = __values(pickles), pickles_1_1 = pickles_1.next(); !pickles_1_1.done; pickles_1_1 = pickles_1.next()) { var pickle = pickles_1_1.value; var id = makeId(data, pickle.locations); result.push(cucumber_messages_1.messages.Envelope.fromObject({ pickle: __assign(__assign({}, pickle), { id: id, uri: uri }), })); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (pickles_1_1 && !pickles_1_1.done && (_a = pickles_1.return)) _a.call(pickles_1); } finally { if (e_2) throw e_2.error; } } } } catch (err) { var errors = err.errors || [err]; try { for (var errors_1 = __values(errors), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) { var error = errors_1_1.value; result.push(cucumber_messages_1.messages.Envelope.fromObject({ attachment: { source: { uri: uri, location: { line: error.location.line, column: error.location.column, }, }, data: error.message, }, })); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (errors_1_1 && !errors_1_1.done && (_b = errors_1.return)) _b.call(errors_1); } finally { if (e_3) throw e_3.error; } } } return result; } exports.default = generateMessages; //# sourceMappingURL=generateMessages.js.map