Skip to content

Commit 8d9fa68

Browse files
committed
Avoid global mutations
This removes a few places where global compiler bindings are mutated: 1. Make `sysLog` call an internal binding which is changed via `setSysLog`. 2. Use `Object.assign` to change values *in* `objectAllocator` instead of mutating the binding itself. (The type should verify that any future uses of this will properly override all bindings.) 3. `localizedDiagnosticMessages` is not really needed as an exported value, there's only one place that uses it to test whether it is set or not. So drop the export and replace it with a new `maybeSetLocalizedDiagnosticMessages` (internal) function.
1 parent 9e2b7ad commit 8d9fa68

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/compiler/sys.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,16 @@ namespace ts {
499499
/*@internal*/
500500
export const ignoredPaths = ["/node_modules/.", "/.git", "/.#"];
501501

502+
let curSysLog: (s: string) => void = noop; // eslint-disable-line prefer-const
503+
502504
/*@internal*/
503-
export let sysLog: (s: string) => void = noop; // eslint-disable-line prefer-const
505+
export function sysLog(s: string) {
506+
return curSysLog(s);
507+
}
504508

505509
/*@internal*/
506510
export function setSysLog(logger: typeof sysLog) {
507-
sysLog = logger;
511+
curSysLog = logger;
508512
}
509513

510514
/*@internal*/

src/compiler/utilities.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5899,7 +5899,7 @@ namespace ts {
58995899
}
59005900

59015901
// eslint-disable-next-line prefer-const
5902-
export let objectAllocator: ObjectAllocator = {
5902+
export const objectAllocator: ObjectAllocator = {
59035903
getNodeConstructor: () => Node as any,
59045904
getTokenConstructor: () => Token as any,
59055905
getIdentifierConstructor: () => Identifier as any,
@@ -5912,20 +5912,27 @@ namespace ts {
59125912
};
59135913

59145914
export function setObjectAllocator(alloc: ObjectAllocator) {
5915-
objectAllocator = alloc;
5915+
Object.assign(objectAllocator, alloc);
59165916
}
59175917

59185918
export function formatStringFromArgs(text: string, args: ArrayLike<string | number>, baseIndex = 0): string {
59195919
return text.replace(/{(\d+)}/g, (_match, index: string) => "" + Debug.checkDefined(args[+index + baseIndex]));
59205920
}
59215921

5922-
export let localizedDiagnosticMessages: MapLike<string> | undefined;
5922+
let localizedDiagnosticMessages: MapLike<string> | undefined;
59235923

59245924
/* @internal */
59255925
export function setLocalizedDiagnosticMessages(messages: typeof localizedDiagnosticMessages) {
59265926
localizedDiagnosticMessages = messages;
59275927
}
59285928

5929+
/* @internal */
5930+
export function maybeSetLocalizedDiagnosticMessages(getMessages: undefined | (() => typeof localizedDiagnosticMessages)) {
5931+
if (!localizedDiagnosticMessages && getMessages) {
5932+
localizedDiagnosticMessages = getMessages();
5933+
}
5934+
}
5935+
59295936
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
59305937
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key] || message.message;
59315938
}

src/services/services.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,10 +1259,9 @@ namespace ts {
12591259
: NoopCancellationToken;
12601260

12611261
const currentDirectory = host.getCurrentDirectory();
1262-
// Check if the localized messages json is set, otherwise query the host for it
1263-
if (!localizedDiagnosticMessages && host.getLocalizedDiagnosticMessages) {
1264-
setLocalizedDiagnosticMessages(host.getLocalizedDiagnosticMessages());
1265-
}
1262+
1263+
// Checks if the localized messages json is set, and if not, query the host for it
1264+
maybeSetLocalizedDiagnosticMessages(host.getLocalizedDiagnosticMessages?.bind(host));
12661265

12671266
function log(message: string) {
12681267
if (host.log) {

0 commit comments

Comments
 (0)