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 line alignment tags support when using always #703

Merged
merged 13 commits into from May 10, 2021

Conversation

renatho
Copy link
Contributor

@renatho renatho commented Mar 14, 2021

Part of #680
Close #689

This PR adds support to the tags option when using the always option.

The first PR for this feature was the #689, but the approach wasn't being enough, needing a lot of hacks. So I changed the approach, creating a new transform based on the original align, but adding support to custom tags.

It still reports the errors for the entire block, different than what was suggested in #680 (comment).

Another idea for a new feature in the future:

  • We could add a new option to force the alignment similar to the never for the tags that aren't in the tags when using the always.

@renatho
Copy link
Contributor Author

renatho commented Mar 15, 2021

Ah, it still needs some more tests for full coverage.

EDIT: Done! ✅

@brettz9 brettz9 force-pushed the feature/line-alignment-always-tags branch from 36bab4a to 29a7cc5 Compare March 16, 2021 12:45
@TheJaredWilcurt
Copy link

TheJaredWilcurt commented Mar 24, 2021

Ran it against my codebase and it worked without any bugs. I just wish that you could have 2 spaces after a variable name. without it, it becomes hard to read and looks like it's all just one sentence. example:

/**
 * Mocks the process.platform to a specified value.
 *
 * @param {string} platform 'win32', 'linux', or 'darwin'.
 */

I'd prefer a two space buffer before and after a variable name

/**
 * Mocks the process.platform to a specified value.
 *
 * @param {string}  platform  'win32', 'linux', or 'darwin'.
 */

would be nice if there was a setting for this like min-spaces-before-variable-name: 2 and min-spaces-before-variable-description: 2

@renatho
Copy link
Contributor Author

renatho commented Mar 27, 2021

@TheJaredWilcurt, thank you for testing! About these other requisites, I think it could be part of a different PR.

I also tested it against the Gutenberg project and I identified only one issue. Basically, if we have only the @return tag, it'll add 2 spaces after the tag. I think it's because the approach used with the tokenizers. This issue is currently present already, so I think we could merge this that already have improvements, and we could fix it in another PR.

I'm not sure yet how we could fix this. Do you have any idea @brettz9? I'd like to fix it without changing the align transformer because I tried to make it more similar to the original as possible, so we could remove this if we add it to the comment parser lib. If you don't have any idea, maybe I'll try to fix it in the align transformer code.

@renatho
Copy link
Contributor Author

renatho commented Mar 27, 2021

I checked a little more, and actually I think there is not problem with how it's using the tokenizers. So I already fixed it through the transformer in this commit: 6d29610

Anyway, I'd appreciate your feedback when you have some time, @brettz9 =)

@renatho
Copy link
Contributor Author

renatho commented Mar 28, 2021

I found another issue related to the indentation with tabs. Fixed here: 8389ad3

@renatho
Copy link
Contributor Author

renatho commented Mar 28, 2021

It's almost there. Now I found another weird error with the template tag (duplicating some content).

I think it's probably because this, but I'll try to check it another day.

@renatho
Copy link
Contributor Author

renatho commented Mar 31, 2021

It's almost there. Now I found another weird error with the template tag (duplicating some content).

I think it's probably because this, but I'll try to check it another day.

I took some time to debug it. The issue is that we're getting the name and the description tokens duplicated in the following case, for example:

          /**
           * With template tag.
           *
           * @template T
           *
           * @param {string} lorem Description.
           * @param {int}    sit   Description multi words.
           */
          const fn = ( lorem, sit ) => {}

See the result debugging the commented part.

Screen Shot 2021-03-31 at 18 44 13

The same happens for @template {string} T.

@renatho
Copy link
Contributor Author

renatho commented Apr 20, 2021

Hey @brettz9!
Do you have any idea of where the error could be in that part I mentioned in the previous comment? I suspect it could be in the pos, but honestly, I'm a little confused about the meanings of the variables remainder, pos, and extra. If you have some clues, I could try to fix it.

@brettz9
Copy link
Collaborator

brettz9 commented Apr 21, 2021

