Skip to content

Commit 110c3ac

Browse files
authored
Add locale options to tsserver (#12369)
* add locale options to tsserver * make sys an argument * fix mistakes and address code review
1 parent 998246a commit 110c3ac

File tree

3 files changed

+82
-70
lines changed

3 files changed

+82
-70
lines changed

src/compiler/tsc.ts

+1-61
Original file line numberDiff line numberDiff line change
@@ -43,66 +43,6 @@ namespace ts {
4343
}
4444
}
4545

46-
/**
47-
* Checks to see if the locale is in the appropriate format,
48-
* and if it is, attempts to set the appropriate language.
49-
*/
50-
function validateLocaleAndSetLanguage(locale: string, errors: Diagnostic[]): boolean {
51-
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
52-
53-
if (!matchResult) {
54-
errors.push(createCompilerDiagnostic(Diagnostics.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, "en", "ja-jp"));
55-
return false;
56-
}
57-
58-
const language = matchResult[1];
59-
const territory = matchResult[3];
60-
61-
// First try the entire locale, then fall back to just language if that's all we have.
62-
// Either ways do not fail, and fallback to the English diagnostic strings.
63-
if (!trySetLanguageAndTerritory(language, territory, errors)) {
64-
trySetLanguageAndTerritory(language, undefined, errors);
65-
}
66-
67-
return true;
68-
}
69-
70-
function trySetLanguageAndTerritory(language: string, territory: string, errors: Diagnostic[]): boolean {
71-
const compilerFilePath = normalizePath(sys.getExecutingFilePath());
72-
const containingDirectoryPath = getDirectoryPath(compilerFilePath);
73-
74-
let filePath = combinePaths(containingDirectoryPath, language);
75-
76-
if (territory) {
77-
filePath = filePath + "-" + territory;
78-
}
79-
80-
filePath = sys.resolvePath(combinePaths(filePath, "diagnosticMessages.generated.json"));
81-
82-
if (!sys.fileExists(filePath)) {
83-
return false;
84-
}
85-
86-
// TODO: Add codePage support for readFile?
87-
let fileContents = "";
88-
try {
89-
fileContents = sys.readFile(filePath);
90-
}
91-
catch (e) {
92-
errors.push(createCompilerDiagnostic(Diagnostics.Unable_to_open_file_0, filePath));
93-
return false;
94-
}
95-
try {
96-
ts.localizedDiagnosticMessages = JSON.parse(fileContents);
97-
}
98-
catch (e) {
99-
errors.push(createCompilerDiagnostic(Diagnostics.Corrupted_locale_file_0, filePath));
100-
return false;
101-
}
102-
103-
return true;
104-
}
105-
10646
function countLines(program: Program): number {
10747
let count = 0;
10848
forEach(program.getSourceFiles(), file => {
@@ -263,7 +203,7 @@ namespace ts {
263203
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--locale"), /* host */ undefined);
264204
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
265205
}
266-
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
206+
validateLocaleAndSetLanguage(commandLine.options.locale, sys, commandLine.errors);
267207
}
268208

269209
// If there are any errors due to command line parsing and/or

src/compiler/utilities.ts

+76-9
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,15 @@ namespace ts {
472472

473473
export function getTextOfPropertyName(name: PropertyName): string {
474474
switch (name.kind) {
475-
case SyntaxKind.Identifier:
476-
return (<Identifier>name).text;
477-
case SyntaxKind.StringLiteral:
478-
case SyntaxKind.NumericLiteral:
479-
return (<LiteralExpression>name).text;
480-
case SyntaxKind.ComputedPropertyName:
481-
if (isStringOrNumericLiteral((<ComputedPropertyName>name).expression)) {
482-
return (<LiteralExpression>(<ComputedPropertyName>name).expression).text;
483-
}
475+
case SyntaxKind.Identifier:
476+
return (<Identifier>name).text;
477+
case SyntaxKind.StringLiteral:
478+
case SyntaxKind.NumericLiteral:
479+
return (<LiteralExpression>name).text;
480+
case SyntaxKind.ComputedPropertyName:
481+
if (isStringOrNumericLiteral((<ComputedPropertyName>name).expression)) {
482+
return (<LiteralExpression>(<ComputedPropertyName>name).expression).text;
483+
}
484484
}
485485

486486
return undefined;
@@ -4554,4 +4554,71 @@ namespace ts {
45544554

45554555
return flags;
45564556
}
4557+
4558+
/**
4559+
* Checks to see if the locale is in the appropriate format,
4560+
* and if it is, attempts to set the appropriate language.
4561+
*/
4562+
export function validateLocaleAndSetLanguage(
4563+
locale: string,
4564+
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string },
4565+
errors?: Diagnostic[]) {
4566+
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
4567+
4568+
if (!matchResult) {
4569+
if (errors) {
4570+
errors.push(createCompilerDiagnostic(Diagnostics.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, "en", "ja-jp"));
4571+
}
4572+
return;
4573+
}
4574+
4575+
const language = matchResult[1];
4576+
const territory = matchResult[3];
4577+
4578+
// First try the entire locale, then fall back to just language if that's all we have.
4579+
// Either ways do not fail, and fallback to the English diagnostic strings.
4580+
if (!trySetLanguageAndTerritory(language, territory, errors)) {
4581+
trySetLanguageAndTerritory(language, /*territory*/ undefined, errors);
4582+
}
4583+
4584+
function trySetLanguageAndTerritory(language: string, territory: string, errors?: Diagnostic[]): boolean {
4585+
const compilerFilePath = normalizePath(sys.getExecutingFilePath());
4586+
const containingDirectoryPath = getDirectoryPath(compilerFilePath);
4587+
4588+
let filePath = combinePaths(containingDirectoryPath, language);
4589+
4590+
if (territory) {
4591+
filePath = filePath + "-" + territory;
4592+
}
4593+
4594+
filePath = sys.resolvePath(combinePaths(filePath, "diagnosticMessages.generated.json"));
4595+
4596+
if (!sys.fileExists(filePath)) {
4597+
return false;
4598+
}
4599+
4600+
// TODO: Add codePage support for readFile?
4601+
let fileContents = "";
4602+
try {
4603+
fileContents = sys.readFile(filePath);
4604+
}
4605+
catch (e) {
4606+
if (errors) {
4607+
errors.push(createCompilerDiagnostic(Diagnostics.Unable_to_open_file_0, filePath));
4608+
}
4609+
return false;
4610+
}
4611+
try {
4612+
ts.localizedDiagnosticMessages = JSON.parse(fileContents);
4613+
}
4614+
catch (e) {
4615+
if (errors) {
4616+
errors.push(createCompilerDiagnostic(Diagnostics.Corrupted_locale_file_0, filePath));
4617+
}
4618+
return false;
4619+
}
4620+
4621+
return true;
4622+
}
4623+
}
45574624
}

src/server/server.ts

+5
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ namespace ts.server {
578578
}
579579
}
580580

581+
const localeStr = findArgument("--locale");
582+
if (localeStr) {
583+
validateLocaleAndSetLanguage(localeStr, sys);
584+
}
585+
581586
const useSingleInferredProject = hasArgument("--useSingleInferredProject");
582587
const disableAutomaticTypingAcquisition = hasArgument("--disableAutomaticTypingAcquisition");
583588
const telemetryEnabled = hasArgument(Arguments.EnableTelemetry);

0 commit comments

Comments
 (0)