Skip to content

Commit

Permalink
feat: Improve scope management for select expressions to ensure prope…
Browse files Browse the repository at this point in the history
…r method scoping in WITH clause
  • Loading branch information
felipebz committed Oct 1, 2024
1 parent 99c3c05 commit 4f142b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.sonar.plsqlopen.symbols
import com.felipebz.flr.api.AstNode
import com.felipebz.flr.api.AstNodeType
import org.sonar.plsqlopen.typeIs
import org.sonar.plugins.plsqlopen.api.DmlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlKeyword
import org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck
Expand Down Expand Up @@ -49,7 +50,8 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
PlSqlGrammar.BLOCK_STATEMENT,
PlSqlGrammar.FOR_STATEMENT,
PlSqlGrammar.CURSOR_DECLARATION,
PlSqlGrammar.FORALL_STATEMENT)
PlSqlGrammar.FORALL_STATEMENT,
DmlGrammar.SELECT_EXPRESSION)

val symbolTable = SymbolTableImpl()
private lateinit var fileScope: Scope
Expand Down Expand Up @@ -171,6 +173,8 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
visitPackage(node)
} else if (node.type === PlSqlGrammar.LITERAL) {
visitLiteral(node)
} else if (node.type === DmlGrammar.SELECT_EXPRESSION) {
visitSelectExpression(node)
}
}

Expand Down Expand Up @@ -368,6 +372,10 @@ class SymbolVisitor(private val typeSolver: DefaultTypeSolver, private val globa
(node as SemanticAstNode).plSqlDatatype = solveType(node)
}

private fun visitSelectExpression(node: AstNode) {
enterScope(node)
}

private fun createSymbol(identifier: AstNode, kind: Symbol.Kind, plSqlDatatype: PlSqlDatatype): Symbol {
val scope = currentScope
if (scope == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.assertj.core.groups.Tuple
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.sonar.plsqlopen.TestPlSqlVisitorRunner
import org.sonar.plugins.plsqlopen.api.DmlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.symbols.PlSqlType
import org.sonar.plugins.plsqlopen.api.symbols.Symbol
Expand Down Expand Up @@ -633,6 +634,29 @@ end;
assertThat(text.innerScope).isNull()
}

@Test
fun selectWithFunctionDeclaration() {
val symbols = scan(
"""
with
function func return number is
begin
return 1;
end;
select func
"""
)
assertThat(symbols).hasSize(1)

val func = symbols.find("func", 2, 12)
assertThat(func.type).isEqualTo(PlSqlType.NUMERIC)
assertThat(func.references).containsExactly(
tuple(6, 8)
)
assertThat(func.scope.type).isEqualTo(DmlGrammar.SELECT_EXPRESSION)
assertThat(func.innerScope).isNotNull()
}

private fun scan(contents: String): List<Symbol> {
val file = tempFolder.resolve("test.sql")
file.writeText(contents.trim())
Expand Down

0 comments on commit 4f142b1

Please sign in to comment.