Skip to content

Commit 1a0b9d7

Browse files
fix: use maps instead of literals in configureCategories (backport #1598) (#1599)
# Backport This will backport the following commits from `main` to `maintenance/v5.4`: - [fix: use maps instead of literals in &#x60;configureCategories&#x60; (#1598)](#1598) <!--- Backport version: 9.5.1 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) Co-authored-by: Eli Polonsky <[email protected]>
1 parent 7dd6d70 commit 1a0b9d7

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/jsii-diagnostic.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ export class Code<T extends DiagnosticMessageFormatter = DiagnosticMessageFormat
8686
*/
8787
public static lookup(codeOrName: string | number): Code | undefined {
8888
if (typeof codeOrName === 'number') {
89-
return this.byCode[codeOrName];
89+
return this.byCode.get(codeOrName);
9090
}
91-
return this.byName[codeOrName];
91+
return this.byName.get(codeOrName);
9292
}
9393

94-
private static readonly byCode: { [code: number]: Code } = {};
95-
private static readonly byName: { [name: string]: Code } = {};
94+
private static readonly byCode: Map<number, Code> = new Map();
95+
private static readonly byName: Map<string, Code> = new Map();
9696

9797
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
9898
readonly #defaultCategory: ts.DiagnosticCategory;
@@ -126,7 +126,8 @@ export class Code<T extends DiagnosticMessageFormatter = DiagnosticMessageFormat
126126
if (name in Code.byName) {
127127
throw new Error(`Attempted to create two instances of ${this.constructor.name} with name ${name}`);
128128
}
129-
Code.byCode[code] = Code.byName[name] = this;
129+
Code.byCode.set(code, this);
130+
Code.byName.set(name, this);
130131
}
131132

132133
/**

test/jsii-diagnostic.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ describe('jsii diagnostics', () => {
99
code.category = DiagnosticCategory.Suggestion;
1010
});
1111

12+
test('throws on __proto__ key', () => {
13+
expect(() => configureCategories(JSON.parse('{"__proto__":{"pollutedKey":123}}'))).toThrow(
14+
`Unrecognized diagnostic code '__proto__'`,
15+
);
16+
});
17+
1218
test('diagnostic by name', () => {
1319
configureCategories({
1420
'metadata/package-json-missing-description': DiagnosticCategory.Error,

0 commit comments

Comments
 (0)