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

feat: add the fixer function to eslint/rules/vars-order #3224

Merged
merged 10 commits into from
Nov 29, 2024

Conversation

headlessNode
Copy link
Member

Resolves #3222.

Description

What is the purpose of this pull request?

This pull request:

  • Add a fixer function to eslint/rules/vars-order to allow auto-formatting during code review.

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@headlessNode headlessNode added the Feature Issue or pull request for adding a new feature. label Nov 23, 2024
Signed-off-by: Muhammad Haris <[email protected]>
@headlessNode headlessNode marked this pull request as draft November 23, 2024 06:49
@headlessNode
Copy link
Member Author

headlessNode commented Nov 23, 2024

@Planeshifter @kgryte I would like your reviews on this. I got the fixer function working independently .i.e. outside stdlib. But in this branch it doesn't work. I guess there is a step to rebuild that I'd need run?

The rule falls in the --fix-type layout category so that is not the issue I think.

@Planeshifter
Copy link
Member

Planeshifter commented Nov 23, 2024

@headlessNode Yes, you are correct: one currently needs to run make init to generate n custom ESLint bundle with our custom rules in order for them to be applied.

Please update the test fixtures of this package though to verify that the new lint rule works. See e.g. https://github.com/stdlib-js/stdlib/blob/develop/lib/node_modules/%40stdlib/_tools/eslint/rules/require-spaces/test/fixtures/invalid.js for how to specify expected output.

@headlessNode
Copy link
Member Author

@Planeshifter I've updated the test fixtures. The last case fails and that is because of the fixer function's logic.

The fixer function currently swaps the two variables where the error arises. For the case like so:

  var x;
  var y;
  var xy;

The function first swaps y and xy, linter checks again, errors is thrown, then the fixer swaps xy and x. All variables are ordered.

The reason behind the swapping logic is that this solution seem more "elegant" to me and it is also more efficient than replacing the whole block.

What are your thoughts on this?

@kgryte kgryte added the Tools Issue or pull request related to project tooling. label Nov 24, 2024
@kgryte
Copy link
Member

kgryte commented Nov 24, 2024

While recursive linting may work, I am curious what is the relative performance cost? Linting can already take a long time for a single file, especially for devs running VMs. If recursive linting is significantly more costly, I'd be a bit leery about favoring that approach over single-pass ordering of all variables.

@headlessNode
Copy link
Member Author

headlessNode commented Nov 25, 2024

@kgryte Worst case scenario with recursive is definitely more resource intensive. For using the single-pass do we have a sorting function in stdlib?

@kgryte
Copy link
Member

kgryte commented Nov 26, 2024

@headlessNode If you are working with normal arrays, just use the built-in x.sort() method.

@kgryte
Copy link
Member

kgryte commented Nov 26, 2024

You will need a custom callback, however, in order to order by length, rather than lexicographically.

@headlessNode
Copy link
Member Author

@kgryte @Planeshifter The PR now looks promising. I had introduced a few bugs due to experimenting but now they are addressed. I also add test cases for testing the order of variables in nested functions and they are passing 🥳.

But the nested case fails when the outer functions' variables are also not ordered correctly. The error statement is something like expected One error but got two.

@headlessNode headlessNode marked this pull request as ready for review November 26, 2024 21:21
@Planeshifter Planeshifter requested a review from Copilot November 29, 2024 15:22

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no suggestions.

Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

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

PR looks good, thanks for your work @headlessNode.

With respect to the test failure when both inner and outer function's variable declarations were in the wrong order, we just had to list two elements in the errors array because now there are two lint failure instances in the code; see the commit I pushed to the branch.

I will go ahead and merge your PR shortly.

@Planeshifter Planeshifter added the Ready To Merge A pull request which is ready to be merged. label Nov 29, 2024
@stdlib-bot
Copy link
Contributor

PR Commit Message

feat: add the `fixer` function to `eslint/rules/vars-order`

PR-URL: https://github.com/stdlib-js/stdlib/pull/3224
Closes: https://github.com/stdlib-js/stdlib/issues/3222

Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
Signed-off-by: Muhammad Haris <[email protected]>

Please review the above commit message and make any necessary adjustments.

@Planeshifter Planeshifter merged commit 4231541 into stdlib-js:develop Nov 29, 2024
14 checks passed
@headlessNode
Copy link
Member Author

@Planeshifter Yep, makes sense. Thanks!

@headlessNode headlessNode deleted the fixer-vars branch November 29, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Ready To Merge A pull request which is ready to be merged. Tools Issue or pull request related to project tooling.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: Add fixer function to the ESLint rule vars-order
4 participants