UNPKG

5.83 kBJavaScriptView Raw
1"use strict";
2var __values = (this && this.__values) || function (o) {
3 var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
4 if (m) return m.call(o);
5 return {
6 next: function () {
7 if (o && i >= o.length) o = void 0;
8 return { value: o && o[i++], done: !o };
9 }
10 };
11};
12var __importDefault = (this && this.__importDefault) || function (mod) {
13 return (mod && mod.__esModule) ? mod : { "default": mod };
14};
15Object.defineProperty(exports, "__esModule", { value: true });
16var ParameterType_1 = __importDefault(require("./ParameterType"));
17var TreeRegexp_1 = __importDefault(require("./TreeRegexp"));
18var Argument_1 = __importDefault(require("./Argument"));
19var Errors_1 = require("./Errors");
20// RegExps with the g flag are stateful in JavaScript. In order to be able
21// to reuse them we have to wrap them in a function.
22// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
23// Does not include (){} characters because they have special meaning
24var ESCAPE_REGEXP = function () { return /([\\^[$.|?*+])/g; };
25var PARAMETER_REGEXP = function () { return /(\\\\)?{([^}]*)}/g; };
26var OPTIONAL_REGEXP = function () { return /(\\\\)?\(([^)]+)\)/g; };
27var ALTERNATIVE_NON_WHITESPACE_TEXT_REGEXP = function () {
28 return /([^\s^/]+)((\/[^\s^/]+)+)/g;
29};
30var DOUBLE_ESCAPE = '\\\\';
31var PARAMETER_TYPES_CANNOT_BE_ALTERNATIVE = 'Parameter types cannot be alternative: ';
32var PARAMETER_TYPES_CANNOT_BE_OPTIONAL = 'Parameter types cannot be optional: ';
33var CucumberExpression = /** @class */ (function () {
34 /**
35 * @param expression
36 * @param parameterTypeRegistry
37 */
38 function CucumberExpression(expression, parameterTypeRegistry) {
39 this.expression = expression;
40 this.parameterTypeRegistry = parameterTypeRegistry;
41 this.parameterTypes = [];
42 var expr = this.processEscapes(expression);
43 expr = this.processOptional(expr);
44 expr = this.processAlternation(expr);
45 expr = this.processParameters(expr, parameterTypeRegistry);
46 expr = "^" + expr + "$";
47 this.treeRegexp = new TreeRegexp_1.default(expr);
48 }
49 CucumberExpression.prototype.processEscapes = function (expression) {
50 return expression.replace(ESCAPE_REGEXP(), '\\$1');
51 };
52 CucumberExpression.prototype.processOptional = function (expression) {
53 var _this = this;
54 return expression.replace(OPTIONAL_REGEXP(), function (match, p1, p2) {
55 if (p1 === DOUBLE_ESCAPE) {
56 return "\\(" + p2 + "\\)";
57 }
58 _this.checkNoParameterType(p2, PARAMETER_TYPES_CANNOT_BE_OPTIONAL);
59 return "(?:" + p2 + ")?";
60 });
61 };
62 CucumberExpression.prototype.processAlternation = function (expression) {
63 var _this = this;
64 return expression.replace(ALTERNATIVE_NON_WHITESPACE_TEXT_REGEXP(), function (match) {
65 var e_1, _a;
66 // replace \/ with /
67 // replace / with |
68 var replacement = match.replace(/\//g, '|').replace(/\\\|/g, '/');
69 if (replacement.indexOf('|') !== -1) {
70 try {
71 for (var _b = __values(replacement.split(/\|/)), _c = _b.next(); !_c.done; _c = _b.next()) {
72 var part = _c.value;
73 _this.checkNoParameterType(part, PARAMETER_TYPES_CANNOT_BE_ALTERNATIVE);
74 }
75 }
76 catch (e_1_1) { e_1 = { error: e_1_1 }; }
77 finally {
78 try {
79 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
80 }
81 finally { if (e_1) throw e_1.error; }
82 }
83 return "(?:" + replacement + ")";
84 }
85 else {
86 return replacement;
87 }
88 });
89 };
90 CucumberExpression.prototype.processParameters = function (expression, parameterTypeRegistry) {
91 var _this = this;
92 return expression.replace(PARAMETER_REGEXP(), function (match, p1, p2) {
93 if (p1 === DOUBLE_ESCAPE) {
94 return "\\{" + p2 + "\\}";
95 }
96 var typeName = p2;
97 ParameterType_1.default.checkParameterTypeName(typeName);
98 var parameterType = parameterTypeRegistry.lookupByTypeName(typeName);
99 if (!parameterType) {
100 throw new Errors_1.UndefinedParameterTypeError(typeName);
101 }
102 _this.parameterTypes.push(parameterType);
103 return buildCaptureRegexp(parameterType.regexpStrings);
104 });
105 };
106 CucumberExpression.prototype.match = function (text) {
107 return Argument_1.default.build(this.treeRegexp, text, this.parameterTypes);
108 };
109 Object.defineProperty(CucumberExpression.prototype, "regexp", {
110 get: function () {
111 return this.treeRegexp.regexp;
112 },
113 enumerable: true,
114 configurable: true
115 });
116 Object.defineProperty(CucumberExpression.prototype, "source", {
117 get: function () {
118 return this.expression;
119 },
120 enumerable: true,
121 configurable: true
122 });
123 CucumberExpression.prototype.checkNoParameterType = function (s, message) {
124 if (s.match(PARAMETER_REGEXP())) {
125 throw new Errors_1.CucumberExpressionError(message + this.source);
126 }
127 };
128 return CucumberExpression;
129}());
130exports.default = CucumberExpression;
131function buildCaptureRegexp(regexps) {
132 if (regexps.length === 1) {
133 return "(" + regexps[0] + ")";
134 }
135 var captureGroups = regexps.map(function (group) {
136 return "(?:" + group + ")";
137 });
138 return "(" + captureGroups.join('|') + ")";
139}
140//# sourceMappingURL=CucumberExpression.js.map
\No newline at end of file