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

Add no-redundant-pipe #225

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

OliverJAsh
Copy link
Contributor

@OliverJAsh OliverJAsh commented Dec 11, 2021

Depends on #224

Closes #221

cc @gabro

@kanbanbot kanbanbot added the WIP label Dec 11, 2021
@OliverJAsh OliverJAsh marked this pull request as ready for review December 11, 2021 11:15
@gabro
Copy link
Member

gabro commented Dec 13, 2021

Thanks! I'll take a look once we merge #224 and rebase this on main

@gabro
Copy link
Member

gabro commented Dec 13, 2021

@OliverJAsh could you please rebase this on main now that #224 is merged?

@OliverJAsh OliverJAsh force-pushed the oja/no-redundant-pipe branch from 057be04 to f5d0612 Compare December 13, 2021 18:34
@OliverJAsh
Copy link
Contributor Author

Done!

Copy link
Member

@gabro gabro left a comment

Choose a reason for hiding this comment

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

Great job! I have some potentially blocking comment about the AST generation

suggestions: [
{
messageId: "removePipe",
// TODO: ideally we would preserve the comment here but I'm not sure how
Copy link
Member

@gabro gabro Dec 14, 2021

Choose a reason for hiding this comment

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

If I'm reading this correctly, this is due to the fact that we're synthesizing a new AST node. It's hard to preserve trivias (comments, whitespaces, etc) when manipulating the AST because it's a lossy representation of the source code.
This is why I tend to lean towards reading AST but writing tokens (aka characters). It's harder to work with, but it's less invasive for the final user.

Do you think this fix could be implemented without generating a new AST node? Accidentally removing comments is a show-stopper in my opinion, I think a fix that can produce invalid syntax in some edge cases is still preferable to a silent removal of a comment

Copy link
Contributor Author

@OliverJAsh OliverJAsh Dec 14, 2021

Choose a reason for hiding this comment

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

Thanks for bringing this up! I agree it's not good if we accidentally remove a comment. To be clear, this is only an issue for comments inside the pipe—comments outside are preserved.

Do you think this fix could be implemented without generating a new AST node?

Yes, I think it would be the same as the fix we used to have for no-redundant-flow before b76044a which was to fix #222. Maybe there is another way to fix #222?

That said, in my opinion it's extremely error prone to manipulate code as text rather than as an AST—although this unfortunately seems to be the status quo for ESLint rules, as far as I can tell. As far as I understand, there is no information about comments in TSESTree's representation of the AST. However, there is some information about comments in TypeScript's AST. I wonder if we could implement the fix using TypeScript's AST instead, e.g. by mapping the TSESTree node to a TS node, constructing a new node and then using TypeScript's AST printer. Do you have any experience doing this inside of ESLint rules?

Copy link
Member

Choose a reason for hiding this comment

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

I don't have direct experience with Typescript AST, no, but if it supports not blowing away the comments maybe it's worth a try.

Otherwise we can think of a more ad-hoc fix for the specific use case

Copy link
Contributor Author

@OliverJAsh OliverJAsh Dec 14, 2021

Choose a reason for hiding this comment

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

I think the ad-hoc fix would be to revert b76044a and then find another way to workaround #222, but I'm not sure how else we can workaround this? If the trailing comma is there, we need to adjust the fix to remove that as well.

The TSESTree AST doesn't seem to have any information about whether a Node has a trailing comma, but maybe we can read this from the TS AST instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add no-redundant-pipe
3 participants