From c2254de69ca927b9676ae6a553f07fa9e3b2787f Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 22 Apr 2022 16:57:40 +0300 Subject: [PATCH] style: Rename type arguments to be consistent and less terse --- src/doc/Document.ts | 34 ++++++++++++++-------------- src/nodes/Node.ts | 46 +++++++++++++++++++------------------- src/nodes/Pair.ts | 14 ++++++------ src/nodes/Scalar.ts | 6 ++--- src/nodes/YAMLMap.ts | 30 ++++++++++++------------- src/nodes/YAMLSeq.ts | 20 ++++++++--------- src/public-api.ts | 14 ++++++------ src/schema/yaml-1.1/set.ts | 18 +++++++-------- 8 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/doc/Document.ts b/src/doc/Document.ts index 4eebe2ab..d1444d5e 100644 --- a/src/doc/Document.ts +++ b/src/doc/Document.ts @@ -36,13 +36,13 @@ import { Directives } from './directives.js' export type Replacer = any[] | ((key: any, value: any) => unknown) export declare namespace Document { - interface Parsed extends Document { + interface Parsed extends Document { directives: Directives range: Range } } -export class Document { +export class Document { readonly [NODE_TYPE]: symbol /** A comment before this Document */ @@ -52,7 +52,7 @@ export class Document { comment: string | null = null /** The document contents. */ - contents: T | null + contents: Value | null directives?: Directives @@ -133,7 +133,7 @@ export class Document { 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 } } @@ -142,8 +142,8 @@ export class Document { * * Custom Node values that inherit from `Object` still refer to their original instances. */ - clone(): Document { - const copy: Document = Object.create(Document.prototype, { + clone(): Document { + const copy: Document = Object.create(Document.prototype, { [NODE_TYPE]: { value: DOC } }) copy.commentBefore = this.commentBefore @@ -154,7 +154,7 @@ export class Document { 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 @@ -193,12 +193,12 @@ export class Document { * Convert any value into a `Node` using the current schema, recursively * turning objects into collections. */ - createNode(value: T, options?: CreateNodeOptions): NodeType - createNode( - value: T, + createNode(value: Value, options?: CreateNodeOptions): NodeType + createNode( + value: Value, replacer: Replacer | CreateNodeOptions | null, options?: CreateNodeOptions - ): NodeType + ): NodeType createNode( value: unknown, replacer?: Replacer | CreateNodeOptions | null, @@ -251,13 +251,13 @@ export class Document { * Convert a key and a value into a `Pair` using the current schema, * recursively wrapping all values as `Scalar` or `Collection` nodes. */ - createPair( + createPair( 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) } @@ -335,7 +335,7 @@ export class Document { this.schema, [key], value - ) as unknown as T + ) as unknown as Value } else if (assertCollection(this.contents)) { this.contents.set(key, value) } @@ -346,13 +346,13 @@ export class Document { * boolean to add/remove the item from the set. */ setIn(path: Iterable | 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) } diff --git a/src/nodes/Node.ts b/src/nodes/Node.ts index 36bc34bd..4d715ac0 100644 --- a/src/nodes/Node.ts +++ b/src/nodes/Node.ts @@ -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 = +export type Node = | Alias - | Scalar - | YAMLMap - | YAMLSeq + | Scalar + | YAMLMap + | YAMLSeq /** Utility type mapper */ -export type NodeType = T extends string | number | bigint | boolean | null - ? Scalar - : T extends Array - ? YAMLSeq> - : T extends { [key: string | number]: any } - ? YAMLMap, NodeType> +export type NodeType = Value extends string | number | bigint | boolean | null + ? Scalar + : Value extends Array + ? YAMLSeq> + : Value extends { [key: string | number]: any } + ? YAMLMap, NodeType> : Node export type ParsedNode = @@ -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 = ( +export const isDocument = ( node: any -): node is Document => +): node is Document => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC -export const isMap = ( +export const isMap = ( node: any -): node is YAMLMap => +): node is YAMLMap => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP -export const isPair = ( +export const isPair = ( node: any -): node is Pair => +): node is Pair => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR -export const isScalar = (node: any): node is Scalar => +export const isScalar = (node: any): node is Scalar => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR -export const isSeq = (node: any): node is YAMLSeq => +export const isSeq = (node: any): node is YAMLSeq => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ -export function isCollection( +export function isCollection( node: any -): node is YAMLMap | YAMLSeq { +): node is YAMLMap | YAMLSeq { if (node && typeof node === 'object') switch (node[NODE_TYPE]) { case MAP: @@ -74,7 +74,7 @@ export function isCollection( return false } -export function isNode(node: any): node is Node { +export function isNode(node: any): node is Node { if (node && typeof node === 'object') switch (node[NODE_TYPE]) { case ALIAS: @@ -86,9 +86,9 @@ export function isNode(node: any): node is Node { return false } -export const hasAnchor = ( +export const hasAnchor = ( node: unknown -): node is Scalar | YAMLMap | YAMLSeq => +): node is Scalar | YAMLMap | YAMLSeq => (isScalar(node) || isCollection(node)) && !!node.anchor export abstract class NodeBase { diff --git a/src/nodes/Pair.ts b/src/nodes/Pair.ts index be235aa8..9660be30 100644 --- a/src/nodes/Pair.ts +++ b/src/nodes/Pair.ts @@ -17,28 +17,28 @@ export function createPair( return new Pair(k, v) } -export class Pair { +export class Pair { 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 { + clone(schema?: Schema): Pair { 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) } diff --git a/src/nodes/Scalar.ts b/src/nodes/Scalar.ts index ac17e017..eca54012 100644 --- a/src/nodes/Scalar.ts +++ b/src/nodes/Scalar.ts @@ -21,14 +21,14 @@ export declare namespace Scalar { type Type = BLOCK_FOLDED | BLOCK_LITERAL | PLAIN | QUOTE_DOUBLE | QUOTE_SINGLE } -export class Scalar extends NodeBase { +export class Scalar 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 @@ -49,7 +49,7 @@ export class Scalar 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 } diff --git a/src/nodes/YAMLMap.ts b/src/nodes/YAMLMap.ts index 119092e3..d3eec7a6 100644 --- a/src/nodes/YAMLMap.ts +++ b/src/nodes/YAMLMap.ts @@ -9,8 +9,8 @@ import { Pair } from './Pair.js' import { isScalarValue, Scalar } from './Scalar.js' import type { ToJSContext } from './toJS.js' -export function findPair( - items: Iterable>, +export function findPair( + items: Iterable>, key: unknown ) { const k = isScalar(key) ? key.value : key @@ -25,21 +25,21 @@ export function findPair( export declare namespace YAMLMap { interface Parsed< - K extends ParsedNode = ParsedNode, - V extends ParsedNode | null = ParsedNode | null - > extends YAMLMap { - items: Pair[] + Key extends ParsedNode = ParsedNode, + Value extends ParsedNode | null = ParsedNode | null + > extends YAMLMap { + items: Pair[] range: Range srcToken?: BlockMap | FlowCollection } } -export class YAMLMap extends Collection { +export class YAMLMap extends Collection { static get tagName(): 'tag:yaml.org,2002:map' { return 'tag:yaml.org,2002:map' } - items: Pair[] = [] + items: Pair[] = [] constructor(schema?: Schema) { super(MAP, schema) @@ -51,12 +51,12 @@ export class YAMLMap 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 | { key: K; value: V }, overwrite?: boolean): void { - let _pair: Pair + add(pair: Pair | { key: Key; value: Value }, overwrite?: boolean): void { + let _pair: Pair if (isPair(pair)) _pair = pair else if (!pair || typeof pair !== 'object' || !('key' in pair)) { // In TypeScript, this never happens. - _pair = new Pair(pair as any, (pair as any)?.value) + _pair = new Pair(pair as any, (pair as any)?.value) } else _pair = new Pair(pair.key, pair.value) const prev = findPair(this.items, _pair.key) @@ -83,9 +83,9 @@ export class YAMLMap extends Collection { return del.length > 0 } - get(key: unknown, keepScalar: true): Scalar | undefined - get(key: unknown, keepScalar?: false): T | undefined - get( + get(key: unknown, keepScalar: true): Scalar | undefined + get(key: unknown, keepScalar?: false): T | undefined + get( key: unknown, keepScalar?: boolean ): T | Scalar | undefined @@ -99,7 +99,7 @@ export class YAMLMap 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) } diff --git a/src/nodes/YAMLSeq.ts b/src/nodes/YAMLSeq.ts index 39fe64ef..2b4a6b18 100644 --- a/src/nodes/YAMLSeq.ts +++ b/src/nodes/YAMLSeq.ts @@ -10,26 +10,26 @@ import { toJS, ToJSContext } from './toJS.js' export declare namespace YAMLSeq { interface Parsed< - T extends ParsedNode | Pair = ParsedNode - > extends YAMLSeq { - items: T[] + Value extends ParsedNode | Pair = ParsedNode + > extends YAMLSeq { + items: Value[] range: Range srcToken?: BlockSequence | FlowCollection } } -export class YAMLSeq extends Collection { +export class YAMLSeq 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) } @@ -56,9 +56,9 @@ export class YAMLSeq extends Collection { * `key` must contain a representation of an integer for this to succeed. * It may be wrapped in a `Scalar`. */ - get(key: unknown, keepScalar: true): Scalar | undefined - get(key: unknown, keepScalar?: false): S | undefined - get( + get(key: unknown, keepScalar: true): Scalar | undefined + get(key: unknown, keepScalar?: false): S | undefined + get( key: unknown, keepScalar?: boolean ): S | Scalar | undefined @@ -87,7 +87,7 @@ export class YAMLSeq 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}.`) diff --git a/src/public-api.ts b/src/public-api.ts index 695f4d2c..a8fc6396 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -37,10 +37,10 @@ function parseOptions(options: ParseOptions) { * EmptyStream and contain additional stream information. In * TypeScript, you should use `'empty' in docs` as a type guard for it. */ -export function parseAllDocuments( +export function parseAllDocuments( source: string, options: ParseOptions & DocumentOptions & SchemaOptions = {} -): Document.Parsed[] | EmptyStream { +): Document.Parsed[] | EmptyStream { const { lineCounter, prettyErrors } = parseOptions(options) const parser = new Parser(lineCounter?.addNewLine) const composer = new Composer(options) @@ -52,16 +52,16 @@ export function parseAllDocuments( doc.warnings.forEach(prettifyError(source, lineCounter)) } - if (docs.length > 0) return docs as Document.Parsed[] + if (docs.length > 0) return docs as Document.Parsed[] return Object.assign< - Document.Parsed[], + Document.Parsed[], { empty: true }, ReturnType >([], { empty: true }, composer.streamInfo()) } /** Parse an input string into a single YAML.Document */ -export function parseDocument( +export function parseDocument( source: string, options: ParseOptions & DocumentOptions & SchemaOptions = {} ) { @@ -70,13 +70,13 @@ export function parseDocument( const composer = new Composer(options) // `doc` is always set by compose.end(true) at the very latest - let doc: Document.Parsed = null as any + let doc: Document.Parsed = null as any for (const _doc of composer.compose( parser.parse(source), true, source.length )) { - if (!doc) doc = _doc as Document.Parsed + if (!doc) doc = _doc as Document.Parsed else if (doc.options.logLevel !== 'silent') { doc.errors.push( new YAMLParseError( diff --git a/src/schema/yaml-1.1/set.ts b/src/schema/yaml-1.1/set.ts index 64db9187..9842c117 100644 --- a/src/schema/yaml-1.1/set.ts +++ b/src/schema/yaml-1.1/set.ts @@ -7,7 +7,7 @@ import { YAMLMap, findPair } from '../../nodes/YAMLMap.js' import type { StringifyContext } from '../../stringify/stringify.js' import type { CollectionTag } from '../types.js' -export class YAMLSet extends YAMLMap | null> { +export class YAMLSet extends YAMLMap | null> { static tag = 'tag:yaml.org,2002:set' constructor(schema?: Schema) { @@ -17,11 +17,11 @@ export class YAMLSet extends YAMLMap | null> { add( key: - | T - | Pair | null> - | { key: T; value: Scalar | null } + | Value + | Pair | null> + | { key: Value; value: Scalar | null } ) { - let pair: Pair | null> + let pair: Pair | null> if (isPair(key)) pair = key else if ( typeof key === 'object' && @@ -30,7 +30,7 @@ export class YAMLSet extends YAMLMap | null> { key.value === null ) pair = new Pair(key.key, null) - else pair = new Pair(key as T, null) + else pair = new Pair(key as Value, null) const prev = findPair(this.items, pair.key) if (!prev) this.items.push(pair) } @@ -48,11 +48,11 @@ export class YAMLSet extends YAMLMap | null> { : pair } - set(key: T, value: boolean): void + set(key: Value, value: boolean): void /** @deprecated Will throw; `value` must be boolean */ - set(key: T, value: null): void - set(key: T, value: boolean | null) { + set(key: Value, value: null): void + set(key: Value, value: boolean | null) { if (typeof value !== 'boolean') throw new Error( `Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`