-
Notifications
You must be signed in to change notification settings - Fork 6k
refactor: add func and tests for getNlsConfiguration #3640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,56 @@ const options = getOptions() | |
// TODO: Add proper types. | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
let nlsConfig: any | ||
// NOTE@jsjoeio | ||
// This lives here ../../../lib/vscode/src/vs/base/common/platform.ts#L106 | ||
export const nlsConfigElementId = "vscode-remote-nls-configuration" | ||
|
||
type NlsConfiguration = { | ||
locale: string | ||
availableLanguages: { [key: string]: string } | {} | ||
_languagePackId?: string | ||
_translationsConfigFile?: string | ||
_cacheRoot?: string | ||
_resolvedLanguagePackCoreLocation?: string | ||
_corruptedFile?: string | ||
_languagePackSupport?: boolean | ||
loadBundle?: any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incremental type improvements 😄 One day, we will be able to remove that |
||
} | ||
|
||
/** | ||
* A helper function to get the NLS Configuration settings. | ||
* | ||
* This is used by VSCode for localizations (i.e. changing | ||
* the display language). | ||
* | ||
* Make sure to wrap this in a try/catch block when you call it. | ||
**/ | ||
export function getNlsConfiguration(document: Document) { | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const errorMsgPrefix = "[vscode]" | ||
const nlsConfigElement = document?.getElementById(nlsConfigElementId) | ||
const nlsConfig = nlsConfigElement?.getAttribute("data-settings") | ||
|
||
if (!document) { | ||
throw new Error(`${errorMsgPrefix} Could not parse NLS configuration. document is undefined.`) | ||
} | ||
|
||
if (!nlsConfigElement) { | ||
throw new Error( | ||
`${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}`, | ||
) | ||
} | ||
|
||
if (!nlsConfig) { | ||
throw new Error( | ||
`${errorMsgPrefix} Could not parse NLS configuration. Found nlsConfigElement but missing data-settings attribute.`, | ||
) | ||
} | ||
|
||
return JSON.parse(nlsConfig) as NlsConfiguration | ||
} | ||
|
||
try { | ||
nlsConfig = JSON.parse(document.getElementById("vscode-remote-nls-configuration")!.getAttribute("data-settings")!) | ||
const nlsConfig = getNlsConfiguration(document) | ||
if (nlsConfig._resolvedLanguagePackCoreLocation) { | ||
const bundles = Object.create(null) | ||
nlsConfig.loadBundle = (bundle: any, _language: any, cb: any): void => { | ||
|
@@ -26,26 +73,25 @@ try { | |
.catch(cb) | ||
} | ||
} | ||
;(self.require as any) = { | ||
// Without the full URL VS Code will try to load file://. | ||
baseUrl: `${window.location.origin}${options.csStaticBase}/lib/vscode/out`, | ||
recordStats: true, | ||
paths: { | ||
"vscode-textmate": `../node_modules/vscode-textmate/release/main`, | ||
"vscode-oniguruma": `../node_modules/vscode-oniguruma/release/main`, | ||
xterm: `../node_modules/xterm/lib/xterm.js`, | ||
"xterm-addon-search": `../node_modules/xterm-addon-search/lib/xterm-addon-search.js`, | ||
"xterm-addon-unicode11": `../node_modules/xterm-addon-unicode11/lib/xterm-addon-unicode11.js`, | ||
"xterm-addon-webgl": `../node_modules/xterm-addon-webgl/lib/xterm-addon-webgl.js`, | ||
"tas-client-umd": `../node_modules/tas-client-umd/lib/tas-client-umd.js`, | ||
"iconv-lite-umd": `../node_modules/iconv-lite-umd/lib/iconv-lite-umd.js`, | ||
jschardet: `../node_modules/jschardet/dist/jschardet.min.js`, | ||
}, | ||
"vs/nls": nlsConfig, | ||
} | ||
} catch (error) { | ||
/* Probably fine. */ | ||
} | ||
|
||
;(self.require as any) = { | ||
// Without the full URL VS Code will try to load file://. | ||
baseUrl: `${window.location.origin}${options.csStaticBase}/lib/vscode/out`, | ||
recordStats: true, | ||
paths: { | ||
"vscode-textmate": `../node_modules/vscode-textmate/release/main`, | ||
"vscode-oniguruma": `../node_modules/vscode-oniguruma/release/main`, | ||
xterm: `../node_modules/xterm/lib/xterm.js`, | ||
"xterm-addon-search": `../node_modules/xterm-addon-search/lib/xterm-addon-search.js`, | ||
"xterm-addon-unicode11": `../node_modules/xterm-addon-unicode11/lib/xterm-addon-unicode11.js`, | ||
"xterm-addon-webgl": `../node_modules/xterm-addon-webgl/lib/xterm-addon-webgl.js`, | ||
"tas-client-umd": `../node_modules/tas-client-umd/lib/tas-client-umd.js`, | ||
"iconv-lite-umd": `../node_modules/iconv-lite-umd/lib/iconv-lite-umd.js`, | ||
jschardet: `../node_modules/jschardet/dist/jschardet.min.js`, | ||
}, | ||
"vs/nls": nlsConfig, | ||
console.error(error) | ||
} | ||
|
||
try { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { JSDOM } from "jsdom" | ||
import { getNlsConfiguration, nlsConfigElementId } from "../../../src/browser/pages/vscode" | ||
|
||
describe("vscode", () => { | ||
describe("getNlsConfiguration", () => { | ||
beforeEach(() => { | ||
const { window } = new JSDOM() | ||
global.document = window.document | ||
}) | ||
|
||
it("should throw an error if Document is undefined", () => { | ||
const errorMsgPrefix = "[vscode]" | ||
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. document is undefined.` | ||
|
||
expect(() => { | ||
getNlsConfiguration(undefined as any as Document) | ||
}).toThrowError(errorMessage) | ||
}) | ||
it("should throw an error if no nlsConfigElement", () => { | ||
const errorMsgPrefix = "[vscode]" | ||
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}` | ||
|
||
expect(() => { | ||
getNlsConfiguration(document) | ||
}).toThrowError(errorMessage) | ||
}) | ||
it("should throw an error if no nlsConfig", () => { | ||
const mockElement = document.createElement("div") | ||
mockElement.setAttribute("id", nlsConfigElementId) | ||
document.body.appendChild(mockElement) | ||
|
||
const errorMsgPrefix = "[vscode]" | ||
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Found nlsConfigElement but missing data-settings attribute.` | ||
|
||
expect(() => { | ||
getNlsConfiguration(document) | ||
}).toThrowError(errorMessage) | ||
|
||
document.body.removeChild(mockElement) | ||
}) | ||
it("should return the correct configuration", () => { | ||
const mockElement = document.createElement("div") | ||
const dataSettings = { | ||
first: "Jane", | ||
last: "Doe", | ||
} | ||
|
||
mockElement.setAttribute("id", nlsConfigElementId) | ||
mockElement.setAttribute("data-settings", JSON.stringify(dataSettings)) | ||
document.body.appendChild(mockElement) | ||
const actual = getNlsConfiguration(global.document) | ||
|
||
expect(actual).toStrictEqual(dataSettings) | ||
|
||
document.body.removeChild(mockElement) | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.