If you log against the existing tests, you can see spec.source[0].tokens, and how description gives the remainder after @template (there's no need for name as it is not populated).

pos is the position at which the first whitespace that is itself not preceded by whitespace or a comma is found (i.e., where the template name or sequence of names stop).

extra is then everything after that post-name(s) whitespace (i.e., any extra postName whitespace and any description).

@renatho renatho force-pushed the feature/line-alignment-always-tags branch from 64f77fb to efa2efb Compare May 7, 2021 18:45
@renatho
Copy link
Contributor Author

renatho commented May 7, 2021

If you log against the existing tests, you can see spec.source[0].tokens, and how description gives the remainder after @template (there's no need for name as it is not populated).

pos is the position at which the first whitespace that is itself not preceded by whitespace or a comma is found (i.e., where the template name or sequence of names stop).

extra is then everything after that post-name(s) whitespace (i.e., any extra postName whitespace and any description).

Hey @brettz9 !

Thank you for your explanation! I was creating a fix (just adding a conditional to the postName and description):

        let postName = '';
        let description = '';
        if (pos !== -1) {
          [, postName, description] = extra.match(/(\s*)(.*)/);
        }

But I noticed that part was extracted for another dependency (@es-joy/jsdoccomment), right? I checked the dependency, but the repo URL didn't open for me. I'm thinking that we could merge this PR, and introduce this fix to @es-joy/jsdoccomment. Do you think you could do that?

@brettz9
Copy link
Collaborator

brettz9 commented May 7, 2021

The repo is at https://github.com/es-joy/jsdoccomment , but I can add if I can see the reason for it. However, I don't imagine we can add such a conditional because if it is something like the example used at https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template * @template {string} K - K must be a string or string literal, making it conditional will shave off the postName and description. What is the concrete problem you are trying to solve in making this change?

@renatho
Copy link
Contributor Author

renatho commented May 9, 2021

The repo is at https://github.com/es-joy/jsdoccomment , but I can add if I can see the reason for it. However, I don't imagine we can add such a conditional because if it is something like the example used at https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template * @template {string} K - K must be a string or string literal, making it conditional will shave off the postName and description. What is the concrete problem you are trying to solve in making this change?

Hey @brettz9 !

I'm not in the computer now, but I remember I tested with the examples of this link and it worked well.

When pos is not found (-1), I think it means we don't have anything after the name, right?

Basically, the problem is with a case like (when we don't have anything after the name):

@template T

In this case, it's setting T for name and for description.

@brettz9
Copy link
Collaborator

brettz9 commented May 9, 2021

Ah yes, sorry, @renatho . Fixed in 31.1.1

@renatho
Copy link
Contributor Author

renatho commented May 9, 2021

Nice!!! Than you @brettz9 !! \o/
In this case, this PR is ready for a final review :)

Update: Just added a test with the @template tag using multiple formats.

@renatho renatho force-pushed the feature/line-alignment-always-tags branch 2 times, most recently from 21c1239 to e9b1314 Compare May 9, 2021 21:48
@brettz9 brettz9 force-pushed the feature/line-alignment-always-tags branch from e9b1314 to 09438bf Compare May 10, 2021 13:20
@brettz9 brettz9 merged commit eb10260 into gajus:master May 10, 2021
@brettz9
Copy link
Collaborator

brettz9 commented May 10, 2021

Thanks for all your work and persistence on this, @renatho !

@renatho renatho deleted the feature/line-alignment-always-tags branch May 10, 2021 13:43
@renatho
Copy link
Contributor Author

renatho commented May 10, 2021

Thanks for all your work and persistence on this, @renatho !

Thank you for all your support @brettz9! =)
Just to know, is there a plan to release these changes soon?

brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this pull request May 10, 2021
brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this pull request May 10, 2021
…ys"; gajus#703

Adds custom align transform with support for tags

Co-authored-by: Renatho De Carli Rosa <renatho@automattic.com>
Co-authored-by: Brett Zamir <brettz9@yahoo.com>
@brettz9
Copy link
Collaborator

brettz9 commented May 10, 2021

Ugh, seems my commit message was not well-formed. Adding a commit to revert and yet another to add back in the proper format.

@gajus
Copy link
Owner

gajus commented May 10, 2021

🎉 This PR is included in version 33.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label May 10, 2021
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.

None yet

4 participants