transformDTS transforms a `.d.ts` file for deno/editor-lsp
(ctx *BuildContext, dts string, buildArgsPrefix string, marker *set.Set[string])
| 29 | |
| 30 | // transformDTS transforms a `.d.ts` file for deno/editor-lsp |
| 31 | func transformDTS(ctx *BuildContext, dts string, buildArgsPrefix string, marker *set.Set[string]) (n int, err error) { |
| 32 | entry := marker == nil |
| 33 | if entry { |
| 34 | marker = set.New[string]() |
| 35 | } |
| 36 | |
| 37 | dtsPath := path.Join("/"+ctx.esmPath.PackageId(), buildArgsPrefix, dts) |
| 38 | if marker.Has(dtsPath) { |
| 39 | // already transformed |
| 40 | return |
| 41 | } |
| 42 | marker.Add(dtsPath) |
| 43 | |
| 44 | savePath := normalizeSavePath(path.Join("types", dtsPath)) |
| 45 | // check if the dts file has been transformed |
| 46 | _, err = ctx.storage.Stat(savePath) |
| 47 | if err == nil || err != storage.ErrNotFound { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | dtsFilename := path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, dts) |
| 52 | dtsContent, err := os.Open(dtsFilename) |
| 53 | if err != nil { |
| 54 | // if the dts file does not exist, print a warning but continue to build |
| 55 | if os.IsNotExist(err) { |
| 56 | if entry { |
| 57 | err = fmt.Errorf("types not found") |
| 58 | } else { |
| 59 | err = nil |
| 60 | } |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | defer dtsContent.Close() |
| 65 | |
| 66 | buffer := &bytes.Buffer{} |
| 67 | |
| 68 | deps := set.New[string]() |
| 69 | |
| 70 | err = parseDts(dtsContent, buffer, func(specifier string, kind TsImportKind, position int) (string, error) { |
| 71 | if ctx.esmPath.PkgName == "@types/node" { |
| 72 | if strings.HasPrefix(specifier, "node:") || nodeBuiltinModules[specifier] || isRelPathSpecifier(specifier) { |
| 73 | return specifier, nil |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // normalize specifier |
| 78 | specifier = normalizeImportSpecifier(specifier) |
| 79 | |
| 80 | if isRelPathSpecifier(specifier) { |
| 81 | dtsDir := path.Dir(dtsFilename) |
| 82 | specifier = strings.TrimSuffix(specifier, ".d") |
| 83 | if !endsWith(specifier, ".d.ts", ".d.mts", ".d.cts") { |
| 84 | var p npm.PackageJSONRaw |
| 85 | var isSubmodule bool |
| 86 | if utils.ParseJSONFile(path.Join(dtsDir, specifier, "package.json"), &p) == nil { |
| 87 | dir := path.Join("/", path.Dir(dts)) |
| 88 | if types := p.Types.String(); types != "" { |
no test coverage detected