Skip to content

Commit 588b417

Browse files
authored
fix: fix typing in theme overriding (#32)
This: `{ [K in keyof typeof HighlightSubject]?: string } ` - gives a type with UPPER CASE keys, but the default theme and how it's resolved via `TOKEN_TYPE_TO_HIGHLIGHT` expects the values, i.e. lower case - string is wrong type as a `StyleFunction` is expected and then called I.e. the types suggest this `new SqlHighlighter({ RESERVED: 'red' })` but this has no effect. To get it to work w/o this fix: `new SqlHighlighter({ reserved: c.red.bold } as any)`
1 parent 1006a0d commit 588b417

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/SqlHighlighter.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import c from 'ansi-colors';
22
import { Token, Tokenizer } from './Tokenizer';
33
import { TokenType, HighlightSubject, TOKEN_TYPE_TO_HIGHLIGHT } from './enums';
44

5+
type Theme = { [key in HighlightSubject]?: c.StyleFunction };
6+
57
export class SqlHighlighter {
68

7-
static readonly DEFAULT_THEME = {
9+
static readonly DEFAULT_THEME: Theme = {
810
[HighlightSubject.QUOTE]: c.yellow,
911
[HighlightSubject.BACKTICK_QUOTE]: c.yellow,
1012
[HighlightSubject.RESERVED]: c.white.bold,
@@ -16,11 +18,13 @@ export class SqlHighlighter {
1618
[HighlightSubject.FUNCTIONS]: c.green.bold,
1719
[HighlightSubject.BUILT_IN]: c.cyan,
1820
[HighlightSubject.LITERAL]: c.cyan,
21+
[HighlightSubject.WHITESPACE]: undefined,
22+
[HighlightSubject.ERROR]: undefined,
1923
};
2024

2125
private readonly tokenizer = new Tokenizer();
2226

23-
constructor(private readonly theme: { [K in keyof typeof HighlightSubject]?: string } = {}) {
27+
constructor(private readonly theme: Theme = {}) {
2428
this.theme = { ...SqlHighlighter.DEFAULT_THEME, ...this.theme };
2529
}
2630

@@ -50,11 +54,15 @@ export class SqlHighlighter {
5054
}
5155

5256
private colorize(type: TokenType, value: string): string {
53-
if (!TOKEN_TYPE_TO_HIGHLIGHT[type] || !this.theme[TOKEN_TYPE_TO_HIGHLIGHT[type]]) {
57+
const subject = TOKEN_TYPE_TO_HIGHLIGHT[type];
58+
if (!subject) {
5459
return value;
5560
}
56-
57-
return this.theme[TOKEN_TYPE_TO_HIGHLIGHT[type]](value);
61+
const fct = this.theme[subject];
62+
if (!fct) {
63+
return value;
64+
}
65+
return fct(value);
5866
}
5967

6068
}

src/enums.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ export enum HighlightSubject {
2828
FUNCTIONS = 'functions',
2929
LITERAL = 'literal',
3030
BUILT_IN = 'builtIn',
31+
WHITESPACE = 'whitespace',
32+
ERROR = 'error',
3133
}
3234

33-
export const TOKEN_TYPE_TO_HIGHLIGHT = {
35+
export const TOKEN_TYPE_TO_HIGHLIGHT: Record<TokenType, HighlightSubject | undefined> = {
3436
[TokenType.BOUNDARY]: HighlightSubject.BOUNDARY,
3537
[TokenType.WORD]: HighlightSubject.WORD,
3638
[TokenType.BACKTICK_QUOTE]: HighlightSubject.BACKTICK_QUOTE,
@@ -44,4 +46,6 @@ export const TOKEN_TYPE_TO_HIGHLIGHT = {
4446
[TokenType.BLOCK_COMMENT]: HighlightSubject.COMMENT,
4547
[TokenType.LITERAL]: HighlightSubject.LITERAL,
4648
[TokenType.BUILT_IN]: HighlightSubject.BUILT_IN,
49+
[TokenType.WHITESPACE]: HighlightSubject.WHITESPACE,
50+
[TokenType.ERROR]: HighlightSubject.ERROR,
4751
};

0 commit comments

Comments
 (0)