Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
d0cec9b
fix(indexer): end the store write a grace period after the deadline
claude Nov 15, 2026
d6abc2d
test(projectgraph): list golang.org/x/text for colly in the method co…
claude Nov 15, 2026
ec0b792
fix(projectgraph): link the implicit instance methods of Python classes
claude Nov 15, 2026
c8ffaf3
fix(rootmatch): make each component file and route file a root
claude Nov 15, 2026
91740aa
fix(golang): record a call of a local function value for a source search
claude Nov 15, 2026
b21a40d
fix(golang): check the binding of a typed name at each call
claude Nov 15, 2026
880d9f0
fix(golang): record a range over a function
claude Nov 15, 2026
7852d79
fix(golang): record a function with no body
claude Nov 15, 2026
2f9372f
test(golang): call a map element at a variable key from a source
claude Nov 15, 2026
21f8c73
fix(projectgraph): resolve a Rust path through a use of a module
claude Nov 15, 2026
d30405a
fix(indexer): record a Rust call on a name that a second let binds
claude Nov 15, 2026
1c353ed
fix(indexer): read a Rust macro of the file with the name of a std macro
claude Nov 15, 2026
11e3618
fix(lang): read self and super of a Rust use in its inline module
claude Nov 15, 2026
94d9ef2
fix(indexer): follow a Rust use or a type parameter that shadows a name
claude Nov 15, 2026
91e5c8a
fix(projectgraph): resolve a Rust type in its inline module
claude Nov 15, 2026
b7ca05f
fix(projectgraph): resolve a Rust glob use and an exported macro of a…
claude Nov 15, 2026
9f4b761
test(lang): cover each form of a Rust self or super path in an inline…
claude Nov 15, 2026
12e1272
perf(projectgraph): skip the Rust lookups that no name of the graph n…
claude Nov 15, 2026
c0b88cd
fix(projectgraph): undo a providers memo that an unsettled glob pass …
claude Nov 15, 2026
0ca0672
chore(projectgraph): bump fileindex to 17 and projectgraph to 20
claude Nov 15, 2026
5fc7953
fix(indexer): stop the grace timer when the store write returns
claude Nov 15, 2026
ad7b5e1
fix(golang): keep the type of a name that a function binds once
claude Nov 15, 2026
415c60e
fix(golang): narrow the function-value records of fields and closures
claude Nov 15, 2026
b0b9c74
fix(golang): record a range over a function value of a declared type
claude Nov 15, 2026
7a795cc
fix(projectgraph): link Python instance methods from the calls that m…
claude Nov 15, 2026
ac08891
fix(rootmatch): reach the actions of a SvelteKit route and resolve $lib
claude Nov 15, 2026
7451215
fix(indexer): record the exports of a JavaScript destructuring declar…
claude Nov 15, 2026
e93a29d
fix(indexer): keep a Rust call after a second let of the same type
claude Nov 15, 2026
cdb47d5
fix(indexer): give a Rust type and a method call the path of a use
claude Nov 15, 2026
2d2bcb8
fix(projectgraph): follow a Rust glob of a unit into a third crate
claude Nov 15, 2026
c902438
perf(projectgraph): keep the answer of a Rust glob path that no guard…
claude Nov 15, 2026
7172aa6
test(projectgraph): update the hash rows of fileindex 17 and projectg…
claude Nov 15, 2026
c2b4470
fix(golang): close the gaps of a second review of function values
claude Nov 15, 2026
b437769
fix(rootmatch): skip test files in path rules and scope markup by lan…
claude Nov 15, 2026
035d6e5
fix(projectgraph): link Python instance methods from each site that c…
claude Nov 15, 2026
d6de58f
fix(rootmatch): reach the route exports that are no object literal
claude Nov 15, 2026
c175d38
perf(projectgraph): read each Rust glob module once per lookup of a name
claude Nov 15, 2026
5a8c0f0
fix(projectgraph): join a Rust impl block only through a use of the root
claude Nov 15, 2026
a40bf41
fix(indexer): record a Rust call whose type name a glob use can bring
claude Nov 15, 2026
47438ad
fix(projectgraph): read only pub items of a Rust glob use, in its own…
claude Nov 15, 2026
302193b
fix(projectgraph): let a Rust macro of the crate shadow a macro of a …
claude Nov 15, 2026
932854d
fix(indexer): read two Rust lets that call one function of a type as …
claude Nov 15, 2026
013d683
fix(callgraph): record a Rust macro rule that calls a metavariable
claude Nov 15, 2026
9ea9225
docs(projectgraph): describe the Rust macro and let rules of the seco…
claude Nov 15, 2026
28cc26b
fix(projectgraph): keep no Rust glob list that a cut of an outer look…
claude Nov 15, 2026
376da5d
fix(projectgraph): keep no external base that a cut search gave
claude Nov 15, 2026
7773305
chore(reach): bump reach to 6 and update the hash rows
claude Nov 15, 2026
e417024
fix(golang): close the gaps of a third review of function values
claude Nov 15, 2026
2001444
fix(rootmatch): skip test paths only for the component rule
claude Nov 15, 2026
9962d57
fix(rootmatch): scope opaque route exports to JavaScript and narrow them
claude Nov 15, 2026
3f47afa
fix(projectgraph): find more Python sites that make an instance, and …
claude Nov 15, 2026
250b4d0
perf(projectgraph): keep the external base of a search that started idle
claude Nov 15, 2026
d45b96d
fix(projectgraph): keep no stale Rust glob answer, and keep glob path…
claude Nov 15, 2026
2cee839
fix(projectgraph): bring only what another crate sees through a Rust …
claude Nov 15, 2026
9dbc4d8
fix(projectgraph): let only a Rust macro_rules! shadow a macro of a d…
claude Nov 15, 2026
11530b0
perf(projectgraph): read the local Rust path once in the scope fallback
claude Nov 15, 2026
5ec9470
test(projectgraph): update the hash rows after the third review
claude Nov 15, 2026
628568a
test(projectgraph): assert the outcome of the Rust glob tests
claude Nov 15, 2026
bb05738
refactor(projectgraph): share the guard, memo and origin code
claude Nov 15, 2026
c5ddb59
refactor(indexer): share the walks and the predicates of the Go, Rust…
claude Nov 15, 2026
cbe713d
docs(projectgraph): state the comments and the case descriptions in t…
claude Nov 15, 2026
5ced53b
fix(projectgraph): decide the root fallback per language family
claude Nov 15, 2026
7b8583a
fix(reach): scope markup records by the imports of the whole family
claude Nov 15, 2026
ced83de
fix(callgraph): give a Rust value path its namespace as its value
claude Nov 15, 2026
e7cc6fb
fix(indexer): read a Rust self or super path of an inline module from…
claude Nov 15, 2026
d9f0f30
fix(projectgraph): resolve a Rust path through a glob use of a module…
claude Nov 15, 2026
5c89910
fix(golang): type a write of an element of a map or a slice
claude Nov 15, 2026
0d6b7eb
fix(golang): type a range variable and an element of a declared conta…
claude Nov 15, 2026
804f5b6
perf(indexer): read the Go top level and the struct types once per file
claude Nov 15, 2026
7fe82ce
fix(indexer): type a Rust value of a self or super path in an inline …
claude Nov 15, 2026
72053f2
test(projectgraph): update the hash rows of fileindex 17 and projectg…
claude Nov 15, 2026
7765b0f
fix(projectgraph): leave Node.js builtin modules out of the family im…
claude Nov 16, 2026
b21feaa
fix(projectgraph): link the key of a third crate that a walk into a u…
claude Nov 16, 2026
42156ce
perf(indexer): read the Rust uses of a list once and the binders only…
claude Nov 16, 2026
0978580
test(projectgraph): update the hash rows for the third-crate re-expor…
claude Nov 16, 2026
76106a4
test(projectgraph): move the indexed Rust glob test out of the CGO-fr…
claude Nov 16, 2026
c86e581
fix(rootmatch): root no helper function inside a member of a route ob…
claude Nov 16, 2026
a7eab88
docs(projectgraph): keep the guide to the API and drop the rule-by-ru…
claude Nov 16, 2026
22b8a78
fix(indexer): stop the class parameter of a classmethod at a nested b…
claude Nov 16, 2026
49a68e5
test(projectgraph): update the hash rows for the nested classmethod case
claude Nov 16, 2026
a363338
fix(projectgraph): give another crate no private Rust module of a fil…
claude Nov 16, 2026
de177cd
test(projectgraph): update the hash rows for the private module case
claude Nov 16, 2026
0a5ac04
fix(indexer): keep the Rust receiver type that a path names through a…
claude Nov 16, 2026
251b8ae
fix(indexer): read the value of an angle-bracket assertion in an export
claude Nov 16, 2026
878bb6c
fix(projectgraph): take the most derived metaclass of the bases of a …
claude Nov 16, 2026
8ececee
test(projectgraph): update the hash rows for the receiver and metacla…
claude Nov 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1,243 changes: 232 additions & 1,011 deletions docs/projectgraph.md

