Skip to content

Commit b8faaea

Browse 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

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/compiler/utilitiesPublic.ts

+6-2
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

src/testRunner/unittests/publicApi.ts

+27
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)