Skip to content

Commit 2c69ada

Browse files
committed
refactor: add func getNlsConfiguration & tests
This PR refactors part of vscode.ts and adds a function to get the NLS Configuration. This makes the code more readable and easier to test. And it adds multiple tests for this part of the codebase.
1 parent c5f0b10 commit 2c69ada

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

src/browser/pages/vscode.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,63 @@ const options = getOptions()
55
// TODO: Add proper types.
66
/* eslint-disable @typescript-eslint/no-explicit-any */
77

8-
let nlsConfig: any
8+
// NOTE@jsjoeio
9+
// This lives here ../../../lib/vscode/src/vs/base/common/platform.ts#L106
10+
export const nlsConfigElementId = "vscode-remote-nls-configuration"
11+
12+
type NlsConfiguration = {
13+
locale: string
14+
availableLanguages: { [key: string]: string } | {}
15+
_languagePackId?: string
16+
_translationsConfigFile?: string
17+
_cacheRoot?: string
18+
_resolvedLanguagePackCoreLocation?: string
19+
_corruptedFile?: string
20+
_languagePackSupport?: boolean
21+
loadBundle?: any
22+
}
23+
24+
/**
25+
* A helper function to get the NLS Configuration settings.
26+
*
27+
* This is used by VSCode for localizations (i.e. changing
28+
* the display language).
29+
*
30+
* Make sure to wrap this in a try/catch block when you call it.
31+
**/
32+
export function getNlsConfiguration(document: Document) {
33+
const errorMsgPrefix = "[vscode]"
34+
const nlsConfigElement = document?.getElementById(nlsConfigElementId)
35+
const nlsConfig = nlsConfigElement?.getAttribute("data-settings")
36+
37+
if (!document) {
38+
throw new Error(`${errorMsgPrefix} Could not parse NLS configuration. document is undefined.`)
39+
}
40+
41+
if (!nlsConfigElement) {
42+
throw new Error(
43+
`${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}`,
44+
)
45+
}
46+
47+
if (!nlsConfig) {
48+
return undefined
49+
}
50+
51+
return JSON.parse(nlsConfig) as NlsConfiguration
52+
}
53+
954
try {
10-
nlsConfig = JSON.parse(document.getElementById("vscode-remote-nls-configuration")!.getAttribute("data-settings")!)
11-
if (nlsConfig._resolvedLanguagePackCoreLocation) {
55+
const nlsConfig = getNlsConfiguration(document)
56+
if (nlsConfig?._resolvedLanguagePackCoreLocation) {
1257
const bundles = Object.create(null)
1358
nlsConfig.loadBundle = (bundle: any, _language: any, cb: any): void => {
1459
const result = bundles[bundle]
1560
if (result) {
1661
return cb(undefined, result)
1762
}
1863
// FIXME: Only works if path separators are /.
19-
const path = nlsConfig._resolvedLanguagePackCoreLocation + "/" + bundle.replace(/\//g, "!") + ".nls.json"
64+
const path = nlsConfig?._resolvedLanguagePackCoreLocation + "/" + bundle.replace(/\//g, "!") + ".nls.json"
2065
fetch(`${options.base}/vscode/resource/?path=${encodeURIComponent(path)}`)
2166
.then((response) => response.json())
2267
.then((json) => {
@@ -27,6 +72,7 @@ try {
2772
}
2873
}
2974
} catch (error) {
75+
console.error(error)
3076
/* Probably fine. */
3177
}
3278

test/unit/browser/vscode.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import { JSDOM } from "jsdom"
5+
import { getNlsConfiguration, nlsConfigElementId } from "../../../src/browser/pages/vscode"
6+
7+
describe("vscode", () => {
8+
describe("getNlsConfiguration", () => {
9+
beforeEach(() => {
10+
const { window } = new JSDOM()
11+
global.document = window.document
12+
})
13+
14+
it("should throw an error if Document is undefined", () => {
15+
const errorMsgPrefix = "[vscode]"
16+
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. document is undefined.`
17+
18+
expect(() => {
19+
getNlsConfiguration(undefined as any as Document)
20+
}).toThrowError(errorMessage)
21+
})
22+
it("should throw and error if no nlsConfigElement", () => {
23+
const errorMsgPrefix = "[vscode]"
24+
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}`
25+
26+
expect(() => {
27+
getNlsConfiguration(document)
28+
}).toThrowError(errorMessage)
29+
})
30+
it("should return undefined if no nlsConfig", () => {
31+
const mockElement = document.createElement("div")
32+
mockElement.setAttribute("id", nlsConfigElementId)
33+
document.body.appendChild(mockElement)
34+
35+
const actual = getNlsConfiguration(document)
36+
const expected = undefined
37+
38+
expect(actual).toBe(expected)
39+
40+
document.body.removeChild(mockElement)
41+
})
42+
it("should return the correct configuration", () => {
43+
const mockElement = document.createElement("div")
44+
const dataSettings = {
45+
first: "Jane",
46+
last: "Doe",
47+
}
48+
49+
mockElement.setAttribute("id", nlsConfigElementId)
50+
mockElement.setAttribute("data-settings", JSON.stringify(dataSettings))
51+
document.body.appendChild(mockElement)
52+
const actual = getNlsConfiguration(global.document)
53+
54+
expect(actual).toStrictEqual(dataSettings)
55+
56+
document.body.removeChild(mockElement)
57+
})
58+
})
59+
})

0 commit comments

Comments
 (0)