Skip to content

Commit

Permalink
correctly handle <empty> in .content steps (#4377)
Browse files Browse the repository at this point in the history
* correctly handle <empty> in .content steps

also use .slice() in order to avoid crashing in .substring(). This just
papers over issues with invalid offsets, so it's arguable whether we
actually want to do this?

* refactor .macroExpansion step
  • Loading branch information
maltek authored Mar 21, 2024
1 parent eca7ab8 commit 5894357
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ class CallMethods(val node: Call) extends AnyVal with NodeExtension with HasLoca
case expr: Expression if expr.argumentIndex == index => expr
}

def macroExpansion: Iterator[Expression] =
Iterator
.single(node)
.dispatchTypeExact(DispatchTypes.INLINED)
.astChildren
.sortBy(_.order)
.reverseIterator
.isBlock
.astChildren
.isExpression
def macroExpansion: Iterator[Expression] = {
if (node.dispatchType != DispatchTypes.INLINED) return Iterator.empty

node.astChildren.isBlock.maxByOption(_.order).iterator.expressionDown
}

override def location: NewLocation = {
LocationCreator(node, node.code, node.label, node.lineNumber, node.method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ class MethodMethods(val method: Method) extends AnyVal with NodeExtension with H
}

def content: Option[String] = {
val contentOption = method.file.content.headOption
val offsetOption = method.offset
val offsetEndOption = method.offsetEnd

if (contentOption.isDefined && offsetOption.isDefined && offsetEndOption.isDefined) {
contentOption.map(_.substring(offsetOption.get, offsetEndOption.get))
} else {
None
}
for {
content <- method.file.content.headOption
if content != File.PropertyDefaults.Content
offset <- method.offset
offsetEnd <- method.offsetEnd
} yield content.slice(offset, offsetEnd)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,11 @@ object TypeDeclTraversal {
private val maxAliasExpansions = 100

private def contentOnSingle(typeDecl: TypeDecl): Option[String] = {
val contentOption = typeDecl.file.content.headOption
val offsetOption = typeDecl.offset
val offsetEndOption = typeDecl.offsetEnd

if (contentOption.isDefined && offsetOption.isDefined && offsetEndOption.isDefined) {
contentOption.map(_.substring(offsetOption.get, offsetEndOption.get))
} else {
None
}
for {
content <- typeDecl.file.content.headOption
if content != File.PropertyDefaults.Content
offset <- typeDecl.offset
offsetEnd <- typeDecl.offsetEnd
} yield content.slice(offset, offsetEnd)
}
}

0 comments on commit 5894357

Please sign in to comment.