Skip to content

Commit 3ac4950

Browse files
authored
Merge pull request #52 from poiru/fix-is-optional
Fix utils.isOptional()
2 parents bbbc9db + f0bab89 commit 3ac4950

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

lib/module-declaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const generateModuleDeclaration = (module, index, API) => {
6666
newType = utils.genMethodString(paramInterfaces, module, eventListenerArg, eventListenerArg.parameters, null, true)
6767
}
6868

69-
args.push(`${argString}${utils.paramify(eventListenerArg.name)}${utils.isOptional(eventListenerArg, false) ? '?' : ''}: ${newType}`)
69+
args.push(`${argString}${utils.paramify(eventListenerArg.name)}${utils.isOptional(eventListenerArg) ? '?' : ''}: ${newType}`)
7070
})
7171
listener = `(${args.join(`,\n${indent}`)}) => void`
7272
}

lib/utils.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,16 @@ const isEmitter = (module) => {
116116
return true
117117
}
118118
}
119-
const isOptional = (param, defaultReturn) => {
120-
if (typeof defaultReturn === 'undefined') {
121-
defaultReturn = true
122-
}
123-
// Does the description contain the word "optional"?
124-
if (/optional/i.test(param.description)) {
125-
return true
126-
}
127-
119+
const isOptional = (param) => {
128120
// Did we pass a "required"?
129121
if (typeof param.required !== 'undefined') {
130122
return !param.required
131123
}
132124

133-
// Does the description not contain the word "required"?
134-
if (param.description && !/required/i.test(param.description) && /\(.+\)/.test(param.description)) {
135-
return true
136-
}
137-
138-
// Let's be optimistic - having optional props marked as required
139-
// ruins the developer experience, why a false negative is only
140-
// slightly annoying
125+
// Assume that methods are never optional because electron-docs-linter
126+
// doesn't currently mark them as required.
141127
debug(`Could not determine optionality for ${param.name}`)
142-
return defaultReturn
128+
return param.type !== 'Function'
143129
}
144130

145131
const genMethodString = (paramInterfaces, module, moduleMethod, parameters, returns, includeType, paramTypePrefix) => {

test/utils_spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ describe('utils', () => {
100100
})
101101

102102
describe('isOptional', () => {
103-
it('should return false for an empty description', () => {
104-
expect(utils.isOptional({ description: '', required: true })).to.eq(false)
103+
it('should return true if param is not required', () => {
104+
expect(utils.isOptional({})).to.eq(true)
105105
})
106106

107-
it('should return true if optional is in the description', () => {
108-
expect(utils.isOptional({ description: 'This param is completely optional' })).to.eq(true)
107+
it('should return false if param is required', () => {
108+
expect(utils.isOptional({ required: true })).to.eq(false)
109109
})
110110

111-
it('should return true if the description has brackets and is not required', () => {
112-
expect(utils.isOptional({ description: '(not needed) - This thing' })).to.eq(true)
111+
it('should default to true if param is a non-function', () => {
112+
expect(utils.isOptional({ type: 'Foo' })).to.eq(true)
113113
})
114114

115-
it('should return false if the description has brackets but is required', () => {
116-
expect(utils.isOptional({ description: '(not needed) - This thing is required', required: true })).to.eq(false)
115+
it('should default to false if param is a function', () => {
116+
expect(utils.isOptional({ type: 'Function' })).to.eq(false)
117117
})
118118
})
119119
})

0 commit comments

Comments
 (0)