@autometa/cucumber-expressions 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ /dist
2
+ /out
3
+ node_modules
package/.eslintrc.cjs ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ["custom"],
4
+ };
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) undefined Ben Aherne
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Introduction
2
+
3
+ There's nothing here yet
@@ -0,0 +1,432 @@
1
+ var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+
15
+ // src/step-matcher.ts
16
+ import { diffWordsWithSpace } from "diff";
17
+ import { distance } from "closest-match";
18
+ import { AssertKey } from "@autometa/asserters";
19
+ function checkMatch(text, it) {
20
+ return it.matches(text);
21
+ }
22
+ function limitDiffs(sameStepType, differentStepType, max) {
23
+ const sameDistances = sameStepType.map((it) => it.distance);
24
+ const maxSameStepDistance = Math.max(...sameDistances);
25
+ const otherStepDistance = differentStepType.map((it) => it.distance);
26
+ const minDifferentStepDistance = Math.min(...otherStepDistance);
27
+ if (maxSameStepDistance > minDifferentStepDistance) {
28
+ const filter = sameStepType.filter(
29
+ (it) => it.distance <= minDifferentStepDistance
30
+ );
31
+ if (filter.length >= max) {
32
+ return { same: filter.slice(0, max), other: [] };
33
+ }
34
+ const diff = max - filter.length;
35
+ const sameSlice = sameStepType.slice(0, diff);
36
+ const differentSlice = differentStepType.slice(0, max - diff);
37
+ return { same: sameSlice, other: differentSlice };
38
+ }
39
+ const maxIndex = Math.min(max, sameStepType.length);
40
+ const result = { same: sameStepType.slice(0, maxIndex), other: [] };
41
+ return result;
42
+ }
43
+ function getDiffs(text, maxResults, step) {
44
+ const sorted = step.map((it) => {
45
+ if (checkMatch(text, it)) {
46
+ return { merged: text, step: it, gherkin: text, distance: 0 };
47
+ }
48
+ AssertKey(it, "expression");
49
+ AssertKey(it, "matches");
50
+ const diff = getDiff(text, it);
51
+ const refined = refineDiff(diff);
52
+ const dist = distance(text, refined);
53
+ return { merged: refined, step: it, gherkin: text, distance: dist };
54
+ }).sort((a, b) => a.distance - b.distance);
55
+ const max = Math.min(maxResults, sorted.length);
56
+ return sorted.slice(0, max);
57
+ }
58
+ function getDiff(text, it) {
59
+ return diffWordsWithSpace(text, it.expression.source);
60
+ }
61
+ function refineDiff(diff) {
62
+ const strings = [];
63
+ for (let index = 0; index < diff.length; index++) {
64
+ const gherkinChange = diff[index];
65
+ const scopeChange = diff[index + 1];
66
+ if (isExpressionCandidate(gherkinChange, scopeChange)) {
67
+ strings.push(gherkinChange.value);
68
+ index++;
69
+ continue;
70
+ }
71
+ if (gherkinChange.value) {
72
+ strings.push(gherkinChange.value);
73
+ continue;
74
+ }
75
+ }
76
+ return strings.join("");
77
+ }
78
+ function isExpressionCandidate(change1, change2) {
79
+ if (change1.removed && change2.added) {
80
+ const scopeText = change2.value;
81
+ return /{.*}/.test(scopeText);
82
+ }
83
+ return false;
84
+ }
85
+
86
+ // src/search-report.ts
87
+ import chalk from "chalk";
88
+ var SameStepTypeMatch = class {
89
+ constructor(diff) {
90
+ this.keyword = diff.step.keyword;
91
+ this.text = diff.merged;
92
+ this.distance = diff.distance;
93
+ }
94
+ toString() {
95
+ const keyword = chalk.green(this.keyword);
96
+ const text = chalk.whiteBright(this.text);
97
+ const distance2 = chalk.blue(`[${this.distance}]`);
98
+ return `${distance2} ${keyword} ${text}`;
99
+ }
100
+ };
101
+ var DifferentStepTypeMatch = class {
102
+ constructor(diff) {
103
+ this.keyword = diff.step.keyword;
104
+ this.text = diff.merged;
105
+ this.distance = diff.distance;
106
+ }
107
+ toString() {
108
+ const keywordColor = chalk.ansi256(208);
109
+ const keyword = keywordColor(this.keyword);
110
+ const text = chalk.whiteBright(this.text);
111
+ const distance2 = chalk.blue(`[${this.distance}]`);
112
+ return `${distance2} ${keyword} ${text}`;
113
+ }
114
+ };
115
+ var _sameMatchTypes, sameMatchTypes_get, _differentMatchTypes, differentMatchTypes_get;
116
+ var FuzzySearchReport = class {
117
+ constructor() {
118
+ __privateAdd(this, _sameMatchTypes);
119
+ __privateAdd(this, _differentMatchTypes);
120
+ this.matches = [];
121
+ this.children = [];
122
+ }
123
+ get length() {
124
+ return this.matches.length + this.children.length;
125
+ }
126
+ addHeading(headingText) {
127
+ this.headingText = headingText;
128
+ return this;
129
+ }
130
+ addMatch(match) {
131
+ this.matches.push(match);
132
+ return this;
133
+ }
134
+ addChild(child) {
135
+ this.children.push(child);
136
+ return this;
137
+ }
138
+ toString() {
139
+ const same = __privateGet(this, _sameMatchTypes, sameMatchTypes_get).map((it) => it.toString()).join("\n");
140
+ const sameMessage = same.length > 0 ? `Steps with matching step type:` : "";
141
+ const different = __privateGet(this, _differentMatchTypes, differentMatchTypes_get).map((it) => it.toString()).join("\n");
142
+ const differentMessage = different.length > 0 ? `Steps with different step type:` : "";
143
+ const messageArray = [];
144
+ appendSubMessage(messageArray, sameMessage);
145
+ appendSubMessage(messageArray, same, TAB);
146
+ appendSubMessage(messageArray, differentMessage);
147
+ appendSubMessage(messageArray, different, TAB);
148
+ const children = [];
149
+ this.children.forEach((child) => {
150
+ appendChild(children, child);
151
+ });
152
+ const formatChildren = children.map((it) => it.toString().replace(/^/gm, " ")).join("\n");
153
+ const message = messageArray.join(`
154
+ ${TAB}`);
155
+ const heading = chalk.bgBlackBright(this.headingText);
156
+ return `${heading}
157
+ ${TAB}${message}
158
+ ${formatChildren}`;
159
+ }
160
+ };
161
+ _sameMatchTypes = new WeakSet();
162
+ sameMatchTypes_get = function() {
163
+ return this.matches.filter(
164
+ (it) => it instanceof SameStepTypeMatch
165
+ );
166
+ };
167
+ _differentMatchTypes = new WeakSet();
168
+ differentMatchTypes_get = function() {
169
+ return this.matches.filter(
170
+ (it) => it instanceof DifferentStepTypeMatch
171
+ );
172
+ };
173
+ function appendSubMessage(arr, message, prefix) {
174
+ if (message && message.length > 0) {
175
+ const str = prefix ? `${prefix}${message}` : message;
176
+ arr.push(str);
177
+ }
178
+ }
179
+ function appendChild(arr, message) {
180
+ if (message) {
181
+ arr.push(message);
182
+ }
183
+ }
184
+ var SPACE = " ";
185
+ var TAB = SPACE.repeat(2);
186
+ function buildFuzzySearchReport({ same, other }) {
187
+ const report = new FuzzySearchReport();
188
+ same.forEach((diff) => {
189
+ report.addMatch(new SameStepTypeMatch(diff));
190
+ });
191
+ other.forEach((diff) => {
192
+ report.addMatch(new DifferentStepTypeMatch(diff));
193
+ });
194
+ return report;
195
+ }
196
+
197
+ // src/parameters.ts
198
+ import {
199
+ Argument,
200
+ ParameterType
201
+ } from "@cucumber/cucumber-expressions";
202
+ import {
203
+ def,
204
+ func,
205
+ instance,
206
+ overloads,
207
+ string,
208
+ nil,
209
+ array
210
+ } from "@autometa/overloaded";
211
+ Argument.prototype.getValue = function(thisObj) {
212
+ const groupValues = this.group ? this.group.value ? [this.group.value] : this.group.values : null;
213
+ return this.parameterType.transform(thisObj, groupValues);
214
+ };
215
+ var regexp = instance(RegExp).or(array([instance(RegExp)]));
216
+ function defineParameterType(registry, ...params) {
217
+ params.forEach((param) => {
218
+ const { name, regexpPattern, transform, type, primitive } = param;
219
+ return registerParameterType(
220
+ registry,
221
+ name,
222
+ regexpPattern,
223
+ transform,
224
+ type,
225
+ primitive
226
+ );
227
+ });
228
+ }
229
+ function registerParameterType(registry, name, regexpPattern, transform, type, primitive) {
230
+ return overloads(
231
+ def(
232
+ string(),
233
+ regexp,
234
+ func("type"),
235
+ func("transform"),
236
+ func("primitive")
237
+ ).matches((name2, regexp2, type2, transform2, primitive2) => {
238
+ const primitivePrototype = primitive2;
239
+ const typePrototype = type2;
240
+ const wrapper = (val) => {
241
+ const asPrimitive = primitivePrototype(val);
242
+ const asType = new typePrototype(asPrimitive);
243
+ return transform2(asType);
244
+ };
245
+ const param = new ParameterType(name2, regexp2, type2, wrapper);
246
+ registry.defineParameterType(param);
247
+ }),
248
+ def`allTransforms`(
249
+ string(),
250
+ instance(RegExp),
251
+ func("type"),
252
+ func("transform"),
253
+ nil("primitive")
254
+ ).matches((name2, regexp2, type2, transform2) => {
255
+ const typePrototype = type2;
256
+ const wrapper = (val) => {
257
+ const asType = new typePrototype(val);
258
+ return transform2(asType);
259
+ };
260
+ const param = new ParameterType(name2, regexp2, type2, wrapper);
261
+ registry.defineParameterType(param);
262
+ }),
263
+ def`transformPrimitive`(
264
+ string(),
265
+ instance(RegExp),
266
+ nil("type"),
267
+ func("transform"),
268
+ func("primitive")
269
+ ).matches((name2, regexp2, _, transform2, primitive2) => {
270
+ const primitivePrototype = primitive2;
271
+ const wrapper = (val) => {
272
+ const asPrimitive = primitivePrototype(val);
273
+ return transform2(asPrimitive);
274
+ };
275
+ const param = new ParameterType(name2, regexp2, primitive2, wrapper);
276
+ registry.defineParameterType(param);
277
+ }),
278
+ def`encapuslatePrimitive`(
279
+ string(),
280
+ instance(RegExp),
281
+ func("type"),
282
+ nil("transform"),
283
+ func("primitive")
284
+ ).matches((name2, regexp2, type2, _, primitive2) => {
285
+ const primitivePrototype = primitive2;
286
+ const typePrototype = type2;
287
+ const wrapper = (val) => {
288
+ const asPrimitive = primitivePrototype(val);
289
+ return new typePrototype(asPrimitive);
290
+ };
291
+ const param = new ParameterType(name2, regexp2, type2, wrapper);
292
+ registry.defineParameterType(param);
293
+ }),
294
+ def`makeType`(
295
+ string(),
296
+ instance(RegExp),
297
+ func("type"),
298
+ nil(),
299
+ nil()
300
+ ).matches((name2, pattern, type2) => {
301
+ const prototype = type2;
302
+ const transform2 = (val) => new prototype(val);
303
+ const param = new ParameterType(name2, pattern, null, transform2);
304
+ registry.defineParameterType(param);
305
+ }),
306
+ def`makePrimitive`(
307
+ string(),
308
+ instance(RegExp),
309
+ nil(),
310
+ nil(),
311
+ func("primitive")
312
+ ).matches((name2, pattern, _, __, primitive2) => {
313
+ const prototype = primitive2;
314
+ const transform2 = (val) => prototype(val);
315
+ const param = new ParameterType(name2, pattern, null, transform2);
316
+ registry.defineParameterType(param);
317
+ }),
318
+ def`transformValue`(
319
+ string(),
320
+ instance(RegExp),
321
+ nil(),
322
+ func("transform"),
323
+ nil()
324
+ ).matches((name2, pattern, _, transform2, __) => {
325
+ const param = new ParameterType(name2, pattern, null, transform2);
326
+ registry.defineParameterType(param);
327
+ }),
328
+ def`default`(string(), instance(RegExp), nil(), nil(), nil()).matches(
329
+ (name2, pattern, _, __, ___) => {
330
+ const transform2 = (val) => val;
331
+ const param = new ParameterType(name2, pattern, null, transform2);
332
+ registry.defineParameterType(param);
333
+ }
334
+ )
335
+ ).use([name, regexpPattern, type, transform, primitive]);
336
+ }
337
+
338
+ // src/default.parameters.ts
339
+ import { def as def2, fallback, overloads as overloads2, string as string2 } from "@autometa/overloaded";
340
+ import { Dates } from "@autometa/datetime";
341
+ import { AssertIs } from "@autometa/asserters";
342
+ import { DateTime } from "luxon";
343
+ var strNum = /['"]-?\d+['"]/;
344
+ var isodateRegexp = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;
345
+ var shortDateRegex = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\d)/;
346
+ var boolTypesActive = ["active", "inactive"];
347
+ var boolTypesEnabled = ["enabled", "disabled"];
348
+ var boolTypes = ["true", "false"];
349
+ var NumberParam = {
350
+ name: "number",
351
+ regexpPattern: /\d+/,
352
+ primitive: Number
353
+ };
354
+ var BooleanParam = {
355
+ name: "boolean",
356
+ regexpPattern: /true|false/,
357
+ primitive: Boolean
358
+ };
359
+ var PrimitiveParam = {
360
+ name: "primitive",
361
+ regexpPattern: [
362
+ /true|false/,
363
+ /enabled|disabled/,
364
+ /active|inactive/,
365
+ /null/,
366
+ /empty/,
367
+ /undefined/,
368
+ /NaN/,
369
+ /Infinity/,
370
+ /-Infinity/,
371
+ isodateRegexp,
372
+ shortDateRegex,
373
+ /-?\d+/,
374
+ /"([^"]*)"/,
375
+ /'([^']*)'/
376
+ ],
377
+ transform: (value) => {
378
+ return overloads2(
379
+ def2(string2({ equals: "null" })).matches((_) => null),
380
+ def2(string2({ equals: "undefined" })).matches((_) => void 0),
381
+ def2(string2({ in: boolTypes })).matches((val) => Boolean(val)),
382
+ def2(string2({ equals: "NaN" })).matches((_) => NaN),
383
+ def2(string2({ equals: "Infinity" })).matches((_) => Infinity),
384
+ def2(string2({ equals: "-Infinity" })).matches((_) => -Infinity),
385
+ def2(string2({ pattern: isodateRegexp })).matches(parseIso),
386
+ def2(string2({ pattern: shortDateRegex })).matches(parseDate),
387
+ def2(string2({ pattern: strNum })).matches(trimQuotes),
388
+ def2(string2({ pattern: /-?\d+/ })).matches((val) => Number(val)),
389
+ def2(string2({ in: boolTypesEnabled })).matches(boolEnabled),
390
+ def2(string2({ in: boolTypesActive })).matches(boolActive),
391
+ fallback((val) => {
392
+ AssertIs(val, "string");
393
+ const fromPhrase = Dates.fromPhrase(val);
394
+ if (fromPhrase && !isNaN(fromPhrase.getTime())) {
395
+ return fromPhrase;
396
+ }
397
+ return val;
398
+ })
399
+ ).use([value]);
400
+ }
401
+ };
402
+ function boolActive(val) {
403
+ return val === "active";
404
+ }
405
+ function boolEnabled(val) {
406
+ return val === "enabled";
407
+ }
408
+ function parseDate(val) {
409
+ return new Date(Date.parse(val));
410
+ }
411
+ function trimQuotes(val) {
412
+ return val.slice(1, -1);
413
+ }
414
+ function parseIso(val) {
415
+ return DateTime.fromISO(val).toJSDate();
416
+ }
417
+ export {
418
+ BooleanParam,
419
+ DifferentStepTypeMatch,
420
+ FuzzySearchReport,
421
+ NumberParam,
422
+ PrimitiveParam,
423
+ SameStepTypeMatch,
424
+ buildFuzzySearchReport,
425
+ checkMatch,
426
+ defineParameterType,
427
+ getDiff,
428
+ getDiffs,
429
+ isExpressionCandidate,
430
+ limitDiffs,
431
+ refineDiff
432
+ };
@@ -0,0 +1,83 @@
1
+ import { Change } from 'diff';
2
+ import { Expression, ParameterTypeRegistry } from '@cucumber/cucumber-expressions';
3
+ import { StepType, StepKeyword, Class } from '@autometa/types';
4
+
5
+ interface Matchable {
6
+ matches(text: string): boolean;
7
+ }
8
+ interface ExpressionWrapper {
9
+ expression: Expression;
10
+ }
11
+ interface GherkinKeyword {
12
+ type: StepType;
13
+ }
14
+ type StepDiff = {
15
+ merged: string;
16
+ step: {
17
+ keyword: StepKeyword;
18
+ expression: Expression;
19
+ type: StepType;
20
+ matches: (text: string) => boolean;
21
+ };
22
+ gherkin: string;
23
+ distance: number;
24
+ };
25
+ type StepDiffs = StepDiff[];
26
+ type LimitedStepDiffs = {
27
+ same: StepDiffs;
28
+ other: StepDiffs;
29
+ };
30
+ declare function checkMatch<T extends Matchable>(text: string, it: T): boolean;
31
+ declare function limitDiffs(sameStepType: StepDiff[], differentStepType: StepDiff[], max: number): LimitedStepDiffs;
32
+ declare function getDiffs<T extends Matchable & ExpressionWrapper>(text: string, maxResults: number, step: T[]): {
33
+ merged: string;
34
+ step: T;
35
+ gherkin: string;
36
+ distance: number;
37
+ }[];
38
+ declare function getDiff<T extends ExpressionWrapper>(text: string, it: T): Change[];
39
+ declare function refineDiff(diff: Change[]): string;
40
+ declare function isExpressionCandidate(change1: Change, change2: Change): boolean;
41
+
42
+ declare class SameStepTypeMatch {
43
+ readonly keyword: string;
44
+ readonly text: string;
45
+ readonly distance: number;
46
+ constructor(diff: StepDiff);
47
+ toString(): string;
48
+ }
49
+ declare class DifferentStepTypeMatch {
50
+ readonly keyword: string;
51
+ readonly text: string;
52
+ readonly distance: number;
53
+ constructor(diff: StepDiff);
54
+ toString(): string;
55
+ }
56
+ declare class FuzzySearchReport {
57
+ #private;
58
+ headingText: string;
59
+ matches: (SameStepTypeMatch | DifferentStepTypeMatch)[];
60
+ children: FuzzySearchReport[];
61
+ get length(): number;
62
+ addHeading(headingText: string): this;
63
+ addMatch(match: SameStepTypeMatch | DifferentStepTypeMatch): this;
64
+ addChild(child: FuzzySearchReport): this;
65
+ toString(): string;
66
+ }
67
+ declare function buildFuzzySearchReport({ same, other }: LimitedStepDiffs): FuzzySearchReport;
68
+
69
+ type ParamTypeDefinition = {
70
+ name: string;
71
+ regexpPattern: RegExp | RegExp[];
72
+ transform?: (value: unknown) => unknown;
73
+ type?: Class<unknown>;
74
+ primitive?: typeof String | typeof Number | typeof Boolean | typeof BigInt;
75
+ };
76
+ declare function defineParameterType<T extends ParamTypeDefinition[]>(registry: ParameterTypeRegistry, ...params: T): void;
77
+
78
+ type AutoParamTypeDefinition = Omit<ParamTypeDefinition, "transform">;
79
+ declare const NumberParam: AutoParamTypeDefinition;
80
+ declare const BooleanParam: AutoParamTypeDefinition;
81
+ declare const PrimitiveParam: ParamTypeDefinition;
82
+
83
+ export { BooleanParam, DifferentStepTypeMatch, ExpressionWrapper, FuzzySearchReport, GherkinKeyword, LimitedStepDiffs, Matchable, NumberParam, ParamTypeDefinition, PrimitiveParam, SameStepTypeMatch, StepDiff, StepDiffs, buildFuzzySearchReport, checkMatch, defineParameterType, getDiff, getDiffs, isExpressionCandidate, limitDiffs, refineDiff };
package/dist/index.js ADDED
@@ -0,0 +1,470 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var __accessCheck = (obj, member, msg) => {
30
+ if (!member.has(obj))
31
+ throw TypeError("Cannot " + msg);
32
+ };
33
+ var __privateGet = (obj, member, getter) => {
34
+ __accessCheck(obj, member, "read from private field");
35
+ return getter ? getter.call(obj) : member.get(obj);
36
+ };
37
+ var __privateAdd = (obj, member, value) => {
38
+ if (member.has(obj))
39
+ throw TypeError("Cannot add the same private member more than once");
40
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
41
+ };
42
+
43
+ // src/index.ts
44
+ var src_exports = {};
45
+ __export(src_exports, {
46
+ BooleanParam: () => BooleanParam,
47
+ DifferentStepTypeMatch: () => DifferentStepTypeMatch,
48
+ FuzzySearchReport: () => FuzzySearchReport,
49
+ NumberParam: () => NumberParam,
50
+ PrimitiveParam: () => PrimitiveParam,
51
+ SameStepTypeMatch: () => SameStepTypeMatch,
52
+ buildFuzzySearchReport: () => buildFuzzySearchReport,
53
+ checkMatch: () => checkMatch,
54
+ defineParameterType: () => defineParameterType,
55
+ getDiff: () => getDiff,
56
+ getDiffs: () => getDiffs,
57
+ isExpressionCandidate: () => isExpressionCandidate,
58
+ limitDiffs: () => limitDiffs,
59
+ refineDiff: () => refineDiff
60
+ });
61
+ module.exports = __toCommonJS(src_exports);
62
+
63
+ // src/step-matcher.ts
64
+ var import_diff = require("diff");
65
+ var import_closest_match = require("closest-match");
66
+ var import_asserters = require("@autometa/asserters");
67
+ function checkMatch(text, it) {
68
+ return it.matches(text);
69
+ }
70
+ function limitDiffs(sameStepType, differentStepType, max) {
71
+ const sameDistances = sameStepType.map((it) => it.distance);
72
+ const maxSameStepDistance = Math.max(...sameDistances);
73
+ const otherStepDistance = differentStepType.map((it) => it.distance);
74
+ const minDifferentStepDistance = Math.min(...otherStepDistance);
75
+ if (maxSameStepDistance > minDifferentStepDistance) {
76
+ const filter = sameStepType.filter(
77
+ (it) => it.distance <= minDifferentStepDistance
78
+ );
79
+ if (filter.length >= max) {
80
+ return { same: filter.slice(0, max), other: [] };
81
+ }
82
+ const diff = max - filter.length;
83
+ const sameSlice = sameStepType.slice(0, diff);
84
+ const differentSlice = differentStepType.slice(0, max - diff);
85
+ return { same: sameSlice, other: differentSlice };
86
+ }
87
+ const maxIndex = Math.min(max, sameStepType.length);
88
+ const result = { same: sameStepType.slice(0, maxIndex), other: [] };
89
+ return result;
90
+ }
91
+ function getDiffs(text, maxResults, step) {
92
+ const sorted = step.map((it) => {
93
+ if (checkMatch(text, it)) {
94
+ return { merged: text, step: it, gherkin: text, distance: 0 };
95
+ }
96
+ (0, import_asserters.AssertKey)(it, "expression");
97
+ (0, import_asserters.AssertKey)(it, "matches");
98
+ const diff = getDiff(text, it);
99
+ const refined = refineDiff(diff);
100
+ const dist = (0, import_closest_match.distance)(text, refined);
101
+ return { merged: refined, step: it, gherkin: text, distance: dist };
102
+ }).sort((a, b) => a.distance - b.distance);
103
+ const max = Math.min(maxResults, sorted.length);
104
+ return sorted.slice(0, max);
105
+ }
106
+ function getDiff(text, it) {
107
+ return (0, import_diff.diffWordsWithSpace)(text, it.expression.source);
108
+ }
109
+ function refineDiff(diff) {
110
+ const strings = [];
111
+ for (let index = 0; index < diff.length; index++) {
112
+ const gherkinChange = diff[index];
113
+ const scopeChange = diff[index + 1];
114
+ if (isExpressionCandidate(gherkinChange, scopeChange)) {
115
+ strings.push(gherkinChange.value);
116
+ index++;
117
+ continue;
118
+ }
119
+ if (gherkinChange.value) {
120
+ strings.push(gherkinChange.value);
121
+ continue;
122
+ }
123
+ }
124
+ return strings.join("");
125
+ }
126
+ function isExpressionCandidate(change1, change2) {
127
+ if (change1.removed && change2.added) {
128
+ const scopeText = change2.value;
129
+ return /{.*}/.test(scopeText);
130
+ }
131
+ return false;
132
+ }
133
+
134
+ // src/search-report.ts
135
+ var import_chalk = __toESM(require("chalk"), 1);
136
+ var SameStepTypeMatch = class {
137
+ constructor(diff) {
138
+ this.keyword = diff.step.keyword;
139
+ this.text = diff.merged;
140
+ this.distance = diff.distance;
141
+ }
142
+ toString() {
143
+ const keyword = import_chalk.default.green(this.keyword);
144
+ const text = import_chalk.default.whiteBright(this.text);
145
+ const distance2 = import_chalk.default.blue(`[${this.distance}]`);
146
+ return `${distance2} ${keyword} ${text}`;
147
+ }
148
+ };
149
+ var DifferentStepTypeMatch = class {
150
+ constructor(diff) {
151
+ this.keyword = diff.step.keyword;
152
+ this.text = diff.merged;
153
+ this.distance = diff.distance;
154
+ }
155
+ toString() {
156
+ const keywordColor = import_chalk.default.ansi256(208);
157
+ const keyword = keywordColor(this.keyword);
158
+ const text = import_chalk.default.whiteBright(this.text);
159
+ const distance2 = import_chalk.default.blue(`[${this.distance}]`);
160
+ return `${distance2} ${keyword} ${text}`;
161
+ }
162
+ };
163
+ var _sameMatchTypes, sameMatchTypes_get, _differentMatchTypes, differentMatchTypes_get;
164
+ var FuzzySearchReport = class {
165
+ constructor() {
166
+ __privateAdd(this, _sameMatchTypes);
167
+ __privateAdd(this, _differentMatchTypes);
168
+ this.matches = [];
169
+ this.children = [];
170
+ }
171
+ get length() {
172
+ return this.matches.length + this.children.length;
173
+ }
174
+ addHeading(headingText) {
175
+ this.headingText = headingText;
176
+ return this;
177
+ }
178
+ addMatch(match) {
179
+ this.matches.push(match);
180
+ return this;
181
+ }
182
+ addChild(child) {
183
+ this.children.push(child);
184
+ return this;
185
+ }
186
+ toString() {
187
+ const same = __privateGet(this, _sameMatchTypes, sameMatchTypes_get).map((it) => it.toString()).join("\n");
188
+ const sameMessage = same.length > 0 ? `Steps with matching step type:` : "";
189
+ const different = __privateGet(this, _differentMatchTypes, differentMatchTypes_get).map((it) => it.toString()).join("\n");
190
+ const differentMessage = different.length > 0 ? `Steps with different step type:` : "";
191
+ const messageArray = [];
192
+ appendSubMessage(messageArray, sameMessage);
193
+ appendSubMessage(messageArray, same, TAB);
194
+ appendSubMessage(messageArray, differentMessage);
195
+ appendSubMessage(messageArray, different, TAB);
196
+ const children = [];
197
+ this.children.forEach((child) => {
198
+ appendChild(children, child);
199
+ });
200
+ const formatChildren = children.map((it) => it.toString().replace(/^/gm, " ")).join("\n");
201
+ const message = messageArray.join(`
202
+ ${TAB}`);
203
+ const heading = import_chalk.default.bgBlackBright(this.headingText);
204
+ return `${heading}
205
+ ${TAB}${message}
206
+ ${formatChildren}`;
207
+ }
208
+ };
209
+ _sameMatchTypes = new WeakSet();
210
+ sameMatchTypes_get = function() {
211
+ return this.matches.filter(
212
+ (it) => it instanceof SameStepTypeMatch
213
+ );
214
+ };
215
+ _differentMatchTypes = new WeakSet();
216
+ differentMatchTypes_get = function() {
217
+ return this.matches.filter(
218
+ (it) => it instanceof DifferentStepTypeMatch
219
+ );
220
+ };
221
+ function appendSubMessage(arr, message, prefix) {
222
+ if (message && message.length > 0) {
223
+ const str = prefix ? `${prefix}${message}` : message;
224
+ arr.push(str);
225
+ }
226
+ }
227
+ function appendChild(arr, message) {
228
+ if (message) {
229
+ arr.push(message);
230
+ }
231
+ }
232
+ var SPACE = " ";
233
+ var TAB = SPACE.repeat(2);
234
+ function buildFuzzySearchReport({ same, other }) {
235
+ const report = new FuzzySearchReport();
236
+ same.forEach((diff) => {
237
+ report.addMatch(new SameStepTypeMatch(diff));
238
+ });
239
+ other.forEach((diff) => {
240
+ report.addMatch(new DifferentStepTypeMatch(diff));
241
+ });
242
+ return report;
243
+ }
244
+
245
+ // src/parameters.ts
246
+ var import_cucumber_expressions = require("@cucumber/cucumber-expressions");
247
+ var import_overloaded = require("@autometa/overloaded");
248
+ import_cucumber_expressions.Argument.prototype.getValue = function(thisObj) {
249
+ const groupValues = this.group ? this.group.value ? [this.group.value] : this.group.values : null;
250
+ return this.parameterType.transform(thisObj, groupValues);
251
+ };
252
+ var regexp = (0, import_overloaded.instance)(RegExp).or((0, import_overloaded.array)([(0, import_overloaded.instance)(RegExp)]));
253
+ function defineParameterType(registry, ...params) {
254
+ params.forEach((param) => {
255
+ const { name, regexpPattern, transform, type, primitive } = param;
256
+ return registerParameterType(
257
+ registry,
258
+ name,
259
+ regexpPattern,
260
+ transform,
261
+ type,
262
+ primitive
263
+ );
264
+ });
265
+ }
266
+ function registerParameterType(registry, name, regexpPattern, transform, type, primitive) {
267
+ return (0, import_overloaded.overloads)(
268
+ (0, import_overloaded.def)(
269
+ (0, import_overloaded.string)(),
270
+ regexp,
271
+ (0, import_overloaded.func)("type"),
272
+ (0, import_overloaded.func)("transform"),
273
+ (0, import_overloaded.func)("primitive")
274
+ ).matches((name2, regexp2, type2, transform2, primitive2) => {
275
+ const primitivePrototype = primitive2;
276
+ const typePrototype = type2;
277
+ const wrapper = (val) => {
278
+ const asPrimitive = primitivePrototype(val);
279
+ const asType = new typePrototype(asPrimitive);
280
+ return transform2(asType);
281
+ };
282
+ const param = new import_cucumber_expressions.ParameterType(name2, regexp2, type2, wrapper);
283
+ registry.defineParameterType(param);
284
+ }),
285
+ import_overloaded.def`allTransforms`(
286
+ (0, import_overloaded.string)(),
287
+ (0, import_overloaded.instance)(RegExp),
288
+ (0, import_overloaded.func)("type"),
289
+ (0, import_overloaded.func)("transform"),
290
+ (0, import_overloaded.nil)("primitive")
291
+ ).matches((name2, regexp2, type2, transform2) => {
292
+ const typePrototype = type2;
293
+ const wrapper = (val) => {
294
+ const asType = new typePrototype(val);
295
+ return transform2(asType);
296
+ };
297
+ const param = new import_cucumber_expressions.ParameterType(name2, regexp2, type2, wrapper);
298
+ registry.defineParameterType(param);
299
+ }),
300
+ import_overloaded.def`transformPrimitive`(
301
+ (0, import_overloaded.string)(),
302
+ (0, import_overloaded.instance)(RegExp),
303
+ (0, import_overloaded.nil)("type"),
304
+ (0, import_overloaded.func)("transform"),
305
+ (0, import_overloaded.func)("primitive")
306
+ ).matches((name2, regexp2, _, transform2, primitive2) => {
307
+ const primitivePrototype = primitive2;
308
+ const wrapper = (val) => {
309
+ const asPrimitive = primitivePrototype(val);
310
+ return transform2(asPrimitive);
311
+ };
312
+ const param = new import_cucumber_expressions.ParameterType(name2, regexp2, primitive2, wrapper);
313
+ registry.defineParameterType(param);
314
+ }),
315
+ import_overloaded.def`encapuslatePrimitive`(
316
+ (0, import_overloaded.string)(),
317
+ (0, import_overloaded.instance)(RegExp),
318
+ (0, import_overloaded.func)("type"),
319
+ (0, import_overloaded.nil)("transform"),
320
+ (0, import_overloaded.func)("primitive")
321
+ ).matches((name2, regexp2, type2, _, primitive2) => {
322
+ const primitivePrototype = primitive2;
323
+ const typePrototype = type2;
324
+ const wrapper = (val) => {
325
+ const asPrimitive = primitivePrototype(val);
326
+ return new typePrototype(asPrimitive);
327
+ };
328
+ const param = new import_cucumber_expressions.ParameterType(name2, regexp2, type2, wrapper);
329
+ registry.defineParameterType(param);
330
+ }),
331
+ import_overloaded.def`makeType`(
332
+ (0, import_overloaded.string)(),
333
+ (0, import_overloaded.instance)(RegExp),
334
+ (0, import_overloaded.func)("type"),
335
+ (0, import_overloaded.nil)(),
336
+ (0, import_overloaded.nil)()
337
+ ).matches((name2, pattern, type2) => {
338
+ const prototype = type2;
339
+ const transform2 = (val) => new prototype(val);
340
+ const param = new import_cucumber_expressions.ParameterType(name2, pattern, null, transform2);
341
+ registry.defineParameterType(param);
342
+ }),
343
+ import_overloaded.def`makePrimitive`(
344
+ (0, import_overloaded.string)(),
345
+ (0, import_overloaded.instance)(RegExp),
346
+ (0, import_overloaded.nil)(),
347
+ (0, import_overloaded.nil)(),
348
+ (0, import_overloaded.func)("primitive")
349
+ ).matches((name2, pattern, _, __, primitive2) => {
350
+ const prototype = primitive2;
351
+ const transform2 = (val) => prototype(val);
352
+ const param = new import_cucumber_expressions.ParameterType(name2, pattern, null, transform2);
353
+ registry.defineParameterType(param);
354
+ }),
355
+ import_overloaded.def`transformValue`(
356
+ (0, import_overloaded.string)(),
357
+ (0, import_overloaded.instance)(RegExp),
358
+ (0, import_overloaded.nil)(),
359
+ (0, import_overloaded.func)("transform"),
360
+ (0, import_overloaded.nil)()
361
+ ).matches((name2, pattern, _, transform2, __) => {
362
+ const param = new import_cucumber_expressions.ParameterType(name2, pattern, null, transform2);
363
+ registry.defineParameterType(param);
364
+ }),
365
+ import_overloaded.def`default`((0, import_overloaded.string)(), (0, import_overloaded.instance)(RegExp), (0, import_overloaded.nil)(), (0, import_overloaded.nil)(), (0, import_overloaded.nil)()).matches(
366
+ (name2, pattern, _, __, ___) => {
367
+ const transform2 = (val) => val;
368
+ const param = new import_cucumber_expressions.ParameterType(name2, pattern, null, transform2);
369
+ registry.defineParameterType(param);
370
+ }
371
+ )
372
+ ).use([name, regexpPattern, type, transform, primitive]);
373
+ }
374
+
375
+ // src/default.parameters.ts
376
+ var import_overloaded2 = require("@autometa/overloaded");
377
+ var import_datetime = require("@autometa/datetime");
378
+ var import_asserters2 = require("@autometa/asserters");
379
+ var import_luxon = require("luxon");
380
+ var strNum = /['"]-?\d+['"]/;
381
+ var isodateRegexp = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;
382
+ var shortDateRegex = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\d)/;
383
+ var boolTypesActive = ["active", "inactive"];
384
+ var boolTypesEnabled = ["enabled", "disabled"];
385
+ var boolTypes = ["true", "false"];
386
+ var NumberParam = {
387
+ name: "number",
388
+ regexpPattern: /\d+/,
389
+ primitive: Number
390
+ };
391
+ var BooleanParam = {
392
+ name: "boolean",
393
+ regexpPattern: /true|false/,
394
+ primitive: Boolean
395
+ };
396
+ var PrimitiveParam = {
397
+ name: "primitive",
398
+ regexpPattern: [
399
+ /true|false/,
400
+ /enabled|disabled/,
401
+ /active|inactive/,
402
+ /null/,
403
+ /empty/,
404
+ /undefined/,
405
+ /NaN/,
406
+ /Infinity/,
407
+ /-Infinity/,
408
+ isodateRegexp,
409
+ shortDateRegex,
410
+ /-?\d+/,
411
+ /"([^"]*)"/,
412
+ /'([^']*)'/
413
+ ],
414
+ transform: (value) => {
415
+ return (0, import_overloaded2.overloads)(
416
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ equals: "null" })).matches((_) => null),
417
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ equals: "undefined" })).matches((_) => void 0),
418
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ in: boolTypes })).matches((val) => Boolean(val)),
419
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ equals: "NaN" })).matches((_) => NaN),
420
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ equals: "Infinity" })).matches((_) => Infinity),
421
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ equals: "-Infinity" })).matches((_) => -Infinity),
422
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ pattern: isodateRegexp })).matches(parseIso),
423
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ pattern: shortDateRegex })).matches(parseDate),
424
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ pattern: strNum })).matches(trimQuotes),
425
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ pattern: /-?\d+/ })).matches((val) => Number(val)),
426
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ in: boolTypesEnabled })).matches(boolEnabled),
427
+ (0, import_overloaded2.def)((0, import_overloaded2.string)({ in: boolTypesActive })).matches(boolActive),
428
+ (0, import_overloaded2.fallback)((val) => {
429
+ (0, import_asserters2.AssertIs)(val, "string");
430
+ const fromPhrase = import_datetime.Dates.fromPhrase(val);
431
+ if (fromPhrase && !isNaN(fromPhrase.getTime())) {
432
+ return fromPhrase;
433
+ }
434
+ return val;
435
+ })
436
+ ).use([value]);
437
+ }
438
+ };
439
+ function boolActive(val) {
440
+ return val === "active";
441
+ }
442
+ function boolEnabled(val) {
443
+ return val === "enabled";
444
+ }
445
+ function parseDate(val) {
446
+ return new Date(Date.parse(val));
447
+ }
448
+ function trimQuotes(val) {
449
+ return val.slice(1, -1);
450
+ }
451
+ function parseIso(val) {
452
+ return import_luxon.DateTime.fromISO(val).toJSDate();
453
+ }
454
+ // Annotate the CommonJS export names for ESM import in node:
455
+ 0 && (module.exports = {
456
+ BooleanParam,
457
+ DifferentStepTypeMatch,
458
+ FuzzySearchReport,
459
+ NumberParam,
460
+ PrimitiveParam,
461
+ SameStepTypeMatch,
462
+ buildFuzzySearchReport,
463
+ checkMatch,
464
+ defineParameterType,
465
+ getDiff,
466
+ getDiffs,
467
+ isExpressionCandidate,
468
+ limitDiffs,
469
+ refineDiff
470
+ });
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@autometa/cucumber-expressions",
3
+ "version": "0.0.0",
4
+ "description": "wrapper on @cucumber/cucumber-expressions",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/esm/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ "import": "./dist/esm/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/esm/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ },
15
+ "license": "MIT",
16
+ "devDependencies": {
17
+ "@types/node": "^18.11.18",
18
+ "@types/strip-color": "^0.1.0",
19
+ "@types/uuid": "^9.0.1",
20
+ "@typescript-eslint/eslint-plugin": "^5.54.1",
21
+ "@typescript-eslint/parser": "^5.54.1",
22
+ "eslint": "^8.37.0",
23
+ "eslint-config-custom": "0.5.1",
24
+ "eslint-config-prettier": "^8.3.0",
25
+ "rimraf": "^4.1.2",
26
+ "strip-color": "^0.1.0",
27
+ "tsconfig": " *",
28
+ "tsup": "^6.7.0",
29
+ "typescript": "^4.9.5",
30
+ "vitest": "^0.29.8"
31
+ },
32
+ "dependencies": {
33
+ "@autometa/asserters": "^0.0.0",
34
+ "@autometa/datetime": "^0.0.0",
35
+ "@autometa/errors": "^0.0.0",
36
+ "@autometa/overloaded": "^0.2.9",
37
+ "@autometa/types": "^0.3.1",
38
+ "@cucumber/cucumber-expressions": "^16.1.2",
39
+ "@types/diff": "^5.0.3",
40
+ "@types/luxon": "^3.3.1",
41
+ "chalk": "^5.3.0",
42
+ "closest-match": "^1.3.3",
43
+ "diff": "^5.1.0",
44
+ "luxon": "^3.3.0"
45
+ },
46
+ "scripts": {
47
+ "test": "vitest run --passWithNoTests",
48
+ "prettify": "prettier --config .prettierrc 'src/**/*.ts' --write",
49
+ "lint": "eslint . --max-warnings 0",
50
+ "lint:fix": "eslint . --fix",
51
+ "clean": "rimraf dist",
52
+ "build": "tsup",
53
+ "build:watch": "tsup --watch"
54
+ },
55
+ "readme": "# Introduction\n\nThere's nothing here yet"
56
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ clean: true, // clean up the dist folder
5
+ format: ["cjs", "esm"], // generate cjs and esm files
6
+ dts: true,
7
+ sourcemap: false, // generate sourcemaps
8
+ skipNodeModulesBundle: true,
9
+ entryPoints: ["src/index.ts"],
10
+ target: "es2020",
11
+ outDir: "dist",
12
+ legacyOutput: true,
13
+ external: ["dist"],
14
+ });