Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 976cc31

Browse files
committed
Kotlin: Add support for string templates
1 parent 168786a commit 976cc31

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,17 @@ class X {
14191419
val exprParent = parent.expr(e, callable)
14201420
extractCall(e, callable, exprParent.parent, exprParent.idx)
14211421
}
1422+
is IrStringConcatenation -> {
1423+
val exprParent = parent.expr(e, callable)
1424+
val id = tw.getFreshIdLabel<DbStringtemplateexpr>()
1425+
val type = useType(e.type)
1426+
val locId = tw.getLocation(e)
1427+
tw.writeExprs_stringtemplateexpr(id, type.javaResult.id, type.kotlinResult.id, exprParent.parent, exprParent.idx)
1428+
tw.writeHasLocation(id, locId)
1429+
e.arguments.forEachIndexed { i, a ->
1430+
extractExpressionExpr(a, callable, id, i)
1431+
}
1432+
}
14221433
is IrConst<*> -> {
14231434
val exprParent = parent.expr(e, callable)
14241435
val v = e.value

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ case @expr.kind of
670670
| 77 = @safecastexpr
671671
| 78 = @notinstanceofexpr
672672
| 79 = @stmtexpr
673+
| 80 = @stringtemplateexpr
673674
;
674675

675676
/** Holds if this `when` expression was written as an `if` expression. */

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,3 +2246,24 @@ class StmtExpr extends Expr, @stmtexpr {
22462246

22472247
override string getAPrimaryQlClass() { result = "StmtExpr" }
22482248
}
2249+
2250+
/**
2251+
* A Kotlin string template expression. For example, `"foo${bar}baz"`.
2252+
*/
2253+
class StringTemplateExpr extends Expr, @stringtemplateexpr {
2254+
/**
2255+
* Gets the `i`th component of this string template.
2256+
*
2257+
* For example, in the string template `"foo${bar}baz"`, the 0th
2258+
* component is the string literal `"foo"`, the 1st component is
2259+
* the variable access `bar`, and the 2nd component is the string
2260+
* literal `"bar"`.
2261+
*/
2262+
Expr getComponent(int i) { result.isNthChildOf(this, i) }
2263+
2264+
override string toString() { result = "\"...\"" }
2265+
2266+
override string getHalsteadID() { result = "StringTemplateExpr" }
2267+
2268+
override string getAPrimaryQlClass() { result = "StringTemplateExpr" }
2269+
}

0 commit comments

Comments
 (0)