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

Improve prefer-lookaround rule to report when there are leading/trailing assertions #480

Merged
merged 5 commits into from Nov 20, 2022

Conversation

ota-meshi
Copy link
Owner

close #478

Copy link
Collaborator

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

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

Thank you for implementing this!

I find 2 things we should talk about.

lib/rules/prefer-lookaround.ts Outdated Show resolved Hide resolved
lib/rules/prefer-lookaround.ts Outdated Show resolved Hide resolved
@@ -172,6 +180,190 @@ function getSideEffectsWhenReplacingCapturingGroup(
}
}

/** Checks if the given element is a zero length element that can be placed as a leading/trailing element. */
function isLeadingTrailingElement(element: Element): boolean {
if (element.type === "CapturingGroup") return false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it not allowed to be a capturing groups? A zero-length capturing group is useless, but would it be wrong if we transformed those capturing groups? E.g. /foo(bar)(\b|$)/ -> /foo(?=bar(\b|$))/ should be fine as long as this capturing group isn't used in the replacement string, right?

Copy link
Owner Author

Choose a reason for hiding this comment

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

You're right! I will change this PR.

Copy link
Collaborator

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

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

Thank you @ota-meshi!

@RunDevelopment RunDevelopment merged commit e040e79 into master Nov 20, 2022
@RunDevelopment RunDevelopment deleted the prefer-lookaround branch November 20, 2022 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prefer-lookaround should ignore ^ and $
2 participants