Skip to content

Commit

Permalink
fix(reflect): Remove unnecessary base class when create class using p…
Browse files Browse the repository at this point in the history
…roperty definition
  • Loading branch information
ktutnik committed Dec 9, 2022
1 parent 2cc9e82 commit 92a7eea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/reflect/src/helpers.ts
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
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
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
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

0 comments on commit 92a7eea

Please sign in to comment.