Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/configWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function watchConfigurationChanges(

const newValue = getValue();

if (!isDeepStrictEqual(newValue, appliedValues.get(setting))) {
if (!configValuesEqual(newValue, appliedValues.get(setting))) {
changedSettings.push(setting);
appliedValues.set(setting, newValue);
}
Expand All @@ -37,3 +37,17 @@ export function watchConfigurationChanges(
}
});
}

function configValuesEqual(a: unknown, b: unknown): boolean {
return isDeepStrictEqual(normalizeEmptyValue(a), normalizeEmptyValue(b));
}

/**
* Normalize "empty" string values to a canonical form for comparison.
Comment thread
EhabY marked this conversation as resolved.
Outdated
*/
function normalizeEmptyValue(value: unknown): unknown {
if (value === undefined || value === null || value === "") {
Comment thread
EhabY marked this conversation as resolved.
Outdated
return undefined;
}
return value;
}
39 changes: 39 additions & 0 deletions test/unit/configWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,43 @@ describe("watchConfigurationChanges", () => {
expect(changes).toEqual([["test.setting"]]);
dispose();
});

it("treats undefined, null, and empty string as equivalent", () => {
const config = new MockConfigurationProvider();
config.set("test.setting", undefined);
const { changes, dispose } = createWatcher("test.setting");

config.set("test.setting", null);
config.set("test.setting", "");
config.set("test.setting", undefined);

expect(changes).toEqual([]);
dispose();
});

interface ValueChangeTestCase {
name: string;
from: unknown;
to: unknown;
}

it.each<ValueChangeTestCase>([
{ name: "undefined to value", from: undefined, to: "value" },
{ name: "value to empty string", from: "value", to: "" },
{ name: "undefined to false", from: undefined, to: false },
{ name: "undefined to zero", from: undefined, to: 0 },
{ name: "undefined to empty array", from: undefined, to: [] },
{ name: "null to value", from: null, to: "value" },
{ name: "empty string to value", from: "", to: "value" },
{ name: "value to different value", from: "old", to: "new" },
])("detects change: $name", ({ from, to }) => {
const config = new MockConfigurationProvider();
config.set("test.setting", from);
const { changes, dispose } = createWatcher("test.setting");

config.set("test.setting", to);

expect(changes).toEqual([["test.setting"]]);
dispose();
});
});