Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(conventional-commits-parser): add flag to enable/disable multiple-scopes (default: enabled) #1233

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/conventional-commits-parser/src/CommitParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ describe('conventional-commits-parser', () => {
expect(result.scope).toBe('hello/world')
expect(result.subject).toBe('message')
})

it('should parse scopes with delimiters when default isEnableMultipleScopes and scopeDelimitersPattern option', () => {
const commitsWithScopeDelimiters = [
{
commit: 'feat(hello/world): message',
parsedScore: 'hello/world'
},
{
commit: 'feat(hello\\world): message',
parsedScore: 'hello\\world'
},
{
commit: 'feat(hello,world): message',
parsedScore: 'hello,world'
},
{
commit: 'feat(hello, world): message',
parsedScore: 'hello, world'
}
]

Object.values(commitsWithScopeDelimiters).forEach(({ commit, parsedScore }) => {
const result = parser.parse(commit)

expect(result.scope).toBe(parsedScore)
expect(result.scopes).toEqual(['hello', 'world'])
})
})
})

describe('custom options', () => {
Expand Down Expand Up @@ -257,6 +285,37 @@ describe('conventional-commits-parser', () => {

expect(commit.body).toBe('this is some body before a scissors-line')
})

it('should not parse scopes with delimiters when isEnableMultipleScopes is false', () => {
const parser = new CommitParser({
isEnableMultipleScopes: false
})
const commitsWithScopeDelimiters = [
{
commit: 'feat(hello/world): message',
parsedScore: 'hello/world'
},
{
commit: 'feat(hello\\world): message',
parsedScore: 'hello\\world'
},
{
commit: 'feat(hello,world): message',
parsedScore: 'hello,world'
},
{
commit: 'feat(hello, world): message',
parsedScore: 'hello, world'
}
]

Object.values(commitsWithScopeDelimiters).forEach(({ commit, parsedScore }) => {
const result = parser.parse(commit)

expect(result.scope).toBe(parsedScore)
expect(result.scopes).toBeUndefined()
})
})
})

describe('comments', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/conventional-commits-parser/src/CommitParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ export class CommitParser {
})
}

private parseScopes() {
const { commit, options: { isEnableMultipleScopes, scopeDelimitersPattern } } = this

if (!isEnableMultipleScopes || !scopeDelimitersPattern || !commit.scope) {
return
}

const messageScopes = commit.scope.split(scopeDelimitersPattern)

if (messageScopes.length > 1) {
commit.scopes = messageScopes
}
}

/**
* Parse commit message string into an object.
* @param input - Commit message string.
Expand Down Expand Up @@ -432,6 +446,7 @@ export class CommitParser {
this.parseBreakingHeader()
this.parseMentions(input)
this.parseRevert(input)
this.parseScopes()
this.cleanupCommit()

return commit
Expand Down
6 changes: 4 additions & 2 deletions packages/conventional-commits-parser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export const defaultOptions: ParserOptions = {
'resolves',
'resolved'
],
headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/,
headerPattern: /^(\w*)(?:\(([\w$.\-*/\\, ]*)\))?: (.*)$/,
headerCorrespondence: [
'type',
'scope',
'subject'
],
revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
fieldPattern: /^-(.*?)-$/
fieldPattern: /^-(.*?)-$/,
isEnableMultipleScopes: true,
scopeDelimitersPattern: /\/|\\|, ?/g
}
11 changes: 11 additions & 0 deletions packages/conventional-commits-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export interface ParserOptions {
* Keywords to reference an issue. This value is case **insensitive**.
*/
referenceActions?: string[]
/**
* Used to determine whether to separate scopes by slash or comma delimiters contain in scope.
* The default value of isEnableMultipleScopes is true.
*/
isEnableMultipleScopes?: boolean
/**
* RegExp used to separate scopes.
* The default value of scopeDelimitersPattern is `/\/|\\|, ?/g`.
*/
scopeDelimitersPattern?: RegExp
}

export interface ParserStreamOptions extends ParserOptions {
Expand Down Expand Up @@ -99,6 +109,7 @@ export interface CommitBase {
notes: CommitNote[]
mentions: string[]
references: CommitReference[]
scopes: string[]
}

export type Commit = CommitBase & CommitMeta