Skip to content

Commit

Permalink
Remove id from paths to instances in style warnings
Browse files Browse the repository at this point in the history
Fixes kaitai-io/kaitai_struct#920

Error count: 62 -> 58 (-4). Fixed:

[info] - style_bad_len_inst_pos *** FAILED ***
[info]   [style_bad_len_inst_pos.ksy: /instances/size_of_foo/id:
[info]          warning: use `len_foo` instead of `size_of_foo`, given that it's only used as a byte size of `foo` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ]
[info]     did not equal
[info]   [style_bad_len_inst_pos.ksy: /instances/size_of_foo:
[info]          warning: use `len_foo` instead of `size_of_foo`, given that it's only used as a byte size of `foo` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ] (SimpleMatchers.scala:34)
[info] - style_bad_len_inst_value *** FAILED ***
[info]   [style_bad_len_inst_value.ksy: /instances/size_of_foo/id:
[info]          warning: use `len_foo` instead of `size_of_foo`, given that it's only used as a byte size of `foo` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ]
[info]     did not equal
[info]   [style_bad_len_inst_value.ksy: /instances/size_of_foo:
[info]          warning: use `len_foo` instead of `size_of_foo`, given that it's only used as a byte size of `foo` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ] (SimpleMatchers.scala:34)
[info] - style_bad_num_inst_pos *** FAILED ***
[info]   [style_bad_num_inst_pos.ksy: /instances/size_of_foo/id:
[info]          warning: use `num_foos` instead of `size_of_foo`, given that it's only used as repeat count of `foos` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ]
[info]     did not equal
[info]   [style_bad_num_inst_pos.ksy: /instances/size_of_foo:
[info]          warning: use `num_foos` instead of `size_of_foo`, given that it's only used as repeat count of `foos` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ] (SimpleMatchers.scala:34)
[info] - style_bad_num_inst_value *** FAILED ***
[info]   [style_bad_num_inst_value.ksy: /instances/size_of_foo/id:
[info]          warning: use `num_foos` instead of `size_of_foo`, given that it's only used as repeat count of `foos` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ]
[info]     did not equal
[info]   [style_bad_num_inst_value.ksy: /instances/size_of_foo:
[info]          warning: use `num_foos` instead of `size_of_foo`, given that it's only used as repeat count of `foos` (see https://doc.kaitai.io/ksy_style_guide.html#attr-id)
[info]   ] (SimpleMatchers.scala:34)
  • Loading branch information
Mingun committed Sep 8, 2024
1 parent 049b464 commit 639e3eb
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,53 @@ class StyleCheckIds(specs: ClassSpecs) extends PrecompileStep {
}
}

/**
* @param spec Owner of `attr`
* @param attr Attribute that references attribute with probably not-conformant name
*/
def getSizeRefProblem(spec: ClassSpec, attr: MemberSpec): Option[CompilationProblem] = {
getSizeReference(spec, attr.dataType).flatMap(sizeRefAttr => {
val existingName = sizeRefAttr.id.humanReadable
val goodName = s"len_${attr.id.humanReadable}"
if (existingName != goodName) {
// Report error at position where referenced attribute is defined.
// Add `id` for attributes in `seq`, do not add for instances
val path = sizeRefAttr match {
case _: InstanceSpec => sizeRefAttr.path
case _ => sizeRefAttr.path :+ "id"
}
Some(StyleWarningSizeLen(
goodName,
existingName,
attr.id.humanReadable,
ProblemCoords(path = Some(sizeRefAttr.path ++ List("id")))
ProblemCoords(path = Some(path))
))
} else {
None
}
})
}

/**
* @param spec Owner of `attr`
* @param attr Attribute that references attribute with probably not-conformant name
*/
def getRepeatExprRefProblem(spec: ClassSpec, attr: AttrLikeSpec): Option[CompilationProblem] = {
getRepeatExprReference(spec, attr).flatMap(repeatExprRefAttr => {
val existingName = repeatExprRefAttr.id.humanReadable
val goodName = s"num_${attr.id.humanReadable}"
if (existingName != goodName) {
// Report error at position where referenced attribute is defined.
// Add `id` for attributes in `seq`, do not add for instances
val path = repeatExprRefAttr match {
case _: InstanceSpec => repeatExprRefAttr.path
case _ => repeatExprRefAttr.path :+ "id"
}
Some(StyleWarningRepeatExprNum(
goodName,
existingName,
attr.id.humanReadable,
ProblemCoords(path = Some(repeatExprRefAttr.path ++ List("id")))
ProblemCoords(path = Some(path))
))
} else {
None
Expand Down

0 comments on commit 639e3eb

Please sign in to comment.