harmonyc 0.6.0-6 → 0.6.0-8

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/compile.js CHANGED
@@ -4,11 +4,11 @@ import { parse } from './syntax.js';
4
4
  import { stepsFileName, testFileName } from './filenames/filenames.js';
5
5
  export function compileFeature(fileName, src) {
6
6
  const feature = parse({ fileName, src });
7
- const outFn = testFileName(fileName);
8
- const outFile = new OutFile(outFn);
7
+ const testFn = testFileName(fileName);
8
+ const testFile = new OutFile(testFn);
9
9
  const stepsFn = stepsFileName(fileName);
10
10
  const stepsFile = new OutFile(stepsFn);
11
- const cg = new NodeTest(outFile, stepsFile);
11
+ const cg = new NodeTest(testFile, stepsFile);
12
12
  feature.toCode(cg);
13
- return { outFile, stepsFile };
13
+ return { outFile: testFile, stepsFile };
14
14
  }
package/compiler.js CHANGED
@@ -5,16 +5,22 @@ export async function compileFiles(pattern) {
5
5
  const fns = await glob(pattern);
6
6
  if (!fns.length)
7
7
  throw new Error(`No files found for pattern: ${String(pattern)}`);
8
- const outFns = await Promise.all(fns.map((fn) => compileFile(fn)));
8
+ const features = await Promise.all(fns.map((fn) => compileFile(fn)));
9
9
  console.log(`Compiled ${fns.length} file${fns.length === 1 ? '' : 's'}.`);
10
- return { fns, outFns };
10
+ const generated = features.filter((f) => f.stepsFileAction === 'generated');
11
+ if (generated.length) {
12
+ console.log(`Generated ${generated.length} steps file${generated.length === 1 ? '' : 's'}.`);
13
+ }
14
+ return { fns, outFns: features.map((f) => f.outFile.name) };
11
15
  }
12
16
  export async function compileFile(fn) {
13
17
  const src = readFileSync(fn, 'utf8').toString();
14
18
  const { outFile, stepsFile } = compileFeature(fn, src);
15
19
  writeFileSync(outFile.name, outFile.value);
20
+ let stepsFileAction = 'ignored';
16
21
  if (!existsSync(stepsFile.name)) {
22
+ stepsFileAction = 'generated';
17
23
  writeFileSync(stepsFile.name, stepsFile.value);
18
24
  }
19
- return outFile.name;
25
+ return { stepsFileAction, outFile, stepsFile };
20
26
  }
@@ -1,5 +1,5 @@
1
1
  import glob from 'fast-glob';
2
- const { globSync } = glob;
2
+ const { globSync, convertPathToPattern } = glob;
3
3
  export function base(fn) {
4
4
  return fn.replace(/\.harmony\.md$/i, '');
5
5
  }
@@ -7,9 +7,11 @@ export function testFileName(fn) {
7
7
  return base(fn) + '.test.mjs';
8
8
  }
9
9
  export function stepsFileName(fn) {
10
- const existing = globSync(base(fn) + '.steps.*');
10
+ const baseFn = base(fn);
11
+ const pattern = convertPathToPattern(baseFn);
12
+ const existing = globSync(`${pattern}.steps.*`);
11
13
  if (existing.length) {
12
14
  return existing.sort()[0];
13
15
  }
14
- return base(fn) + '.steps.ts';
16
+ return `${baseFn}.steps.ts`;
15
17
  }
package/js_api/js_api.js CHANGED
@@ -11,14 +11,14 @@ class Definition {
11
11
  this.fn = fn;
12
12
  }
13
13
  }
