@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als
1 lines • 4.39 kB
Source Map (JSON)
{"version":3,"file":"getPrototypeChainOf.mjs","names":["baseClassPrototype"],"sources":["../../../src/utils/reflection/getPrototypeChainOf.ts"],"sourcesContent":["/* eslint-disable jsdoc/no-defaults */\nimport type { TupleIndices } from '../miscTypes.ts';\nimport { getPrototypeOf } from './getPrototypeOf.ts';\nimport {\n baseClassProto as baseClassPrototype,\n type BaseClassProto as BaseClassPrototype,\n type ClassLike,\n type ConstructorConstraint,\n type ProtoChainOfClass as PrototypeChainOfClass,\n type ProtoChainOfClassInstance as PrototypeChainOfClassInstance,\n type SuperClassLike,\n type WithProto as WithPrototype,\n} from './inheritance.ts';\nimport { isConstructor } from './isConstructor.ts';\n\ntype PrototypeChain<\n Class extends ClassLike<ConstructorConstraint<Class> & WithPrototype<SuperClassLike | BaseClassPrototype>>,\n ClassesOrInstances extends 'classes' | 'classInstances',\n> = [ClassesOrInstances] extends ['classInstances'] ? PrototypeChainOfClassInstance<Class>\n : [ClassesOrInstances] extends ['classes'] ? PrototypeChainOfClass<Class>\n : never;\n\n/**\n * Iterate through the class and its base/super classes until an anonymous function is reached. This is the default superclass all classes extend.\n * @template Class Any {@link ClassLike} type.\n * @template ClassesOrInstances 'classes' or 'classInstances'\n * @param classDefinition Any class type satisfying {@link Class}. This class (or its instance type) is included in the return value.\n * @param [returnType='classes'] Determines return type. If 'classInstances', the return type is an array of the classes' `.prototype`. Else, the classes themselves are returned.\n * @since 3.0.0\n * @returns\n * `returnType extends 'classInstances' ? ClassLike<T>[] : ClassLike<T>[].map(c => c.prototype)`\n * Excludes default superclasses e.g. anonymous functions, native code.\n */\nexport function getPrototypesChainOf<\n Class extends ClassLike<ConstructorConstraint<Class> & WithPrototype<SuperClassLike | BaseClassPrototype>>,\n ClassesOrInstances extends 'classes' | 'classInstances',\n>(\n classDefinition: Class,\n returnType: ClassesOrInstances,\n): PrototypeChain<Class, ClassesOrInstances> {\n // class definitions or their respective .prototype; exclude default superclasses.\n let current: PrototypeChainOfClass<Class>[TupleIndices<PrototypeChainOfClass<Class>>] = classDefinition as PrototypeChainOfClass<Class>[0 extends TupleIndices<PrototypeChainOfClass<Class>> ? 0 : never];\n const returnValue = [] as unknown as\n PrototypeChain<Class, ClassesOrInstances>;\n let index: TupleIndices<typeof returnValue> = 0 as TupleIndices<typeof returnValue>;\n\n while (baseClassPrototype !== current) {\n const parent: typeof current & WithPrototype<SuperClassLike | BaseClassPrototype> | object\n = getPrototypeOf(current);\n // current is a Class symbol/constructor. Object.getOwnPropertyDescriptors on current will include static properties.\n if (!isConstructor(current))\n break;\n if (returnType === 'classInstances') {\n const instanceOfCurrent = current.prototype as InstanceType<typeof current>;\n\n returnValue[index] = instanceOfCurrent as typeof returnValue[typeof index];\n }\n else {\n returnValue[index] = current as typeof returnValue[typeof index];\n }\n\n /**\n * Assign the super class to current.\n * If the argument is a class, Reflect.getPrototypeOf method returns the\n * superclass.\n */\n if (\n isConstructor(parent)\n && 'name' in parent\n && typeof parent.name === 'string'\n && '' !== parent.name // it's possible for a Function/Constructor to be anonymous...\n ) {\n current = parent as PrototypeChainOfClass<Class>[TupleIndices<PrototypeChainOfClass<Class>>];\n }\n else {\n break;\n }\n index++;\n }\n return returnValue;\n /*\n assuming current is NugetProjectProperties...\n Reflect.getPrototypeOf(current).name is 'MSBuildProjectProperties'\n */\n}\n"],"mappings":";;;;;;;;;;;;;;;AAiCA,SAAgB,qBAId,iBACA,YAC2C;CAE3C,IAAI,UAAoF;CACxF,MAAM,cAAc,CAAC;CAErB,IAAI,QAA0C;CAE9C,OAAOA,mBAAuB,SAAS;EACrC,MAAM,SACF,eAAe,OAAO;EAE1B,IAAI,CAAC,cAAc,OAAO,GACxB;EACF,IAAI,eAAe,kBAGjB,YAAY,SAFc,QAAQ;OAKlC,YAAY,SAAS;;;;;;EAQvB,IACE,cAAc,MAAM,KACjB,UAAU,UACV,OAAO,OAAO,SAAS,YACvB,OAAO,OAAO,MAEjB,UAAU;OAGV;EAEF;CACF;CACA,OAAO;AAKT"}