Skip to content

Commit

Permalink
style: Rename type arguments to be consistent and less terse
Browse files Browse the repository at this point in the history
  • Loading branch information
golergka committed Apr 22, 2022
1 parent c0db02a commit c2254de
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 91 deletions.
34 changes: 17 additions & 17 deletions src/doc/Document.ts
Expand Up @@ -36,13 +36,13 @@ import { Directives } from './directives.js'
export type Replacer = any[] | ((key: any, value: any) => unknown)

export declare namespace Document {
interface Parsed<T extends ParsedNode = ParsedNode> extends Document<T> {
interface Parsed<Value extends ParsedNode = ParsedNode> extends Document<Value> {
directives: Directives
range: Range
}
}

export class Document<T extends Node = Node> {
export class Document<Value extends Node = Node> {
readonly [NODE_TYPE]: symbol

/** A comment before this Document */
Expand All @@ -52,7 +52,7 @@ export class Document<T extends Node = Node> {
comment: string | null = null

/** The document contents. */
contents: T | null
contents: Value | null

directives?: Directives

Expand Down Expand Up @@ -133,7 +133,7 @@ export class Document<T extends Node = Node> {

if (value === undefined) this.contents = null
else {
this.contents = this.createNode(value, _replacer, options) as unknown as T
this.contents = this.createNode(value, _replacer, options) as unknown as Value
}
}

Expand All @@ -142,8 +142,8 @@ export class Document<T extends Node = Node> {
*
* Custom Node values that inherit from `Object` still refer to their original instances.
*/
clone(): Document<T> {
const copy: Document<T> = Object.create(Document.prototype, {
clone(): Document<Value> {
const copy: Document<Value> = Object.create(Document.prototype, {
[NODE_TYPE]: { value: DOC }
})
copy.commentBefore = this.commentBefore
Expand All @@ -154,7 +154,7 @@ export class Document<T extends Node = Node> {
if (this.directives) copy.directives = this.directives.clone()
copy.schema = this.schema.clone()
copy.contents = isNode(this.contents)
? (this.contents.clone(copy.schema) as unknown as T)
? (this.contents.clone(copy.schema) as unknown as Value)
: this.contents
if (this.range) copy.range = this.range.slice() as Document['range']
return copy
Expand Down Expand Up @@ -193,12 +193,12 @@ export class Document<T extends Node = Node> {
* Convert any value into a `Node` using the current schema, recursively
* turning objects into collections.
*/
createNode<T = unknown>(value: T, options?: CreateNodeOptions): NodeType<T>
createNode<T = unknown>(
value: T,
createNode<Value = unknown>(value: Value, options?: CreateNodeOptions): NodeType<Value>
createNode<Value = unknown>(
value: Value,
replacer: Replacer | CreateNodeOptions | null,
options?: CreateNodeOptions
): NodeType<T>
): NodeType<Value>
createNode(
value: unknown,
replacer?: Replacer | CreateNodeOptions | null,
Expand Down Expand Up @@ -251,13 +251,13 @@ export class Document<T extends Node = Node> {
* Convert a key and a value into a `Pair` using the current schema,
* recursively wrapping all values as `Scalar` or `Collection` nodes.
*/
createPair<K extends Node = Node, V extends Node = Node>(
createPair<Key extends Node = Node, Value extends Node = Node>(
key: unknown,
value: unknown,
options: CreateNodeOptions = {}
) {
const k = this.createNode(key, null, options) as K
const v = this.createNode(value, null, options) as V
const k = this.createNode(key, null, options) as Key
const v = this.createNode(value, null, options) as Value
return new Pair(k, v)
}

Expand Down Expand Up @@ -335,7 +335,7 @@ export class Document<T extends Node = Node> {
this.schema,
[key],
value
) as unknown as T
) as unknown as Value
} else if (assertCollection(this.contents)) {
this.contents.set(key, value)
}
Expand All @@ -346,13 +346,13 @@ export class Document<T extends Node = Node> {
* boolean to add/remove the item from the set.
*/
setIn(path: Iterable<unknown> | null, value: unknown): void {
if (isEmptyPath(path)) this.contents = value as T
if (isEmptyPath(path)) this.contents = value as Value
else if (this.contents == null) {
this.contents = collectionFromPath(
this.schema,
Array.from(path),
value
) as unknown as T
) as unknown as Value
} else if (assertCollection(this.contents)) {
this.contents.setIn(path, value)
}
Expand Down
46 changes: 23 additions & 23 deletions src/nodes/Node.ts
Expand Up @@ -7,19 +7,19 @@ import type { Scalar } from './Scalar.js'
import type { YAMLMap } from './YAMLMap.js'
import type { YAMLSeq } from './YAMLSeq.js'

export type Node<T = unknown> =
export type Node<Value = unknown> =
| Alias
| Scalar<T>
| YAMLMap<unknown, T>
| YAMLSeq<T>
| Scalar<Value>
| YAMLMap<unknown, Value>
| YAMLSeq<Value>

/** Utility type mapper */
export type NodeType<T> = T extends string | number | bigint | boolean | null
? Scalar<T>
: T extends Array<any>
? YAMLSeq<NodeType<T[number]>>
: T extends { [key: string | number]: any }
? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>>
export type NodeType<Value> = Value extends string | number | bigint | boolean | null
? Scalar<Value>
: Value extends Array<any>
? YAMLSeq<NodeType<Value[number]>>
: Value extends { [key: string | number]: any }
? YAMLMap<NodeType<keyof Value>, NodeType<Value[keyof Value]>>
: Node

export type ParsedNode =
Expand All @@ -41,30 +41,30 @@ export const NODE_TYPE = Symbol.for('yaml.node.type')
export const isAlias = (node: any): node is Alias =>
!!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS

export const isDocument = <T extends Node = Node>(
export const isDocument = <Value extends Node = Node>(
node: any
): node is Document<T> =>
): node is Document<Value> =>
!!node && typeof node === 'object' && node[NODE_TYPE] === DOC

export const isMap = <K = unknown, V = unknown>(
export const isMap = <Key = unknown, Value = unknown>(
node: any
): node is YAMLMap<K, V> =>
): node is YAMLMap<Key, Value> =>
!!node && typeof node === 'object' && node[NODE_TYPE] === MAP

export const isPair = <K = unknown, V = unknown>(
export const isPair = <Key = unknown, Value = unknown>(
node: any
): node is Pair<K, V> =>
): node is Pair<Key, Value> =>
!!node && typeof node === 'object' && node[NODE_TYPE] === PAIR

export const isScalar = <T = unknown>(node: any): node is Scalar<T> =>
export const isScalar = <Value = unknown>(node: any): node is Scalar<Value> =>
!!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR

export const isSeq = <T = unknown>(node: any): node is YAMLSeq<T> =>
export const isSeq = <Value = unknown>(node: any): node is YAMLSeq<Value> =>
!!node && typeof node === 'object' && node[NODE_TYPE] === SEQ

export function isCollection<K = unknown, V = unknown>(
export function isCollection<Key = unknown, Value = unknown>(
node: any
): node is YAMLMap<K, V> | YAMLSeq<V> {
): node is YAMLMap<Key, Value> | YAMLSeq<Value> {
if (node && typeof node === 'object')
switch (node[NODE_TYPE]) {
case MAP:
Expand All @@ -74,7 +74,7 @@ export function isCollection<K = unknown, V = unknown>(
return false
}

export function isNode<T = unknown>(node: any): node is Node<T> {
export function isNode<Value = unknown>(node: any): node is Node<Value> {
if (node && typeof node === 'object')
switch (node[NODE_TYPE]) {
case ALIAS:
Expand All @@ -86,9 +86,9 @@ export function isNode<T = unknown>(node: any): node is Node<T> {
return false
}

export const hasAnchor = <K = unknown, V = unknown>(
export const hasAnchor = <Key = unknown, Value = unknown>(
node: unknown
): node is Scalar<V> | YAMLMap<K, V> | YAMLSeq<V> =>
): node is Scalar<Value> | YAMLMap<Key, Value> | YAMLSeq<Value> =>
(isScalar(node) || isCollection(node)) && !!node.anchor

export abstract class NodeBase {
Expand Down
14 changes: 7 additions & 7 deletions src/nodes/Pair.ts
Expand Up @@ -17,28 +17,28 @@ export function createPair(
return new Pair(k, v)
}

export class Pair<K = unknown, V = unknown> {
export class Pair<Key = unknown, Value = unknown> {
readonly [NODE_TYPE]: symbol

/** Always Node or null when parsed, but can be set to anything. */
key: K
key: Key

/** Always Node or null when parsed, but can be set to anything. */
value: V | null
value: Value | null

/** The CST token that was composed into this pair. */
declare srcToken?: CollectionItem

constructor(key: K, value: V | null = null) {
constructor(key: Key, value: Value | null = null) {
Object.defineProperty(this, NODE_TYPE, { value: PAIR })
this.key = key
this.value = value
}

clone(schema?: Schema): Pair<K, V> {
clone(schema?: Schema): Pair<Key, Value> {
let { key, value } = this
if (isNode(key)) key = key.clone(schema) as unknown as K
if (isNode(value)) value = value.clone(schema) as unknown as V
if (isNode(key)) key = key.clone(schema) as unknown as Key
if (isNode(value)) value = value.clone(schema) as unknown as Value
return new Pair(key, value)
}

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Scalar.ts
Expand Up @@ -21,14 +21,14 @@ export declare namespace Scalar {
type Type = BLOCK_FOLDED | BLOCK_LITERAL | PLAIN | QUOTE_DOUBLE | QUOTE_SINGLE
}

export class Scalar<T = unknown> extends NodeBase {
export class Scalar<Value = unknown> extends NodeBase {
static readonly BLOCK_FOLDED = 'BLOCK_FOLDED'
static readonly BLOCK_LITERAL = 'BLOCK_LITERAL'
static readonly PLAIN = 'PLAIN'
static readonly QUOTE_DOUBLE = 'QUOTE_DOUBLE'
static readonly QUOTE_SINGLE = 'QUOTE_SINGLE'

value: T
value: Value

/** An optional anchor on this node. Used by alias nodes. */
declare anchor?: string
Expand All @@ -49,7 +49,7 @@ export class Scalar<T = unknown> extends NodeBase {
/** The scalar style used for the node's string representation */
declare type?: Scalar.Type

constructor(value: T) {
constructor(value: Value) {
super(SCALAR)
this.value = value
}
Expand Down
30 changes: 15 additions & 15 deletions src/nodes/YAMLMap.ts
Expand Up @@ -9,8 +9,8 @@ import { Pair } from './Pair.js'
import { isScalarValue, Scalar } from './Scalar.js'
import type { ToJSContext } from './toJS.js'

export function findPair<K = unknown, V = unknown>(
items: Iterable<Pair<K, V>>,
export function findPair<Key = unknown, Value = unknown>(
items: Iterable<Pair<Key, Value>>,
key: unknown
) {
const k = isScalar(key) ? key.value : key
Expand All @@ -25,21 +25,21 @@ export function findPair<K = unknown, V = unknown>(

export declare namespace YAMLMap {
interface Parsed<
K extends ParsedNode = ParsedNode,
V extends ParsedNode | null = ParsedNode | null
> extends YAMLMap<K, V> {
items: Pair<K, V>[]
Key extends ParsedNode = ParsedNode,
Value extends ParsedNode | null = ParsedNode | null
> extends YAMLMap<Key, Value> {
items: Pair<Key, Value>[]
range: Range
srcToken?: BlockMap | FlowCollection
}
}

export class YAMLMap<K = unknown, V = unknown> extends Collection {
export class YAMLMap<Key = unknown, Value = unknown> extends Collection {
static get tagName(): 'tag:yaml.org,2002:map' {
return 'tag:yaml.org,2002:map'
}

items: Pair<K, V>[] = []
items: Pair<Key, Value>[] = []

constructor(schema?: Schema) {
super(MAP, schema)
Expand All @@ -51,12 +51,12 @@ export class YAMLMap<K = unknown, V = unknown> extends Collection {
* @param overwrite - If not set `true`, using a key that is already in the
* collection will throw. Otherwise, overwrites the previous value.
*/
add(pair: Pair<K, V> | { key: K; value: V }, overwrite?: boolean): void {
let _pair: Pair<K, V>
add(pair: Pair<Key, Value> | { key: Key; value: Value }, overwrite?: boolean): void {
let _pair: Pair<Key, Value>
if (isPair(pair)) _pair = pair
else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
// In TypeScript, this never happens.
_pair = new Pair<K, V>(pair as any, (pair as any)?.value)
_pair = new Pair<Key, Value>(pair as any, (pair as any)?.value)
} else _pair = new Pair(pair.key, pair.value)

const prev = findPair(this.items, _pair.key)
Expand All @@ -83,9 +83,9 @@ export class YAMLMap<K = unknown, V = unknown> extends Collection {
return del.length > 0
}

get<T extends V = V>(key: unknown, keepScalar: true): Scalar<T> | undefined
get<T extends V = V>(key: unknown, keepScalar?: false): T | undefined
get<T extends V = V>(
get<T extends Value = Value>(key: unknown, keepScalar: true): Scalar<T> | undefined
get<T extends Value = Value>(key: unknown, keepScalar?: false): T | undefined
get<T extends Value = Value>(
key: unknown,
keepScalar?: boolean
): T | Scalar<T> | undefined
Expand All @@ -99,7 +99,7 @@ export class YAMLMap<K = unknown, V = unknown> extends Collection {
return !!findPair(this.items, key)
}

set(key: K, value: V): void {
set(key: Key, value: Value): void {
this.add(new Pair(key, value), true)
}

Expand Down
20 changes: 10 additions & 10 deletions src/nodes/YAMLSeq.ts
Expand Up @@ -10,26 +10,26 @@ import { toJS, ToJSContext } from './toJS.js'

export declare namespace YAMLSeq {
interface Parsed<
T extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode
> extends YAMLSeq<T> {
items: T[]
Value extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode
> extends YAMLSeq<Value> {
items: Value[]
range: Range
srcToken?: BlockSequence | FlowCollection
}
}

export class YAMLSeq<T = unknown> extends Collection {
export class YAMLSeq<Value = unknown> extends Collection {
static get tagName(): 'tag:yaml.org,2002:seq' {
return 'tag:yaml.org,2002:seq'
}

items: T[] = []
items: Value[] = []

constructor(schema?: Schema) {
super(SEQ, schema)
}

add(value: T): void {
add(value: Value): void {
this.items.push(value)
}

Expand All @@ -56,9 +56,9 @@ export class YAMLSeq<T = unknown> extends Collection {
* `key` must contain a representation of an integer for this to succeed.
* It may be wrapped in a `Scalar`.
*/
get<S extends T = T>(key: unknown, keepScalar: true): Scalar<S> | undefined
get<S extends T = T>(key: unknown, keepScalar?: false): S | undefined
get<S extends T = T>(
get<S extends Value = Value>(key: unknown, keepScalar: true): Scalar<S> | undefined
get<S extends Value = Value>(key: unknown, keepScalar?: false): S | undefined
get<S extends Value = Value>(
key: unknown,
keepScalar?: boolean
): S | Scalar<S> | undefined
Expand Down Expand Up @@ -87,7 +87,7 @@ export class YAMLSeq<T = unknown> extends Collection {
* If `key` does not contain a representation of an integer, this will throw.
* It may be wrapped in a `Scalar`.
*/
set(key: unknown, value: T): void {
set(key: unknown, value: Value): void {
const idx = asItemIndex(key)
if (typeof idx !== 'number')
throw new Error(`Expected a valid index, not ${key}.`)
Expand Down

0 comments on commit c2254de

Please sign in to comment.