src/rules/style/size.bend checks
raw source on the hub · import bolt@1.67.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; or a def whose body is
one list of data, a [..] holding nothing but numbers, strings,
chars, commas, capitalized and dotted names and [..] and
{..} groups of them (no plain name, so no variable and no
call; no operator; no (..)), with nothing after its ] and
no line under it
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. A generated list of
records (a standard's 2231 named character references) is a table too:
its length is how many rows there are.
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 47 · raw
Data
what of a def a rule measures
DArityDim
DLengthDim
DDepthDim
Definitions
def slug source · line 53 · raw
@dim:Dim -> String
the rule's slug
def limit_name source · line 63 · raw
@dim:Dim -> String
the name a bolt.bend sets the rule's limit by
def limit source · line 73 · raw
@dim:Dim -> U32
the rule's limit when no bolt.bend sets one
def most source · line 83 · raw
@+a:U32 -> @+b:U32 -> U32
the greater of two
def arity.one source · line 87 · raw
@name:String -> @+acc:U32 -> U32
one more when the parameter has a name of its own
def arity.go source · line 91 · raw
@ps:List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node> -> @acc:U32 -> U32
how many of the parameters have a name of their own, onto acc
def arity source · line 99 · raw
@sig:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> U32
a def's parameters, type parameters aside
def last.close source · line 103 · raw
@close:Maybe<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/lex.Tok> -> @best:U32 -> U32
a closing bracket's line, or the line so far
def last source · line 111 · raw
@n:0xd1af11cb3fe9c11256ed247a0003f75f/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 125 · raw
@+sig:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> @body:0xd1af11cb3fe9c11256ed247a0003f75f/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 131 · raw
@body:0xd1af11cb3fe9c11256ed247a0003f75f/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 142 · raw
@ns:0xd1af11cb3fe9c11256ed247a0003f75f/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 data.token source · line 154 · raw
@k:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/lex.TokKind -> Bool
is this token one a list of data holds: a number, a string, a char, a comma, or a capitalized or dotted name (a constructor's, or a def's named and not called)?
def data.bracket source · line 172 · raw
@+t:String -> Bool
does this bracket open a group of data: [ or {, not a call's (?
def data source · line 177 · raw
@ns:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> Bool
are these the elements of a list of data: tokens a list of data holds, and
[..] and {..} groups of them, nothing else?
def none source · line 189 · raw
@n:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> Bool
is a chain empty?
def table.list source · line 198 · raw
@kids:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> @under:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> Bool
is a statement a list of data: its own tokens one [..] group of data,
with nothing under it?
def table.stmt source · line 207 · raw
@+kids:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> @+under:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> Bool
is a statement, a def's whole body, a table: it opens a block with the arms of a table under it, or it is a list of data
def table source · line 212 · raw
@body:0xd1af11cb3fe9c11256ed247a0003f75f/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, or one statement that is a list of data?
def length.of source · line 220 · raw
@+sig:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> @+body:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> U32
the lines length holds a def to: none for a table
def depth.under source · line 225 · 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 234 · raw
@n:0xd1af11cb3fe9c11256ed247a0003f75f/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 244 · raw
@dim:Dim -> @sig:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> @body:0xd1af11cb3fe9c11256ed247a0003f75f/src/syntax/tree.Node -> U32
what a rule measures of a def
def say source · line 254 · raw
@dim:Dim -> @+name:String -> @size:U32 -> @max:U32 -> String
what a finding says
def found source · line 267 · raw
@hit:Bool -> @+dim:Dim -> @+name:String -> @line:U32 -> @size:U32 -> @max:U32 -> @path:String -> @more:List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding> -> List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding>
a finding on the def's name when it is over, before the rest
def sized source · line 284 · raw
@+dim:Dim -> @name:String -> @line:U32 -> @+size:U32 -> @+max:U32 -> @path:String -> @more:List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding> -> List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding>
a def measured against the limit
def check.go source · line 296 · raw
@ds:List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/rules/calls.Def> -> @+dim:Dim -> @+path:String -> @+max:U32 -> List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding>
every def over the limit, in order; an exempt def is not measured
def check source · line 306 · raw
@dim:Dim -> @s:0xd1af11cb3fe9c11256ed247a0003f75f/src/src.Src -> @max:U32 -> List<&2, 0xd1af11cb3fe9c11256ed247a0003f75f/src/finding.Finding>
a rule of the family, at a limit