From 1880d0bc70387923c2876f3e1c60eaff9a0115f4 Mon Sep 17 00:00:00 2001 From: Krzysztof Kwiatkowski Date: Mon, 1 Feb 2021 11:22:36 +0100 Subject: [PATCH] fix: avoid duplicate parameter names (#12) * add test case to capture duplicate parameter name * skip reserved names by using '0' as a suffix instead of '_' --- src/index.ts | 2 +- test/test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index c2258ea..3598000 100644 --- a/src/index.ts +++ b/src/index.ts @@ -227,7 +227,7 @@ function getName(num: number) { num = ~~(num / chars.length) - 1; } while (num >= 0); - return reserved.test(name) ? `${name}_` : name; + return reserved.test(name) ? `${name}0` : name; } function isPrimitive(thing: any) { diff --git a/test/test.ts b/test/test.ts index 1fb98f6..06f14d0 100644 --- a/test/test.ts +++ b/test/test.ts @@ -144,5 +144,12 @@ describe('devalue', () => { // test('objects with symbolic keys', { [Symbol()]: null, 'key': 'val' }, '{key:"val"}'); + it('do not have duplicates in generated argument names', () => { + const foo = new Array(20000).fill(0).map((_, i) => i); + const bar = foo.map((_, i) => ({ [i]: foo[i] })); + const serialized = devalue([foo, ...bar]); + + assert.doesNotThrow(() => eval(serialized), /Duplicate parameter name/); + }); }); });