Skip to content

Commit d3a4861

Browse files
jbedardwalkerburgin
authored andcommitted
fix(cli): handle non @types imports from tsconfig compilerOptions.types (#7548)
Synced from #785 by walkerburgin: --- The TypeScript docs for `compilerOptions.types` make it sound like `compilerOptions.types` only applies to `@types` packages, but in practice it can also be used to include types from non `@types` in the global scope. For example, `cypress` recommends this in their [docs](https://docs.cypress.io/app/tooling/typescript-support#Configure-tsconfigjson): ```json { "compilerOptions": { "target": "es5", "lib": ["es5", "dom"], "types": ["cypress", "node"] }, "include": ["**/*.ts"] } ``` From what I can tell, if you don't specify `compilerOptions.types`, TypeScript will **not** automatically include types from a visible non `@types` package like `cypress` (`describe`, `beforeEach`, etc). But you can include non `@types` packages in `compilerOptions.types` to allow list them into the global scope. --- ### Changes are visible to end-users: no ### Test plan I updated the existing test case for `compilerOptions.types`. - Covered by existing test cases - New test cases added Closes #785 Co-authored-by: Walker Burgin <wburgin@anduril.com> GitOrigin-RevId: 4321e2b70b9e8d1f68fff219b42cae02a5d3205a
1 parent 9bf63b9 commit d3a4861

File tree

7 files changed

+16
-8
lines changed

7 files changed

+16
-8
lines changed

gazelle/js/generate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,15 @@ func (ts *typeScriptLang) collectTsConfigImports(cfg *JsGazelleConfig, args lang
336336
}
337337

338338
for _, t := range tsconfig.Types {
339-
if typesImport := toAtTypesPackage(t); !cfg.IsImportIgnored(typesImport) {
339+
if !cfg.IsImportIgnored(t) {
340340
imports = append(imports, ImportStatement{
341341
ImportSpec: resolve.ImportSpec{
342342
Lang: LanguageName,
343-
Imp: typesImport,
343+
Imp: t,
344344
},
345345
ImportPath: t,
346346
SourcePath: SourcePath,
347+
TypesOnly: true,
347348
})
348349
}
349350
}

gazelle/js/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (ts *typeScriptLang) resolveImports(
326326
deps.Add(typesDep)
327327
}
328328

329-
if dep != nil {
329+
if dep != nil && (!imp.TypesOnly || len(types) == 0) {
330330
deps.Add(dep)
331331
}
332332

gazelle/js/target.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ type ImportStatement struct {
2323

2424
// If the import is optional and failure to resolve should not be an error
2525
Optional bool
26+
27+
// If the import is explicitly for types, in which case prefer @types package
28+
// dependencies when types are shipped separately
29+
TypesOnly bool
2630
}
2731

2832
// Npm link-all rule import data

gazelle/js/tests/tsconfig_deps/pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# gazelle:js_ignore_imports @types/ignored
2-
# gazelle:js_ignore_imports @types/test__ignored
1+
# gazelle:js_ignore_imports ignored
2+
# gazelle:js_ignore_imports @test/ignored
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")
22

3-
# gazelle:js_ignore_imports @types/ignored
4-
# gazelle:js_ignore_imports @types/test__ignored
3+
# gazelle:js_ignore_imports ignored
4+
# gazelle:js_ignore_imports @test/ignored
55

66
ts_config(
77
name = "tsconfig",
88
src = "tsconfig.json",
99
deps = [
1010
"//:node_modules/@types/jquery",
1111
"//:node_modules/@types/testing-library__jest-dom",
12+
"//:node_modules/cypress",
1213
"//:tsconfig",
1314
],
1415
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"types": ["jquery", "@testing-library/jest-dom", "ignored", "@test/ignored"]
4+
"types": ["jquery", "@testing-library/jest-dom", "ignored", "@test/ignored", "cypress"]
55
}
66
}

0 commit comments

Comments
 (0)