Fix missing global flag in sanitizeId regex (#303603)

* Fix missing global flag in sanitizeId regex

The regex in sanitizeId was missing the 'g' flag, so only the first
occurrence of '.' or '/' was replaced with '_'. Since settings IDs
contain multiple dots (e.g. 'editor.font.size'), this meant subsequent
dots were left in the sanitized ID.

* Add regression test for sanitizeId global replacement

Export sanitizeId and add a test verifying that all occurrences of '.'
and '/' are replaced in generated tree element IDs, not just the first.

---------

Co-authored-by: Shehab Sherif <shehabsherif0@users.noreply.github.com>
This commit is contained in:
Shehab Sherif
2026-03-20 23:18:13 +00:00
committed by GitHub
co-authored by Shehab Sherif
parent ba9458c65c
commit 5ce6509b44
2 changed files with 20 additions and 3 deletions
@@ -751,8 +751,8 @@ export function inspectSetting(key: string, target: SettingsTarget, languageFilt
return { isConfigured, inspected, targetSelector, inspectedLanguageOverrides, languageSelector: languageFilter };
}
function sanitizeId(id: string): string {
return id.replace(/[\.\/]/, '_');
export function sanitizeId(id: string): string {
return id.replace(/[\.\/]/g, '_');
}
export function settingKeyToDisplayFormat(key: string, groupId: string = '', isLanguageTagSetting: boolean = false): { category: string; label: string } {
@@ -5,7 +5,7 @@
import assert from 'assert';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
import { settingKeyToDisplayFormat, parseQuery, IParsedQuery } from '../../browser/settingsTreeModels.js';
import { settingKeyToDisplayFormat, parseQuery, IParsedQuery, sanitizeId } from '../../browser/settingsTreeModels.js';
suite('SettingsTree', () => {
test('settingKeyToDisplayFormat', () => {
@@ -329,5 +329,22 @@ suite('SettingsTree', () => {
});
});
test('sanitizeId replaces all dots and slashes', () => {
assert.deepStrictEqual(
[
sanitizeId('root.editor.font.size'),
sanitizeId('group/subgroup/setting.key'),
sanitizeId('no-special-chars'),
sanitizeId('single.dot'),
],
[
'root_editor_font_size',
'group_subgroup_setting_key',
'no-special-chars',
'single_dot',
]
);
});
ensureNoDisposablesAreLeakedInTestSuite();
});