src/rules/style/size.bend checks
raw source on the hub · import bolt@1.61.0.0/src/rules/style/size.bend as Size
rules arity, length and depth: a def that holds more than a reader can.
Each measures one thing of a def and reports the def when the measure is
over the rule's limit, one finding on the def's name:
arity its parameters, type parameters aside (-A: Type, ~A: Data
have no lowercase name of their own): over 8
length its lines, from the def line to the line of its last token:
over 40. A table is not measured: a def whose body is one
match with nothing under it but case arms, each holding at
most one statement, which opens no block
depth how deep its statements nest. The statements right under the def
are level 1, and a statement that opens a block (its own tokens
hold a :, as match x: and case A: do) puts the statements
under it one level down: over 6, which is a third match inside
two
A bolt.bend sets another limit with def arity_max() -> U32,
def length_max() or def depth_max().
A line an expression wraps onto is under its statement in bolt's tree but
opens no block, so it is no level. A block inside brackets (a lambda's
body) is not read.
Left alone: a law file, a proof file, and a def that is a proof (no type,
or one that returns -> {..}), as the cost rules leave them
(src/rules/calls.bend): a proof is as long as its statement needs.
The fixes are structural, and a name cannot buy them: parameters that
travel together become one value of a type; a step of a long def, or the
inner match of a deep one, becomes a helper. A table has no step to move:
its length is how many cases there are, and the one way to shorten it is
to cut it in two at some line, which helps no reader. Of the 116 defs
length reported in 82 public repos, 22 were tables.
The limits are where public Bend 2 code stops: 99 in 100 defs have 9
parameters or fewer, 19 lines or fewer, and nest 6 levels or fewer.
7 imports
import Base import ../../src.bend as Src import ../../finding.bend as F import ../../lazy/lazy.bend as Lazy import ../../syntax/lex.bend as Lex import ../../syntax/tree.bend as Tree import ../calls.bend as Calls
Types
type Dim source · line 40 · raw
Data
what of a def a rule measures
DArityDim
DLengthDim
DDepthDim
Definitions
def slug source · line 46 · raw
@dim:Dim -> String
the rule's slug
def limit_name source · line 56 · raw
@dim:Dim -> String
the name a bolt.bend sets the rule's limit by
def limit source · line 66 · raw
@dim:Dim -> U32
the rule's limit when no bolt.bend sets one
def most source · line 76 · raw
@+a:U32 -> @+b:U32 -> U32
the greater of two
def arity.one source · line 80 · raw
@name:String -> @+acc:U32 -> U32
one more when the parameter has a name of its own
def arity.go source · line 84 · raw
@ps:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node> -> @acc:U32 -> U32
how many of the parameters have a name of their own, onto acc
def arity source · line 92 · raw
@sig:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> U32
a def's parameters, type parameters aside
def last.close source · line 96 · raw
@close:Maybe<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.Tok> -> @best:U32 -> U32
a closing bracket's line, or the line so far
def last source · line 104 · raw
@n:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> @best:U32 -> U32
the greatest line of a token under the node, or best when it is greater
def length source · line 118 · raw
@+sig:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> @body:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> U32
a def's lines: from its first line to the line of its last token
def table.arm source · line 124 · raw
@body:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> Bool
is this what an arm of a table holds: nothing under the case line, or
one statement that opens no block?
def table.arms source · line 135 · raw
@ns:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> Bool
are these the arms of a table: case statements and nothing else, each
holding what an arm of a table holds?
def table source · line 146 · raw
@body:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> Bool
is a def's body a table: one statement that opens a block, with the arms of a table under it?
def length.of source · line 154 · raw
@+sig:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> @+body:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> U32
the lines length holds a def to: none for a table
def depth.under source · line 159 · raw
@opens:Bool -> @at:U32 -> U32
the level the statements under a statement sit at: one down when it opens a block
def depth source · line 168 · raw
@n:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> @+at:U32 -> @best:U32 -> U32
the deepest level of a statement among these, which sit at level at, or
best when it is deeper
def measure source · line 178 · raw
@dim:Dim -> @sig:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> @body:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/tree.Node -> U32
what a rule measures of a def
def say source · line 188 · raw
@dim:Dim -> @+name:String -> @size:U32 -> @max:U32 -> String
what a finding says
def found source · line 201 · raw
@hit:Bool -> @+dim:Dim -> @+name:String -> @line:U32 -> @size:U32 -> @max:U32 -> @path:String -> @more:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding> -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
a finding on the def's name when it is over, before the rest
def sized source · line 218 · raw
@+dim:Dim -> @name:String -> @line:U32 -> @+size:U32 -> @+max:U32 -> @path:String -> @more:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding> -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
a def measured against the limit
def check.go source · line 230 · raw
@ds:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/rules/calls.Def> -> @+dim:Dim -> @+path:String -> @+max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
every def over the limit, in order; an exempt def is not measured
def check source · line 240 · raw
@dim:Dim -> @s:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/src.Src -> @max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
a rule of the family, at a limit