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

Commit

Permalink
fix: Fix data type inheritance issue (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Nov 13, 2020
1 parent 70c4136 commit 01f7e22
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
PrivateDecorator,
TypeDecorator,
TypeOverride,
TypeOverrideOption,
} from "./types"

// --------------------------------------------------------------------- //
Expand Down
8 changes: 6 additions & 2 deletions src/extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ function mergeMember(ref: ClassReflection, child: MemberReflection | undefined,
// copy parent parameters if number of current parameters = 0, else just merge existing parameters with parent
const copyParentParameters = childParameters.length === 0
const parameters = mergeMembers(ref, childParameters, parent.parameters, copyParentParameters) as ParameterReflection[]
return { ...merged, returnType: parent.returnType, typeClassification: parent.typeClassification, decorators, parameters }
const returnType = merged.returnType ?? parent.returnType
const typeClassification = merged.typeClassification ?? parent.typeClassification
return { ...merged, returnType, typeClassification, decorators, parameters }
}
else {
const merged = (child ?? parent) as PropertyReflection | ParameterReflection
return { ...merged, type: parent.type, typeClassification: parent.typeClassification, decorators }
const type = merged.type ?? parent.type
const typeClassification = merged.typeClassification ?? parent.typeClassification
return { ...merged, type, typeClassification, decorators }
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export interface NativeParameterDecorator extends NativeDecorator {
targetType: "Parameter",
targetIndex: number
}
export interface TypeOverrideOption {
genericType: TypeOverride[]
}

export type Reflection = ParameterReflection | FunctionReflection | PropertyReflection | MethodReflection | ClassReflection | ObjectReflection | ConstructorReflection

Expand Down
51 changes: 51 additions & 0 deletions test/__snapshots__/inheritance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,57 @@ Object {
}
`;

exports[`Inheritance Should able to specify method return type on overridden method 1`] = `
Object {
"ctor": Object {
"kind": "Constructor",
"name": "constructor",
"parameters": Array [],
},
"decorators": Array [],
"kind": "Class",
"methods": Array [
Object {
"decorators": Array [
Object {
"genericParams": Array [],
"kind": "Override",
"target": ChildClass,
"type": Number,
Symbol(tinspector:decoratorId): Symbol(override),
Symbol(tinspector:decoratorOption): Object {
"allowMultiple": false,
"applyTo": Array [],
"inherit": false,
"removeApplied": true,
},
},
],
"kind": "Method",
"name": "myMethod",
"parameters": Array [
Object {
"decorators": Array [],
"fields": Object {},
"index": 0,
"kind": "Parameter",
"name": "a",
"type": String,
"typeClassification": "Primitive",
},
],
"returnType": Number,
"typeClassification": "Primitive",
},
],
"name": "ChildClass",
"properties": Array [],
"super": BaseClass,
"type": ChildClass,
"typeClassification": "Class",
}
`;

exports[`Inheritance Should get base class from deep inheritance 1`] = `
Object {
"ctor": Object {
Expand Down
15 changes: 14 additions & 1 deletion test/inheritance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decorateClass, decorateMethod, decorateParameter, decorateProperty, DecoratorId, reflect, noop } from "../src"
import { decorateClass, decorateMethod, decorateParameter, decorateProperty, DecoratorId, reflect, noop, type } from "../src"
import * as metadata from "../src/parser"
import { metadata as metadataHelper} from "../src/helpers"

Expand Down Expand Up @@ -133,6 +133,19 @@ describe("Inheritance", () => {
expect(meta).toMatchSnapshot()
})

it("Should able to specify method return type on overridden method", () => {
class BaseClass {
@type(String)
myMethod(a: string) { return "" }
}
class ChildClass extends BaseClass {
@type(Number)
myMethod(a: string) { return "Hello" }
}
const meta = reflect(ChildClass)
expect(meta).toMatchSnapshot()
})

it("Should not override private method", () => {
class BaseClass {
@reflect.ignore()
Expand Down

0 comments on commit 01f7e22

Please sign in to comment.