Skip to content

Commit

Permalink
style: Prefer nullish coalescing ?? over ||
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 3, 2022
1 parent cfde57e commit da4568d
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rules:
'@typescript-eslint/no-unsafe-member-access': off
'@typescript-eslint/no-unused-vars': [warn, argsIgnorePattern: '^_']
'@typescript-eslint/prefer-includes': warn
'@typescript-eslint/prefer-nullish-coalescing': warn
'@typescript-eslint/prefer-optional-chain': warn
'@typescript-eslint/restrict-template-expressions': off

Expand Down
2 changes: 1 addition & 1 deletion src/compose/compose-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function composeDoc(
}
const props = resolveProps(start, {
indicator: 'doc-start',
next: value || end?.[0],
next: value ?? end?.[0],
offset,
onError,
startOnNewline: true
Expand Down
6 changes: 3 additions & 3 deletions src/compose/compose-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export function composeScalar(
try {
const res = tag.resolve(
value,
msg => onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg),
msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg),
ctx.options
)
scalar = isScalar(res) ? res : new Scalar(res)
} catch (error) {
const msg = error instanceof Error ? error.message : String(error)
onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg)
onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg)
scalar = new Scalar(value)
}
scalar.range = range
Expand Down Expand Up @@ -99,7 +99,7 @@ function findScalarTagByTest(

if (schema.compat) {
const compat =
schema.compat.find(tag => tag.default && tag.test?.test(value)) ||
schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
schema[SCALAR]
if (tag.tag !== compat.tag) {
const ts = directives.tagString(tag.tag)
Expand Down
1 change: 1 addition & 0 deletions src/compose/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class Composer {
private warnings: YAMLWarning[] = []

constructor(options: ParseOptions & DocumentOptions & SchemaOptions = {}) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.directives = new Directives({ version: options.version || '1.2' })
this.options = options
}
Expand Down
4 changes: 2 additions & 2 deletions src/compose/resolve-block-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function resolveBlockMap(
// key properties
const keyProps = resolveProps(start, {
indicator: 'explicit-key-ind',
next: key || sep?.[0],
next: key ?? sep?.[0],
offset,
onError,
startOnNewline: true
Expand Down Expand Up @@ -72,7 +72,7 @@ export function resolveBlockMap(
onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique')

// value properties
const valueProps = resolveProps(sep || [], {
const valueProps = resolveProps(sep ?? [], {
indicator: 'map-value-ind',
next: value,
offset: keyNode.range[2],
Expand Down
6 changes: 3 additions & 3 deletions src/compose/resolve-flow-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function resolveFlowCollection(
const props = resolveProps(start, {
flow: fcName,
indicator: 'explicit-key-ind',
next: key || sep?.[0],
next: key ?? sep?.[0],
offset,
onError,
startOnNewline: false
Expand Down Expand Up @@ -92,7 +92,7 @@ export function resolveFlowCollection(
}
if (prevItemComment) {
let prev = coll.items[coll.items.length - 1]
if (isPair(prev)) prev = prev.value || prev.key
if (isPair(prev)) prev = prev.value ?? prev.key
if (prev.comment) prev.comment += '\n' + prevItemComment
else prev.comment = prevItemComment
props.comment = props.comment.substring(prevItemComment.length + 1)
Expand Down Expand Up @@ -120,7 +120,7 @@ export function resolveFlowCollection(
if (isBlock(key)) onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg)

// value properties
const valueProps = resolveProps(sep || [], {
const valueProps = resolveProps(sep ?? [], {
flow: fcName,
indicator: 'map-value-ind',
next: value,
Expand Down
4 changes: 2 additions & 2 deletions src/compose/resolve-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SourceToken, Token } from '../parse/cst.js'
import type { ComposeErrorHandler } from './composer.js'

export interface ResolvePropsArg {
flow?: string
flow?: 'flow map' | 'flow sequence'
indicator: 'doc-start' | 'explicit-key-ind' | 'map-value-ind' | 'seq-item-ind'
next: Token | null | undefined
offset: number
Expand Down Expand Up @@ -119,7 +119,7 @@ export function resolveProps(
onError(
token,
'UNEXPECTED_TOKEN',
`Unexpected ${token.source} in ${flow || 'collection'}`
`Unexpected ${token.source} in ${flow ?? 'collection'}`
)
found = token
atNewline = false
Expand Down
6 changes: 4 additions & 2 deletions src/doc/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class Document<T = unknown> {
if (!node.anchor) {
const prev = anchorNames(this)
node.anchor =
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
!name || prev.has(name) ? findNewAnchor(name || 'a', prev) : name
}
return new Alias(node.anchor)
Expand Down Expand Up @@ -224,9 +225,10 @@ export class Document<T = unknown> {
keepUndefined,
onTagObj,
tag
} = options || {}
} = options ?? {}
const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(
this,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
anchorPrefix || 'a'
)
const ctx: CreateNodeContext = {
Expand Down Expand Up @@ -423,7 +425,7 @@ export class Document<T = unknown> {
maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100,
stringify
}
const res = toJS(this.contents, jsonArg || '', ctx)
const res = toJS(this.contents, jsonArg ?? '', ctx)
if (typeof onAnchor === 'function')
for (const { count, res } of ctx.anchors.values()) onAnchor(res, count)
return typeof reviver === 'function'
Expand Down
2 changes: 1 addition & 1 deletion src/doc/createNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function findTagObject(
) {
if (tagName) {
const match = tags.filter(t => t.tag === tagName)
const tagObj = match.find(t => !t.format) || match[0]
const tagObj = match.find(t => !t.format) ?? match[0]
if (!tagObj) throw new Error(`Tag ${tagName} not found`)
return tagObj
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class Lexer {
this.lineEndPos = null
}
this.atEnd = !incomplete
let next: State | null = this.next || 'stream'
let next: State | null = this.next ?? 'stream'
while (next && (incomplete || this.hasChars(1)))
next = yield* this.parseNext(next)
}
Expand Down
4 changes: 2 additions & 2 deletions src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getPrevProps(parent: Token) {
return parent.start
case 'block-map': {
const it = parent.items[parent.items.length - 1]
return it.sep || it.start
return it.sep ?? it.start
}
case 'block-seq':
return parent.items[parent.items.length - 1].start
Expand Down Expand Up @@ -291,7 +291,7 @@ export class Parser {
}

private *pop(error?: Token): Generator<Token, void> {
const token = error || this.stack.pop()
const token = error ?? this.stack.pop()
/* istanbul ignore if should not happen */
if (!token) {
const message = 'Tried to pop an empty stack'
Expand Down
2 changes: 1 addition & 1 deletion src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function stringify(
options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent }
}
if (value === undefined) {
const { keepUndefined } = options || (replacer as CreateNodeOptions) || {}
const { keepUndefined } = options ?? (replacer as CreateNodeOptions) ?? {}
if (!keepUndefined) return undefined
}
return new Document(value, _replacer, options).toString(options)
Expand Down
8 changes: 6 additions & 2 deletions src/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ export class Schema {
this.name = (typeof schema === 'string' && schema) || 'core'
this.knownTags = resolveKnownTags ? coreKnownTags : {}
this.tags = getTags(customTags, this.name)
this.toStringOptions = toStringDefaults || null
this.toStringOptions = toStringDefaults ?? null

Object.defineProperty(this, MAP, { value: map })
Object.defineProperty(this, SCALAR, { value: string })
Object.defineProperty(this, SEQ, { value: seq })

// Used by createMap()
this.sortMapEntries =
sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null
typeof sortMapEntries === 'function'
? sortMapEntries
: sortMapEntries === true
? sortMapEntriesByKey
: null
}

clone(): Schema {
Expand Down
2 changes: 1 addition & 1 deletion src/schema/yaml-1.1/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function resolvePairs(
? `${item.commentBefore}\n${pair.key.commentBefore}`
: item.commentBefore
if (item.comment) {
const cn = pair.value || pair.key
const cn = pair.value ?? pair.key
cn.comment = cn.comment
? `${item.comment}\n${cn.comment}`
: item.comment
Expand Down
8 changes: 4 additions & 4 deletions src/stringify/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function getTagObject(tags: Array<ScalarTag | CollectionTag>, item: Node) {
if (item.tag) {
const match = tags.filter(t => t.tag === item.tag)
if (match.length > 0)
return match.find(t => t.format === (item as Scalar).format) || match[0]
return match.find(t => t.format === (item as Scalar).format) ?? match[0]
}

let tagObj: ScalarTag | CollectionTag | undefined = undefined
Expand All @@ -95,7 +95,7 @@ function getTagObject(tags: Array<ScalarTag | CollectionTag>, item: Node) {
obj = item.value
const match = tags.filter(t => t.identify?.(obj))
tagObj =
match.find(t => t.format === item.format) || match.find(t => !t.format)
match.find(t => t.format === item.format) ?? match.find(t => !t.format)
} else {
obj = item
tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass)
Expand All @@ -121,7 +121,7 @@ function stringifyProps(
anchors.add(anchor)
props.push(`&${anchor}`)
}
const tag = node.tag || (tagObj.default ? null : tagObj.tag)
const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag
if (tag) props.push(doc.directives.tagString(tag))
return props.join(' ')
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export function stringify(

const props = stringifyProps(node, tagObj, ctx)
if (props.length > 0)
ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1
ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1

const str =
typeof tagObj.stringify === 'function'
Expand Down
2 changes: 1 addition & 1 deletion src/test-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function testEvents(src: string) {
events.push(docEnd)
}
} catch (e) {
return { events, error: error || e }
return { events, error: error ?? e }
}
events.push('-STR')
return { events, error }
Expand Down

0 comments on commit da4568d

Please sign in to comment.