Documentation
¶
Index ¶
- Constants
- Variables
- func ChangeExtension(path string, newExtension string) string
- func CombinePaths(firstPath string, paths ...string) string
- func ComparePaths(a string, b string, options ComparePathsOptions) int
- func ComparePathsCaseInsensitive(a string, b string, currentDirectory string) int
- func ComparePathsCaseSensitive(a string, b string, currentDirectory string) int
- func ContainsPath(parent string, child string, options ComparePathsOptions) bool
- func ConvertToRelativePath(absoluteOrRelativePath string, options ComparePathsOptions) string
- func EnsureTrailingDirectorySeparator(path string) string
- func ExtensionIsTs(ext string) bool
- func FileExtensionIs(path string, extension string) bool
- func FileExtensionIsOneOf(path string, extensions []string) bool
- func ForEachAncestorDirectory[T any](directory string, callback func(directory string) (result T, stop bool)) (result T, ok bool)
- func ForEachAncestorDirectoryPath[T any](directory Path, callback func(directory Path) (result T, stop bool)) (result T, ok bool)
- func GetAnyExtensionFromPath(path string, extensions []string, ignoreCase bool) string
- func GetBaseFileName(path string) string
- func GetCanonicalFileName(fileName string, useCaseSensitiveFileNames bool) string
- func GetDeclarationFileExtension(fileName string) string
- func GetDirectoryPath(path string) string
- func GetEncodedRootLength(path string) int
- func GetNormalizedAbsolutePath(fileName string, currentDirectory string) string
- func GetNormalizedPathComponents(path string, currentDirectory string) []string
- func GetPathComponents(path string, currentDirectory string) []string
- func GetPathComponentsRelativeTo(from string, to string, options ComparePathsOptions) []string
- func GetPathFromPathComponents(pathComponents []string) string
- func GetRelativePathFromDirectory(fromDirectory string, to string, options ComparePathsOptions) string
- func GetRelativePathToDirectoryOrUrl(directoryPathOrUrl string, relativeOrAbsolutePath string, ...) string
- func GetRootLength(path string) int
- func HasExtension(fileName string) bool
- func HasImplementationTSFileExtension(path string) bool
- func HasTSFileExtension(path string) bool
- func HasTrailingDirectorySeparator(path string) bool
- func IsDeclarationFileName(fileName string) bool
- func IsDiskPathRoot(path string) bool
- func IsExternalModuleNameRelative(moduleName string) bool
- func IsRootedDiskPath(path string) bool
- func IsUrl(path string) bool
- func NormalizePath(path string) string
- func NormalizeSlashes(path string) string
- func PathIsAbsolute(path string) bool
- func PathIsRelative(path string) bool
- func RemoveExtension(path string, extension string) string
- func RemoveFileExtension(path string) string
- func RemoveTrailingDirectorySeparator(path string) string
- func RemoveTrailingDirectorySeparators(path string) string
- func ResolvePath(path string, paths ...string) string
- func ToFileNameLowerCase(fileName string) string
- func TryExtractTSExtension(fileName string) string
- func TryGetExtensionFromPath(p string) string
- type ComparePathsOptions
- type Path
Constants ¶
const ( ExtensionTs = ".ts" ExtensionTsx = ".tsx" ExtensionDts = ".d.ts" ExtensionJs = ".js" ExtensionJsx = ".jsx" ExtensionJson = ".json" ExtensionTsBuildInfo = ".tsbuildinfo" ExtensionMjs = ".mjs" ExtensionMts = ".mts" ExtensionDmts = ".d.mts" ExtensionCjs = ".cjs" ExtensionCts = ".cts" ExtensionDcts = ".d.cts" )
const (
DirectorySeparator = '/'
)
Internally, we represent paths as strings with '/' as the directory separator. When we make system calls (eg: LanguageServiceHost.getDirectory()), we expect the host to correctly handle paths in our specified format.
Variables ¶
var ( AllSupportedExtensions = [][]string{{ExtensionTs, ExtensionTsx, ExtensionDts, ExtensionJs, ExtensionJsx}, {ExtensionCts, ExtensionDcts, ExtensionCjs}, {ExtensionMts, ExtensionDmts, ExtensionMjs}} SupportedTSExtensions = [][]string{{ExtensionTs, ExtensionTsx, ExtensionDts}, {ExtensionCts, ExtensionDcts}, {ExtensionMts, ExtensionDmts}} SupportedTSExtensionsFlat = []string{ExtensionTs, ExtensionTsx, ExtensionDts, ExtensionCts, ExtensionDcts, ExtensionMts, ExtensionDmts} SupportedJSExtensions = [][]string{{ExtensionJs, ExtensionJsx}, {ExtensionMjs}, {ExtensionCjs}} SupportedJSExtensionsFlat = []string{ExtensionJs, ExtensionJsx, ExtensionMjs, ExtensionCjs} AllSupportedExtensionsWithJson = slices.Concat(AllSupportedExtensions, [][]string{{ExtensionJson}}) SupportedTSExtensionsWithJson = slices.Concat(SupportedTSExtensions, [][]string{{ExtensionJson}}) SupportedTSExtensionsWithJsonFlat = slices.Concat(SupportedTSExtensionsFlat, []string{ExtensionJson}) )
Functions ¶
func ChangeExtension ¶
func CombinePaths ¶
Combines paths. If a path is absolute, it replaces any previous path. Relative paths are not simplified.
``` // Non-rooted CombinePaths("path", "to", "file.ext") === "path/to/file.ext" CombinePaths("path", "dir", "..", "to", "file.ext") === "path/dir/../to/file.ext" // POSIX CombinePaths("/path", "to", "file.ext") === "/path/to/file.ext" CombinePaths("/path", "/to", "file.ext") === "/to/file.ext" // DOS CombinePaths("c:/path", "to", "file.ext") === "c:/path/to/file.ext" CombinePaths("c:/path", "c:/to", "file.ext") === "c:/to/file.ext" // URL CombinePaths("file:///path", "to", "file.ext") === "file:///path/to/file.ext" CombinePaths("file:///path", "file:///to", "file.ext") === "file:///to/file.ext" ```
func ComparePaths ¶
func ComparePaths(a string, b string, options ComparePathsOptions) int
func ContainsPath ¶
func ContainsPath(parent string, child string, options ComparePathsOptions) bool
func ConvertToRelativePath ¶
func ConvertToRelativePath(absoluteOrRelativePath string, options ComparePathsOptions) string
func ExtensionIsTs ¶
func FileExtensionIs ¶
func FileExtensionIsOneOf ¶
func GetAnyExtensionFromPath ¶
Gets the file extension for a path. If extensions are provided, gets the file extension for a path, provided it is one of the provided extensions.
GetAnyExtensionFromPath("/path/to/file.ext", nil, false) == ".ext" GetAnyExtensionFromPath("/path/to/file.ext/", nil, false) == ".ext" GetAnyExtensionFromPath("/path/to/file", nil, false) == "" GetAnyExtensionFromPath("/path/to.ext/file", nil, false) == "" GetAnyExtensionFromPath("/path/to/file.ext", ".ext", true) === ".ext" GetAnyExtensionFromPath("/path/to/file.js", ".ext", true) === "" GetAnyExtensionFromPath("/path/to/file.js", [".ext", ".js"], true) === ".js" GetAnyExtensionFromPath("/path/to/file.ext", ".EXT", false) === ""
func GetBaseFileName ¶
Gets the portion of a path following the last (non-terminal) separator (`/`). Semantics align with NodeJS's `path.basename` except that we support URL's as well. If the base name has any one of the provided extensions, it is removed.
// POSIX GetBaseFileName("/path/to/file.ext") == "file.ext" GetBaseFileName("/path/to/") == "to" GetBaseFileName("/") == "" // DOS GetBaseFileName("c:/path/to/file.ext") == "file.ext" GetBaseFileName("c:/path/to/") == "to" GetBaseFileName("c:/") == "" GetBaseFileName("c:") == "" // URL GetBaseFileName("http://typescriptlang.org/path/to/file.ext") == "file.ext" GetBaseFileName("http://typescriptlang.org/path/to/") == "to" GetBaseFileName("http://typescriptlang.org/") == "" GetBaseFileName("http://typescriptlang.org") == "" GetBaseFileName("file://server/path/to/file.ext") == "file.ext" GetBaseFileName("file://server/path/to/") == "to" GetBaseFileName("file://server/") == "" GetBaseFileName("file://server") == "" GetBaseFileName("file:///path/to/file.ext") == "file.ext" GetBaseFileName("file:///path/to/") == "to" GetBaseFileName("file:///") == "" GetBaseFileName("file://") == ""
func GetCanonicalFileName ¶
func GetDirectoryPath ¶
func GetEncodedRootLength ¶
func GetPathComponents ¶
func GetPathComponentsRelativeTo ¶
func GetPathComponentsRelativeTo(from string, to string, options ComparePathsOptions) []string
func GetRelativePathFromDirectory ¶
func GetRelativePathFromDirectory(fromDirectory string, to string, options ComparePathsOptions) string
func GetRelativePathToDirectoryOrUrl ¶
func GetRelativePathToDirectoryOrUrl(directoryPathOrUrl string, relativeOrAbsolutePath string, isAbsolutePathAnUrl bool, options ComparePathsOptions) string
func GetRootLength ¶
func HasExtension ¶
func HasTSFileExtension ¶
func IsDeclarationFileName ¶
func IsDiskPathRoot ¶
Determines whether a path consists only of a path root.
func IsRootedDiskPath ¶
Determines whether a path is an absolute disk path (e.g. starts with `/`, or a dos path like `c:`, `c:\` or `c:/`).
func IsUrl ¶
Determines whether a path starts with a URL scheme (e.g. starts with `http://`, `ftp://`, `file://`, etc.).
func NormalizePath ¶
func NormalizeSlashes ¶
func PathIsAbsolute ¶
Determines whether a path starts with an absolute path component (i.e. `/`, `c:/`, `file://`, etc.).
``` // POSIX PathIsAbsolute("/path/to/file.ext") === true // DOS PathIsAbsolute("c:/path/to/file.ext") === true // URL PathIsAbsolute("file:///path/to/file.ext") === true // Non-absolute PathIsAbsolute("path/to/file.ext") === false PathIsAbsolute("./path/to/file.ext") === false ```
func PathIsRelative ¶
func RemoveExtension ¶
func RemoveFileExtension ¶
func ResolvePath ¶
Combines and resolves paths. If a path is absolute, it replaces any previous path. Any `.` and `..` path components are resolved. Trailing directory separators are preserved.
```go resolvePath("/path", "to", "file.ext") == "path/to/file.ext" resolvePath("/path", "to", "file.ext/") == "path/to/file.ext/" resolvePath("/path", "dir", "..", "to", "file.ext") == "path/to/file.ext" ```
func ToFileNameLowerCase ¶
func TryExtractTSExtension ¶
func TryGetExtensionFromPath ¶
Types ¶
type ComparePathsOptions ¶
func (ComparePathsOptions) GetComparer ¶
func (o ComparePathsOptions) GetComparer() func(a, b string) int
type Path ¶
type Path string