Skip to content

Commit

Permalink
Removed legacy decorator behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed May 3, 2024
1 parent ccca7eb commit cba86df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ export function struct(options: Partial<StructOptions> = {}) {
* Decorates a class member to be serialized
*/
export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
return function (target: object, context?: ClassMemberDecoratorContext | string | symbol) {
let name = typeof context == 'object' ? context.name : context;
return function (target: object, { name }: ClassMemberDecoratorContext) {
if (typeof name == 'symbol') {
console.warn('Symbol used for struct member name will be coerced to string: ' + name.toString());
name = name.toString();
}

if ((typeof target != 'object' || typeof target != 'function') && !('constructor' in target)) {
if (typeof target != 'object' || typeof target != 'function') {
throw new TypeError('Invalid member for struct field');
}

if (!('constructor' in target)) {
throw new TypeError('Invalid member for struct field');
}

Expand Down Expand Up @@ -248,16 +251,16 @@ export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBuffe
}

function _member<T extends ValidPrimitiveType>(type: T) {
function _(length?: number): (target: object, context?: string | symbol | ClassMemberDecoratorContext) => void;
function _(target: object, context?: string | symbol | ClassMemberDecoratorContext): void;
function _(targetOrLength: object | number, context?: string | symbol | ClassMemberDecoratorContext) {
function decorator(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
function decorator(target: object, context: ClassMemberDecoratorContext): void;
function decorator(targetOrLength: object | number, context?: ClassMemberDecoratorContext) {
if (typeof targetOrLength == 'number') {
return member(type, targetOrLength);
}

return member(type)(targetOrLength, context);
}
return _;
return decorator;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"noImplicitThis": true,
"declaration": true,
"experimentalDecorators": true
"declaration": true
},
"typedocOptions": {
"entryPoints": ["src/index.ts"],
Expand Down

0 comments on commit cba86df

Please sign in to comment.