Skip to content

Commit

Permalink
Fix prod crash hidden by --downLevelIteration (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Jul 25, 2020
1 parent c617bc7 commit 616d0b6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
untransformKey,
untransformValue,
} from './transformer';
import * as IteratorUtils from "./iteratorutils"

export interface Annotations {
root?: TypeAnnotation;
Expand Down Expand Up @@ -51,7 +52,7 @@ export const makeAnnotator = () => {
if (isMap(node)) {
const newNode = new Map<string, any>();

for (const [key, value] of node.entries()) {
IteratorUtils.forEach(node.entries(), ([key, value]) => {
const transformed = transformKey(key);

if (transformed) {
Expand All @@ -64,7 +65,7 @@ export const makeAnnotator = () => {
annotations.keys[stringifyPath([...path, transformed.key])] =
transformed.type;
}
}
})

node = newNode;
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { JSONValue, SuperJSONValue } from 'types';

import * as SuperJSON from './';
import { JSONValue, SuperJSONValue } from './types';
import { Annotations } from './annotator';

describe('stringify & parse', () => {
Expand Down
20 changes: 20 additions & 0 deletions src/iteratorutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function forEach<T>(iterator: Iterator<T>, func: (v: T) => void) {
while (true) {
const { done, value } = iterator.next()
if (done) {
return;
}

func(value);
}
}

export function map<A, B>(iterator: Iterator<A>, map: (v: A, index: number) => B): B[] {
const result: B[] = [];

forEach(iterator, value => {
result.push(map(value, result.length))
})

return result;
}
23 changes: 11 additions & 12 deletions src/plainer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as IteratorUtils from "./iteratorutils"
import { isArray, isMap, isPlainObject, isPrimitive, isSet } from './is';

interface WalkerValue {
Expand All @@ -11,13 +12,13 @@ export type Walker = (v: WalkerValue) => any;
const isDeep = (object: any): boolean =>
isPlainObject(object) || isArray(object) || isMap(object) || isSet(object);

const entries = (object: object | Map<any, any>): [any, any][] => {
const entries = (object: object | Map<any, any>): Iterator<[any, any]> => {
if (isMap(object)) {
return [...object.entries()];
return object.entries();
}

if (isPlainObject(object)) {
return Object.entries(object);
return Object.entries(object).values()
}

throw new Error('Illegal Argument: ' + typeof object);
Expand All @@ -44,17 +45,15 @@ export const plainer = (
walker({ isLeaf: false, path, node: object });

if (isArray(object) || isSet(object)) {
return [...object].map((value, key) =>
plainer(value, walker, [...path, key], alreadySeenObjects)
);
return IteratorUtils.map(object.values(), (value, index) =>
plainer(value, walker, [...path, index], alreadySeenObjects)
);
}

if (isPlainObject(object) || isMap(object)) {
return Object.fromEntries(
entries(object).map(([key, value]) => [
key,
plainer(value, walker, [...path, key], alreadySeenObjects),
])
);
return Object.fromEntries(IteratorUtils.map(entries(object), ([key, value]) => [
key,
plainer(value, walker, [...path, key], alreadySeenObjects),
]));
}
};
13 changes: 2 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"include": ["src", "types"],
"include": ["src"],
"compilerOptions": {
// custom
"downlevelIteration": true,

// tsdx
"module": "esnext",
"lib": ["dom", "esnext"],
"lib": ["esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
Expand All @@ -17,11 +13,6 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
}
}

0 comments on commit 616d0b6

Please sign in to comment.