Skip to content

Commit

Permalink
Perform some minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
start committed Oct 4, 2020
1 parent 520c550 commit 194e71d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 80 deletions.
12 changes: 0 additions & 12 deletions src/Implementation/CollectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,3 @@ export function distinct<T>(...values: T[]): T[] {
: distinctValues.concat([value])
, [] as T[])
}

// Returns the first non-null value in `values`. If `values` does not contain a
// non-null value, this function throws an exception.
export function coalesce<T>(...values: Array<T | undefined>): T {
for (const value of values) {
if (value != null) {
return value
}
}

throw new Error('Every value in `values` was null.')
}
63 changes: 28 additions & 35 deletions src/Implementation/NormalizedSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coalesce, distinct } from './CollectionHelpers'
import { distinct } from './CollectionHelpers'
import { FORWARD_SLASH, HASH_MARK } from './PatternPieces'
import { URL_SCHEME_PATTERN } from './Patterns'
import { Settings } from './Settings'
Expand All @@ -16,7 +16,9 @@ export class NormalizedSettings {
rendering = new NormalizedSettings.Rendering()

constructor(settings?: Settings) {
this.applySettings(settings)
if (settings) {
this.applySettings(settings)
}
}

// Returns a new `NormalizedSettings` object with the changes applied.
Expand All @@ -31,13 +33,14 @@ export class NormalizedSettings {
return clone
}

private applySettings(settings: Settings | undefined): void {
if (!settings) {
return
private applySettings(settings: Settings): void {
if (settings.parsing) {
this.parsing.applySettings(settings.parsing)
}

this.parsing.applySettings(settings.parsing)
this.rendering.applySettings(settings.rendering)
if (settings.rendering) {
this.rendering.applySettings(settings.rendering)
}
}
}

Expand Down Expand Up @@ -65,25 +68,21 @@ export namespace NormalizedSettings {
return clone
}

applySettings(settings: Settings.Parsing | undefined): void {
if (!settings) {
return
}

applySettings(settings: Settings.Parsing): void {
this.createSourceMap =
coalesce(settings.createSourceMap, this.createSourceMap)
settings.createSourceMap ?? this.createSourceMap

this.fancyEllipsis =
coalesce(settings.fancyEllipsis, this.fancyEllipsis)
settings.fancyEllipsis ?? this.fancyEllipsis

this.defaultUrlScheme =
coalesce(settings.defaultUrlScheme, this.defaultUrlScheme)
settings.defaultUrlScheme ?? this.defaultUrlScheme

this.baseForUrlsStartingWithSlash =
coalesce(settings.baseForUrlsStartingWithSlash, this.baseForUrlsStartingWithSlash)
settings.baseForUrlsStartingWithSlash ?? this.baseForUrlsStartingWithSlash

this.baseForUrlsStartingWithHashMark =
coalesce(settings.baseForUrlsStartingWithHashMark, this.baseForUrlsStartingWithHashMark)
settings.baseForUrlsStartingWithHashMark ?? this.baseForUrlsStartingWithHashMark

this.keywords.applySettings(settings.keywords)
}
Expand Down Expand Up @@ -208,18 +207,16 @@ export namespace NormalizedSettings {
return clone
}

applySettings(settings: Settings.Rendering | undefined): void {
if (!settings) {
return
}

applySettings(settings: Settings.Rendering): void {
this.idPrefix =
coalesce(settings.idPrefix, this.idPrefix)
settings.idPrefix ?? this.idPrefix

this.renderDangerousContent =
coalesce(settings.renderDangerousContent, this.renderDangerousContent)
settings.renderDangerousContent ?? this.renderDangerousContent

this.terms.applySettings(settings.terms)
if (settings.terms) {
this.terms.applySettings(settings.terms)
}
}
}

Expand All @@ -244,25 +241,21 @@ export namespace NormalizedSettings {
return clone
}

applySettings(terms: Settings.Rendering.Terms | undefined): void {
if (!terms) {
return
}

applySettings(terms: Settings.Rendering.Terms): void {
this.footnote =
coalesce(terms.footnote, this.footnote)
terms.footnote ?? this.footnote

this.footnoteReference =
coalesce(terms.footnoteReference, this.footnoteReference)
terms.footnoteReference ?? this.footnoteReference

this.hide =
coalesce(terms.hide, this.hide)
terms.hide ?? this.hide

this.reveal =
coalesce(terms.reveal, this.reveal)
terms.reveal ?? this.reveal

this.sectionReferencedByTableOfContents =
coalesce(terms.sectionReferencedByTableOfContents, this.sectionReferencedByTableOfContents)
terms.sectionReferencedByTableOfContents ?? this.sectionReferencedByTableOfContents
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ForgivingConventionHandler {
isPerfectMatch(startDelimiter.remainingLength, endDelimiterLength)

const perfectStartDelimiter =
first(this.openStartDelimiters, isDelimiterPerfectMatch)
this.openStartDelimiters.find(isDelimiterPerfectMatch)

if (perfectStartDelimiter) {
const lengthInCommon =
Expand Down Expand Up @@ -128,14 +128,3 @@ export class ForgivingConventionHandler {
return this.startDelimiters.filter(delimiter => !delimiter.isFullyExhausted)
}
}


function first<T>(items: T[], predicate: (item: T) => boolean): T | undefined {
for (const item of items) {
if (predicate(item)) {
return item
}
}

return undefined
}
2 changes: 1 addition & 1 deletion src/Implementation/Parsing/Inline/Tokenizing/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Tokenizer {

...(
// If we're tokenizing an inline document...
(options != null) && options.isTokenizingInlineDocument
options?.isTokenizingInlineDocument
// We'll treat footnotes differently, because they don't really make sense in an inline document
? this.getFootnoteConventionsForInlineDocuments()
// Otherwise, if we're tokenizing a regular document...
Expand Down
30 changes: 10 additions & 20 deletions src/Implementation/Parsing/Inline/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ function parseAndGetResult(
let tokenIndex = 0
const nodes: InlineSyntaxNode[] = []

function countTokensParsed(): number {
return tokenIndex + 1
}
const countTokensParsed = () => tokenIndex + 1

function consumeChildren(args: { fromHereUntil: TokenRole }): InlineSyntaxNode[] {
const result = parseAndGetResult({
Expand All @@ -56,40 +54,32 @@ function parseAndGetResult(
// Not all tokens have a value, so the `value` field of `ParseableToken` can
// be undefined. However, tokens of certain `role`s will never have always
// have a value, which we rely upon below.
//
// TODO: Implement a more type-safe solution than casting `string | undefined`
// to `string`.
const tokenValue = token.value as string
const tokenValue = token.value!

switch (token.role) {
case until: {
case until:
break TokenLoop
}

case TokenRole.Text: {
case TokenRole.Text:
nodes.push(new Text(tokenValue))
continue
}

case TokenRole.InlineCode: {
case TokenRole.InlineCode:
nodes.push(new InlineCode(tokenValue))
continue
}

case TokenRole.ExampleUserInput: {
case TokenRole.ExampleUserInput:
nodes.push(new ExampleUserInput(tokenValue))
continue
}

case TokenRole.SectionLink: {
case TokenRole.SectionLink:
nodes.push(new SectionLink(tokenValue))
continue
}

case TokenRole.BareUrl: {
const url = tokenValue

const [urlScheme] = URL_SCHEME_PATTERN.exec(url) as string[]
const [urlScheme] = URL_SCHEME_PATTERN.exec(url)!
const urlAfterScheme = url.substr(urlScheme.length)

nodes.push(new LINK.SyntaxNodeType([new Text(urlAfterScheme)], url))
Expand All @@ -103,7 +93,7 @@ function parseAndGetResult(
})

// Our link's URL was in the `LinkEndAndUrl `token, the last token we parsed.
const url = (tokens[tokenIndex].value as string).trim()
const url = (tokens[tokenIndex].value!).trim()

if (children.every(isWhitespace)) {
// As a rule, if link has blank content, we use its URL as its content.
Expand All @@ -122,7 +112,7 @@ function parseAndGetResult(
// The next token will always be a `MediaEndAndUrl` token. All media conventions
// use the same role for their end tokens.
const urlToken = tokens[++tokenIndex]
const url = (urlToken.value as string).trim()
const url = urlToken.value!.trim()

nodes.push(new media.SyntaxNodeType(description || url, url))
continue TokenLoop
Expand Down

0 comments on commit 194e71d

Please sign in to comment.