-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add syntax highlighting to error messages (#4813)
- Loading branch information
1 parent
96dc6e9
commit 8c969de
Showing
13 changed files
with
130 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { type TokenColors, highlight as baseHighlight } from 'tinyhighlight' | ||
import type { ColorName } from './colors' | ||
import { getColors } from './colors' | ||
|
||
type Colors = Record<ColorName, (input: string) => string> | ||
|
||
function getDefs(c: Colors): TokenColors { | ||
const Invalid = (text: string) => c.white(c.bgRed(c.bold(text))) | ||
return { | ||
Keyword: c.magenta, | ||
IdentifierCapitalized: c.yellow, | ||
Punctuator: c.yellow, | ||
StringLiteral: c.green, | ||
NoSubstitutionTemplate: c.green, | ||
MultiLineComment: c.gray, | ||
SingleLineComment: c.gray, | ||
RegularExpressionLiteral: c.cyan, | ||
NumericLiteral: c.blue, | ||
TemplateHead: text => c.green(text.slice(0, text.length - 2)) + c.cyan(text.slice(-2)), | ||
TemplateTail: text => c.cyan(text.slice(0, 1)) + c.green(text.slice(1)), | ||
TemplateMiddle: text => c.cyan(text.slice(0, 1)) + c.green(text.slice(1, text.length - 2)) + c.cyan(text.slice(-2)), | ||
IdentifierCallable: c.blue, | ||
PrivateIdentifierCallable: text => `#${c.blue(text.slice(1))}`, | ||
Invalid, | ||
|
||
JSXString: c.green, | ||
JSXIdentifier: c.yellow, | ||
JSXInvalid: Invalid, | ||
JSXPunctuator: c.yellow, | ||
} | ||
} | ||
|
||
interface HighlightOptions { | ||
jsx?: boolean | ||
colors?: Colors | ||
} | ||
|
||
export function highlight(code: string, options: HighlightOptions = { jsx: false }) { | ||
return baseHighlight(code, { | ||
jsx: options.jsx, | ||
colors: getDefs(options.colors || getColors()), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,5 @@ export interface ErrorWithDiff extends Error { | |
type?: string | ||
frame?: string | ||
diff?: string | ||
codeFrame?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.