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 109 #112

Merged
merged 5 commits into from
Feb 3, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,46 @@ describe('stringify & parse', () => {
output: null,
outputAnnotations: { values: ['undefined'] },
},

'regression #109: nested classes': {
input: () => {
class Pet {
constructor(private name: string) {}

woof() {
return this.name;
}
}

class User {
constructor(public pet: Pet) {}
}

SuperJSON.registerClass(Pet);
SuperJSON.registerClass(User);

const pet = new Pet('Rover');
const user = new User(pet);

return user;
},
output: {
pet: {
name: 'Rover',
},
},
outputAnnotations: {
values: [
['class', 'User'],
{
pet: [['class', 'Pet']],
},
],
},
customExpectations(value) {
expect(value.pet.woof()).toEqual('Rover');
},
},
};

function deepFreeze(object: any, alreadySeenObjects = new Set()) {
Expand Down Expand Up @@ -609,7 +649,9 @@ describe('stringify & parse', () => {
}
expect(meta).toEqual(expectedOutputAnnotations);

const untransformed = SuperJSON.deserialize({ json, meta });
const untransformed = SuperJSON.deserialize(
JSON.parse(JSON.stringify({ json, meta }))
);
if (!dontExpectEquality) {
expect(untransformed).toEqual(inputValue);
}
Expand Down Expand Up @@ -864,7 +906,7 @@ test('performance regression', () => {
SuperJSON.serialize(data);
const t2 = Date.now();
const duration = t2 - t1;
expect(duration).toBeLessThan(500);
expect(duration).toBeLessThan(700);
});

test('regression #95: no undefined', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/plainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('plainer', () => {

const output = plainer(input, ({ path, node }) => {
annotations[path.join('.')] = node;
if (node instanceof Map) {
return [...node.entries()];
}
if (node instanceof Set) {
// eslint-disable-next-line es5/no-es6-methods
return [...node.values()];
}
return node;
});

Expand Down Expand Up @@ -60,8 +67,16 @@ describe('plainer', () => {
2 => "hallo",
undefined => null,
},
"a.0": Array [
2,
"hallo",
],
"a.0.0": 2,
"a.0.1": "hallo",
"a.1": Array [
undefined,
null,
],
"a.1.0": undefined,
"a.1.1": null,
"b": Object {
Expand Down
24 changes: 7 additions & 17 deletions src/plainer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isArray, isMap, isPlainObject, isPrimitive, isSet } from './is';
import { isInstanceOfRegisteredClass } from './transformer';
import { includes, mapValues } from './util';

interface WalkerValue {
Expand All @@ -10,7 +11,11 @@ interface WalkerValue {
export type Walker = (v: WalkerValue) => any;

const isDeep = (object: any): boolean =>
isPlainObject(object) || isArray(object) || isMap(object) || isSet(object);
isPlainObject(object) ||
isArray(object) ||
isMap(object) ||
isSet(object) ||
isInstanceOfRegisteredClass(object);

export const plainer = (
object: any,
Expand All @@ -22,7 +27,7 @@ export const plainer = (
return walker({ isLeaf: true, node: object, path });
}

walker({ isLeaf: false, path, node: object });
object = walker({ isLeaf: false, path, node: object });

if (includes(alreadySeenObjects, object)) {
return null;
Expand All @@ -38,21 +43,6 @@ export const plainer = (
);
}

if (isSet(object)) {
// (sets only exist in es6+)
// eslint-disable-next-line es5/no-es6-methods
return [...object.values()].map((value, index) =>
plainer(value, walker, [...path, index], alreadySeenObjects)
);
}

if (isMap(object)) {
return [...object.entries()].map(([key, value], index) => [
plainer(key, walker, [...path, index, 0], alreadySeenObjects),
plainer(value, walker, [...path, index, 1], alreadySeenObjects),
]);
}

if (isPlainObject(object)) {
return mapValues(object, (value, key) =>
plainer(value, walker, [...path, key], alreadySeenObjects)
Expand Down
28 changes: 16 additions & 12 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ const simpleRules = [
'set',
// (sets only exist in es6+)
// eslint-disable-next-line es5/no-es6-methods
v => Object.values(v),
v => [...v.values()],
v => new Set(v)
),
simpleTransformation(
isMap,
'map',
v => Object.entries(v),
v => [...v.entries()],
v => new Map(v)
),

Expand Down Expand Up @@ -217,24 +217,28 @@ const symbolRule = compositeTransformation(
}
);

export function isInstanceOfRegisteredClass(
potentialClass: any
): potentialClass is any {
if (potentialClass?.constructor) {
const isRegistered = !!ClassRegistry.getIdentifier(
potentialClass.constructor
);
return isRegistered;
}
return false;
}

const classRule = compositeTransformation(
(potentialClass): potentialClass is any => {
if (potentialClass?.constructor) {
const isRegistered = !!ClassRegistry.getIdentifier(
potentialClass.constructor
);
return isRegistered;
}
return false;
},
isInstanceOfRegisteredClass,
clazz => {
const identifier = ClassRegistry.getIdentifier(clazz.constructor);
return ['class', identifier!];
},
clazz => {
const allowedProps = ClassRegistry.getAllowedProps(clazz.constructor);
if (!allowedProps) {
return clazz;
return { ...clazz };
}

const result: any = {};
Expand Down