StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

Sort requires codemod

Open JohnnyMorganz opened this issue 3 years ago • 2 comments

An implementation of the sort requires codemod

Enabling sort requires functionality will make stylua sort requires in the top level block of a file accordingly. All requires will move to the top, and will be ordered alphabetically. Any dependencies used within requires (e.g. ReplicatedStorage in require(ReplicatedStorage.Module)) will also be moved so that they are sorted above requires.

Assumptions:

  • We only touch requires in the form local NAME = require(EXPRESSION). Other types of requires are left alone
  • We only touch requires in the top level of a file. We don't touch requires in nested blocks.
  • Requires have no side-effects, reorganising requires is safe

TODO:

  • Topologically sort dependencies and requires so that dependencies are correctly structured. E.g. local c = script.Parent; local a = child.Module, we need to enforce c goes before a.
  • Add ability to structure requires (and other variables) into sections. This should open up functionality to sort game:GetService() calls at the top, before requires.
  • Add sections ordering.
  • Put newlines in appropriate positions
  • More tests!

What the configuration file will look like when this is implemented:

column_width = 120
line_endings = "Unix"
indent_type = "Tabs"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"

[sort_requires]
enabled = true
sections_order = ["services", "packages", "shared", "requires"] # The order to apply sections in. "requires" is the default section when no other section matches

[sort_requires.sections]
# A map between section names and a list of regex's to match against values
services = ["game:GetService(.)"]
packages = ["require(ReplicatedStorage.Packages.([%w.]+))"]
shared = ["require(ReplicatedStorage.Shared.([%w.]+))"]

Closes #364

JohnnyMorganz avatar May 03 '22 18:05 JohnnyMorganz

Codecov Report

Merging #454 (1e16dac) into master (11a4c8c) will decrease coverage by 0.43%. The diff coverage is 84.26%.

@@            Coverage Diff             @@
##           master     #454      +/-   ##
==========================================
- Coverage   96.44%   96.00%   -0.44%     
==========================================
  Files          14       15       +1     
  Lines        5257     5454     +197     
==========================================
+ Hits         5070     5236     +166     
- Misses        187      218      +31     
Impacted Files Coverage Δ
src/lib.rs 91.38% <76.92%> (-0.96%) :arrow_down:
src/sort_requires.rs 84.78% <84.78%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 11a4c8c...1e16dac. Read the comment docs.

codecov[bot] avatar May 03 '22 18:05 codecov[bot]

Debating whether this really belongs in this tool. It may make more sense for it to be split out into a different tool (similar to isort)

JohnnyMorganz avatar Jun 27 '22 13:06 JohnnyMorganz