Skip to content

Commit a5948b3

Browse files
authored
Merge pull request #49 from electron/fix-optionals
Change default optional value for methods and events
2 parents d183c16 + adaba39 commit a5948b3

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if (inFile) {
3838

3939
const typeCheck = () => {
4040
const tscExec = path.resolve(require.resolve('typescript'), '../../bin/tsc')
41-
const tscChild = childProcess.spawn(tscExec, ['--project', 'tsconfig.json'], {
41+
const tscChild = childProcess.spawn('node', [tscExec, '--project', 'tsconfig.json'], {
4242
cwd: path.resolve(__dirname, 'test-smoke/electron')
4343
})
4444
tscChild.stdout.on('data', d => console.log(d.toString()))

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

lib/utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,30 @@ const isEmitter = (module) => {
116116
return true
117117
}
118118
}
119-
const isOptional = (param) => {
119+
const isOptional = (param, defaultReturn) => {
120+
if (typeof defaultReturn === 'undefined') {
121+
defaultReturn = true
122+
}
120123
// Does the description contain the word "optional"?
121124
if (/optional/i.test(param.description)) {
122125
return true
123126
}
124127

125128
// Did we pass a "required"?
126-
if (param.required) {
129+
if (typeof param.required !== 'undefined') {
127130
return !param.required
128131
}
129132

130133
// Does the description not contain the word "required"?
131-
if (param.description && !/required/i.test(param.description)) {
134+
if (param.description && !/required/i.test(param.description) && /\(.+\)/.test(param.description)) {
132135
return true
133136
}
134137

135138
// Let's be optimistic - having optional props marked as required
136139
// ruins the developer experience, why a false negative is only
137140
// slightly annoying
138141
debug(`Could not determine optionality for ${param.name}`)
139-
return true
142+
return defaultReturn
140143
}
141144

142145
const genMethodString = (paramInterfaces, module, moduleMethod, parameters, returns, includeType, paramTypePrefix) => {
@@ -166,7 +169,7 @@ const genMethodString = (paramInterfaces, module, moduleMethod, parameters, retu
166169
}
167170

168171
const name = paramify(param.name)
169-
const optional = isOptional(param) ? '?' : ''
172+
const optional = isOptional(param, false) ? '?' : ''
170173

171174
// Figure out this parameter's type
172175
let type

0 commit comments

Comments
 (0)