diff --git a/gazelle/js/generate.go b/gazelle/js/generate.go index e8fa237c4..9d2bd9f87 100644 --- a/gazelle/js/generate.go +++ b/gazelle/js/generate.go @@ -336,14 +336,15 @@ func (ts *typeScriptLang) collectTsConfigImports(cfg *JsGazelleConfig, args lang } for _, t := range tsconfig.Types { - if typesImport := toAtTypesPackage(t); !cfg.IsImportIgnored(typesImport) { + if !cfg.IsImportIgnored(t) { imports = append(imports, ImportStatement{ ImportSpec: resolve.ImportSpec{ Lang: LanguageName, - Imp: typesImport, + Imp: t, }, ImportPath: t, SourcePath: SourcePath, + TypesOnly: true, }) } } diff --git a/gazelle/js/resolve.go b/gazelle/js/resolve.go index 6e522fc6b..8e26f078a 100644 --- a/gazelle/js/resolve.go +++ b/gazelle/js/resolve.go @@ -326,7 +326,7 @@ func (ts *typeScriptLang) resolveImports( deps.Add(typesDep) } - if dep != nil { + if dep != nil && (!imp.TypesOnly || len(types) == 0) { deps.Add(dep) } diff --git a/gazelle/js/target.go b/gazelle/js/target.go index f7b5b5275..336bab54d 100644 --- a/gazelle/js/target.go +++ b/gazelle/js/target.go @@ -23,6 +23,10 @@ type ImportStatement struct { // If the import is optional and failure to resolve should not be an error Optional bool + + // If the import is explicitly for types, in which case prefer @types package + // dependencies when types are shipped separately + TypesOnly bool } // Npm link-all rule import data diff --git a/gazelle/js/tests/tsconfig_deps/pnpm-lock.yaml b/gazelle/js/tests/tsconfig_deps/pnpm-lock.yaml index 7cb0f5a20..da54770bf 100644 --- a/gazelle/js/tests/tsconfig_deps/pnpm-lock.yaml +++ b/gazelle/js/tests/tsconfig_deps/pnpm-lock.yaml @@ -4,10 +4,12 @@ specifiers: '@testing-library/jest-dom': ^5.16.5 '@types/jquery': 3.5.14 '@types/testing-library__jest-dom': ^5.14.5 + cypress: ^13.16.0 jquery: 3.6.1 dependencies: '@testing-library/jest-dom': 5.16.5 '@types/jquery': 3.5.14 '@types/testing-library__jest-dom': 5.14.5 + cypress: 13.16.0 jquery: 3.6.1 diff --git a/gazelle/js/tests/tsconfig_deps/types/BUILD.in b/gazelle/js/tests/tsconfig_deps/types/BUILD.in index bf49273f6..ac8334dd2 100644 --- a/gazelle/js/tests/tsconfig_deps/types/BUILD.in +++ b/gazelle/js/tests/tsconfig_deps/types/BUILD.in @@ -1,2 +1,2 @@ -# gazelle:js_ignore_imports @types/ignored -# gazelle:js_ignore_imports @types/test__ignored +# gazelle:js_ignore_imports ignored +# gazelle:js_ignore_imports @test/ignored diff --git a/gazelle/js/tests/tsconfig_deps/types/BUILD.out b/gazelle/js/tests/tsconfig_deps/types/BUILD.out index 1ea81c2af..b05992a5b 100644 --- a/gazelle/js/tests/tsconfig_deps/types/BUILD.out +++ b/gazelle/js/tests/tsconfig_deps/types/BUILD.out @@ -1,7 +1,7 @@ load("@aspect_rules_ts//ts:defs.bzl", "ts_config") -# gazelle:js_ignore_imports @types/ignored -# gazelle:js_ignore_imports @types/test__ignored +# gazelle:js_ignore_imports ignored +# gazelle:js_ignore_imports @test/ignored ts_config( name = "tsconfig", @@ -9,6 +9,7 @@ ts_config( deps = [ "//:node_modules/@types/jquery", "//:node_modules/@types/testing-library__jest-dom", + "//:node_modules/cypress", "//:tsconfig", ], ) diff --git a/gazelle/js/tests/tsconfig_deps/types/tsconfig.json b/gazelle/js/tests/tsconfig_deps/types/tsconfig.json index 82972d112..c591c04f3 100644 --- a/gazelle/js/tests/tsconfig_deps/types/tsconfig.json +++ b/gazelle/js/tests/tsconfig_deps/types/tsconfig.json @@ -1,6 +1,6 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "types": ["jquery", "@testing-library/jest-dom", "ignored", "@test/ignored"] + "types": ["jquery", "@testing-library/jest-dom", "ignored", "@test/ignored", "cypress"] } }