@review-vir/common 3.1.6 → 3.1.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.
@@ -25,3 +25,33 @@
25
25
  * the block and interleaving the owners with their owned-file text.
26
26
  */
27
27
  export declare function parseInsertedCodeOwners(body: string): string[] | undefined;
28
+ /**
29
+ * Same as {@link parseInsertedCodeOwners} but maps each owner username to the file paths listed
30
+ * under them, with pull-request-vir's link text escaping (`\\`, `\[`, `\]`) undone.
31
+ *
32
+ * Usernames keep the case they were written with. The pull request author never has an entry, and
33
+ * neither does anyone in the legacy `**Code owners**: @user` format, since that format doesn't say
34
+ * which files belong to whom. Returns `undefined` when the body has no inserted code owners block.
35
+ *
36
+ * Like {@link parseInsertedCodeOwners}, this must run against the raw markdown `body`.
37
+ *
38
+ * @category Main
39
+ * @example
40
+ *
41
+ * ```ts
42
+ * parseInsertedCodeOwnerFiles(
43
+ * [
44
+ * '<!-- code owners start -->',
45
+ * '## Code Owners',
46
+ * '<details>',
47
+ * '<summary>@my-name Owned files</summary>',
48
+ * '',
49
+ * '- [src/\\[id\\].ts](https://github.com/o/r/pull/1/files#diff-abc)',
50
+ * '',
51
+ * '</details>',
52
+ * '<!-- code owners end -->',
53
+ * ].join('\n'),
54
+ * ); // {'my-name': ['src/[id].ts']}
55
+ * ```
56
+ */
57
+ export declare function parseInsertedCodeOwnerFiles(body: string): Record<string, string[]> | undefined;
@@ -47,3 +47,74 @@ export function parseInsertedCodeOwners(body) {
47
47
  });
48
48
  }));
49
49
  }
50
+ /**
51
+ * The second alternative is the older layout, which put each owner alone on the line above a plain
52
+ * `<summary>Owned files</summary>`.
53
+ */
54
+ const ownerRegExp = /^\s*(?:<summary>@([\w-]+) Owned files<\/summary>|@([\w-]+))\s*$/;
55
+ const fileLinkTextRegExp = /^\s*-\s+\[((?:\\.|[^\\\]])*)\]\(/;
56
+ /**
57
+ * Same as {@link parseInsertedCodeOwners} but maps each owner username to the file paths listed
58
+ * under them, with pull-request-vir's link text escaping (`\\`, `\[`, `\]`) undone.
59
+ *
60
+ * Usernames keep the case they were written with. The pull request author never has an entry, and
61
+ * neither does anyone in the legacy `**Code owners**: @user` format, since that format doesn't say
62
+ * which files belong to whom. Returns `undefined` when the body has no inserted code owners block.
63
+ *
64
+ * Like {@link parseInsertedCodeOwners}, this must run against the raw markdown `body`.
65
+ *
66
+ * @category Main
67
+ * @example
68
+ *
69
+ * ```ts
70
+ * parseInsertedCodeOwnerFiles(
71
+ * [
72
+ * '<!-- code owners start -->',
73
+ * '## Code Owners',
74
+ * '<details>',
75
+ * '<summary>@my-name Owned files</summary>',
76
+ * '',
77
+ * '- [src/\\[id\\].ts](https://github.com/o/r/pull/1/files#diff-abc)',
78
+ * '',
79
+ * '</details>',
80
+ * '<!-- code owners end -->',
81
+ * ].join('\n'),
82
+ * ); // {'my-name': ['src/[id].ts']}
83
+ * ```
84
+ */
85
+ export function parseInsertedCodeOwnerFiles(body) {
86
+ const [, block,] = safeMatch(body, insertedCodeOwnersRegExp);
87
+ if (block == undefined) {
88
+ return undefined;
89
+ }
90
+ return block.split('\n').reduce((state, line) => {
91
+ const [, summaryOwner, lineOwner,] = safeMatch(line, ownerRegExp);
92
+ const owner = summaryOwner || lineOwner;
93
+ if (owner) {
94
+ return {
95
+ currentOwner: owner,
96
+ filesByOwner: {
97
+ ...state.filesByOwner,
98
+ [owner]: state.filesByOwner[owner] || [],
99
+ },
100
+ };
101
+ }
102
+ const [, escapedFilePath,] = safeMatch(line, fileLinkTextRegExp);
103
+ if (escapedFilePath == undefined || !state.currentOwner) {
104
+ return state;
105
+ }
106
+ return {
107
+ currentOwner: state.currentOwner,
108
+ filesByOwner: {
109
+ ...state.filesByOwner,
110
+ [state.currentOwner]: [
111
+ ...(state.filesByOwner[state.currentOwner] || []),
112
+ escapedFilePath.replaceAll(/\\([\\[\]])/g, '$1'),
113
+ ],
114
+ },
115
+ };
116
+ }, {
117
+ currentOwner: undefined,
118
+ filesByOwner: {},
119
+ }).filesByOwner;
120
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@review-vir/common",
3
- "version": "3.1.6",
3
+ "version": "3.1.8",
4
4
  "description": "Common functionality that review-vir primary uses but other external packages need as well.",
5
5
  "keywords": [
6
6
  "review-vir",
@@ -46,7 +46,7 @@
46
46
  "@web/test-runner-playwright": "^1.0.0",
47
47
  "@web/test-runner-visual-regression": "^1.0.1",
48
48
  "istanbul-smart-text-reporter": "^1.1.5",
49
- "markdown-code-example-inserter": "^3.0.6",
49
+ "markdown-code-example-inserter": "^3.1.1",
50
50
  "typedoc": "^0.28.20",
51
51
  "typescript": "^6.0.3"
52
52
  },