refactor(yaml): add KindType generic type argument to Type (#5461)

This commit is contained in:
Asher Gomez 2024-07-17 14:30:32 +10:00 committed by GitHub
parent d708abee9a
commit 8b8333f1fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 43 additions and 36 deletions

View File

@ -27,7 +27,7 @@ import {
VERTICAL_LINE,
} from "./_chars.ts";
import { DEFAULT_SCHEMA, type Schema } from "./_schema.ts";
import type { StyleVariant, Type } from "./_type.ts";
import type { KindType, StyleVariant, Type } from "./_type.ts";
import { type ArrayObject, getObjectTypeString, isObject } from "./_utils.ts";
const STYLE_PLAIN = 1;
@ -118,7 +118,10 @@ function generateNextLine(indent: number, level: number): string {
return `\n${" ".repeat(indent * level)}`;
}
function testImplicitResolving(implicitTypes: Type[], str: string): boolean {
function testImplicitResolving(
implicitTypes: Type<"scalar">[],
str: string,
): boolean {
return implicitTypes.some((type) => type.resolve(str));
}
@ -472,8 +475,8 @@ export class DumperState {
useAnchors: boolean;
compatMode: boolean;
condenseFlow: boolean;
implicitTypes: Type[];
explicitTypes: Type[];
implicitTypes: Type<"scalar">[];
explicitTypes: Type<KindType>[];
tag: string | null = null;
result = "";
duplicates: unknown[] = [];

View File

@ -37,7 +37,7 @@ import {
} from "./_chars.ts";
import { Mark } from "./_mark.ts";
import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts";
import type { Type } from "./_type.ts";
import type { KindType, Type } from "./_type.ts";
import { type ArrayObject, getObjectTypeString, isObject } from "./_utils.ts";
const CONTEXT_FLOW_IN = 1;
@ -146,7 +146,7 @@ class LoaderState {
line = 0;
onWarning?: (error: Error) => void;
allowDuplicateKeys: boolean;
implicitTypes: Type[];
implicitTypes: Type<"scalar">[];
typeMap: TypeMap;
version?: string | null;
@ -1412,7 +1412,7 @@ function composeNode(
let indentStatus = 1; // 1: this>parent, 0: this=parent, -1: this<parent
let atNewLine = false;
let hasContent = false;
let type: Type;
let type: Type<KindType>;
let flowIndent: number;
let blockIndent: number;

View File

@ -24,11 +24,12 @@ import {
undefinedType,
} from "./_type/mod.ts";
function compileList(
// deno-lint-ignore no-explicit-any
function compileList<K extends KindType, D = any>(
schema: Schema,
name: "implicit" | "explicit",
result: Type[],
): Type[] {
result: Type<K, D>[],
): Type<K, D>[] {
const exclude: number[] = [];
for (const includedSchema of schema.include) {
@ -45,14 +46,17 @@ function compileList(
}
}
result.push(currentType);
result.push(currentType as Type<K, D>);
}
return result.filter((_type, index): unknown => !exclude.includes(index));
}
export type TypeMap = Record<KindType | "fallback", ArrayObject<Type<unknown>>>;
function compileMap(...typesList: Type<unknown>[][]): TypeMap {
export type TypeMap = Record<
KindType | "fallback",
ArrayObject<Type<KindType>>
>;
function compileMap(...typesList: Type<KindType>[][]): TypeMap {
const result: TypeMap = {
fallback: {},
mapping: {},
@ -69,17 +73,17 @@ function compileMap(...typesList: Type<unknown>[][]): TypeMap {
}
export class Schema {
implicit: Type[];
explicit: Type[];
implicit: Type<"scalar">[];
explicit: Type<KindType>[];
include: Schema[];
compiledImplicit: Type[];
compiledExplicit: Type[];
compiledImplicit: Type<"scalar">[];
compiledExplicit: Type<KindType>[];
compiledTypeMap: TypeMap;
constructor(definition: {
implicit?: Type[];
explicit?: Type[];
implicit?: Type<"scalar">[];
explicit?: Type<KindType>[];
include?: Schema[];
}) {
this.explicit = definition.explicit || [];

View File

@ -22,9 +22,9 @@ export type StyleVariant =
export type RepresentFn<D> = (data: D, style?: StyleVariant) => string;
// deno-lint-ignore no-explicit-any
export interface Type<D = any> {
export interface Type<K extends KindType, D = any> {
tag: string;
kind: KindType;
kind: K;
instanceOf?: new (...args: unknown[]) => D;
predicate?: (data: unknown) => data is D;
represent?: RepresentFn<D> | ArrayObject<RepresentFn<D>>;

View File

@ -118,7 +118,7 @@ function isBinary(obj: unknown): obj is Uint8Array {
return obj instanceof Uint8Array;
}
export const binary: Type<Uint8Array> = {
export const binary: Type<"scalar", Uint8Array> = {
tag: "tag:yaml.org,2002:binary",
construct: constructYamlBinary,
kind: "scalar",

View File

@ -17,7 +17,7 @@ function constructYamlBoolean(data: string): boolean {
return data === "true" || data === "True" || data === "TRUE";
}
export const bool: Type<boolean> = {
export const bool: Type<"scalar", boolean> = {
tag: "tag:yaml.org,2002:bool",
construct: constructYamlBoolean,
defaultStyle: "lowercase",

View File

@ -114,7 +114,7 @@ function isFloat(object: unknown): object is number {
(object % 1 !== 0 || isNegativeZero(object));
}
export const float: Type<number> = {
export const float: Type<"scalar", number> = {
tag: "tag:yaml.org,2002:float",
construct: constructYamlFloat,
defaultStyle: "lowercase",

View File

@ -156,7 +156,7 @@ function isInteger(object: unknown): object is number {
!isNegativeZero(object);
}
export const int: Type<number> = {
export const int: Type<"scalar", number> = {
tag: "tag:yaml.org,2002:int",
construct: constructYamlInteger,
defaultStyle: "decimal",

View File

@ -5,7 +5,7 @@
import type { Type } from "../_type.ts";
export const map: Type<unknown> = {
export const map: Type<"mapping", unknown> = {
tag: "tag:yaml.org,2002:map",
resolve() {
return true;

View File

@ -9,7 +9,7 @@ function resolveYamlMerge(data: string): boolean {
return data === "<<" || data === null;
}
export const merge: Type<unknown> = {
export const merge: Type<"scalar", unknown> = {
tag: "tag:yaml.org,2002:merge",
kind: "scalar",
resolve: resolveYamlMerge,

View File

@ -22,7 +22,7 @@ function isNull(object: unknown): object is null {
return object === null;
}
export const nil: Type<null> = {
export const nil: Type<"scalar", null> = {
tag: "tag:yaml.org,2002:null",
construct: constructYamlNull,
defaultStyle: "lowercase",

View File

@ -35,7 +35,7 @@ function resolveYamlOmap(data: any): boolean {
return true;
}
export const omap: Type<Record<PropertyKey, unknown>[]> = {
export const omap: Type<"sequence", Record<PropertyKey, unknown>[]> = {
tag: "tag:yaml.org,2002:omap",
kind: "sequence",
resolve: resolveYamlOmap,

View File

@ -43,7 +43,7 @@ function constructYamlPairs(data: string) {
return result;
}
export const pairs: Type = {
export const pairs: Type<"sequence"> = {
tag: "tag:yaml.org,2002:pairs",
construct: constructYamlPairs,
kind: "sequence",

View File

@ -7,7 +7,7 @@ import type { Type } from "../_type.ts";
const REGEXP = /^\/(?<regexp>[\s\S]+)\/(?<modifiers>[gismuy]*)$/;
export const regexp: Type<RegExp> = {
export const regexp: Type<"scalar", RegExp> = {
tag: "tag:yaml.org,2002:js/regexp",
kind: "scalar",
// deno-lint-ignore no-explicit-any

View File

@ -5,7 +5,7 @@
import type { Type } from "../_type.ts";
export const seq: Type<unknown[]> = {
export const seq: Type<"sequence", unknown[]> = {
tag: "tag:yaml.org,2002:seq",
resolve() {
return true;

View File

@ -22,7 +22,7 @@ function constructYamlSet(data: string) {
return data !== null ? data : {};
}
export const set: Type<Record<PropertyKey, unknown>> = {
export const set: Type<"mapping", Record<PropertyKey, unknown>> = {
tag: "tag:yaml.org,2002:set",
construct: constructYamlSet,
kind: "mapping",

View File

@ -4,7 +4,7 @@
import type { Type } from "../_type.ts";
export const str: Type<string> = {
export const str: Type<"scalar", string> = {
tag: "tag:yaml.org,2002:str",
resolve() {
return true;

View File

@ -87,7 +87,7 @@ function representYamlTimestamp(date: Date): string {
return date.toISOString();
}
export const timestamp: Type<Date> = {
export const timestamp: Type<"scalar", Date> = {
tag: "tag:yaml.org,2002:timestamp",
construct: constructYamlTimestamp,
instanceOf: Date,

View File

@ -5,7 +5,7 @@
import type { Type } from "../_type.ts";
export const undefinedType: Type<undefined> = {
export const undefinedType: Type<"scalar", undefined> = {
tag: "tag:yaml.org,2002:js/undefined",
kind: "scalar",
resolve() {