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

fix: avoid overwriting the default values of options with undefined #1018

Conversation

ryamaguchi0220
Copy link
Contributor

@ryamaguchi0220 ryamaguchi0220 commented Jun 23, 2023

The default values of options (append, releaseCount, skipUnstable...) appear to be not working as intended.

If releaseCount option is not specified, the options object initially has the property releaseCount: undefined, and then the default value releaseCount: 1 looks like to be assigned, but actually releaseCount: undefined is assigned again due to merging ...options (which has releaseCount: undefined) with the default values.

options = {
append: false,
releaseCount: 1,
skipUnstable: false,
debug: function () {},
transform: function (commit, cb) {
if (typeof commit.gitTags === 'string') {
const match = rtag.exec(commit.gitTags)
rtag.lastIndex = 0
if (match) {
commit.version = match[1]
}
}
if (commit.committerDate) {
commit.committerDate = dateFormat(commit.committerDate, 'yyyy-mm-dd', true)
}
cb(null, commit)
},
lernaPackage: null,
...options,
pkg: {
transform: function (pkg) {
return pkg
},
...options?.pkg
}
}

This problem has been introduced by #959.

async function mergeConfig (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
let configPromise
let pkgPromise

options = omitUndefinedValueProps(options)
Copy link
Contributor Author

@ryamaguchi0220 ryamaguchi0220 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here omits props whose value is undefined to avoid overwriting the default values with undefined.

@ryamaguchi0220
Copy link
Contributor Author

@dangreen I would appreciate your review 🙏

@dangreen dangreen self-assigned this Jun 25, 2023
@dangreen dangreen self-requested a review June 25, 2023 12:45
Copy link
Member

@dangreen dangreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Please fix CI errors and provide tests for your changes.

@ryamaguchi0220 ryamaguchi0220 force-pushed the fix/avoid-overwriting-default-values-of-options branch from f1f5a1f to c202a18 Compare June 25, 2023 13:28
@coveralls
Copy link

coveralls commented Jun 25, 2023

Coverage Status

coverage: 94.94% (+0.02%) from 94.924% when pulling ddf323b on ryamaguchi0220:fix/avoid-overwriting-default-values-of-options into 11195f2 on conventional-changelog:master.

@ryamaguchi0220
Copy link
Contributor Author

ryamaguchi0220 commented Jun 26, 2023

@dangreen Thanks for your review!
I added the tests for merge-config 229622c

@@ -0,0 +1,61 @@
'use strict'
Copy link
Contributor Author

@ryamaguchi0220 ryamaguchi0220 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test file names (merge-config.spec.js and index.spec.js) are based on other packages such as conventional-commits-parser, conventional-changelog-writer.

expect(options).to.include(defaultOptions)
})

it('should return default options when undefined value is passed', async function () {
Copy link
Contributor Author

@ryamaguchi0220 ryamaguchi0220 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If props whose value is undefined are not omitted, this test will fail since the default values will be overwritten with undefined.

@ryamaguchi0220 ryamaguchi0220 force-pushed the fix/avoid-overwriting-default-values-of-options branch from 095c28a to 229622c Compare June 26, 2023 05:55
Comment on lines 56 to 63
function omitUndefinedValueProps (obj) {
if (obj === null || typeof obj !== 'object') {
return {}
}
const omittedObj = Object.entries(obj).filter(([, value]) => value !== undefined)
return Object.fromEntries(omittedObj)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's optimize it to one loop

Suggested change
function omitUndefinedValueProps (obj) {
if (obj === null || typeof obj !== 'object') {
return {}
}
const omittedObj = Object.entries(obj).filter(([, value]) => value !== undefined)
return Object.fromEntries(omittedObj)
}
function omitUndefinedValueProps (obj) {
if (!obj) {
return {}
}
const omittedObj = {}
for (const key in obj) {
if (obj[key] !== undefined) {
omittedObj[key] = obj[key]
}
}
return omittedObj
}

Copy link
Contributor Author

@ryamaguchi0220 ryamaguchi0220 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimized in ddf323b

@ryamaguchi0220 ryamaguchi0220 force-pushed the fix/avoid-overwriting-default-values-of-options branch from 8f118d5 to ddf323b Compare June 27, 2023 06:43
@dangreen dangreen merged commit 71b0c40 into conventional-changelog:master Jun 28, 2023
5 checks passed
@ryamaguchi0220 ryamaguchi0220 deleted the fix/avoid-overwriting-default-values-of-options branch June 29, 2023 04:19
@ryamaguchi0220
Copy link
Contributor Author

@dangreen Thanks for the quick release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants