Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8faaea

Browse filesBrowse files
authored
Only look for file exists and read file on supported locale directories that we build (microsoft#42505)
Fixes microsoft#42263
1 parent 6ed344f commit b8faaea
Copy full SHA for b8faaea

2 files changed

+33-2Lines changed: 33 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/utilitiesPublic.ts‎

Copy file name to clipboardExpand all lines: src/compiler/utilitiesPublic.ts
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ namespace ts {
318318
return getCombinedFlags(node, n => n.flags);
319319
}
320320

321+
/* @internal */
322+
export const supportedLocaleDirectories = ["cs", "de", "es", "fr", "it", "ja", "ko", "pl", "pt-br", "ru", "tr", "zh-cn", "zh-tw"];
323+
321324
/**
322325
* Checks to see if the locale is in the appropriate format,
323326
* and if it is, attempts to set the appropriate language.
@@ -326,7 +329,8 @@ namespace ts {
326329
locale: string,
327330
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string | undefined },
328331
errors?: Push<Diagnostic>) {
329-
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
332+
const lowerCaseLocale = locale.toLowerCase();
333+
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(lowerCaseLocale);
330334

331335
if (!matchResult) {
332336
if (errors) {
@@ -340,7 +344,7 @@ namespace ts {
340344

341345
// First try the entire locale, then fall back to just language if that's all we have.
342346
// Either ways do not fail, and fallback to the English diagnostic strings.
343-
if (!trySetLanguageAndTerritory(language, territory, errors)) {
347+
if (contains(supportedLocaleDirectories, lowerCaseLocale) && !trySetLanguageAndTerritory(language, territory, errors)) {
344348
trySetLanguageAndTerritory(language, /*territory*/ undefined, errors);
345349
}
346350

Collapse file

‎src/testRunner/unittests/publicApi.ts‎

Copy file name to clipboardExpand all lines: src/testRunner/unittests/publicApi.ts
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,30 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => {
124124
assert.equal(type.flags, ts.TypeFlags.Any);
125125
});
126126
});
127+
128+
describe("unittests:: Public APIs:: validateLocaleAndSetLanguage", () => {
129+
let savedUILocale: string | undefined;
130+
beforeEach(() => savedUILocale = ts.getUILocale());
131+
afterEach(() => ts.setUILocale(savedUILocale));
132+
133+
function verifyValidateLocale(locale: string, expectedToReadFile: boolean) {
134+
it(`Verifying ${locale} ${expectedToReadFile ? "reads" : "does not read"} file`, () => {
135+
const errors: ts.Diagnostic[] = [];
136+
ts.validateLocaleAndSetLanguage(locale, {
137+
getExecutingFilePath: () => "/tsc.js",
138+
resolvePath: ts.identity,
139+
fileExists: fileName => {
140+
assert.isTrue(expectedToReadFile, `Locale : ${locale} ${expectedToReadFile ? "should" : "should not"} check if ${fileName} exists.`);
141+
return expectedToReadFile;
142+
},
143+
readFile: fileName => {
144+
assert.isTrue(expectedToReadFile, `Locale : ${locale} ${expectedToReadFile ? "should" : "should not"} read ${fileName}.`);
145+
// Throw error here so that actual change to localized diagnostics messages doesnt take place
146+
throw new Error("cannot read file");
147+
}
148+
}, errors);
149+
});
150+
}
151+
ts.supportedLocaleDirectories.forEach(locale => verifyValidateLocale(locale, /*expctedToReadFile*/ true));
152+
["en", "en-us"].forEach(locale => verifyValidateLocale(locale, /*expctedToReadFile*/ false));
153+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.