Skip to content

Commit

Permalink
Merge branch 'main' into fsevents-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
adampauls authored Jul 14, 2023
2 parents f9d953c + b865a45 commit a50f8d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/pyright-internal/src/localization/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type StringLookupMap = { [key: string]: string | StringLookupMap };
let localizedStrings: StringLookupMap | undefined = undefined;
let defaultStrings: StringLookupMap = {};

function getRawString(key: string): string {
function getRawStringDefault(key: string): string {
if (localizedStrings === undefined) {
localizedStrings = initialize();
}
Expand All @@ -65,6 +65,16 @@ function getRawString(key: string): string {
fail(`Missing localized string for key "${key}"`);
}

let getRawString = getRawStringDefault;

// Function allowing different strings to be used for messages.
// Returns the previous function used for getting messages.
export function setGetRawString(func: (key: string) => string): (key: string) => string {
const oldLookup = getRawString;
getRawString = func;
return oldLookup;
}

export function getRawStringFromMap(map: StringLookupMap, keyParts: string[]): string | undefined {
let curObj: any = map;

Expand Down
24 changes: 23 additions & 1 deletion packages/pyright-internal/src/tests/localizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import * as assert from 'assert';

import { Localizer, ParameterizedString } from '../localization/localize';
import { Localizer, ParameterizedString, setGetRawString } from '../localization/localize';

const namespaces = [Localizer.Diagnostic, Localizer.DiagnosticAddendum, Localizer.CodeAction];

Expand Down Expand Up @@ -45,3 +45,25 @@ test('Raw strings present', () => {
});
});
});

test('Override a specific string', () => {
// eslint-disable-next-line prefer-const
let originalRawString: ((key: string) => string) | undefined;
function overrideImportResolve(key: string): string {
if (key === 'Diagnostic.importResolveFailure') {
return 'Import is {importName}';
}
return originalRawString!(key);
}
originalRawString = setGetRawString(overrideImportResolve);

const value = Localizer.Diagnostic.importResolveFailure().format({ importName: 'foo' });

try {
assert.equal(value, 'Import is foo');
const nonMovedValue = Localizer.Diagnostic.abstractMethodInvocation().format({ method: 'foo' });
assert.equal(nonMovedValue, 'Method "foo" cannot be called because it is abstract');
} finally {
setGetRawString(originalRawString);
}
});

0 comments on commit a50f8d2

Please sign in to comment.