Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reflect): Remove unnecessary base class when create class using property definition #1031

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/reflect/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ interface CreateClassOption {

function createClass(definition: CustomTypeDefinition, opt?: Partial<CreateClassOption>): Class {
class Base { }
const {extends:BaseClass, ...option} = { extends: Base, name: "DynamicType", genericParams: [], ...opt }
const type = { [option.name]: class extends BaseClass { } }[option.name];
const {extends:BaseClass, ...option} = { name: "DynamicType", genericParams: [], ...opt }
const obj = !!BaseClass ? { [option.name]: class extends BaseClass { } } : { [option.name]: class { } }
const type = obj[option.name];
(type as any)[IsDynamicType] = true
for (const key in definition) {
Reflect.decorate([decorate.type(definition[key])], type.prototype, key)
Expand Down
9 changes: 7 additions & 2 deletions packages/reflect/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ function getNamesFromAst(nodes: any[]) {
}

function getCode(fn: Class | Function) {
const syntaxReplacement = [
{ pattern: /^class(\s*)extends\s*BaseClass\s*{\s*}/gm, replacement: "class DynamicClass extends Parent {}" },
{ pattern: /^class(\s*){\s*}/gm, replacement: "class DynamicClass {}" },
]
const code = fn.toString()
if (code.search(/^class(\s*)extends\s*BaseClass\s*{\s*}/gm) > -1) {
return "class DynamicClass extends Parent {}"
const exclude = syntaxReplacement.find(x => code.search(x.pattern) > -1)
if (exclude) {
return exclude.replacement
}
else
return code.replace("[native code]", "")
Expand Down
8 changes: 4 additions & 4 deletions tests/behavior/reflect/__snapshots__/create.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Object {
"typeClassification": "Array",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down Expand Up @@ -210,7 +210,7 @@ Object {
"typeClassification": "Primitive",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down Expand Up @@ -255,7 +255,7 @@ Object {
"typeClassification": "Class",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down Expand Up @@ -320,7 +320,7 @@ Object {
"typeClassification": "Primitive",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down
4 changes: 2 additions & 2 deletions tests/behavior/reflect/__snapshots__/decorators.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ Object {
"typeClassification": "Primitive",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down Expand Up @@ -2595,7 +2595,7 @@ Object {
"typeClassification": "Primitive",
},
],
"super": Base,
"super": Object,
"type": DynamicType,
"typeClassification": "Class",
}
Expand Down