Skip to content

Commit a0a0173

Browse files
author
Benjamin Pasero
committed
more cleanup of file storage
1 parent 555597e commit a0a0173

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/vs/platform/storage/node/storage.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as fs from 'original-fs';
1010
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1111
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
1212
import { writeFileAndFlushSync } from 'vs/base/node/extfs';
13+
import { isUndefined, isUndefinedOrNull } from 'vs/base/common/types';
1314

1415
export const IStorageService = createDecorator<IStorageService>('storageService');
1516

@@ -33,7 +34,7 @@ export class FileStorage {
3334
}
3435

3536
const res = this.database[key];
36-
if (typeof res === 'undefined') {
37+
if (isUndefinedOrNull(res)) {
3738
return defaultValue;
3839
}
3940

@@ -45,6 +46,11 @@ export class FileStorage {
4546
this.database = this.load();
4647
}
4748

49+
// Remove an item when it is undefined or null
50+
if (isUndefinedOrNull(data)) {
51+
return this.removeItem(key);
52+
}
53+
4854
// Shortcut for primitives that did not change
4955
if (typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean') {
5056
if (this.database[key] === data) {
@@ -61,8 +67,9 @@ export class FileStorage {
6167
this.database = this.load();
6268
}
6369

64-
if (this.database[key]) {
65-
delete this.database[key];
70+
// Only update if the key is actually present (not undefined)
71+
if (!isUndefined(this.database[key])) {
72+
this.database[key] = void 0;
6673
this.save();
6774
}
6875
}

src/vs/platform/storage/test/node/storage.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ suite('StorageService', () => {
4444
service.setItem('some.other.key', 'some.other.value');
4545
assert.equal(service.getItem('some.other.key'), 'some.other.value');
4646

47+
service.setItem('some.undefined.key', void 0);
48+
assert.equal(service.getItem('some.undefined.key', 'some.default'), 'some.default');
49+
50+
service.setItem('some.null.key', null);
51+
assert.equal(service.getItem('some.null.key', 'some.default'), 'some.default');
52+
4753
done();
4854
});
4955
});

0 commit comments

Comments
 (0)