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

Conversation

blankPen
Copy link
Contributor

@blankPen blankPen commented Jan 17, 2022

fix(highlight): When text is an empty string, highlight will die cycle

Q                       A
Fixed Issues? Fixes #14166
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

fix(highlight): When s is an empty string, highlight will die cycle
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 17, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/50797/

Copy link
Contributor

@The-x-Theorist The-x-Theorist left a comment

Choose a reason for hiding this comment

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

It wil good if it gets change if (shouldHighlight(options)) { to if (shouldHighlight(options) && code) {

@@ -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.

@@ -262,6 +262,7 @@ export function getChalk(options: Options) {
* Highlight `code`.
*/
export default function highlight(code: string, options: Options = {}): string {
if (!code) return code;
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.

@@ -262,6 +262,7 @@ export function getChalk(options: Options) {
* Highlight `code`.
*/
export default function highlight(code: string, options: Options = {}): string {
if (!code) return code;
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.

This would look more clean.

@@ -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.

This should be removed.

@JLHwung JLHwung added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jan 17, 2022
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

I'm fine with the current approach, but feel free to merge the two if statements. However, if you merge them the check should be code !== "" && shouldHighlight(options) rather than shouldHighlight(options) && code !== "", because the code !== "" check is less expensive.

@nicolo-ribaudo nicolo-ribaudo changed the title When text is an empty string, highlight will be into the infinite loop Avoid infinite loop when highlighting an empty input Jan 18, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit e6cc107 into babel:main Jan 18, 2022
@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jan 18, 2022

Thanks for your PR!

As a suggestion for the future, avoid opening PR from the main branch of your fork, but always create a new branch. By doing so, you can work on more branches (PRs) in parallel, and you prevent your main branch from diverging from our main branch.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: When text is an empty string, highlight will be into the infinite loop
5 participants