-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.ts
More file actions
317 lines (255 loc) · 7.15 KB
/
parser.ts
File metadata and controls
317 lines (255 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import type { Options, Commit, Reference, Field, Note, Revert } from "./mod.ts";
import type { ParsingRegex } from "./regex.ts";
const reNewlines = /^(?:\r\n|\n|\r)+|(?:\r\n|\n|\r)+$/g;
function trimOffNewlines(str: string) {
return str.replace(reNewlines, "");
}
const CATCH_ALL = /()(.+)/gi;
const SCISSOR = "# ------------------------ >8 ------------------------";
function append(src: string | null, line: string) {
if (src) {
src += "\n" + line;
} else {
src = line;
}
return src;
}
function getCommentFilter(char: string) {
return (line: string) => {
return line.charAt(0) !== char;
};
}
function truncateToScissor(lines: string[]) {
const scissorIndex = lines.indexOf(SCISSOR);
if (scissorIndex === -1) {
return lines;
}
return lines.slice(0, scissorIndex);
}
function getReferences(input: string, regex: ParsingRegex): Reference[] {
const references = [];
let referenceSentences;
let referenceMatch;
const reApplicable = input.match(regex.references) !== null
? regex.references
: CATCH_ALL;
while ((referenceSentences = reApplicable.exec(input))) {
const action = referenceSentences[1] || null;
const sentence = referenceSentences[2];
while ((referenceMatch = regex.referenceParts.exec(sentence))) {
let owner = null;
let repository: string | null = referenceMatch[1] || "";
const ownerRepo = repository.split("/");
if (ownerRepo.length > 1) {
owner = ownerRepo.shift()!;
repository = ownerRepo.join("/");
}
repository ||= null;
const issue = referenceMatch[3];
const raw = referenceMatch[0];
const prefix = referenceMatch[2];
const reference = {
action,
owner,
repository,
issue,
raw,
prefix,
};
references.push(reference);
}
}
return references;
}
function passTrough() {
return true;
}
export function parser(
raw?: string,
options?: Required<Options>,
regex?: ParsingRegex,
): Commit {
if (!raw || !raw.trim()) {
throw new TypeError("Expected a raw commit");
}
if (!options) {
throw new TypeError("Expected options");
}
if (!regex) {
throw new TypeError("Expected regexes");
}
let mentionsMatch;
const commentFilter = typeof options.commentChar === "string"
? getCommentFilter(options.commentChar)
: passTrough;
const rawLines = trimOffNewlines(raw).split(/\r?\n/);
const lines = truncateToScissor(rawLines).filter(commentFilter);
let continueNote = false;
let isBody = true;
const headerCorrespondence = options.headerCorrespondence?.map((part) => {
return part.trim();
});
const revertCorrespondence = options.revertCorrespondence?.map((field) => {
return field.trim();
});
const mergeCorrespondence = options.mergeCorrespondence?.map((field) => {
return field.trim();
});
const mentions: string[] = [];
const notes: Note[] = [];
const references: Reference[] = [];
let body: Field = null;
let footer: Field = null;
let header = null;
let merge = null;
let revert: Revert | null = null;
if (lines.length === 0) {
return {
body: body,
footer: footer,
header: header,
mentions: mentions,
merge: merge,
notes: notes,
references: references,
revert: revert,
scope: null,
subject: null,
type: null,
} as Commit;
}
// msg parts
merge = lines.shift()!;
const mergeParts: { [key: string]: string | null } = {};
const headerParts: { [key: string]: string | null } = {};
body = "";
footer = "";
const mergeMatch = merge.match(options.mergePattern!);
if (mergeMatch && options.mergePattern) {
merge = mergeMatch[0];
header = lines.shift();
while (!header?.trim()) {
header = lines.shift();
}
mergeCorrespondence?.forEach((partName, index) => {
const partValue = mergeMatch![index + 1] ?? null;
mergeParts[partName] = partValue;
});
} else {
header = merge;
merge = null;
mergeCorrespondence?.forEach((partName) => {
mergeParts[partName] = null;
});
}
const headerMatch = header.match(options.headerPattern!);
if (headerMatch) {
headerCorrespondence?.forEach((partName, index) => {
const partValue = headerMatch![index + 1] || null;
headerParts[partName] = partValue;
});
} else {
headerCorrespondence?.forEach((partName) => {
headerParts[partName] = null;
});
}
Array.prototype.push.apply(references, getReferences(header, regex));
// body or footer
const otherFields: { [key: string]: string } = {};
let currentProcessedField: string;
lines.forEach((line) => {
if (options.fieldPattern) {
const fieldMatch = options.fieldPattern.exec(line);
if (fieldMatch) {
currentProcessedField = fieldMatch[1];
return;
}
if (currentProcessedField) {
otherFields[currentProcessedField] = append(
otherFields[currentProcessedField],
line,
);
return;
}
}
let referenceMatched;
// this is a new important note
const notesMatch = line.match(regex.notes);
if (notesMatch) {
continueNote = true;
isBody = false;
footer = append(footer, line);
const note = {
title: notesMatch[1],
text: notesMatch[2],
};
notes.push(note);
return;
}
const lineReferences = getReferences(line, regex);
if (lineReferences.length > 0) {
isBody = false;
referenceMatched = true;
continueNote = false;
}
Array.prototype.push.apply(references, lineReferences);
if (referenceMatched) {
footer = append(footer, line);
return;
}
if (continueNote) {
notes[notes.length - 1].text = append(notes[notes.length - 1].text, line);
footer = append(footer, line);
return;
}
if (isBody) {
body = append(body, line);
} else {
footer = append(footer, line);
}
});
// if (options.breakingHeaderPattern && notes.length === 0) {
// let breakingHeader = header.match(options.breakingHeaderPattern);
// if (breakingHeader) {
// const noteText = breakingHeader[3]; // the description of the change.
// notes.push({
// title: "BREAKING CHANGE",
// text: noteText,
// });
// }
// }
while ((mentionsMatch = regex.mentions.exec(raw))) {
mentions.push(mentionsMatch[1]);
}
// does this commit revert any other commit?
const revertMatch = raw.match(options.revertPattern!);
if (revertMatch) {
revert = {};
revertCorrespondence?.forEach((partName, index) => {
const partValue = revertMatch![index + 1] || null;
revert![partName] = partValue;
});
} else {
revert = null;
}
notes.forEach((note) => {
note.text = trimOffNewlines(note.text);
return note;
});
const msg = Object.assign(
headerParts,
mergeParts,
{
merge: merge,
header: header,
body: body ? trimOffNewlines(body) : null,
footer: footer ? trimOffNewlines(footer) : null,
notes: notes,
references: references,
mentions: mentions,
revert: revert,
},
otherFields,
);
return msg as Commit;
}