@driftsys/markspec 0.12.0 → 0.13.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.
Files changed (317) hide show
  1. package/esm/ast/build.d.ts.map +1 -1
  2. package/esm/ast/build.js +19 -5
  3. package/esm/ast/equivalence.js +14 -2
  4. package/esm/ast/nodes.d.ts +15 -3
  5. package/esm/ast/nodes.d.ts.map +1 -1
  6. package/esm/compiler/inverses.d.ts.map +1 -1
  7. package/esm/compiler/inverses.js +18 -3
  8. package/esm/compiler/mod.d.ts +34 -1
  9. package/esm/compiler/mod.d.ts.map +1 -1
  10. package/esm/compiler/mod.js +112 -48
  11. package/esm/compiler/schema.d.ts +3 -2
  12. package/esm/compiler/schema.d.ts.map +1 -1
  13. package/esm/compiler/schema.js +6 -3
  14. package/esm/config/markspec.d.ts +2 -2
  15. package/esm/config/mod.d.ts.map +1 -1
  16. package/esm/config/mod.js +47 -5
  17. package/esm/criteria/scenarios.d.ts +55 -0
  18. package/esm/criteria/scenarios.d.ts.map +1 -0
  19. package/esm/criteria/scenarios.js +133 -0
  20. package/esm/discovery/mod.d.ts +20 -1
  21. package/esm/discovery/mod.d.ts.map +1 -1
  22. package/esm/discovery/mod.js +105 -17
  23. package/esm/discovery/projects.d.ts +6 -4
  24. package/esm/discovery/projects.d.ts.map +1 -1
  25. package/esm/discovery/projects.js +27 -17
  26. package/esm/formatter/entry_body.d.ts +13 -12
  27. package/esm/formatter/entry_body.d.ts.map +1 -1
  28. package/esm/formatter/entry_body.js +25 -17
  29. package/esm/formatter/feature_file.d.ts +40 -0
  30. package/esm/formatter/feature_file.d.ts.map +1 -0
  31. package/esm/formatter/feature_file.js +88 -0
  32. package/esm/formatter/md_equiv.d.ts +6 -5
  33. package/esm/formatter/md_equiv.d.ts.map +1 -1
  34. package/esm/formatter/md_equiv.js +6 -5
  35. package/esm/formatter/mod.d.ts +25 -3
  36. package/esm/formatter/mod.d.ts.map +1 -1
  37. package/esm/formatter/mod.js +267 -27
  38. package/esm/formatter/tab_markers.d.ts +15 -0
  39. package/esm/formatter/tab_markers.d.ts.map +1 -0
  40. package/esm/formatter/tab_markers.js +133 -0
  41. package/esm/gates/mod.d.ts +2 -2
  42. package/esm/gates/mod.d.ts.map +1 -1
  43. package/esm/gates/mod.js +18 -3
  44. package/esm/gherkin/load.d.ts +35 -0
  45. package/esm/gherkin/load.d.ts.map +1 -0
  46. package/esm/gherkin/load.js +39 -0
  47. package/esm/gherkin/parse.d.ts +11 -0
  48. package/esm/gherkin/parse.d.ts.map +1 -0
  49. package/esm/gherkin/parse.js +311 -0
  50. package/esm/gherkin/position.d.ts +23 -0
  51. package/esm/gherkin/position.d.ts.map +1 -0
  52. package/esm/gherkin/position.js +21 -0
  53. package/esm/gherkin/procedure.d.ts +76 -0
  54. package/esm/gherkin/procedure.d.ts.map +1 -0
  55. package/esm/gherkin/procedure.js +164 -0
  56. package/esm/gherkin/project.d.ts +35 -0
  57. package/esm/gherkin/project.d.ts.map +1 -0
  58. package/esm/gherkin/project.js +64 -0
  59. package/esm/gherkin/role.d.ts +23 -0
  60. package/esm/gherkin/role.d.ts.map +1 -0
  61. package/esm/gherkin/role.js +28 -0
  62. package/esm/gherkin/types.d.ts +118 -0
  63. package/esm/gherkin/types.d.ts.map +1 -0
  64. package/esm/gherkin/types.js +12 -0
  65. package/esm/lock/acquire_compile.d.ts +12 -4
  66. package/esm/lock/acquire_compile.d.ts.map +1 -1
  67. package/esm/lock/acquire_compile.js +15 -5
  68. package/esm/lock/canonical_edges.d.ts +6 -2
  69. package/esm/lock/canonical_edges.d.ts.map +1 -1
  70. package/esm/lock/canonical_edges.js +10 -3
  71. package/esm/lock/model.d.ts +5 -0
  72. package/esm/lock/model.d.ts.map +1 -1
  73. package/esm/lock/parser.d.ts.map +1 -1
  74. package/esm/lock/parser.js +2 -0
  75. package/esm/lock/resolve.d.ts +6 -2
  76. package/esm/lock/resolve.d.ts.map +1 -1
  77. package/esm/lock/resolve.js +36 -4
  78. package/esm/lock/serializer.d.ts.map +1 -1
  79. package/esm/lock/serializer.js +7 -1
  80. package/esm/lock/upstream_deps.js +1 -1
  81. package/esm/mod.d.ts +24 -6
  82. package/esm/mod.d.ts.map +1 -1
  83. package/esm/mod.js +19 -5
  84. package/esm/model/mod.d.ts +43 -2
  85. package/esm/model/mod.d.ts.map +1 -1
  86. package/esm/model/mod.js +1 -1
  87. package/esm/model/relations.d.ts +9 -0
  88. package/esm/model/relations.d.ts.map +1 -1
  89. package/esm/model/relations.js +16 -1
  90. package/esm/parser/attributes.d.ts +82 -3
  91. package/esm/parser/attributes.d.ts.map +1 -1
  92. package/esm/parser/attributes.js +199 -32
  93. package/esm/parser/body_tokens.d.ts +1 -1
  94. package/esm/parser/body_tokens.d.ts.map +1 -1
  95. package/esm/parser/body_tokens.js +49 -35
  96. package/esm/parser/feature_file.d.ts +116 -0
  97. package/esm/parser/feature_file.d.ts.map +1 -0
  98. package/esm/parser/feature_file.js +540 -0
  99. package/esm/parser/markdown.d.ts +7 -0
  100. package/esm/parser/markdown.d.ts.map +1 -1
  101. package/esm/parser/markdown.js +129 -40
  102. package/esm/parser/mod.d.ts +18 -0
  103. package/esm/parser/mod.d.ts.map +1 -1
  104. package/esm/parser/mod.js +51 -0
  105. package/esm/parser/translate.d.ts.map +1 -1
  106. package/esm/parser/translate.js +5 -0
  107. package/esm/refs/mod.d.ts +8 -3
  108. package/esm/refs/mod.d.ts.map +1 -1
  109. package/esm/refs/mod.js +20 -7
  110. package/esm/refs/split.d.ts +58 -0
  111. package/esm/refs/split.d.ts.map +1 -0
  112. package/esm/refs/split.js +137 -0
  113. package/esm/reporter/mod.d.ts +7 -0
  114. package/esm/reporter/mod.d.ts.map +1 -1
  115. package/esm/reporter/mod.js +113 -3
  116. package/esm/sync/mapping.d.ts +4 -3
  117. package/esm/sync/mapping.d.ts.map +1 -1
  118. package/esm/sync/mapping.js +52 -9
  119. package/esm/util/display_id.d.ts +20 -0
  120. package/esm/util/display_id.d.ts.map +1 -0
  121. package/esm/util/display_id.js +19 -0
  122. package/esm/validator/attributes.d.ts.map +1 -1
  123. package/esm/validator/attributes.js +4 -1
  124. package/esm/validator/block_role.d.ts +40 -0
  125. package/esm/validator/block_role.d.ts.map +1 -0
  126. package/esm/validator/block_role.js +109 -0
  127. package/esm/validator/feature_ac.d.ts.map +1 -1
  128. package/esm/validator/feature_ac.js +6 -1
  129. package/esm/validator/feature_parse.d.ts +35 -0
  130. package/esm/validator/feature_parse.d.ts.map +1 -0
  131. package/esm/validator/feature_parse.js +69 -0
  132. package/esm/validator/locators.d.ts +36 -0
  133. package/esm/validator/locators.d.ts.map +1 -0
  134. package/esm/validator/locators.js +184 -0
  135. package/esm/validator/mod.d.ts +10 -1
  136. package/esm/validator/mod.d.ts.map +1 -1
  137. package/esm/validator/mod.js +38 -14
  138. package/esm/validator/normalize.d.ts.map +1 -1
  139. package/esm/validator/normalize.js +8 -3
  140. package/esm/validator/per_type_attrs.d.ts +3 -3
  141. package/esm/validator/per_type_attrs.js +3 -3
  142. package/esm/validator/pipeline.d.ts +10 -16
  143. package/esm/validator/pipeline.d.ts.map +1 -1
  144. package/esm/validator/pipeline.js +37 -16
  145. package/esm/validator/procedure.d.ts +22 -0
  146. package/esm/validator/procedure.d.ts.map +1 -0
  147. package/esm/validator/procedure.js +58 -0
  148. package/esm/validator/trace_types.d.ts.map +1 -1
  149. package/esm/validator/trace_types.js +4 -3
  150. package/esm/validator/traceability.d.ts.map +1 -1
  151. package/esm/validator/traceability.js +4 -1
  152. package/esm/validator/type_resolution.d.ts +5 -3
  153. package/esm/validator/type_resolution.d.ts.map +1 -1
  154. package/esm/validator/type_resolution.js +14 -2
  155. package/esm/validator/types.d.ts +11 -0
  156. package/esm/validator/types.d.ts.map +1 -1
  157. package/esm/validator/types.js +26 -4
  158. package/esm/validator/uxil_family.d.ts +1 -1
  159. package/package.json +3 -1
  160. package/script/ast/build.d.ts.map +1 -1
  161. package/script/ast/build.js +19 -5
  162. package/script/ast/equivalence.js +14 -2
  163. package/script/ast/nodes.d.ts +15 -3
  164. package/script/ast/nodes.d.ts.map +1 -1
  165. package/script/compiler/inverses.d.ts.map +1 -1
  166. package/script/compiler/inverses.js +18 -3
  167. package/script/compiler/mod.d.ts +34 -1
  168. package/script/compiler/mod.d.ts.map +1 -1
  169. package/script/compiler/mod.js +110 -46
  170. package/script/compiler/schema.d.ts +3 -2
  171. package/script/compiler/schema.d.ts.map +1 -1
  172. package/script/compiler/schema.js +6 -3
  173. package/script/config/markspec.d.ts +2 -2
  174. package/script/config/mod.d.ts.map +1 -1
  175. package/script/config/mod.js +47 -5
  176. package/script/criteria/scenarios.d.ts +55 -0
  177. package/script/criteria/scenarios.d.ts.map +1 -0
  178. package/script/criteria/scenarios.js +139 -0
  179. package/script/discovery/mod.d.ts +20 -1
  180. package/script/discovery/mod.d.ts.map +1 -1
  181. package/script/discovery/mod.js +106 -17
  182. package/script/discovery/projects.d.ts +6 -4
  183. package/script/discovery/projects.d.ts.map +1 -1
  184. package/script/discovery/projects.js +27 -17
  185. package/script/formatter/entry_body.d.ts +13 -12
  186. package/script/formatter/entry_body.d.ts.map +1 -1
  187. package/script/formatter/entry_body.js +25 -17
  188. package/script/formatter/feature_file.d.ts +40 -0
  189. package/script/formatter/feature_file.d.ts.map +1 -0
  190. package/script/formatter/feature_file.js +92 -0
  191. package/script/formatter/md_equiv.d.ts +6 -5
  192. package/script/formatter/md_equiv.d.ts.map +1 -1
  193. package/script/formatter/md_equiv.js +6 -5
  194. package/script/formatter/mod.d.ts +25 -3
  195. package/script/formatter/mod.d.ts.map +1 -1
  196. package/script/formatter/mod.js +266 -26
  197. package/script/formatter/tab_markers.d.ts +15 -0
  198. package/script/formatter/tab_markers.d.ts.map +1 -0
  199. package/script/formatter/tab_markers.js +141 -0
  200. package/script/gates/mod.d.ts +2 -2
  201. package/script/gates/mod.d.ts.map +1 -1
  202. package/script/gates/mod.js +18 -3
  203. package/script/gherkin/load.d.ts +35 -0
  204. package/script/gherkin/load.d.ts.map +1 -0
  205. package/script/gherkin/load.js +76 -0
  206. package/script/gherkin/parse.d.ts +11 -0
  207. package/script/gherkin/parse.d.ts.map +1 -0
  208. package/script/gherkin/parse.js +314 -0
  209. package/script/gherkin/position.d.ts +23 -0
  210. package/script/gherkin/position.d.ts.map +1 -0
  211. package/script/gherkin/position.js +24 -0
  212. package/script/gherkin/procedure.d.ts +76 -0
  213. package/script/gherkin/procedure.d.ts.map +1 -0
  214. package/script/gherkin/procedure.js +169 -0
  215. package/script/gherkin/project.d.ts +35 -0
  216. package/script/gherkin/project.d.ts.map +1 -0
  217. package/script/gherkin/project.js +67 -0
  218. package/script/gherkin/role.d.ts +23 -0
  219. package/script/gherkin/role.d.ts.map +1 -0
  220. package/script/gherkin/role.js +31 -0
  221. package/script/gherkin/types.d.ts +118 -0
  222. package/script/gherkin/types.d.ts.map +1 -0
  223. package/script/gherkin/types.js +13 -0
  224. package/script/lock/acquire_compile.d.ts +12 -4
  225. package/script/lock/acquire_compile.d.ts.map +1 -1
  226. package/script/lock/acquire_compile.js +15 -5
  227. package/script/lock/canonical_edges.d.ts +6 -2
  228. package/script/lock/canonical_edges.d.ts.map +1 -1
  229. package/script/lock/canonical_edges.js +10 -3
  230. package/script/lock/model.d.ts +5 -0
  231. package/script/lock/model.d.ts.map +1 -1
  232. package/script/lock/parser.d.ts.map +1 -1
  233. package/script/lock/parser.js +2 -0
  234. package/script/lock/resolve.d.ts +6 -2
  235. package/script/lock/resolve.d.ts.map +1 -1
  236. package/script/lock/resolve.js +36 -4
  237. package/script/lock/serializer.d.ts.map +1 -1
  238. package/script/lock/serializer.js +7 -1
  239. package/script/lock/upstream_deps.js +1 -1
  240. package/script/mod.d.ts +24 -6
  241. package/script/mod.d.ts.map +1 -1
  242. package/script/mod.js +48 -6
  243. package/script/model/mod.d.ts +43 -2
  244. package/script/model/mod.d.ts.map +1 -1
  245. package/script/model/mod.js +2 -1
  246. package/script/model/relations.d.ts +9 -0
  247. package/script/model/relations.d.ts.map +1 -1
  248. package/script/model/relations.js +17 -1
  249. package/script/parser/attributes.d.ts +82 -3
  250. package/script/parser/attributes.d.ts.map +1 -1
  251. package/script/parser/attributes.js +205 -33
  252. package/script/parser/body_tokens.d.ts +1 -1
  253. package/script/parser/body_tokens.d.ts.map +1 -1
  254. package/script/parser/body_tokens.js +49 -35
  255. package/script/parser/feature_file.d.ts +116 -0
  256. package/script/parser/feature_file.d.ts.map +1 -0
  257. package/script/parser/feature_file.js +548 -0
  258. package/script/parser/markdown.d.ts +7 -0
  259. package/script/parser/markdown.d.ts.map +1 -1
  260. package/script/parser/markdown.js +128 -39
  261. package/script/parser/mod.d.ts +18 -0
  262. package/script/parser/mod.d.ts.map +1 -1
  263. package/script/parser/mod.js +52 -1
  264. package/script/parser/translate.d.ts.map +1 -1
  265. package/script/parser/translate.js +5 -0
  266. package/script/refs/mod.d.ts +8 -3
  267. package/script/refs/mod.d.ts.map +1 -1
  268. package/script/refs/mod.js +20 -7
  269. package/script/refs/split.d.ts +58 -0
  270. package/script/refs/split.d.ts.map +1 -0
  271. package/script/refs/split.js +145 -0
  272. package/script/reporter/mod.d.ts +7 -0
  273. package/script/reporter/mod.d.ts.map +1 -1
  274. package/script/reporter/mod.js +113 -3
  275. package/script/sync/mapping.d.ts +4 -3
  276. package/script/sync/mapping.d.ts.map +1 -1
  277. package/script/sync/mapping.js +52 -9
  278. package/script/util/display_id.d.ts +20 -0
  279. package/script/util/display_id.d.ts.map +1 -0
  280. package/script/util/display_id.js +22 -0
  281. package/script/validator/attributes.d.ts.map +1 -1
  282. package/script/validator/attributes.js +4 -1
  283. package/script/validator/block_role.d.ts +40 -0
  284. package/script/validator/block_role.d.ts.map +1 -0
  285. package/script/validator/block_role.js +112 -0
  286. package/script/validator/feature_ac.d.ts.map +1 -1
  287. package/script/validator/feature_ac.js +6 -1
  288. package/script/validator/feature_parse.d.ts +35 -0
  289. package/script/validator/feature_parse.d.ts.map +1 -0
  290. package/script/validator/feature_parse.js +73 -0
  291. package/script/validator/locators.d.ts +36 -0
  292. package/script/validator/locators.d.ts.map +1 -0
  293. package/script/validator/locators.js +188 -0
  294. package/script/validator/mod.d.ts +10 -1
  295. package/script/validator/mod.d.ts.map +1 -1
  296. package/script/validator/mod.js +38 -14
  297. package/script/validator/normalize.d.ts.map +1 -1
  298. package/script/validator/normalize.js +8 -3
  299. package/script/validator/per_type_attrs.d.ts +3 -3
  300. package/script/validator/per_type_attrs.js +3 -3
  301. package/script/validator/pipeline.d.ts +10 -16
  302. package/script/validator/pipeline.d.ts.map +1 -1
  303. package/script/validator/pipeline.js +36 -16
  304. package/script/validator/procedure.d.ts +22 -0
  305. package/script/validator/procedure.d.ts.map +1 -0
  306. package/script/validator/procedure.js +61 -0
  307. package/script/validator/trace_types.d.ts.map +1 -1
  308. package/script/validator/trace_types.js +4 -3
  309. package/script/validator/traceability.d.ts.map +1 -1
  310. package/script/validator/traceability.js +4 -1
  311. package/script/validator/type_resolution.d.ts +5 -3
  312. package/script/validator/type_resolution.d.ts.map +1 -1
  313. package/script/validator/type_resolution.js +14 -2
  314. package/script/validator/types.d.ts +11 -0
  315. package/script/validator/types.d.ts.map +1 -1
  316. package/script/validator/types.js +27 -4
  317. package/script/validator/uxil_family.d.ts +1 -1
