66 * found in the LICENSE file at https://angular.io/license
77 */
88
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' ;
1015
1116/** A parsed commit, containing the information needed to validate the commit. */
1217export interface Commit {
@@ -19,17 +24,17 @@ export interface Commit {
1924 /** The footer of the commit, containing issue references and note sections. */
2025 footer : string ;
2126 /** A list of the references to other issues made throughout the commit message. */
22- references : ParsedCommit . Reference [ ] ;
27+ references : CommitReference [ ] ;
2328 /** The type of the commit message. */
2429 type : string ;
2530 /** The scope of the commit message. */
2631 scope : string ;
2732 /** The subject of the commit message. */
2833 subject : string ;
2934 /** A list of breaking change notes in the commit message. */
30- breakingChanges : ParsedCommit . Note [ ] ;
35+ breakingChanges : CommitNote [ ] ;
3136 /** A list of deprecation notes in the commit message. */
32- deprecations : ParsedCommit . Note [ ] ;
37+ deprecations : CommitNote [ ] ;
3338 /** Whether the commit is a fixup commit. */
3439 isFixup : boolean ;
3540 /** Whether the commit is a squash commit. */
@@ -101,18 +106,17 @@ const headerPattern = /^(\w+)(?:\(([^)]+)\))?: (.*)$/;
101106const headerCorrespondence = [ 'type' , 'scope' , 'subject' ] ;
102107/**
103108 * 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.
107109 */
108- const parseOptions : Options & { notesPattern : ( keywords : string ) => RegExp } = {
110+ const parseOptions : ParserOptions = {
109111 commentChar : '#' ,
110112 headerPattern,
111113 headerCorrespondence,
112114 noteKeywords : [ NoteSections . BREAKING_CHANGE , NoteSections . DEPRECATED ] ,
113115 notesPattern : ( keywords : string ) => new RegExp ( `^\\s*(${ keywords } ): ?(.*)` ) ,
114116} ;
115117
118+ let commitParser : CommitParser | undefined ;
119+
116120/** Parse a commit message into its composite parts. */
117121export const parseCommitMessage : ( fullText : string ) => Commit = parseInternal ;
118122
@@ -130,21 +134,27 @@ function parseInternal(fullText: string | Buffer): CommitFromGitLog | Commit {
130134 . replace ( FIXUP_PREFIX_RE , '' )
131135 . replace ( SQUASH_PREFIX_RE , '' )
132136 . replace ( REVERT_PREFIX_RE , '' ) ;
137+
138+ commitParser ??= new CommitParser ( parseOptions ) ;
139+
133140 /** The initially parsed commit. */
134- const commit = parse ( strippedCommitMsg , parseOptions ) ;
141+ const commit = commitParser . parse ( strippedCommitMsg ) ;
135142 /** A list of breaking change notes from the commit. */
136- const breakingChanges : ParsedCommit . Note [ ] = [ ] ;
143+ const breakingChanges : CommitNote [ ] = [ ] ;
137144 /** A list of deprecation notes from the commit. */
138- const deprecations : ParsedCommit . Note [ ] = [ ] ;
145+ const deprecations : CommitNote [ ] = [ ] ;
139146
140147 // 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 ;
146156 }
147- } ) ;
157+ }
148158
149159 return {
150160 fullText,
@@ -154,9 +164,9 @@ function parseInternal(fullText: string | Buffer): CommitFromGitLog | Commit {
154164 footer : commit . footer || '' ,
155165 header : commit . header || '' ,
156166 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' ] || '' ,
160170 isFixup : FIXUP_PREFIX_RE . test ( fullText ) ,
161171 isSquash : SQUASH_PREFIX_RE . test ( fullText ) ,
162172 isRevert : REVERT_PREFIX_RE . test ( fullText ) ,
0 commit comments