Skip to content

Commit

Permalink
Showing 2 changed files with 77 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/knip/src/util/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import type { SerializableFile, SerializedFile } from '../types/serializable-map.js';
import { timerify } from './Performance.js';

// biome-ignore lint/suspicious/noExplicitAny: TODO
// biome-ignore lint/suspicious/noExplicitAny: deal with it
const serializeObj = (obj: any): any => {
if (!obj) return obj;
if (obj instanceof Set) return Array.from(obj);
if (obj instanceof Map) {
const o = serializeObj(Object.fromEntries(obj.entries()));
o._m = 1;
const o: { [key: string]: unknown } = { _m: 1 };
for (const [key, value] of obj) o[key] = serializeObj(value);
return o;
}
if (typeof obj === 'object') for (const key in obj) obj[key] = serializeObj(obj[key]);
return obj;
};

// biome-ignore lint/suspicious/noExplicitAny: TODO
const deserializeObj = (obj: Record<string, any>): Record<string, any> => {
// biome-ignore lint/suspicious/noExplicitAny: deal with it
const deserializeObj = (obj: any): any => {
if (!obj) return obj;
if (Array.isArray(obj)) return new Set(obj);
if (obj._m) {
// biome-ignore lint/performance/noDelete: _m needs to go
delete obj._m;
return new Map(Object.entries(obj).map(v => [v[0], deserializeObj(v[1])]));
const map = new Map();
for (const key in obj) key !== '_m' && map.set(key, deserializeObj(obj[key]));
return map;
}
if (typeof obj === 'object') for (const key in obj) obj[key] = deserializeObj(obj[key]);
return obj;
67 changes: 67 additions & 0 deletions packages/knip/test/util/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { _deserialize, _serialize } from '../../src/util/serialize.js';

test('Should serialize and deserialize file back to original', () => {
const file = {
imported: undefined,
internalImportCache: undefined,
imports: {
internal: new Map([
[
'file',
{
refs: new Set(['ref1', 'ref2']),
imported: new Map([['name', new Set(['file', 'file2'])]]),
importedAs: new Map([['name', new Map([['alias', new Set(['file', 'file2'])]])]]),
importedNs: new Map([['namespace', new Set(['file', 'file2'])]]),
reExportedBy: new Map([['*', new Set(['file', 'file2'])]]),
reExportedAs: new Map([['name', new Map([['alias', new Set(['file', 'file2'])]])]]),
reExportedNs: new Map([['namespace', new Set(['file', 'file2'])]]),
},
],
[
'file2',
{
refs: new Set(['ref1', 'ref2']),
imported: new Map([['name', new Set(['file', 'file2'])]]),
importedAs: new Map([
[
'name',
new Map([
['alias', new Set(['file', 'file2'])],
['alias2', new Set(['file', 'file2'])],
]),
],
]),
importedNs: new Map([['namespace', new Set(['file', 'file2'])]]),
reExportedBy: new Map([
['*', new Set(['file', 'file2'])],
['id', new Set(['file', 'file2'])],
]),
reExportedAs: new Map([['name', new Map([['alias', new Set(['file', 'file2'])]])]]),
reExportedNs: new Map([
['namespace', new Set(['file', 'file2'])],
['namespace2', new Set(['file', 'file2'])],
]),
},
],
]),
external: new Set(['ext']),
unresolved: new Set([{ specifier: 'unresolved', pos: 1, line: 1, col: 1 }]),
},
exports: {
exported: new Map(),
duplicate: new Set([
[
{ symbol: 'def', pos: 1, line: 1, col: 1 },
{ symbol: 'dup', pos: 1, line: 2, col: 2 },
],
]),
},
scripts: new Set(['script', 'script2']),
traceRefs: new Set(['ref']),
};

assert.deepEqual(_deserialize(_serialize(file)), file);
});

0 comments on commit deb3b9c

Please sign in to comment.