@@ -0,0 +1,540 @@
1
+ /**
2
+ * @module core/parser/feature_file
3
+ *
4
+ * Reads `.feature` files as MarkSpec sources. A header is a run of comment
5
+ * lines that starts with `# [ID] Title` and sits directly above a Gherkin
6
+ * node, optionally with tag lines in between.
7
+ */
8
+ import { parseFeature } from "../gherkin/parse.js";
9
+ import { projectFeature } from "../gherkin/project.js";
10
+ import { parseMarkdown } from "./markdown.js";
11
+ import { buildBlockLineMap } from "./line_map.js";
12
+ import { wrapAsListItem } from "./source.js";
13
+ const LANGUAGE_RE = /^\s*#\s*language\s*:/;
14
+ /**
15
+ * The ID of a `.feature` header: an uppercase letter, then uppercase letters
16
+ * or digits, then at least one `_` or `-` separator, ending in a letter or
17
+ * digit (for example `SC_LOGIN_0001` or `REQ-12`). It is a subset of the core
18
+ * display-ID grammar (`DISPLAY_ID_RE`) without `/`. A bracketed comment such
19
+ * as `# [TODO] fix later`, `# [x] done` or `# [WIP]` does not match, so it
20
+ * stays a plain Cucumber comment.
21
+ */
22
+ const HEADER_ID_SOURCE = "[A-Z][A-Z0-9]*[_-][A-Za-z0-9_.-]*[A-Za-z0-9]";
23
+ /** A header-start comment text: `[ID] ` after the `#`. */
24
+ export const FEATURE_HEADER_START_RE = new RegExp(`^\\[${HEADER_ID_SOURCE}\\]\\s`);
25
+ const HEADER_START_RE = FEATURE_HEADER_START_RE;
26
+ /**
27
+ * Exclusive end index of the header comment run that starts at `titleIdx`.
28
+ *
29
+ * The run is the consecutive comment lines (`^\s*#`) from `titleIdx` down.
30
+ * It stops before a non-comment line, before the next header start (comment
31
+ * text starting with `[ID] `), and before a `# language:` line.
32
+ *
33
+ * @param lines - The file lines, without line endings.
34
+ * @param titleIdx - Zero-based index of the header's first line.
35
+ */
36
+ export function featureHeaderRunEnd(lines, titleIdx) {
37
+ const isComment = (line) => line !== undefined && line.trimStart().startsWith("#");
38
+ let endIdx = titleIdx;
39
+ while (isComment(lines[endIdx + 1])) {
40
+ const next = lines[endIdx + 1];
41
+ const nextText = next.trimStart().slice(1).trimStart();
42
+ if (HEADER_START_RE.test(nextText) || LANGUAGE_RE.test(next))
43
+ break;
44
+ endIdx++;
45
+ }
46
+ return endIdx + 1;
47
+ }
48
+ /** File line of every node keyword a header may head, with its kind. */
49
+ function nodeKinds(document) {
50
+ const kinds = new Map();
51
+ const feature = document.feature;
52
+ if (!feature)
53
+ return kinds;
54
+ kinds.set(feature.location.line, "feature");
55
+ const addSteps = (steps) => {
56
+ for (const step of steps)
57
+ kinds.set(step.location.line, "step");
58
+ };
59
+ const addScenario = (scenario) => {
60
+ kinds.set(scenario.location.line, "scenario");
61
+ addSteps(scenario.steps);
62
+ for (const examples of scenario.examples) {
63
+ kinds.set(examples.location.line, "examples");
64
+ }
65
+ };
66
+ const addBackground = (background) => {
67
+ kinds.set(background.location.line, "background");
68
+ addSteps(background.steps);
69
+ };
70
+ for (const child of feature.children) {
71
+ if (child.background)
72
+ addBackground(child.background);
73
+ if (child.scenario)
74
+ addScenario(child.scenario);
75
+ if (child.rule) {
76
+ kinds.set(child.rule.location.line, "rule");
77
+ for (const ruleChild of child.rule.children) {
78
+ if (ruleChild.background)
79
+ addBackground(ruleChild.background);
80
+ if (ruleChild.scenario)
81
+ addScenario(ruleChild.scenario);
82
+ }
83
+ }
84
+ }
85
+ return kinds;
86
+ }
87
+ /**
88
+ * Find the header comment blocks of a parsed `.feature` file.
89
+ *
90
+ * A comment line whose text starts with `[ID] ` starts a new header, so
91
+ * stacked headers are separate. The result is keyed by the file line of the
92
+ * node keyword that a header sits above. A header that is not directly above
93
+ * a node (only tag lines and plain comments may stand between; a blank line,
94
+ * another header or any other line breaks the link) has `kind: undefined`
95
+ * and is keyed by its own first line, so a caller can report it. The two
96
+ * kinds of key never collide, because a node line is not a comment line.
97
+ *
98
+ * @param document - The parsed Gherkin document of `text`.
99
+ * @param text - The file text. Line endings may be LF or CRLF.
100
+ */
101
+ export function findHeaders(document, text) {
102
+ const result = new Map();
103
+ const fileLines = text.split("\n").map((l) => l.replace(/\r$/, ""));
104
+ const nodes = nodeKinds(document);
105
+ const commentLines = new Set(document.comments.map((c) => c.location.line));
106
+ // The text of a comment line without the `#` and the spaces after it.
107
+ const headerText = (raw) => raw.trimStart().slice(1).trimStart();
108
+ // Group comments into runs of consecutive lines. A `# language:` line is
109
+ // never part of a run; a header-start line begins a new run.
110
+ const runs = [];
111
+ for (const comment of document.comments) {
112
+ const raw = comment.text.replace(/\r$/, "");
113
+ if (LANGUAGE_RE.test(raw)) {
114
+ runs.push([]);
115
+ continue;
116
+ }
117
+ const entry = {
118
+ line: comment.location.line,
119
+ // The parser reports column 1 for comments; use the real `#` column.
120
+ column: raw.length - raw.trimStart().length + 1,
121
+ text: raw,
122
+ };
123
+ const last = runs[runs.length - 1];
124
+ const startsHeader = HEADER_START_RE.test(headerText(raw));
125
+ if (last && last.length > 0 && !startsHeader &&
126
+ last[last.length - 1].line + 1 === entry.line) {
127
+ last.push(entry);
128
+ }
129
+ else {
130
+ runs.push([entry]);
131
+ }
132
+ }
133
+ for (const run of runs) {
134
+ if (run.length === 0)
135
+ continue;
136
+ const lines = [];
137
+ const prefixWidths = [];
138
+ run.forEach((c, i) => {
139
+ const indent = c.text.length - c.text.trimStart().length;
140
+ const afterHash = c.text.slice(indent + 1);
141
+ // The first line drops all spaces after `#`; later lines keep any
142
+ // extra indent, because it can be part of the body.
143
+ const spaces = i === 0
144
+ ? afterHash.length - afterHash.trimStart().length
145
+ : afterHash.startsWith(" ")
146
+ ? 1
147
+ : 0;
148
+ lines.push(afterHash.slice(spaces));
149
+ prefixWidths.push(indent + 1 + spaces);
150
+ });
151
+ if (!HEADER_START_RE.test(lines[0]))
152
+ continue;
153
+ const startLine = run[0].line;
154
+ const endLine = run[run.length - 1].line;
155
+ // The node follows the run, past tag lines and plain comments.
156
+ let next = endLine + 1;
157
+ for (;;) {
158
+ const line = fileLines[next - 1];
159
+ if (line === undefined)
160
+ break;
161
+ if (line.trim().startsWith("@")) {
162
+ next++;
163
+ }
164
+ else if (commentLines.has(next)) {
165
+ if (HEADER_START_RE.test(headerText(line)))
166
+ break;
167
+ next++;
168
+ }
169
+ else
170
+ break;
171
+ }
172
+ const kind = nodes.get(next);
173
+ const header = {
174
+ kind,
175
+ startLine,
176
+ endLine,
177
+ column: run[0].column,
178
+ lines,
179
+ prefixWidths,
180
+ };
181
+ result.set(kind ? next : startLine, header);
182
+ }
183
+ return result;
184
+ }
185
+ /** Diagnostics that do not apply to a `.feature` header. */
186
+ const IGNORED_CODES = new Set([
187
+ // Git-style (unindented) trailers are canonical in a header.
188
+ "MSL-P023",
189
+ // The body may come from the Gherkin node description, added later.
190
+ "MSL-P030",
191
+ ]);
192
+ /**
193
+ * Turn one header into an entry whose positions are file-relative.
194
+ *
195
+ * The header lines are wrapped as a Markdown list item and parsed like a
196
+ * source doc comment. A trailer in git style (no indent) is canonical in a
197
+ * `.feature` header, so the Markdown-only MSL-P023 warning is dropped. An
198
+ * empty body is not an error here: the node description can supply it.
199
+ * A header that is only a title still gives an entry, with an empty body
200
+ * (`allowTitleOnly`).
201
+ * The result has no entry when the header text does not form one.
202
+ *
203
+ * @param header - A header found by {@linkcode findHeaders}.
204
+ * @param file - File path used in locations.
205
+ */
206
+ export function headerEntry(header, file) {
207
+ const lineMap = buildBlockLineMap({
208
+ startLine: header.startLine,
209
+ startColumn: header.column,
210
+ prefixWidths: header.prefixWidths,
211
+ });
212
+ const { entries, diagnostics } = parseMarkdown(wrapAsListItem([...header.lines]), { file, lineMap, allowTitleOnly: true });
213
+ const entry = entries[0];
214
+ return {
215
+ entry,
216
+ diagnostics: diagnostics.filter((d) => !IGNORED_CODES.has(d.code)),
217
+ };
218
+ }
219
+ /** A header line anywhere in a file: `# [ID] ` after optional indent. */
220
+ const HEADER_LINE_RE = new RegExp(`^\\s*#\\s*\\[${HEADER_ID_SOURCE}\\]\\s`, "m");
221
+ /** `true` when the text has a `# [ID]` header line (an entry candidate). */
222
+ export function hasFeatureHeaderLine(text) {
223
+ return HEADER_LINE_RE.test(text);
224
+ }
225
+ /**
226
+ * Read the entries of a `.feature` file (epic #915 Case B).
227
+ *
228
+ * A `Feature`, `Rule` or `Scenario` with a `# [ID] Title` header comment is
229
+ * an entry. A scenario without a header belongs to the nearest enclosing
230
+ * headed `Rule`, else the headed `Feature`; with no headed ancestor it is not
231
+ * in the model. Each entry gets one `FeatureNode`: its `source` is the text
232
+ * of the owning node's span (first tag or keyword line to its last content
233
+ * line), its `feature` is the projected AST with ranges rebased to that
234
+ * span, and its `range` is set so `featureFileLine` returns file lines.
235
+ * The Gherkin description of the node is appended to the body after the
236
+ * header prose. Tags are not labels.
237
+ *
238
+ * Diagnostics: the header parse diagnostics; `MSL-P030` when an entry has
239
+ * neither header prose nor a description and no content (a Scenario entry
240
+ * whose scenario has no step, or a Feature or Rule entry that owns no
241
+ * scenario), at the `#` of the header line; `MSL-B069` (warning) for a header
242
+ * above a Background, Examples or step, or above no node. When the file does
243
+ * not parse, there are no entries and one `MSL-B062` warning at the parser
244
+ * position, but only when the file has a header line, so a plain Cucumber
245
+ * file never gets a new diagnostic.
246
+ *
247
+ * @param text - The file text (LF or CRLF).
248
+ * @param options.file - File path used in locations.
249
+ * @param options.api - The loaded Gherkin parser.
250
+ */
251
+ export function parseFeatureFile(text, options) {
252
+ const { file, api } = options;
253
+ const entries = [];
254
+ const diagnostics = [];
255
+ const hasHeader = HEADER_LINE_RE.test(text);
256
+ const unparsed = (reason, line, column) => {
257
+ if (hasHeader) {
258
+ diagnostics.push({
259
+ code: "MSL-B062",
260
+ severity: "warning",
261
+ message: `Gherkin file does not parse: ${reason}`,
262
+ location: { file, line, column },
263
+ });
264
+ }
265
+ return { entries, diagnostics };
266
+ };
267
+ const firstLineOf = (message) => message.split("\n")[0].replace(/^\(\d+:\d+\):\s*/, "").trim();
268
+ const parsed = parseFeature(api, text);
269
+ if (!parsed.ok) {
270
+ return unparsed(firstLineOf(parsed.message), parsed.line, parsed.column);
271
+ }
272
+ if (!hasHeader)
273
+ return { entries, diagnostics };
274
+ // `parseFeature` parses a wrapped copy of the text; the raw text can still
275
+ // fail (for example `Feature F` without a colon), and the parser throws.
276
+ let document;
277
+ try {
278
+ document = api.parse(text);
279
+ }
280
+ catch (err) {
281
+ const first = err.errors?.[0];
282
+ return unparsed(firstLineOf(first?.message ?? (err instanceof Error ? err.message : String(err))), first?.location ? Math.max(1, first.location.line) : 1, first?.location?.column ?? 1);
283
+ }
284
+ const headers = findHeaders(document, text);
285
+ for (const header of headers.values()) {
286
+ if (header.kind === "feature" || header.kind === "rule" ||
287
+ header.kind === "scenario")
288
+ continue;
289
+ const id = /^\[([^\]]+)\]/.exec(header.lines[0])?.[1] ?? "";
290
+ diagnostics.push({
291
+ code: "MSL-B069",
292
+ severity: "warning",
293
+ message: header.kind === undefined
294
+ ? `[${id}]: a # [ID] header is not directly above a Feature, Rule ` +
295
+ "or Scenario, so it is not an entry; remove the blank line or " +
296
+ "other text between the header and the node"
297
+ : `[${id}]: a # [ID] header stands above a ${header.kind}, which ` +
298
+ "cannot be an entry; move it above a Feature, Rule or Scenario",
299
+ location: { file, line: header.startLine, column: header.column },
300
+ });
301
+ }
302
+ const feature = document.feature;
303
+ if (!feature)
304
+ return { entries, diagnostics: sortByLine(diagnostics) };
305
+ const lines = text.split("\n").map((l) => l.replace(/\r$/, ""));
306
+ // Last content line at or before `bound`, skipping blank and comment
307
+ // lines, but never above `floor`.
308
+ const lastContent = (bound, floor) => {
309
+ let line = bound;
310
+ while (line > floor) {
311
+ const trimmed = (lines[line - 1] ?? "").trim();
312
+ if (trimmed !== "" && !trimmed.startsWith("#"))
313
+ break;
314
+ line--;
315
+ }
316
+ return line;
317
+ };
318
+ // File lines of every header comment block.
319
+ const headerLines = new Set();
320
+ for (const h of headers.values()) {
321
+ for (let l = h.startLine; l <= h.endLine; l++)
322
+ headerLines.add(l);
323
+ }
324
+ // Last line of a node span: its last content line, then the comment
325
+ // lines that follow it directly (a `# Step` label after the last step),
326
+ // but not a header comment and not past `bound`.
327
+ const spanEnd = (bound, floor) => {
328
+ let line = lastContent(bound, floor);
329
+ while (line < bound && (lines[line] ?? "").trim().startsWith("#") &&
330
+ !headerLines.has(line + 1))
331
+ line++;
332
+ return line;
333
+ };
334
+ const firstLine = (node) => Math.min(node.location.line, ...(node.tags ?? []).map((t) => t.location.line));
335
+ const headed = (line, kind) => {
336
+ const header = headers.get(line);
337
+ return header?.kind === kind ? header : undefined;
338
+ };
339
+ const nodes = [];
340
+ const featureEnd = spanEnd(lines.length, feature.location.line);
341
+ const featureHeader = headed(feature.location.line, "feature");
342
+ const featureNode = featureHeader && {
343
+ kind: "feature",
344
+ header: featureHeader,
345
+ keywordLine: feature.location.line,
346
+ startLine: firstLine(feature),
347
+ endLine: featureEnd,
348
+ description: feature.description,
349
+ scenarios: [],
350
+ };
351
+ if (featureNode)
352
+ nodes.push(featureNode);
353
+ // Walk the children in the order `parseFeature` lists scenarios, so a
354
+ // running index addresses `parsed.feature.scenarios`.
355
+ let index = 0;
356
+ const walk = (children, bound, owner) => {
357
+ children.forEach((child, i) => {
358
+ const next = children[i + 1];
359
+ const nextNode = next && (next.scenario ?? next.background ?? next.rule);
360
+ const nextStart = nextNode ? firstLine(nextNode) : bound + 1;
361
+ if (child.scenario) {
362
+ const scenario = child.scenario;
363
+ const header = headed(scenario.location.line, "scenario");
364
+ if (header) {
365
+ nodes.push({
366
+ kind: "scenario",
367
+ header,
368
+ keywordLine: scenario.location.line,
369
+ startLine: firstLine(scenario),
370
+ endLine: spanEnd(nextStart - 1, scenario.location.line),
371
+ description: scenario.description,
372
+ scenarios: [index],
373
+ });
374
+ }
375
+ else
376
+ owner?.scenarios.push(index);
377
+ index++;
378
+ }
379
+ else if (child.rule) {
380
+ const rule = child.rule;
381
+ const end = spanEnd(nextStart - 1, rule.location.line);
382
+ const header = headed(rule.location.line, "rule");
383
+ const ruleNode = header && {
384
+ kind: "rule",
385
+ header,
386
+ keywordLine: rule.location.line,
387
+ startLine: firstLine(rule),
388
+ endLine: end,
389
+ description: rule.description,
390
+ scenarios: [],
391
+ };
392
+ if (ruleNode)
393
+ nodes.push(ruleNode);
394
+ walk(rule.children, end, ruleNode ?? owner);
395
+ }
396
+ });
397
+ };
398
+ walk(feature.children, featureEnd, featureNode);
399
+ nodes.sort((a, b) => a.header.startLine - b.header.startLine);
400
+ const owned = parsed.feature.scenarios;
401
+ for (const node of nodes) {
402
+ const result = headerEntry(node.header, file);
403
+ diagnostics.push(...result.diagnostics);
404
+ const base = result.entry;
405
+ if (!base)
406
+ continue;
407
+ const bodyStart = base.bodyStartLine ?? (base.location.line + 1);
408
+ const description = descriptionBody(node, lines, file, bodyStart);
409
+ const body = [base.body, description.text].filter((p) => p !== "")
410
+ .join("\n\n");
411
+ const ownsContent = node.kind === "scenario"
412
+ ? node.scenarios.some((i) => owned[i].steps.length > 0)
413
+ : node.scenarios.length > 0;
414
+ if (body === "" && !ownsContent && base.shape !== "Reference") {
415
+ diagnostics.push({
416
+ code: "MSL-P030",
417
+ severity: "error",
418
+ message: `${base.displayId}: Authored entry has no body block -- ` +
419
+ "a .feature header needs prose, a node description, or steps",
420
+ location: {
421
+ file,
422
+ line: base.location.line,
423
+ column: node.header.column,
424
+ },
425
+ });
426
+ }
427
+ // Nested nodes that are their own entries: their header and span are
428
+ // left out of this entry's projection and source.
429
+ const exclude = nodes
430
+ .filter((other) => other !== node && other.header.startLine > node.startLine &&
431
+ other.header.startLine <= node.endLine)
432
+ .map((other) => ({
433
+ startLine: other.header.startLine,
434
+ endLine: other.endLine,
435
+ }));
436
+ const sourceLines = lines.slice(node.startLine - 1, node.endLine)
437
+ .map((text, i) => {
438
+ const line = node.startLine + i;
439
+ return exclude.some((x) => line >= x.startLine && line <= x.endLine)
440
+ ? ""
441
+ : text;
442
+ });
443
+ const block = {
444
+ kind: "feature",
445
+ source: sourceLines.join("\n"),
446
+ feature: projectFeature(parsed.feature, node.scenarios, node, exclude),
447
+ range: {
448
+ start: { line: node.startLine - bodyStart, column: 1 },
449
+ end: { line: node.endLine - bodyStart + 1, column: 1 },
450
+ },
451
+ };
452
+ entries.push({
453
+ ...base,
454
+ body,
455
+ bodyAst: [...(base.bodyAst ?? []), ...description.blocks, block],
456
+ bodyTokens: [...base.bodyTokens, ...description.tokens],
457
+ source: { kind: "feature-file", node: node.kind },
458
+ });
459
+ }
460
+ return { entries, diagnostics: sortByLine(diagnostics) };
461
+ }
462
+ /**
463
+ * Parse the Gherkin description of `node` as Markdown body content. The
464
+ * description lines are found in the file after the node keyword line, so
465
+ * body tokens carry file positions. Body-AST ranges are made relative to
466
+ * `bodyStart`, the body start line of the entry they join.
467
+ */
468
+ function descriptionBody(node, lines, file, bodyStart) {
469
+ const text = node.description.split("\n").map((l) => l.trim()).join("\n")
470
+ .trim();
471
+ if (text === "")
472
+ return { text, blocks: [], tokens: [] };
473
+ const count = text.split("\n").length;
474
+ const first = text.split("\n")[0];
475
+ let start = node.keywordLine + 1;
476
+ while (start <= lines.length && lines[start - 1].trim() !== first)
477
+ start++;
478
+ // The parser leaves comment lines out of `node.description`, so take
479
+ // file lines until `count` non-comment lines are collected. A comment line
480
+ // inside the description is not description text: it becomes a blank line,
481
+ // which keeps the file line of every later description line.
482
+ const cleaned = [];
483
+ let taken = 0;
484
+ for (let l = start; l <= lines.length && taken < count; l++) {
485
+ const line = lines[l - 1];
486
+ if (line.trim().startsWith("#")) {
487
+ cleaned.push("");
488
+ }
489
+ else {
490
+ cleaned.push(line);
491
+ taken++;
492
+ }
493
+ }
494
+ const prefixWidths = cleaned.map((l) => l.length - l.trimStart().length);
495
+ // Two synthetic lines (a title and a blank line) precede the body.
496
+ const lineMap = buildBlockLineMap({
497
+ startLine: start - 2,
498
+ startColumn: 1,
499
+ prefixWidths: [0, 0, ...prefixWidths],
500
+ });
501
+ const parsed = parseMarkdown(wrapAsListItem([
502
+ "[DESCRIPTION] D",
503
+ "",
504
+ ...cleaned.map((l) => l.trimStart()),
505
+ ]), { file, lineMap, allowTitleOnly: true });
506
+ const entry = parsed.entries[0];
507
+ if (!entry)
508
+ return { text, blocks: [], tokens: [] };
509
+ const shift = (entry.bodyStartLine ?? start) - bodyStart;
510
+ return {
511
+ text,
512
+ blocks: (entry.bodyAst ?? []).map((b) => shiftRanges(b, shift)),
513
+ tokens: entry.bodyTokens,
514
+ };
515
+ }
516
+ /** Copy `value` with the line of every `range` start and end moved by
517
+ * `delta`. */
518
+ function shiftRanges(value, delta) {
519
+ if (delta === 0 || value === null || typeof value !== "object")
520
+ return value;
521
+ if (Array.isArray(value)) {
522
+ return value.map((v) => shiftRanges(v, delta));
523
+ }
524
+ const out = {};
525
+ for (const [key, v] of Object.entries(value)) {
526
+ if (key === "range" && v && typeof v === "object" && "start" in v) {
527
+ const r = v;
528
+ out[key] = {
529
+ start: { ...r.start, line: r.start.line + delta },
530
+ end: { ...r.end, line: r.end.line + delta },
531
+ };
532
+ }
533
+ else
534
+ out[key] = shiftRanges(v, delta);
535
+ }
536
+ return out;
537
+ }
538
+ function sortByLine(diagnostics) {
539
+ return diagnostics.sort((a, b) => (a.location?.line ?? 0) - (b.location?.line ?? 0));
540
+ }
@@ -24,6 +24,13 @@ export interface ParseMarkdownOptions {
24
24
  * Decision 6.
25
25
  */
26
26
  readonly lineMap?: LineMap;
27
+ /**
28
+ * Keep a title-only list item as an Authored entry with an empty body,
29
+ * as a references document does for referenced entries. Internal: used
30
+ * by the `.feature` header reader, whose body can come from the Gherkin
31
+ * description. Default `false`: a title-only item in Markdown is dropped.
32
+ */
33
+ readonly allowTitleOnly?: boolean;
27
34
  }
28
35
  /** Result returned by {@linkcode parseMarkdown}. */
29
36
  export interface ParseMarkdownResult {
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/parser/markdown.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAa,UAAU,EAAE,KAAK,EAAc,MAAM,iBAAiB,CAAC;AAsBhF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAsB7C,6CAA6C;AAC7C,MAAM,WAAW,oBAAoB;IACnC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC5B;AAsDD,oDAAoD;AACpD,MAAM,WAAW,mBAAmB;IAClC,mDAAmD;IACnD,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;IAC1B,4DAA4D;IAC5D,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAC;CACpC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,mBAAmB,CAgDrB"}
1
+ {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/parser/markdown.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAa,UAAU,EAAE,KAAK,EAAc,MAAM,iBAAiB,CAAC;AAwBhF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAU7C,6CAA6C;AAC7C,MAAM,WAAW,oBAAoB;IACnC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;CACnC;AAsDD,oDAAoD;AACpD,MAAM,WAAW,mBAAmB;IAClC,mDAAmD;IACnD,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;IAC1B,4DAA4D;IAC5D,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAC;CACpC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,mBAAmB,CAiDrB"}