Skip to content

Commit

Permalink
Convert some more types to plain JSDoc style
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Jul 12, 2023
1 parent 97f3d75 commit 193ce6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
12 changes: 7 additions & 5 deletions lib/components/inline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { SourceFile, Buffer } = require('../file')
const { CSN, TypeResolveInfo } = require('./resolver')
const { Visitor } = require('../visitor')
const { docify } = require('./wrappers')

/**
Expand All @@ -11,8 +13,8 @@ class InlineDeclarationResolver {

/**
* @param {string} name
* @param {import('./resolver').TypeResolveInfo} type
* @param {import('../file').Buffer} buffer
* @param {TypeResolveInfo} type
* @param {Buffer} buffer
* @param {string} statementEnd
* @protected
* @abstract
Expand All @@ -22,7 +24,7 @@ class InlineDeclarationResolver {
/**
* Attempts to resolve a type that could reference another type.
* @param {any} items
* @param {import('./resolver').TypeResolveInfo} into @see Visitor.resolveType
* @param {TypeResolveInfo} into @see Visitor.resolveType
* @param {SourceFile} file temporary file to resolve dummy types into.
* @public
*/
Expand Down Expand Up @@ -56,7 +58,7 @@ class InlineDeclarationResolver {
/**
* Visits a single element in an entity.
* @param {string} name name of the element
* @param {import('./resolver').CSN} element CSN data belonging to the the element.
* @param {CSN} element CSN data belonging to the the element.
* @param {SourceFile} file the namespace file the surrounding entity is being printed into.
* @param {Buffer} [buffer] buffer to add the definition to. If no buffer is passed, the passed file's class buffer is used instead.
* @public
Expand Down Expand Up @@ -84,7 +86,7 @@ class InlineDeclarationResolver {
return this.visitor.options.propertiesOptional ? '?:' : ':'
}

/** @param {import('../visitor').Visitor} visitor */
/** @param {Visitor} visitor */
constructor(visitor) {
this.visitor = visitor
// type resolution might recurse. This indicator is used to determine
Expand Down
46 changes: 30 additions & 16 deletions lib/components/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ const { Buffer, SourceFile, Path, Library, baseDefinitions } = require("../file"
const { deepRequire, createToManyAssociation, createToOneAssociation, createArrayOf, createCompositionOfMany, createCompositionOfOne } = require('./wrappers')
const { Visitor } = require("../visitor")
const { StructuredInlineDeclarationResolver } = require("./inline")
const { TypeResolveInfo } = require("./resolver")

/** @typedef {{ cardinality?: { max?: '*' | number } }} EntityCSN */
/** @typedef {{ definitions?: Object<string, EntityCSN> }} CSN */
/**
* @typedef {Object} EntityCSN
* @property {Object} [cardinality] {{ cardinality?: { max?: '*' | number } }} EntityCSN
* @property {'*' | number} [cardinality.max]
*/

/**
* @typedef {Object} CSN
* @property {Object<string, EntityCSN>} definitions
*/

/**
* When nested inline types require additional imports. E.g.:
Expand All @@ -19,17 +28,16 @@ const { StructuredInlineDeclarationResolver } = require("./inline")
* }
* }
* ```
* @typedef {{
* isBuiltin: boolean,
* isInlineDeclaration: boolean,
* isForeignKeyReference: boolean,
* isArray: boolean,
* type: string,
* path?: Path,
* csn?: CSN,
* imports: Path[]
* inner: TypeResolveInfo
* }} TypeResolveInfo
* @typedef {Object} TypeResolveInfo
* @property {boolean} isBuiltin
* @property {boolean} isInlineDeclaration
* @property {boolean} isForeignKeyReference
* @property {boolean} isArray
* @property {string} type
* @property {Path} [path]
* @property {CSN} [csn]
* @property {Array<Path>} [imports]
* @property {TypeResolveInfo} inner
*/

/**
Expand Down Expand Up @@ -86,7 +94,7 @@ class Resolver {
* to end up with both the resolved Path of the namespace,
* and the clean name of the class.
* @param {string} fq the fully qualified name of an entity.
* @returns {[Path, string]} a tuple, [0] holding the path to the namespace, [1] holding the clean name of the entity.
* @returns {Array<Path | string>} a tuple, [0] holding the path (Path) to the namespace, [1] holding the clean name of the entity (string).
*/
untangle(fq) {
const ns = this.resolveNamespace(fq.split('.'))
Expand Down Expand Up @@ -132,7 +140,7 @@ class Resolver {
* - collisions between singular and plural name (resolved by appending a '_' suffix)
* - inline type definitions, which don't really have a linguistic plural,
* but need to expressed as array type to be consumable by the likes of Composition.of.many<T>
* @param {import('./resolver').TypeResolveInfo} typeInfo information about the type gathered so far.
* @param {TypeResolveInfo} typeInfo information about the type gathered so far.
* @param {string} [namespace] namespace the type occurs in. If passed, will be shaved off from the name
* @returns {Inflection}
*/
Expand Down Expand Up @@ -181,6 +189,12 @@ class Resolver {
return { typeName, singular, plural }
}

/**
* @typedef {Object} InflectedTypeResolveInfo
* @extends TypeResolveInfo
* @property {Inflection} inflection
*/

/**
* Convenient API to consume resolveType.
* Internally calls resolveType, determines how it has to be imported,
Expand All @@ -198,7 +212,7 @@ class Resolver {
*
* @param {CSN} element the CSN element to resolve the type for.
* @param {SourceFile} file source file for context.
* @returns {{typeName: string, typeInfo: TypeResolveInfo & { inflection: Inflection } }} info about the resolved type
* @returns {{typeName: Foo, typeInfo: InflectedTypeResolveInfo }} info about the resolved type
*/
resolveAndRequire(element, file) {
const typeInfo = this.resolveType(element, file)
Expand Down

0 comments on commit 193ce6a

Please sign in to comment.