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

Avoid infinite loop when highlighting an empty input #14165

Merged
merged 2 commits into from Jan 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-highlight/src/index.ts
Expand Up @@ -262,6 +262,7 @@ export function getChalk(options: Options) {
* Highlight `code`.
*/
export default function highlight(code: string, options: Options = {}): string {
if (!code) return code;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!code) return code;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed.

if (shouldHighlight(options)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (shouldHighlight(options)) {
if (shouldHighlight(options) && code) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would look more clean.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It suffices to check empty string only code !== "" because regexp#exec always return non-null results for empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, empty string check will be more efficient and safe.

nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
const chalk = getChalk(options);
const defs = getDefs(chalk);
Expand Down