Large diffs are not rendered by default.

62 changes: 51 additions & 11 deletions fileindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// Version is the version of the index records and of the rules that fill
// them. Bump it when a field, a kind, an order or a fill rule changes. A
// store keys an index by Version, so the bump makes every older index miss.
const Version = "16"
const Version = "17"

// DefaultTestPaths are the globs that mark the test files of the common
// layouts, as projectgraph.Options.TestPaths takes them. A glob with no "/"
Expand Down Expand Up @@ -113,13 +113,22 @@ const (
// UnresolvedNoDependencySource is a call on a value whose type comes
// from a package with no code in the graph, as the result of a
// function of a module that no unit gives, or an external key of such a
// package, whose code can call back into the graph.
// package, whose code can call back into the graph. A Go function with
// no body, as a stub of assembly, has the reason too.
UnresolvedNoDependencySource UnresolvedReason = "no-dependency-source"
// UnresolvedFunctionValue is a call of a function value, as f(x)(y) or
// handlers[i](c), in a language whose function values the link step
// joins to the code that holds them. The link step gives the reason
// in place of computed-name, and the record names no target.
UnresolvedFunctionValue UnresolvedReason = "function-value"
// UnresolvedRangeValue is a Go range over a value that the index cannot
// show to be data. A range over a function calls it. The link step
// makes the record a function-value record, or drops it when the graph
// shows that the value is data. Receiver and Method name a member, as
// n and Kids for n.Kids. Method alone names the type of the value, as
// List. With neither, the value is the result of the call at the site.
// The link step never keeps the reason.
UnresolvedRangeValue UnresolvedReason = "range-value"
// UnresolvedMarkup is the markup of a component file, as the template
// of a Vue component, which the index does not parse. The framework
// runs the markup and calls the functions of the component, so the
Expand Down Expand Up @@ -189,6 +198,10 @@ type Index struct {
// if __FILE__ == $0 of Ruby. A root rule reads it, so the link step
// reads no source file.
MainGuard bool `json:"main_guard,omitempty"`
// PrivateModules holds the child modules that a Rust file declares in
// a file of their own with no plain pub, as hidden of mod hidden;.
// Another crate sees no such module through a glob use.
PrivateModules []string `json:"private_modules,omitempty"`

Imports []Import `json:"imports,omitempty"`
Exports []Export `json:"exports,omitempty"`
Expand All @@ -201,6 +214,10 @@ type Index struct {
Truncated []Truncation `json:"truncated,omitempty"`
}

// RustMacroRules is the decorator that marks a definition of a Rust
// macro_rules!, which the index otherwise gives the kind function.
const RustMacroRules = "macro_rules!"

// Import is one import of the file. Module is the module as written, as
// ./utils or .foo, and the link step resolves it. Namespace is the per-file
// "//" form of the import, which loses the leading dots of a relative module.
Expand All @@ -216,6 +233,8 @@ type Import struct {
Kind ImportKind `json:"kind"`
Namespace string `json:"namespace"`
Require bool `json:"require,omitempty"`
// Exported marks a Rust pub use, whose binding another crate can name.
Exported bool `json:"exported,omitempty"`
}

// Export is one export of the file. Name is "default" for a default export.
Expand All @@ -230,6 +249,10 @@ type Export struct {
From string `json:"from,omitempty"`
Item string `json:"item,omitempty"`
Line int `json:"line"`
// Opaque marks a name whose value the index does not follow, as the
// result of a call in export const actions = makeActions(). Code that
// calls a function of the value can run any code of it.
Opaque bool `json:"opaque,omitempty"`
}

// Definition is one module, function, method, class, constructor, module
Expand Down Expand Up @@ -270,9 +293,17 @@ type Definition struct {
Returns string `json:"returns,omitempty"`
Results []string `json:"results,omitempty"`
Wraps string `json:"wraps,omitempty"`
Static bool `json:"static,omitempty"`
Data bool `json:"data,omitempty"`
Flags []Flag `json:"flags,omitempty"`
// ClassParam is the name that holds the class of a Python classmethod,
// as cls or klass: its first parameter, also in a function that the
// classmethod defines and that does not bind the name again.
ClassParam string `json:"class_param,omitempty"`
Static bool `json:"static,omitempty"`
Data bool `json:"data,omitempty"`
// ValueData marks a Go function whose first result holds no function,
// and a Go named type whose values hold no function, as []string or a
// struct. A range over such a value reads data.
ValueData bool `json:"value_data,omitempty"`
Flags []Flag `json:"flags,omitempty"`
// Alias is the type that a DefAlias names, as written, with no type
// arguments, as B for type A = B<u8>.
Alias string `json:"alias,omitempty"`
Expand All @@ -290,13 +321,15 @@ func (d Definition) HoldsData() bool {
// keys. Fields are the fields that declare the type of a class, in source
// order. Abstract are the methods that a Go interface declares, in source
// order. They have no definition, and a type implements the interface when
// its methods hold them.
// its methods hold them. Metaclass is the metaclass of a Python class as
// written, as M in class A(metaclass=M).
type Class struct {
Key string `json:"key"`
Bases []string `json:"bases,omitempty"`
Methods []string `json:"methods,omitempty"`
Fields []Typed `json:"fields,omitempty"`
Abstract []Signature `json:"abstract,omitempty"`
Key string `json:"key"`
Bases []string `json:"bases,omitempty"`
Metaclass string `json:"metaclass,omitempty"`
Methods []string `json:"methods,omitempty"`
Fields []Typed `json:"fields,omitempty"`
Abstract []Signature `json:"abstract,omitempty"`
}

// Signature is a method that an interface declares: its name, the count of
Expand Down Expand Up @@ -353,12 +386,16 @@ type Arg struct {
// uses as a value and does not call, as yaml.load in run(yaml.load). The
// code that gets the value can call it, so the link step counts the target
// as possibly called. Targets are per-file resolutions, as in a Call.
// Check marks a class that the code only tests a value against, as the
// second argument of isinstance in Python, or names as the metaclass of a
// class statement. Such a use makes no instance that code can call.
type Reference struct {
Caller string `json:"caller"`
Line int `json:"line"`
Column int `json:"column"`
Text string `json:"text"`
Targets []string `json:"targets"`
Check bool `json:"check,omitempty"`
}

// Unresolved is a call site whose callee the index does not know in full.
Expand Down Expand Up @@ -649,6 +686,7 @@ func (ix *Index) EachString(fn func(string) string) {
kind(&ix.ContentHash)
kind(&ix.Package)
kind(&ix.Unit)
strs(ix.PrivateModules)
for i := range ix.Imports {
imp := &ix.Imports[i]
kind(&imp.Module)
Expand All @@ -673,6 +711,7 @@ func (ix *Index) EachString(fn func(string) string) {
strs(def.Decorators)
strs(def.Values)
typed(def.Params)
kind(&def.ClassParam)
typed(def.Locals)
kind(&def.Returns)
strs(def.Results)
Expand All @@ -684,6 +723,7 @@ func (ix *Index) EachString(fn func(string) string) {
c := &ix.Classes[i]
kind(&c.Key)
strs(c.Bases)
kind(&c.Metaclass)
strs(c.Methods)
typed(c.Fields)
for j := range c.Abstract {
Expand Down
19 changes: 13 additions & 6 deletions fileindex/roots/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ var manifestFields = map[string][]string{
// on one of them: a package.json, a pyproject.toml, a setup.py, a Pipfile,
// a requirements file, a go.mod, a composer.json or a Cargo.toml. A project
// whose manifests declare no dependency gives no evidence, so the rule
// applies.
// applies. KeepsFallback marks a rule whose roots do not turn off the
// library-surface roots. Such a rule names files that a framework loads,
// but not the entry that loads them, as the index.html of a Vite app.
// SkipsTests marks a rule that does not mark a file of a test path, as a
// component fixture of a test. A route rule names its files by their path
// in the app, and a route can have a test path, as app/api/test/route.ts.
type Rule struct {
ID string `yaml:"id"`
Language string `yaml:"language"`
Kind Kind `yaml:"kind"`
Requires []string `yaml:"requires,omitempty"`
Match Match `yaml:"match"`
ID string `yaml:"id"`
Language string `yaml:"language"`
Kind Kind `yaml:"kind"`
Requires []string `yaml:"requires,omitempty"`
KeepsFallback bool `yaml:"keeps_fallback,omitempty"`
SkipsTests bool `yaml:"skips_tests,omitempty"`
Match Match `yaml:"match"`
}

// MatchKey names one key of a Match.
Expand Down
21 changes: 21 additions & 0 deletions fileindex/roots/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# A callee is a symbol key as the graph writes it, as django//urls//path for
# path of django.urls. requires names the packages of the framework of a
# rule, and the rule applies only to a project that depends on one of them.
# keeps_fallback marks a rule whose roots do not turn off the library-surface
# roots, because it names the files that a framework loads and not the entry.
# skips_tests marks a rule that does not mark a file of a test path.
version: 1
rules:
- id: go.main
Expand Down Expand Up @@ -92,6 +95,24 @@ rules:
kind: framework
requires: [next]
match: { path: ["app/**/route.*", "app/**/page.*", "app/**/layout.*", "src/app/**/route.*", "src/app/**/page.*", "src/app/**/layout.*", "pages/**", "src/pages/**"] }
- id: node.component
language: javascript
kind: framework
keeps_fallback: true
skips_tests: true
match: { path: ["**/*.vue", "**/*.svelte", "**/*.astro"] }
- id: node.sveltekit.route
language: javascript
kind: framework
requires: ["@sveltejs/kit"]
keeps_fallback: true
match: { path: ["**/src/routes/**/+*", "**/src/hooks.*", "**/src/params/*", "**/src/service-worker.*", "**/src/service-worker/index.*"] }
- id: node.astro.page
language: javascript
kind: framework
requires: [astro]
keeps_fallback: true
match: { path: ["**/src/pages/**", "**/src/middleware.*", "**/src/middleware/index.*", "**/src/actions/index.*", "**/src/content/config.*", "**/src/content.config.*", "**/astro.config.*"] }
- id: node.serverless.handler
language: javascript
kind: framework
Expand Down
3 changes: 2 additions & 1 deletion fileindex/roots/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestDefaultRules(t *testing.T) {
"python.project-scripts", "python.setup-py", "procfile", "dockerfile",
"python.flask.route", "python.fastapi.route", "python.django.urls", "python.django.view", "python.click.command",
"python.click.subcommand", "python.typer.command", "node.express.route", "node.koa.route",
"node.commander.action", "node.next.route", "node.serverless.handler", "java.spring.web", "java.spring.component",
"node.commander.action", "node.next.route", "node.component", "node.sveltekit.route", "node.astro.page",
"node.serverless.handler", "java.spring.web", "java.spring.component",
"csharp.main", "csharp.top-level-statements", "csharp.aspnet.action", "csharp.aspnet.controllers",
"php.script", "php.composer-bin", "php.laravel.routes", "php.laravel.controllers", "php.symfony.attribute",
"ruby.main-guard", "ruby.script", "ruby.rails.app",
Expand Down
2 changes: 1 addition & 1 deletion fileindex/testdata/index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "16",
"version": "17",
"path": "src/app.py",
"language": "python",
"content_hash": "a172cedcae47474b615c54d510a5d84a8dea3032e958587430b413538be3f333",
Expand Down
18 changes: 13 additions & 5 deletions lang/ecmascript/ecmascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,23 +873,31 @@ func (a *Analysis) Name(n *sitter.Node) string {
}

// Unparen returns the expression inside parentheses, comma expressions,
// assignments and TypeScript assertions, as eval in (0, eval), in
// (e = eval) and in (require as any). Each step goes to a child, so the
// loop ends.
// assignments and TypeScript assertions, as Unparen of the package does
// for the dialect of the analysis.
func (a *Analysis) Unparen(n *sitter.Node) *sitter.Node {
return Unparen(n, a.typescript)
}

// Unparen returns the expression inside parentheses, comma expressions,
// assignments and, for TypeScript, assertions, as eval in (0, eval), in
// (e = eval), in (require as any) and in <Handler>make(). An angle-bracket
// assertion holds its type first, so its value is its last expression.
// Each step goes to a child, so the loop ends.
func Unparen(n *sitter.Node, typescript bool) *sitter.Node {
for n != nil {
switch n.Type() {
case "parenthesized_expression", "sequence_expression":
n = lastExpression(n)
case "assignment_expression":
n = n.ChildByFieldName("right")
case "as_expression", "satisfies_expression", "non_null_expression":
if !a.typescript {
if !typescript {
return n
}
n = n.NamedChild(0)
case "type_assertion":
if !a.typescript {
if !typescript {
return n
}
n = lastExpression(n)
Expand Down
27 changes: 26 additions & 1 deletion lang/rust_resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package lang
import (
"context"
"fmt"
"slices"
"unicode"
"unicode/utf8"

"github.com/safedep/code/core"
"github.com/safedep/code/core/ast"
"github.com/safedep/code/langcode"
sitter "github.com/smacker/go-tree-sitter"
)

Expand Down Expand Up @@ -45,7 +47,13 @@ func (r *rustResolvers) ResolveImports(ctx context.Context, tree core.ParseTree)
err = walkNamed(ctx, tree.Tree().RootNode(), func(node *sitter.Node) bool {
switch node.Type() {
case "use_declaration":
imports = append(imports, rustUseImports(data, node.ChildByFieldName("argument"), nil, nil)...)
uses := rustUseImports(data, node.ChildByFieldName("argument"), nil, nil)
if mods := rustInlineModules(data, node); len(mods) > 0 {
for _, imp := range uses {
imp.SetModuleName(langcode.RustFromFile(imp.ModuleName(), mods))
}
}
imports = append(imports, uses...)
return false
case "extern_crate_declaration":
name := node.ChildByFieldName("name")
Expand Down Expand Up @@ -84,6 +92,23 @@ func (r *rustResolvers) ResolveImports(ctx context.Context, tree core.ParseTree)
return imports, nil
}

// rustInlineModules returns the names of the inline modules that hold a
// node, the outermost first, as [outer inner] for a use in mod outer { mod
// inner { .. } }.
func rustInlineModules(data *[]byte, node *sitter.Node) []string {
var mods []string
for n := node.Parent(); n != nil; n = n.Parent() {
if n.Type() != "mod_item" {
continue
}
if name := n.ChildByFieldName("name"); name != nil {
mods = append(mods, name.Content(*data))
}
}
slices.Reverse(mods)
return mods
}

// rustUseImports returns the imports of a use tree. The prefix is the path of
// the outer use list and the module of each import. The nested path is the
// path of a use list inside it. An import node has one node for each part, so
Expand Down
29 changes: 29 additions & 0 deletions langcode/rust.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package langcode

import (
"slices"
"strings"
)

// RustFromFile writes a path that starts with self or super in an inline
// module as a path from the module of the file, as self::outer::x for
// super::x in mod outer { mod inner { .. } }. mods holds the names of the
// inline modules, the outermost first. The index reads such a path from the
// module of the file. So self::x in mod m named the x of the file, and a
// call through it lost its edge to m::x.
func RustFromFile(path string, mods []string) string {
parts := strings.Split(path, "::")
ups := 0
for ups < len(parts) && parts[ups] == "super" {
ups++
}
switch {
case parts[0] == "self":
return strings.Join(slices.Concat([]string{"self"}, mods, parts[1:]), "::")
case ups == 0:
return path
case ups > len(mods):
return strings.Join(slices.Concat(slices.Repeat([]string{"super"}, ups-len(mods)), parts[ups:]), "::")
}
return strings.Join(slices.Concat([]string{"self"}, mods[:len(mods)-ups], parts[ups:]), "::")
}
28 changes: 28 additions & 0 deletions langcode/rust_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package langcode

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRustFromFile(t *testing.T) {
cases := []struct {
path string
mods []string
want string
}{
{"self::tls::conn", []string{"client"}, "self::client::tls::conn"},
{"super::tls", []string{"client"}, "self::tls"},
{"super::sub", []string{"outer", "inner"}, "self::outer::sub"},
{"super::super::x", []string{"inner"}, "super::x"},
{"super", []string{"tests"}, "self"},
{"crate::store::disk", []string{"client"}, "crate::store::disk"},
{"dep::html", []string{"client"}, "dep::html"},
}
for _, c := range cases {
t.Run(c.path, func(t *testing.T) {
assert.Equal(t, c.want, RustFromFile(c.path, c.mods))
})
}
}
Loading
Loading