From b8faaea90cd26dc496707e08ccf016f0a80feaf0 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 26 Jan 2021 17:43:37 -0800 Subject: [PATCH] Only look for file exists and read file on supported locale directories that we build (#42505) Fixes #42263 --- src/compiler/utilitiesPublic.ts | 8 ++++++-- src/testRunner/unittests/publicApi.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index fcb82c377e3..d45eb73478d 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -318,6 +318,9 @@ namespace ts { return getCombinedFlags(node, n => n.flags); } + /* @internal */ + export const supportedLocaleDirectories = ["cs", "de", "es", "fr", "it", "ja", "ko", "pl", "pt-br", "ru", "tr", "zh-cn", "zh-tw"]; + /** * Checks to see if the locale is in the appropriate format, * and if it is, attempts to set the appropriate language. @@ -326,7 +329,8 @@ namespace ts { locale: string, sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string | undefined }, errors?: Push) { - const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase()); + const lowerCaseLocale = locale.toLowerCase(); + const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(lowerCaseLocale); if (!matchResult) { if (errors) { @@ -340,7 +344,7 @@ namespace ts { // First try the entire locale, then fall back to just language if that's all we have. // Either ways do not fail, and fallback to the English diagnostic strings. - if (!trySetLanguageAndTerritory(language, territory, errors)) { + if (contains(supportedLocaleDirectories, lowerCaseLocale) && !trySetLanguageAndTerritory(language, territory, errors)) { trySetLanguageAndTerritory(language, /*territory*/ undefined, errors); } diff --git a/src/testRunner/unittests/publicApi.ts b/src/testRunner/unittests/publicApi.ts index 79d4b9a2082..eb8e5f18c46 100644 --- a/src/testRunner/unittests/publicApi.ts +++ b/src/testRunner/unittests/publicApi.ts @@ -124,3 +124,30 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => { assert.equal(type.flags, ts.TypeFlags.Any); }); }); + +describe("unittests:: Public APIs:: validateLocaleAndSetLanguage", () => { + let savedUILocale: string | undefined; + beforeEach(() => savedUILocale = ts.getUILocale()); + afterEach(() => ts.setUILocale(savedUILocale)); + + function verifyValidateLocale(locale: string, expectedToReadFile: boolean) { + it(`Verifying ${locale} ${expectedToReadFile ? "reads" : "does not read"} file`, () => { + const errors: ts.Diagnostic[] = []; + ts.validateLocaleAndSetLanguage(locale, { + getExecutingFilePath: () => "/tsc.js", + resolvePath: ts.identity, + fileExists: fileName => { + assert.isTrue(expectedToReadFile, `Locale : ${locale} ${expectedToReadFile ? "should" : "should not"} check if ${fileName} exists.`); + return expectedToReadFile; + }, + readFile: fileName => { + assert.isTrue(expectedToReadFile, `Locale : ${locale} ${expectedToReadFile ? "should" : "should not"} read ${fileName}.`); + // Throw error here so that actual change to localized diagnostics messages doesnt take place + throw new Error("cannot read file"); + } + }, errors); + }); + } + ts.supportedLocaleDirectories.forEach(locale => verifyValidateLocale(locale, /*expctedToReadFile*/ true)); + ["en", "en-us"].forEach(locale => verifyValidateLocale(locale, /*expctedToReadFile*/ false)); +});