Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Fix applyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Nov 28, 2020
1 parent 695747d commit 1970347
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 598 deletions.
3 changes: 1 addition & 2 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export function ignore() {
}

export function type(type: TypeOverride | ((x: any) => TypeOverride), ...genericParams: (string | string[])[]) {
// type is not inheritable because derived class can define their own type override
return decorate((target: any) => <TypeDecorator>{ [DecoratorId]: symOverride, kind: "Override", type, genericParams, target }, ["Parameter", "Method", "Property"], { inherit: false, allowMultiple: false })
return decorate((target: any) => <TypeDecorator>{ [DecoratorId]: symOverride, kind: "Override", type, genericParams: genericParams, target }, ["Parameter", "Method", "Property"], { inherit: true, allowMultiple: false })
}

export function noop() {
Expand Down
8 changes: 4 additions & 4 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function setMetadata(data: any, targetClass: Class, memberName?: string |
for (const apply of applyTo) {
meta.push({ targetClass, memberName: apply, data })
}
meta.push({ targetClass, data })
meta.push({ targetClass, memberName, parIndex, data })
}
storage.set(targetClass, meta)
}
Expand All @@ -35,7 +35,7 @@ function getMetadataFromStorage(target: Class, memberName?: string, parIndex?: n
.filter(x => x.memberName === memberName && x.parIndex === parIndex)
.filter(x => {
const opt: DecoratorOption = x.data[DecoratorOptionId]
return opt.applyTo?.length === 0
return opt.applyTo!.length === 0
})
.map(x => x.data)
}
Expand All @@ -59,9 +59,9 @@ export function getMetadata(targetClass: Class, memberName?: string, parIndex?:
return mergeMetadata(childMeta, parentMeta)
}

export function getMetadataForApplyTo(targetClass: Class, memberName?: string) {
export function getMetadataForApplyTo(targetClass: Class, memberName?: string, parIndex?:number) {
return (storage.get(targetClass) ?? [])
.filter(x => x.memberName === memberName)
.filter(x => x.memberName === memberName && x.parIndex === parIndex)
.filter(x => {
const opt: DecoratorOption = x.data[DecoratorOptionId]
return opt.applyTo!.length > 0
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function getClassMembers(fun: Function) {
.filter(x => !!x.memberName && !x.parIndex)
.filter(x => {
const opt:DecoratorOption = x.data[DecoratorOptionId]
return opt.applyTo?.length === 0
return opt.applyTo!.length === 0
})
.map(x => x.memberName as string)
const names = members.concat(properties)
Expand Down
8 changes: 7 additions & 1 deletion src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ function reflectClass(target: Class): ClassReflection {
})
return walkReflectionMembers(ref, {
classPath: [], target, parent:ref, memberVisitor: pipe([
v.addsApplyToDecorator
v.addsApplyToDecorator,
// remove generic type overridden, because it may cause issue for addsTypeOverridden
v.removeGenericTypeOverridden,
// add datatype info specified by @type decorator
v.addsTypeOverridden,
// add typeClassification information
v.addsTypeClassification,
])})
}

Expand Down
24 changes: 21 additions & 3 deletions src/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,16 @@ namespace memberVisitors {
}

export function addsApplyToDecorator(meta: TypedReflection, ctx: WalkMemberContext) {
if (metadata.isParameterProperties(meta)) {
// get copy own metadata of constructor
const decorators = getMetadataForApplyTo(ctx.target, "constructor", meta.index)
// and also a copy of metadata of the property (using applyTo)
.concat(...getMetadataForApplyTo(ctx.target, meta.name))
return { ...meta, decorators: meta.decorators.concat(decorators) }
}
if (meta.kind === "Method" || meta.kind === "Property") {
const decorators = getMetadataForApplyTo(ctx.target, meta.name)
return { ...meta, decorators: mergeMetadata(decorators, meta.decorators, false) }
return { ...meta, decorators: mergeMetadata(decorators, meta.decorators, false) }
}
if (meta.kind === "Class") {
const rawDecorators = getMetadataForApplyTo(meta.type)
Expand All @@ -170,13 +177,23 @@ namespace memberVisitors {
return !option.removeApplied
})
const result = { ...meta, decorators: meta.decorators.concat(decorators) }
if(removedDecorators.length > 0)
if (removedDecorators.length > 0)
result.removedDecorators = removedDecorators
return result
}
return meta
}

export function removeGenericTypeOverridden(meta: TypedReflection, ctx: WalkMemberContext): TypedReflection {
const isGeneric = (decorator: any): decorator is { type: TypeOverride[], genericParams: (string | string[])[] } => {
const singleType = Array.isArray(decorator.type) ? decorator.type[0] : decorator.type
return typeof singleType === "string" || decorator.genericParams.length > 0
}
if (meta.kind === "Constructor") return meta
const decorators = meta.decorators.filter((x: TypeDecorator) => x.kind !== "Override" || !isGeneric(x))
return { ...meta, decorators }
}

export function addsTypeOverridden(meta: TypedReflection, ctx: WalkMemberContext): TypedReflection {
if (meta.kind === "Constructor" || meta.kind === "Class") return meta
const overridden = getTypeOverrideFromDecorator(meta.decorators)
Expand Down Expand Up @@ -207,6 +224,7 @@ namespace memberVisitors {
if (!decorator || !decorator.type || !isGeneric(decorator)) return meta
const map = new GenericMap(ctx.classPath)
const type = isString(decorator) ? map.get(decorator.type as any) : getGenericType(map, decorator)

// if current class has @generic.template() then process
if (meta.kind === "Method") {
// if type is not a generic template type then return immediately
Expand Down Expand Up @@ -278,7 +296,7 @@ function walkReflectionMembers(ref: TypedReflection, ctx: WalkMemberContext) {
return result as ClassReflection
}

function walkTypeMembers(type: Class, memberVisitor: WalkMemberVisitor, classPath: Class[] = []) {
function walkTypeMembers(type: Class, memberVisitor: WalkMemberVisitor, classPath: Class[]) {
const rawMeta = parseClass(type)
return walkReflectionMembers(rawMeta, { memberVisitor, parent: rawMeta, target: type, classPath })
}
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/class.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down Expand Up @@ -64,7 +64,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down Expand Up @@ -137,7 +137,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down Expand Up @@ -249,7 +249,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down Expand Up @@ -522,7 +522,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down Expand Up @@ -597,7 +597,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down
40 changes: 6 additions & 34 deletions test/__snapshots__/create.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,12 @@ Object {
"kind": "Class",
"methods": Array [
Object {
"decorators": Array [
Object {
"genericParams": Array [],
"kind": "Override",
"target": SuperClass,
"type": "T",
Symbol(tinspector:decoratorId): Symbol(override),
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"removeApplied": true,
},
},
],
"decorators": Array [],
"kind": "Method",
"name": "method",
"parameters": Array [
Object {
"decorators": Array [
Object {
"genericParams": Array [],
"kind": "Override",
"target": SuperClass,
"type": "U",
Symbol(tinspector:decoratorId): Symbol(override),
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"removeApplied": true,
},
},
],
"decorators": Array [],
"fields": Object {},
"index": 0,
"kind": "Parameter",
Expand Down Expand Up @@ -140,7 +112,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand All @@ -167,7 +139,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand All @@ -194,7 +166,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand All @@ -221,7 +193,7 @@ Object {
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"inherit": true,
"removeApplied": true,
},
},
Expand Down
Loading

0 comments on commit 1970347

Please sign in to comment.