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