| 1 | // Copyright 2013 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Most of the required functions were available in the |
| 6 | // "golang.org/x/tools/go/ast/astutil" package, but not exported. |
| 7 | // They were copied from https://github.com/golang/tools/blob/2b0845dc783e36ae26d683f4915a5840ef01ab0f/go/ast/astutil/imports.go |
| 8 | |
| 9 | package outlinescip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/ |
| 10 | |
| 11 | import ( |
| 12 | "go/astscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/" |
| 13 | "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 15 | ) |
| 16 | |
| 17 | // packageNameForImport returns the package name for the package. If the package |
| 18 | // is not imported, it returns nil. "Package name" refers to `pkgname` in the |
| 19 | // call expression `pkgname.ExportedIdentifier`. Examples: |
| 20 | // (import path not found) -> nil |
| 21 | // "import example.com/pkg/foo" -> "foo" |
| 22 | // "import fooalias example.com/pkg/foo" -> "fooalias" |
| 23 | // "import . example.com/pkg/foo" -> "" |
| 24 | func packageNameForImportscip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/packageNameForImport().(f *astscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/File#, path string) *string { |
| 25 | spec := importSpecscip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/importSpec().(f, path) |
| 26 | if spec == nil { |
| 27 | return nil |
| 28 | } |
| 29 | name := spec.Namescip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/ImportSpec#Name..Stringscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/Ident#String().() |
| 30 | if name == "<nil>" { |
| 31 | name = "ginkgo" |
| 32 | } |
| 33 | if name == "." { |
| 34 | name = "" |
| 35 | } |
| 36 | return &name |
| 37 | } |
| 38 | |
| 39 | // importSpec returns the import spec if f imports path, |
| 40 | // or nil otherwise. |
| 41 | func importSpecscip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/importSpec().(f *astscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/File#, path string) *astscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/ImportSpec# { |
| 42 | for _, s := range f.Importsscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/File#Imports. { |
| 43 | if stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(importPathscip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/importPath().(s), path) { |
| 44 | return s |
| 45 | } |
| 46 | } |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | // importPath returns the unquoted import path of s, |
| 51 | // or "" if the path is not properly quoted. |
| 52 | func importPathscip-go gomod github.com/onsi/ginkgo/v2 . `github.com/onsi/ginkgo/v2/ginkgo/outline`/importPath().(s *astscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/ImportSpec#) string { |
| 53 | t, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.24.0 strconv/Unquote().(s.Pathscip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.24.0 `go/ast`/BasicLit#Value.) |
| 54 | if err != nil { |
| 55 | return "" |
| 56 | } |
| 57 | return t |
| 58 | } |