feat: do not write undefined fields to config files (#3643)

closes #3058
This commit is contained in:
Haoqun Jiang
2019-04-09 00:49:12 +08:00
committed by GitHub
parent a02ef3988f
commit cb113971e2
2 changed files with 35 additions and 1 deletions

View File

@@ -79,3 +79,29 @@ module.exports = {
}`
)
})
test(`add a new undefined property`, () => {
const value = {
foo: undefined
}
const source = `module.exports = {
bar: 123
}`
expect(extend(value, source)).toMatch(source)
})
test(`change an existing property to undefined`, () => {
const value = {
foo: undefined
}
const source = `module.exports = {
foo: 123,
bar: 456
}`
expect(extend(value, source)).toMatch(
`module.exports = {
bar: 456
}`
)
})

View File

@@ -45,13 +45,21 @@ module.exports = function extendJSConfig (value, source) {
const props = valueAST.program.body[0].expression.properties
const existingProps = node.properties
for (const prop of props) {
const isUndefinedProp =
prop.value.type === 'Identifier' && prop.value.name === 'undefined'
const existing = existingProps.findIndex(p => {
return !p.computed && p.key.name === prop.key.name
})
if (existing > -1) {
// replace
existingProps[existing].value = prop.value
} else {
// remove `undefined` props
if (isUndefinedProp) {
existingProps.splice(existing, 1)
}
} else if (!isUndefinedProp) {
// append
existingProps.push(prop)
}