Skip to content

Commit

Permalink
fix: avoid duplicate parameter names (#12)
Browse files Browse the repository at this point in the history
* add test case to capture duplicate parameter name

* skip reserved names by using '0' as a suffix instead of '_'
  • Loading branch information
kwiatkk1 authored Feb 1, 2021
1 parent ebd18d6 commit 1880d0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
});
});

0 comments on commit 1880d0b

Please sign in to comment.