Skip to content

Commit 79384b1

Browse files
authored
perf: use optional chaining (#850)
1 parent 4d6c358 commit 79384b1

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

lib/mode/static.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ module.exports = function (fastify, opts, done) {
6464
}
6565

6666
function swagger (opts) {
67-
if (opts && opts.yaml) {
67+
if (opts?.yaml) {
6868
if (cache.swaggerString) return cache.swaggerString
6969
} else {
7070
if (cache.swaggerObject) return cache.swaggerObject
7171
}
7272

73-
if (opts && opts.yaml) {
73+
if (opts?.yaml) {
7474
const swaggerString = yaml.stringify(swaggerObject, { strict: false })
7575
cache.swaggerString = swaggerString
7676
return swaggerString

lib/spec/openapi/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
1010
const defOpts = prepareDefaultOptions(opts)
1111

1212
return function (opts) {
13-
if (opts && opts.yaml) {
13+
if (opts?.yaml) {
1414
if (cache.string) return cache.string
1515
} else {
1616
if (cache.object) return cache.object
@@ -74,7 +74,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
7474
? defOpts.transformObject({ openapiObject })
7575
: openapiObject
7676

77-
if (opts && opts.yaml) {
77+
if (opts?.yaml) {
7878
cache.string = yaml.stringify(transformObjectResult, { strict: false })
7979
return cache.string
8080
}

lib/spec/openapi/utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function schemaToMediaRecursive (schema) {
265265
function resolveBodyParams (body, schema, consumes, ref) {
266266
const resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))
267267

268-
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
268+
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
269269
for (const contentType in schema.content) {
270270
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
271271
}
@@ -279,11 +279,11 @@ function resolveBodyParams (body, schema, consumes, ref) {
279279
body.content[consume] = media
280280
})
281281

282-
if (resolved && resolved.required && resolved.required.length) {
282+
if (resolved?.required?.length) {
283283
body.required = true
284284
}
285285

286-
if (resolved && resolved.description) {
286+
if (resolved?.description) {
287287
body.description = resolved.description
288288
}
289289
}
@@ -294,7 +294,7 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
294294
let resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))
295295

296296
// if the resolved definition is in global schema
297-
if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) {
297+
if (resolved.$ref?.startsWith(schemasPath)) {
298298
const parts = resolved.$ref.split(schemasPath)
299299
const pathParts = parts[1].split('/')
300300
resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions)
@@ -361,7 +361,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
361361

362362
// add schema when type is not 'null'
363363
if (rawJsonSchema.type !== 'null') {
364-
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
364+
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
365365
response.content = resolved.content
366366
} else {
367367
const content = {}
@@ -446,8 +446,8 @@ function prepareOpenapiMethod (schema, ref, openapiObject, url) {
446446

447447
// Parse out the security prop keys to ignore
448448
const securityIgnores = [
449-
...(openapiObject && openapiObject.security ? openapiObject.security : []),
450-
...(schema && schema.security ? schema.security : [])
449+
...(openapiObject?.security || []),
450+
...(schema?.security || [])
451451
]
452452
.reduce((acc, securitySchemeGroup) => {
453453
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {

lib/spec/swagger/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
1010
const defOpts = prepareDefaultOptions(opts)
1111

1212
return function (opts) {
13-
if (opts && opts.yaml) {
13+
if (opts?.yaml) {
1414
if (cache.string) return cache.string
1515
} else {
1616
if (cache.object) return cache.object
@@ -66,7 +66,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
6666
? defOpts.transformObject({ swaggerObject })
6767
: swaggerObject
6868

69-
if (opts && opts.yaml) {
69+
if (opts?.yaml) {
7070
cache.string = yaml.stringify(transformObjectResult, { strict: false })
7171
return cache.string
7272
}

lib/spec/swagger/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function resolveBodyParams (parameters, schema, ref) {
195195
parameters.push({
196196
name: 'body',
197197
in: 'body',
198-
description: resolved && resolved.description ? resolved.description : undefined,
198+
description: resolved?.description,
199199
schema: resolved
200200
})
201201
}
@@ -274,8 +274,8 @@ function prepareSwaggerMethod (schema, ref, swaggerObject, url) {
274274

275275
// Parse out the security prop keys to ignore
276276
const securityIgnores = [
277-
...(swaggerObject && swaggerObject.security ? swaggerObject.security : []),
278-
...(schema && schema.security ? schema.security : [])
277+
...(swaggerObject?.security || []),
278+
...(schema?.security || [])
279279
]
280280
.reduce((acc, securitySchemeGroup) => {
281281
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {

lib/util/should-route-hide.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
function shouldRouteHide (schema, opts) {
44
const { hiddenTag, hideUntagged } = opts
55

6-
if (schema && schema.hide) {
6+
if (schema?.hide) {
77
return true
88
}
99

10-
const tags = (schema && schema.tags) || []
10+
const tags = schema?.tags || []
1111

1212
if (tags.length === 0 && hideUntagged) {
1313
return true

0 commit comments

Comments
 (0)