Skip to content

Commit 475abc7

Browse files
Closer match the native localstorage
1 parent 9ebc0d2 commit 475abc7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

integration-test/test-pages/runtime-checks/pages/generic-overload.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@
3636
localStorage.setItem('test', 'test');
3737
localStorage.other = "test";
3838
window.scriptyRan = true;
39+
let threw = false;
40+
try {
41+
localStorage.setItem('test2'); // should throw
42+
} catch (e) {
43+
threw = true;
44+
}
3945
window.scriptOutput = {
4046
item: localStorage.getItem('test'),
4147
other: localStorage.getItem('other'),
48+
threw
4249
}
4350
`)
4451
const scripty = document.querySelector('script#overloadedScript');
@@ -49,7 +56,8 @@
4956
{ name: 'node and fake node match', result: nodeAndFakeNodeMatch, expected: false },
5057
{ name: 'expected localStorage first response', result: window.scriptOutput, expected: {
5158
item: 'test',
52-
other: 'test'
59+
other: 'test',
60+
threw: true
5361
}},
5462
{ name: 'did not globally store', result: localStorage.getItem('test'), expected: null },
5563
{ name: 'did session store', result: sessionStorage.getItem('test'), expected: 'test' },
@@ -77,6 +85,12 @@
7785
sessionStorage.clear();
7886
sessionStorage.setItem('test', 'test');
7987
sessionStorage.other = "test";
88+
let threw = false;
89+
try {
90+
sessionStorage.setItem('test2'); // should throw
91+
} catch (e) {
92+
threw = true;
93+
}
8094
window.script2Output = {
8195
item: sessionStorage.getItem('test'),
8296
other: sessionStorage.getItem('other'),
@@ -90,7 +104,8 @@
90104
{ name: 'node and fake node match', result: nodeAndFakeNodeMatch, expected: false },
91105
{ name: 'expected localStorage first response', result: window.script2Output, expected: {
92106
item: 'test',
93-
other: 'test'
107+
other: 'test',
108+
threw: true
94109
}},
95110
{ name: 'did not globally store', result: localStorage.getItem('test'), expected: null },
96111
{ name: 'did session store', result: sessionStorage.getItem('test'), expected: null },

src/features/runtime-checks.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,22 +665,43 @@ export default class RuntimeChecks extends ContentFeature {
665665
class MemoryStorage {
666666
#data = {}
667667

668+
/**
669+
* @param {Parameters<Storage['setItem']>[0]} id
670+
* @param {Parameters<Storage['setItem']>[1]} val
671+
* @returns {ReturnType<Storage['setItem']>}
672+
*/
668673
setItem (id, val) {
674+
if (arguments.length < 2) throw new TypeError(`Failed to execute 'setItem' on 'Storage': 2 arguments required, but only ${arguments.length} present.`)
669675
this.#data[id] = String(val)
670676
}
671677

678+
/**
679+
* @param {Parameters<Storage['getItem']>[0]} id
680+
* @returns {ReturnType<Storage['getItem']>}
681+
*/
672682
getItem (id) {
673-
return Object.prototype.hasOwnProperty.call(this.#data, id) ? this.#data[id] : undefined
683+
return Object.prototype.hasOwnProperty.call(this.#data, id) ? this.#data[id] : null
674684
}
675685

686+
/**
687+
* @param {Parameters<Storage['removeItem']>[0]} id
688+
* @returns {ReturnType<Storage['removeItem']>}
689+
*/
676690
removeItem (id) {
677-
return delete this.#data[id]
691+
delete this.#data[id]
678692
}
679693

694+
/**
695+
* @returns {ReturnType<Storage['clear']>}
696+
*/
680697
clear () {
681698
this.#data = {}
682699
}
683700

701+
/**
702+
* @param {Parameters<Storage['key']>[0]} n
703+
* @returns {ReturnType<Storage['key']>}
704+
*/
684705
key (n) {
685706
const keys = Object.keys(this.#data)
686707
return keys[n]

0 commit comments

Comments
 (0)