Refactor jsdoc types (#16)

* WIP

* Fix type 1

* Missing types info

* Some fixes

* Several types refactoring and fixes

* WIP

* Fix grammar
This commit is contained in:
barsdeveloper
2023-09-22 22:56:33 +02:00
committed by GitHub
parent 78c62ee59a
commit fdd86ce5de
78 changed files with 413 additions and 1010 deletions

View File

@@ -1,12 +1,7 @@
import Serializer from "./Serializer.js"
/**
* @typedef {import("../entity/IEntity.js").AnyValue} AnyValue
* @typedef {import("../entity/IEntity.js").AnyValueConstructor<*>} AnyValueConstructor
*/
/**
* @template {AnyValue} T
* @template {SimpleValueType<SimpleValue>} T
* @extends {Serializer<T>}
*/
export default class CustomSerializer extends Serializer {
@@ -14,8 +9,8 @@ export default class CustomSerializer extends Serializer {
#objectWriter
/**
* @param {(v: T, insideString: Boolean) => String} objectWriter
* @param {AnyValueConstructor} entityType
* @param {(v: ConstructedType<T>, insideString: Boolean) => String} objectWriter
* @param {T} entityType
*/
constructor(objectWriter, entityType) {
super(entityType)
@@ -23,7 +18,7 @@ export default class CustomSerializer extends Serializer {
}
/**
* @param {T} entity
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
* @returns {String}
*/

View File

@@ -6,17 +6,6 @@ import Serializable from "./Serializable.js"
import Union from "../entity/Union.js"
import Utility from "../Utility.js"
/**
* @typedef {import ("../entity/IEntity").AnyValue} AnyValue
* @typedef {import ("../entity/IEntity").AttributeType} AttributeType
* @typedef {import ("../entity/IEntity").AttributeInformation} AttributeInformation
* @typedef {import ("../entity/IEntity").EntityConstructor} EntityConstructor
*/
/**
* @template {AnyValue} T
* @typedef {import ("../entity/IEntity").AnyValueConstructor<T>} AnyValueConstructor
*/
let P = Parsimmon
export default class Grammar {
@@ -133,9 +122,10 @@ export default class Grammar {
}
/**
* @param {AttributeType} type
* @returns {Parsimmon.Parser<any>}
*/
* @template {SimpleValueType<SimpleValue>} T
* @param {T} type
* @returns {Parsimmon.Parser<ConstructedType<T>>}
*/
static grammarFor(
attribute,
type = attribute?.constructor === Object
@@ -181,7 +171,6 @@ export default class Grammar {
break
default:
if (type?.prototype instanceof Serializable) {
// @ts-expect-error
return /** @type {typeof Serializable} */(type).grammar
}
}
@@ -202,8 +191,8 @@ export default class Grammar {
}
/**
* @template {AnyValue} T
* @param {AnyValueConstructor<T>} entityType
* @template {SimpleValueType<SimpleValue>} T
* @param {T} entityType
* @param {String[]} key
* @returns {AttributeInformation}
*/
@@ -256,7 +245,7 @@ export default class Grammar {
/**
* @template {IEntity} T
* @param {new (...args: any) => T} entityType
* @param {AnyConstructor<T> & EntityConstructor} entityType
* @param {Boolean | Number} acceptUnknownKeys Number to specify the limit or true, to let it be a reasonable value
* @returns {Parsimmon.Parser<T>}
*/

View File

@@ -5,6 +5,7 @@ import PinEntity from "../entity/PinEntity.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
/** @extends Serializer<ObjectEntityConstructor> */
export default class ObjectSerializer extends Serializer {
constructor(entityType = ObjectEntity) {

View File

@@ -3,32 +3,22 @@ import IEntity from "../entity/IEntity.js"
import SerializerFactory from "./SerializerFactory.js"
import Utility from "../Utility.js"
/**
* @typedef {import("../entity/IEntity.js").AnyValue} AnyValue
* @typedef {import("../entity/IEntity.js").EntityConstructor} EntityConstructor
*/
/**
* @template {AnyValue} T
* @typedef {import("../entity/IEntity.js").AnyValueConstructor<T>} AnyValueConstructor
*/
/** @template {AnyValue} T */
/** @template {SimpleValueType<SimpleValue>} T */
export default class Serializer {
/** @type {(v: String) => String} */
static same = v => v
/** @type {(entity: AnyValue, serialized: String) => String} */
/** @type {(entity: SimpleValue, serialized: String) => String} */
static notWrapped = (entity, serialized) => serialized
/** @type {(entity: AnyValue, serialized: String) => String} */
/** @type {(entity: SimpleValue, serialized: String) => String} */
static bracketsWrapped = (entity, serialized) => `(${serialized})`
/** @param {AnyValueConstructor<T>} entityType */
/** @param {T} entityType */
constructor(
entityType,
/** @type {(entity: T, serialized: String) => String} */
/** @type {(entity: ConstructedType<T>, serialized: String) => String} */
wrap = (entity, serialized) => serialized,
attributeSeparator = ",",
trailingSeparator = false,
@@ -45,13 +35,13 @@ export default class Serializer {
/**
* @param {String} value
* @returns {T}
* @returns {ConstructedType<T>}
*/
read(value) {
return this.doRead(value.trim())
}
/** @param {T} value */
/** @param {ConstructedType<T>} value */
write(value, insideString = false) {
// @ts-expect-error
return this.doWrite(value, insideString)
@@ -59,7 +49,7 @@ export default class Serializer {
/**
* @param {String} value
* @returns {T}
* @returns {ConstructedType<T>}
*/
doRead(value) {
let grammar = Grammar.grammarFor(undefined, this.entityType)
@@ -71,7 +61,7 @@ export default class Serializer {
}
/**
* @param {T & IEntity} entity
* @param {ConstructedType<T> & IEntity} entity
* @param {Boolean} insideString
* @returns {String}
*/
@@ -152,7 +142,6 @@ export default class Serializer {
}
showProperty(entity, key) {
// @ts-expect-error
const attribute = /** @type {EntityConstructor} */(this.entityType).attributes[key]
if (attribute?.constructor === Object && attribute.ignored) {
return false

View File

@@ -1,38 +1,22 @@
/**
* @typedef {import("../entity/IEntity.js").default} IEntity
* @typedef {import("../entity/IEntity.js").AnyValue} AnyValue
*/
/**
* @template {AnyValue} T
* @typedef {import("../entity/IEntity.js").AnyValueConstructor<T>} AnyValueConstructor
*/
/**
* @template {AnyValue} T
* @typedef {import("./Serializer.js").default<T>} Serializer
*/
export default class SerializerFactory {
/** @type {Map<AnyValueConstructor<AnyValue>, Serializer<AnyValue>>} */
static #serializers = new Map()
/**
* @template {AnyValue} T
* @param {AnyValueConstructor<T>} entity
* @template {SimpleValueType<SimpleValue>} T
* @param {T} type
* @param {Serializer<T>} object
*/
static registerSerializer(entity, object) {
SerializerFactory.#serializers.set(entity, object)
static registerSerializer(type, object) {
SerializerFactory.#serializers.set(type, object)
}
/**
* @template {AnyValue} T
* @param {new (...any) => T} entity
* @returns {Serializer<T>}
* @template {SimpleValueType<any>} T
* @param {T} type
* @returns {Serializer<ConstructedType<T>>}
*/
static getSerializer(entity) {
// @ts-expect-error
return SerializerFactory.#serializers.get(entity)
static getSerializer(type) {
return SerializerFactory.#serializers.get(type)
}
}

View File

@@ -2,23 +2,18 @@ import Serializer from "./Serializer.js"
import Utility from "../Utility.js"
/**
* @typedef {import("../entity/IEntity.js").AnyValue} AnyValue
* @typedef {import("../entity/IEntity.js").AnyValueConstructor<*>} AnyValueConstructor
*/
/**
* @template {AnyValue} T
* @template {SimpleValueType<SimpleValue>} T
* @extends {Serializer<T>}
*/
export default class ToStringSerializer extends Serializer {
/** @param {AnyValueConstructor} entityType */
/** @param {T} entityType */
constructor(entityType) {
super(entityType)
}
/**
* @param {T} entity
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
*/
doWrite(entity, insideString, indentation = "") {

View File

@@ -39,8 +39,6 @@ import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
import Vector2DEntity from "../entity/Vector2DEntity.js"
import VectorEntity from "../entity/VectorEntity.js"
/** @typedef {import("../entity/IEntity.js").AnySimpleValue} AnySimpleValue */
Grammar.unknownValue =
Parsimmon.alt(
// Remember to keep the order, otherwise parsing might fail
@@ -78,7 +76,6 @@ export default function initializeSerializerFactory() {
SerializerFactory.registerSerializer(
Array,
new CustomSerializer(
/** @param {AnySimpleValue[]} array */
(array, insideString) =>
`(${array
.map(v =>
@@ -253,9 +250,7 @@ export default function initializeSerializerFactory() {
String,
new CustomSerializer(
(value, insideString) => insideString
// @ts-expect-error
? Utility.escapeString(value)
// @ts-expect-error
: `"${Utility.escapeString(value)}"`,
String
)