correct this handling

This commit is contained in:
2026-02-18 11:00:51 -06:00
parent bd7f9f34ec
commit 187d7e9832
3 changed files with 29 additions and 12 deletions

View File

@@ -261,8 +261,12 @@ var index_ast = function(ast, tokens, filename) {
// Function / arrow function expression — walk body.
if (node.kind == "function" || node.kind == "arrow function") {
enc = enclosing
if (node.name != null && node.function_nr != null) {
enc = filename + ":" + node.name + ":fn"
if (node.function_nr != null) {
if (node.name != null) {
enc = filename + ":" + node.name + ":fn"
} else {
enc = filename + ":anon_" + text(node.function_nr) + ":fn"
}
}
// Record params as symbols.
if (node.list != null) {

View File

@@ -1,10 +1,10 @@
// cell query — Semantic queries across packages.
//
// Usage:
// cell query --this [<package>] Top-level this references
// cell query --intrinsic <name> [<package>] Find built-in intrinsic usage
// cell query --decl <name> [<package>] Variable declarations by name
// cell query --help Show usage
// cell query --this [--top|--fn] [<package>] this references
// cell query --intrinsic <name> [<package>] Find built-in intrinsic usage
// cell query --decl <name> [<package>] Variable declarations by name
// cell query --help Show usage
var shop = use('internal/shop')
var query_mod = use('query')
@@ -12,6 +12,7 @@ var fd = use('fd')
var mode = null
var name = null
var this_scope = null
var pkg_filter = null
var show_help = false
var i = 0
@@ -19,6 +20,10 @@ var i = 0
for (i = 0; i < length(args); i++) {
if (args[i] == '--this') {
mode = "this"
} else if (args[i] == '--top') {
this_scope = "top"
} else if (args[i] == '--fn') {
this_scope = "fn"
} else if (args[i] == '--intrinsic') {
mode = "intrinsic"
if (i + 1 < length(args) && !starts_with(args[i + 1], '-')) {
@@ -65,7 +70,9 @@ if (show_help) {
log.console("Semantic queries across packages.")
log.console("")
log.console("Options:")
log.console(" --this Top-level this references")
log.console(" --this All this references")
log.console(" --this --top Top-level this only (not inside functions)")
log.console(" --this --fn this inside functions only")
log.console(" --intrinsic <name> Find built-in intrinsic usage (e.g., print)")
log.console(" --decl <name> Variable declarations by name")
log.console("")
@@ -93,7 +100,7 @@ if (show_help) {
hits = null
if (mode == "this") {
hits = query_mod.top_level_this(idx)
hits = query_mod.find_this(idx, this_scope)
} else if (mode == "intrinsic") {
hits = query_mod.intrinsic(idx, name)
} else if (mode == "decl") {

View File

@@ -4,15 +4,21 @@
var query = {}
// Top-level this: references where name=="this" and enclosing==null.
query.top_level_this = function(idx) {
// Find this references. scope: "top" (top-level only), "fn" (in functions), null (all).
query.find_this = function(idx, scope) {
var hits = []
var i = 0
var ref = null
while (i < length(idx.references)) {
ref = idx.references[i]
if (ref.name == "this" && ref.enclosing == null) {
hits[] = ref
if (ref.name == "this") {
if (scope == null) {
hits[] = ref
} else if (scope == "top" && ref.enclosing == null) {
hits[] = ref
} else if (scope == "fn" && ref.enclosing != null) {
hits[] = ref
}
}
i = i + 1
}