This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix generic inheritance issue when passed generic template
- Loading branch information
Showing
23 changed files
with
800 additions
and
6,440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
ClassReflection, | ||
DecoratorId, | ||
DecoratorOption, | ||
DecoratorOptionId, | ||
MethodReflection, | ||
ParameterReflection, | ||
PropertyReflection, | ||
} from "./types" | ||
|
||
type MemberReflection = PropertyReflection | MethodReflection | ParameterReflection | ||
|
||
// --------------------------------------------------------------------- // | ||
// ------------------------------ EXTENDER ----------------------------- // | ||
// --------------------------------------------------------------------- // | ||
|
||
function mergeDecorators(ownDecorators: any[], parentDecorators: any[]) { | ||
const result = [...ownDecorators] | ||
for (const decorator of parentDecorators) { | ||
const options: DecoratorOption = decorator[DecoratorOptionId]! | ||
// continue, if the decorator is not inheritable | ||
if (!options.inherit) continue | ||
// continue, if allow multiple and already has decorator with the same ID | ||
if (!options.allowMultiple && ownDecorators.some(x => x[DecoratorId] === decorator[DecoratorId])) continue | ||
result.push(decorator) | ||
} | ||
return result | ||
} | ||
|
||
function mergeMember(child: MemberReflection | undefined, parent: MemberReflection): MemberReflection { | ||
const decorators = mergeDecorators(child?.decorators ?? [], parent.decorators) | ||
if (parent.kind === "Method") { | ||
const childParameters = (child as MethodReflection)?.parameters ?? [] | ||
const merged = (child ?? parent) as MethodReflection | ||
// copy parent parameters if number of current parameters = 0, else just merge existing parameters with parent | ||
const copyParentParameters = childParameters.length === 0 | ||
const parameters = mergeMembers(childParameters, parent.parameters, copyParentParameters) as ParameterReflection[] | ||
return { ...merged, returnType: parent.returnType, typeClassification: parent.typeClassification, decorators, parameters } | ||
} | ||
else { | ||
const merged = (child ?? parent) as PropertyReflection | ParameterReflection | ||
return { ...merged, type: parent.type, typeClassification: parent.typeClassification, decorators } | ||
} | ||
} | ||
|
||
function mergeMembers(children: MemberReflection[], parents: MemberReflection[], copy = true): MemberReflection[] { | ||
const result: MemberReflection[] = [] | ||
const isExists: { [key: string]: true } = {} | ||
for (const child of children) { | ||
const parent = parents.find(x => x.name === child.name) | ||
if (parent) { | ||
result.push(mergeMember(child, parent)) | ||
isExists[child.name] = true | ||
} | ||
else | ||
result.push(child) | ||
} | ||
if(copy){ | ||
for (const parent of parents) { | ||
if (isExists[parent.name]) continue | ||
result.push(mergeMember(undefined, parent)) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
function extendsMetadata(child: ClassReflection, parent: ClassReflection): ClassReflection { | ||
return { | ||
...child, | ||
decorators: mergeDecorators(child.decorators, parent.decorators), | ||
methods: mergeMembers(child.methods, parent.methods) as MethodReflection[], | ||
properties: mergeMembers(child.properties, parent.properties) as PropertyReflection[] | ||
} | ||
} | ||
|
||
export { extendsMetadata } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.