14
- const features = new Map();
14
+ const featureDefs = new Map();
15
15
  class FeatureContext {
16
16
  constructor() {
17
17
  _FeatureContext_actions.set(this, []);
18
18
  _FeatureContext_responses.set(this, []);
19
19
  _FeatureContext_parameterTypeRegistry.set(this, new ParameterTypeRegistry());
20
20
  this.Action = ((s, fn) => {
21
- if (fn) {
21
+ if (typeof fn === 'function') {
22
22
  const expr = new CucumberExpression(s, __classPrivateFieldGet(this, _FeatureContext_parameterTypeRegistry, "f"));
23
23
  const def = new Definition(expr, fn);
24
24
  __classPrivateFieldGet(this, _FeatureContext_actions, "f").push(def);
@@ -37,10 +37,11 @@ class FeatureContext {
37
37
  }
38
38
  const match = matches[matching[0]];
39
39
  const def = __classPrivateFieldGet(this, _FeatureContext_actions, "f")[matching[0]];
40
- return Promise.resolve(def.fn(...match.map((m) => m.getValue(undefined))));
40
+ const docstring = fn;
41
+ return Promise.resolve(def.fn(...match.map((m) => m.getValue(undefined)), docstring));
41
42
  });
42
43
  this.Response = ((s, fn) => {
43
- if (fn) {
44
+ if (typeof fn === 'function') {
44
45
  const expr = new CucumberExpression(s, __classPrivateFieldGet(this, _FeatureContext_parameterTypeRegistry, "f"));
45
46
  const def = new Definition(expr, fn);
46
47
  __classPrivateFieldGet(this, _FeatureContext_responses, "f").push(def);
@@ -59,23 +60,24 @@ class FeatureContext {
59
60
  }
60
61
  const match = matches[matching[0]];
61
62
  const def = __classPrivateFieldGet(this, _FeatureContext_responses, "f")[matching[0]];
62
- return Promise.resolve(def.fn(...match.map((m) => m.getValue(undefined))));
63
+ const docstring = fn;
64
+ return Promise.resolve(def.fn(...match.map((m) => m.getValue(undefined)), docstring));
63
65
  });
64
66
  }
65
67
  }
66
68
  _FeatureContext_actions = new WeakMap(), _FeatureContext_responses = new WeakMap(), _FeatureContext_parameterTypeRegistry = new WeakMap();
67
69
  export function Feature(s, fn) {
68
- let ctx;
69
70
  if (!fn) {
70
- ctx = features.get(s);
71
- if (!ctx)
71
+ // instantiate the feature
72
+ const fn = featureDefs.get(s);
73
+ if (!fn)
72
74
  throw new Error(`Feature not found: ${s}`);
75
+ const ctx = new FeatureContext();
76
+ fn(ctx);
77
+ return ctx;
73
78
  }
74
79
  else {
75
- // redefine the feature
76
- ctx = new FeatureContext();
77
- features.set(s, ctx);
80
+ // (re)define the feature
81
+ featureDefs.set(s, fn);
78
82
  }
79
- fn === null || fn === void 0 ? void 0 : fn(ctx);
80
- return ctx;
81
83
  }
@@ -1,7 +1,7 @@
1
1
  import { basename } from 'path';
2
2
  export class NodeTest {
3
- constructor(of, sf) {
4
- this.of = of;
3
+ constructor(tf, sf) {
4
+ this.tf = tf;
5
5
  this.sf = sf;
6
6
  this.framework = 'vitest';
7
7
  this.phrases = [];
@@ -10,21 +10,21 @@ export class NodeTest {
10
10
  const stepsModule = './' + basename(this.sf.name.replace(/.(js|ts)$/, ''));
11
11
  this.phrases = [];
12
12
  if (this.framework === 'vitest') {
13
- this.of.print(`import { test, expect } from 'vitest';`);
14
- this.of.print(`import { Feature } from 'harmonyc/test';`);
15
- this.of.print(`import ${JSON.stringify(stepsModule)};`);
13
+ this.tf.print(`import { test, expect } from 'vitest';`);
14
+ this.tf.print(`import { Feature } from 'harmonyc/test';`);
15
+ this.tf.print(`import ${str(stepsModule)};`);
16
16
  }
17
17
  for (const test of feature.tests) {
18
18
  test.toCode(this);
19
19
  }
20
20
  this.sf.print(`import { Feature } from 'harmonyc/test';`);
21
21
  this.sf.print('');
22
- this.sf.print(`Feature(${JSON.stringify(feature.name)}, ({ Action, Response }) => {`);
22
+ this.sf.print(`Feature(${str(feature.name)}, ({ Action, Response }) => {`);
23
23
  this.sf.indent(() => {
24
24
  for (const phrase of this.phrases) {
25
- this.sf.print(`${capitalize(phrase.kind)}(${JSON.stringify(phrase.text)}, () => {`);
25
+ this.sf.print(`${capitalize(phrase.kind)}(${str(phrase.text)}, () => {`);
26
26
  this.sf.indent(() => {
27
- this.sf.print(`throw new Error(${JSON.stringify(`Pending: ${phrase.text}`)})`);
27
+ this.sf.print(`throw new Error(${str(`Pending: ${phrase.text}`)})`);
28
28
  });
29
29
  this.sf.print('})');
30
30
  }
@@ -32,22 +32,56 @@ export class NodeTest {
32
32
  this.sf.print('})');
33
33
  }
34
34
  test(t) {
35
- this.of.print(`test('${t.name}', async (context) => {`);
36
- this.of.indent(() => {
35
+ this.featureVars = new Map();
36
+ this.tf.print(`test('${t.name}', async (context) => {`);
37
+ this.tf.indent(() => {
37
38
  for (const step of t.steps) {
38
39
  step.toCode(this);
39
40
  }
40
41
  });
41
- this.of.print('})');
42
- this.of.print('');
42
+ this.tf.print('})');
43
+ this.tf.print('');
43
44
  }
44
45
  phrase(p) {
45
46
  if (!this.phrases.some((x) => x.text === p.text))
46
47
  this.phrases.push(p);
47
48
  const feature = p.feature.name;
48
- this.of.print(`await Feature(${JSON.stringify(feature)}).${capitalize(p.kind)}(${JSON.stringify(p.text)})`);
49
+ let f = this.featureVars.get(feature);
50
+ if (!f) {
51
+ f = toId(feature, this.featureVars);
52
+ this.tf.print(`const ${f} = Feature(${str(feature)})`);
53
+ }
54
+ const docstring = p.docstring ? ', \n' + templateStr(p.docstring) : '';
55
+ this.tf.print(`await ${f}.${capitalize(p.kind)}(${str(p.text)}${docstring})`);
49
56
  }
50
57
  }
58
+ function str(s) {
59
+ let r = JSON.stringify(s);
60
+ return r;
61
+ }
62
+ function templateStr(s) {
63
+ return '`' + s.replace(/([`$])/g, '\\$1') + '`';
64
+ }
51
65
  function capitalize(s) {
52
66
  return s.charAt(0).toUpperCase() + s.slice(1);
53
67
  }
68
+ function toId(s, previous) {
69
+ if (previous.has(s))
70
+ return previous.get(s);
71
+ let base = pascalCase(s);
72
+ let id = base;
73
+ if ([...previous.values()].includes(id)) {
74
+ let i = 1;
75
+ while ([...previous.values()].includes(id + i))
76
+ i++;
77
+ id = base + i;
78
+ }
79
+ previous.set(s, id);
80
+ return id;
81
+ }
82
+ function pascalCase(s) {
83
+ return s
84
+ .split(/[^a-zA-Z0-9]/)
85
+ .map(capitalize)
86
+ .join('');
87
+ }
package/outFile.js CHANGED
@@ -40,7 +40,10 @@ export class OutFile {
40
40
  return this;
41
41
  }
42
42
  get value() {
43
- return (this.lines.join('\n') +
44
- `\n\n//# sourceMappingURL=data:application/json,${encodeURIComponent(this.sm.toString())}`);
43
+ let res = this.lines.join('\n');
44
+ if (this.currentLoc) {
45
+ res += `\n\n//# sourceMappingURL=data:application/json,${encodeURIComponent(this.sm.toString())}`;
46
+ }
47
+ return res;
45
48
  }
46
49
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "harmonyc",
3
3
  "description": "Harmony Code - model-driven BDD for Vitest",
4
- "version": "0.6.0-6",
4
+ "version": "0.6.0-8",
5
5
  "author": "Bernát Kalló",
6
6
  "type": "module",
7
7
  "bin": {
package/run.js CHANGED
@@ -1,4 +1,4 @@
1
- import { exec } from 'child_process';
1
+ import { exec, spawn } from 'child_process';
2
2
  function runCommand(patterns) {
3
3
  return `npx vitest run ${args(patterns)}`;
4
4
  }
@@ -9,18 +9,13 @@ function args(patterns) {
9
9
  return patterns.map((s) => JSON.stringify(s)).join(' ');
10
10
  }
11
11
  export function run(patterns) {
12
- var _a, _b, _c;
12
+ var _a, _b;
13
13
  const cmd = runCommand(patterns);
14
14
  const p = exec(cmd, { cwd: process.cwd() });
15
- (_a = p.stdin) === null || _a === void 0 ? void 0 : _a.pipe(process.stdin);
16
- (_b = p.stdout) === null || _b === void 0 ? void 0 : _b.pipe(process.stdout);
17
- (_c = p.stderr) === null || _c === void 0 ? void 0 : _c.pipe(process.stderr);
15
+ (_a = p.stdout) === null || _a === void 0 ? void 0 : _a.pipe(process.stdout);
16
+ (_b = p.stderr) === null || _b === void 0 ? void 0 : _b.pipe(process.stderr);
18
17
  }
19
18
  export function runWatch(patterns) {
20
- var _a, _b, _c;
21
19
  const cmd = runWatchCommand(patterns);
22
- const p = exec(cmd, { cwd: process.cwd() });
23
- (_a = p.stdin) === null || _a === void 0 ? void 0 : _a.pipe(process.stdin);
24
- (_b = p.stdout) === null || _b === void 0 ? void 0 : _b.pipe(process.stdout);
25
- (_c = p.stderr) === null || _c === void 0 ? void 0 : _c.pipe(process.stderr);
20
+ spawn(cmd, { cwd: process.cwd(), stdio: 'inherit', shell: true });
26
21
  }
package/syntax.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import remarkParse from 'remark-parse';
2
2
  import { unified } from 'unified';
3
3
  import { Feature, Section, Step } from './model.js';
4
- import { definitions } from './definitions.js';
5
4
  export function parse({ fileName, src, }) {
6
5
  const tree = unified().use(remarkParse).parse(src);
7
6
  const rootNodes = tree.children;
@@ -36,23 +35,12 @@ export function parse({ fileName, src, }) {
36
35
  return [];
37
36
  if (node.type == 'list')
38
37
  return list(node);
39
- if (node.type === 'code')
40
- return code(node);
41
38
  return [];
42
39
  }
43
40
  function list(node) {
44
41
  const isFork = node.ordered === false;
45
42
  return node.children.map((item) => listItem(item, isFork));
46
43
  }
47
- function code(node) {
48
- var _a;
49
- if (!((_a = node.meta) === null || _a === void 0 ? void 0 : _a.match(/harmony/)))
50
- return [];
51
- const code = node.value;
52
- const marker = '///';
53
- definitions({ marker, code, feature });
54
- return [];
55
- }
56
44
  function listItem(node, isFork) {
57
45
  const first = node.children[0];
58
46
  const text = textContent(first);
package/definitions.js DELETED
@@ -1,22 +0,0 @@
1
- import { CucumberExpression, ParameterTypeRegistry, } from '@cucumber/cucumber-expressions';
2
- const registry = new ParameterTypeRegistry();
3
- export function definitions({ marker, code, feature, }) {
4
- var _a, _b;
5
- const re = new RegExp(`^\s*${q(marker)}(.*?)$`, 'gm');
6
- let match = re.exec(code);
7
- const start = (_a = match === null || match === void 0 ? void 0 : match.index) !== null && _a !== void 0 ? _a : code.length;
8
- feature.prelude += code.slice(0, start);
9
- while (match) {
10
- const bodyStart = match.index + match[0].length;
11
- const head = match[1].trim();
12
- match = re.exec(code);
13
- const end = (_b = match === null || match === void 0 ? void 0 : match.index) !== null && _b !== void 0 ? _b : code.length;
14
- const body = code.slice(bodyStart, end).trim();
15
- if (body) {
16
- feature.definitions.set(new CucumberExpression(head, registry), body);
17
- }
18
- }
19
- }
20
- function q(pattern) {
21
- return pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
22
- }