Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use maps instead of literals in configureCategories (backport #1598) #1600

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/jsii-diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export class Code<T extends DiagnosticMessageFormatter = DiagnosticMessageFormat
*/
public static lookup(codeOrName: string | number): Code | undefined {
if (typeof codeOrName === 'number') {
return this.byCode[codeOrName];
return this.byCode.get(codeOrName);
}
return this.byName[codeOrName];
return this.byName.get(codeOrName);
}

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

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

/**
Expand Down
6 changes: 6 additions & 0 deletions test/jsii-diagnostic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ describe('jsii diagnostics', () => {
code.category = DiagnosticCategory.Suggestion;
});

test('throws on __proto__ key', () => {
expect(() => configureCategories(JSON.parse('{"__proto__":{"pollutedKey":123}}'))).toThrow(
`Unrecognized diagnostic code '__proto__'`,
);
});

test('diagnostic by name', () => {
configureCategories({
'metadata/package-json-missing-description': DiagnosticCategory.Error,
Expand Down
Loading