Skip to content

Commit

Permalink
fix(cli): support direct import of outputted dts files (#6977)
Browse files Browse the repository at this point in the history
Things like package.json might import both the .d.ts and .js version of
a file. Those can come from source .d.ts or .ts.

---

### Changes are visible to end-users: no

### Test plan

- New test cases added

GitOrigin-RevId: 500e11903f791b324a15c05ad2fca9a0a843a058
  • Loading branch information
jbedard committed Oct 23, 2024
1 parent 6300c67 commit ef386d5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 23 deletions.
31 changes: 23 additions & 8 deletions gazelle/js/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,12 @@ func toImportPaths(p string) []string {
paths := make([]string, 0, 1)

if isDeclarationFileType(p) {
// With the js extension
paths = append(paths, swapDeclarationExtension(p))
// The import of the raw dts file
paths = append(paths, p)

// Assume the js extension also exists
// TODO: don't do that
paths = append(paths, stripDeclarationExtensions(p)+toJsExt(p))

// Without the js extension
if isImplicitSourceFileType(p) {
Expand All @@ -737,8 +741,9 @@ func toImportPaths(p string) []string {
paths = append(paths, path.Dir(p))
}
} else if isTranspiledSourceFileType(p) {
// The import with the transpiled file extension
// The transpiled files extensions
paths = append(paths, swapSourceExtension(p))
paths = append(paths, stripSourceFileExtension(p)+toDtsExt(p))

// Without the extension if it is implicit
if isImplicitSourceFileType(p) {
Expand Down Expand Up @@ -892,11 +897,6 @@ func stripDeclarationExtensions(f string) string {
return stripSourceFileExtension(stripSourceFileExtension(f))
}

// Swap compile to runtime extensions of of a path, assuming it isDeclarationFileType()
func swapDeclarationExtension(f string) string {
return stripDeclarationExtensions(f) + toJsExt(f)
}

func toJsExt(f string) string {
e := path.Ext(f)
switch e {
Expand All @@ -916,6 +916,21 @@ func toJsExt(f string) string {
}
}

func toDtsExt(f string) string {
e := path.Ext(f)
switch e {
case ".ts", ".tsx":
return ".d.ts"
case ".cts":
return ".d.cts"
case ".mts":
return ".d.mts"
default:
BazelLog.Errorf("Unknown extension %q", e)
return ".d.ts"
}
}

// Normalize the given import statement from a relative path
// to a path relative to the workspace.
func toImportSpecPath(importFrom, importPath string) string {
Expand Down
30 changes: 15 additions & 15 deletions gazelle/js/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ func TestGenerate(t *testing.T) {

t.Run("toImportPaths", func(t *testing.T) {
// Traditional [.d].ts[x] don't require an extension
assertImports(t, "bar.ts", []string{"bar", "bar.js"})
assertImports(t, "bar.tsx", []string{"bar", "bar.js"})
assertImports(t, "bar.d.ts", []string{"bar", "bar.js"})
assertImports(t, "foo/bar.ts", []string{"foo/bar", "foo/bar.js"})
assertImports(t, "foo/bar.tsx", []string{"foo/bar", "foo/bar.js"})
assertImports(t, "foo/bar.d.ts", []string{"foo/bar", "foo/bar.js"})
assertImports(t, "bar.ts", []string{"bar", "bar.d.ts", "bar.js"})
assertImports(t, "bar.tsx", []string{"bar", "bar.d.ts", "bar.js"})
assertImports(t, "bar.d.ts", []string{"bar", "bar.d.ts", "bar.js"})
assertImports(t, "foo/bar.ts", []string{"foo/bar", "foo/bar.d.ts", "foo/bar.js"})
assertImports(t, "foo/bar.tsx", []string{"foo/bar", "foo/bar.d.ts", "foo/bar.js"})
assertImports(t, "foo/bar.d.ts", []string{"foo/bar", "foo/bar.d.ts", "foo/bar.js"})

// Traditional [.d].ts[x] index files
assertImports(t, "bar/index.ts", []string{"bar/index", "bar/index.js", "bar"})
assertImports(t, "bar/index.d.ts", []string{"bar/index", "bar/index.js", "bar"})
assertImports(t, "bar/index.tsx", []string{"bar/index", "bar/index.js", "bar"})
assertImports(t, "bar/index.ts", []string{"bar/index", "bar/index.d.ts", "bar/index.js", "bar"})
assertImports(t, "bar/index.d.ts", []string{"bar/index", "bar/index.d.ts", "bar/index.js", "bar"})
assertImports(t, "bar/index.tsx", []string{"bar/index", "bar/index.d.ts", "bar/index.js", "bar"})

// .mjs and .cjs files require an extension
assertImports(t, "bar.mts", []string{"bar.mjs"})
assertImports(t, "bar/index.mts", []string{"bar/index.mjs", "bar"})
assertImports(t, "bar.d.mts", []string{"bar.mjs"})
assertImports(t, "bar.cts", []string{"bar.cjs"})
assertImports(t, "bar/index.cts", []string{"bar/index.cjs", "bar"})
assertImports(t, "bar.d.cts", []string{"bar.cjs"})
assertImports(t, "bar.mts", []string{"bar.mjs", "bar.d.mts"})
assertImports(t, "bar/index.mts", []string{"bar/index.mjs", "bar/index.d.mts", "bar"})
assertImports(t, "bar.d.mts", []string{"bar.d.mts", "bar.mjs"})
assertImports(t, "bar.cts", []string{"bar.cjs", "bar.d.cts"})
assertImports(t, "bar/index.cts", []string{"bar/index.cjs", "bar/index.d.cts", "bar"})
assertImports(t, "bar.d.cts", []string{"bar.d.cts", "bar.cjs"})
})
}

Expand Down
2 changes: 2 additions & 0 deletions gazelle/js/tests/npm_package_deps_tsconfig/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:js_package_rule_kind npm_package
33 changes: 33 additions & 0 deletions gazelle/js/tests/npm_package_deps_tsconfig/scenario1/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
load("@npm//:defs.bzl", "npm_link_all_packages")

# gazelle:js_package_rule_kind npm_package

npm_link_all_packages(name = "node_modules")

ts_project(
name = "tsc",
srcs = [
"src/index.ts",
"src/types/index.ts",
],
declaration = True,
declaration_map = True,
out_dir = "dist",
root_dir = "src",
tsconfig = ":tsconfig",
)

npm_package(
name = "pkg",
srcs = [
"package.json",
":tsc",
],
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "scenario1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./types": "./dist/types/index.d.ts"
}
}
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions gazelle/js/tests/npm_package_deps_tsconfig/scenario1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"rootDir": "src",
"outDir": "dist",
"isolatedModules": false,
"declaration": true,
"declarationMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

0 comments on commit ef386d5

Please sign in to comment.