6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Commit as ParsedCommit , Options , sync as parse } from 'conventional-commits-parser' ;
9
+ import {
10
+ CommitReference ,
11
+ CommitNote ,
12
+ ParserOptions ,
13
+ CommitParser ,
14
+ } from 'conventional-commits-parser' ;
10
15
11
16
/** A parsed commit, containing the information needed to validate the commit. */
12
17
export interface Commit {
@@ -19,17 +24,17 @@ export interface Commit {
19
24
/** The footer of the commit, containing issue references and note sections. */
20
25
footer : string ;
21
26
/** A list of the references to other issues made throughout the commit message. */
22
- references : ParsedCommit . Reference [ ] ;
27
+ references : CommitReference [ ] ;
23
28
/** The type of the commit message. */
24
29
type : string ;
25
30
/** The scope of the commit message. */
26
31
scope : string ;
27
32
/** The subject of the commit message. */
28
33
subject : string ;
29
34
/** A list of breaking change notes in the commit message. */
30
- breakingChanges : ParsedCommit . Note [ ] ;
35
+ breakingChanges : CommitNote [ ] ;
31
36
/** A list of deprecation notes in the commit message. */
32
- deprecations : ParsedCommit . Note [ ] ;
37
+ deprecations : CommitNote [ ] ;
33
38
/** Whether the commit is a fixup commit. */
34
39
isFixup : boolean ;
35
40
/** Whether the commit is a squash commit. */
@@ -101,18 +106,17 @@ const headerPattern = /^(\w+)(?:\(([^)]+)\))?: (.*)$/;
101
106
const headerCorrespondence = [ 'type' , 'scope' , 'subject' ] ;
102
107
/**
103
108
* Configuration options for the commit parser.
104
- *
105
- * NOTE: An extended type from `Options` must be used because the current
106
- * @types /conventional-commits-parser version does not include the `notesPattern` field.
107
109
*/
108
- const parseOptions : Options & { notesPattern : ( keywords : string ) => RegExp } = {
110
+ const parseOptions : ParserOptions = {
109
111
commentChar : '#' ,
110
112
headerPattern,
111
113
headerCorrespondence,
112
114
noteKeywords : [ NoteSections . BREAKING_CHANGE , NoteSections . DEPRECATED ] ,
113
115
notesPattern : ( keywords : string ) => new RegExp ( `^\\s*(${ keywords } ): ?(.*)` ) ,
114
116
} ;
115
117
118
+ let commitParser : CommitParser | undefined ;
119
+
116
120
/** Parse a commit message into its composite parts. */
117
121
export const parseCommitMessage : ( fullText : string ) => Commit = parseInternal ;
118
122
@@ -130,21 +134,27 @@ function parseInternal(fullText: string | Buffer): CommitFromGitLog | Commit {
130
134
. replace ( FIXUP_PREFIX_RE , '' )
131
135
. replace ( SQUASH_PREFIX_RE , '' )
132
136
. replace ( REVERT_PREFIX_RE , '' ) ;
137
+
138
+ commitParser ??= new CommitParser ( parseOptions ) ;
139
+
133
140
/** The initially parsed commit. */
134
- const commit = parse ( strippedCommitMsg , parseOptions ) ;
141
+ const commit = commitParser . parse ( strippedCommitMsg ) ;
135
142
/** A list of breaking change notes from the commit. */
136
- const breakingChanges : ParsedCommit . Note [ ] = [ ] ;
143
+ const breakingChanges : CommitNote [ ] = [ ] ;
137
144
/** A list of deprecation notes from the commit. */
138
- const deprecations : ParsedCommit . Note [ ] = [ ] ;
145
+ const deprecations : CommitNote [ ] = [ ] ;
139
146
140
147
// Extract the commit message notes by marked types into their respective lists.
141
- commit . notes . forEach ( ( note : ParsedCommit . Note ) => {
142
- if ( note . title === NoteSections . BREAKING_CHANGE ) {
143
- breakingChanges . push ( note ) ;
144
- } else if ( note . title === NoteSections . DEPRECATED ) {
145
- deprecations . push ( note ) ;
148
+ for ( const note of commit . notes ) {
149
+ switch ( note . title ) {
150
+ case NoteSections . BREAKING_CHANGE :
151
+ breakingChanges . push ( note ) ;
152
+ break ;
153
+ case NoteSections . DEPRECATED :
154
+ deprecations . push ( note ) ;
155
+ break ;
146
156
}
147
- } ) ;
157
+ }
148
158
149
159
return {
150
160
fullText,
@@ -154,9 +164,9 @@ function parseInternal(fullText: string | Buffer): CommitFromGitLog | Commit {
154
164
footer : commit . footer || '' ,
155
165
header : commit . header || '' ,
156
166
references : commit . references ,
157
- scope : commit . scope || '' ,
158
- subject : commit . subject || '' ,
159
- type : commit . type || '' ,
167
+ scope : commit [ ' scope' ] || '' ,
168
+ subject : commit [ ' subject' ] || '' ,
169
+ type : commit [ ' type' ] || '' ,
160
170
isFixup : FIXUP_PREFIX_RE . test ( fullText ) ,
161
171
isSquash : SQUASH_PREFIX_RE . test ( fullText ) ,
162
172
isRevert : REVERT_PREFIX_RE . test ( fullText ) ,
0 commit comments