From 897a5d50051fb2e13c482a18f4b189410f847475 Mon Sep 17 00:00:00 2001 From: Evgeniy Timokhov Date: Wed, 17 Apr 2024 23:29:36 +0100 Subject: [PATCH] Fixed incorrect caching of symbols usage values Fixes #318 --- src/types-usage-evaluator.ts | 6 +++++- tests/e2e/test-cases/recursive-types/config.ts | 5 +++++ tests/e2e/test-cases/recursive-types/index.spec.js | 1 + tests/e2e/test-cases/recursive-types/input.ts | 3 +++ tests/e2e/test-cases/recursive-types/output.d.ts | 6 ++++++ tests/e2e/test-cases/recursive-types/types.ts | 5 +++++ 6 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/e2e/test-cases/recursive-types/config.ts create mode 100644 tests/e2e/test-cases/recursive-types/index.spec.js create mode 100644 tests/e2e/test-cases/recursive-types/input.ts create mode 100644 tests/e2e/test-cases/recursive-types/output.d.ts create mode 100644 tests/e2e/test-cases/recursive-types/types.ts diff --git a/src/types-usage-evaluator.ts b/src/types-usage-evaluator.ts index ee5603bf..a7004f29 100644 --- a/src/types-usage-evaluator.ts +++ b/src/types-usage-evaluator.ts @@ -57,7 +57,11 @@ export class TypesUsageEvaluator { visitedSymbols.add(fromSymbol); - return this.setUsageCacheValue(fromSymbol, toSymbol, false); + // note that we can't save negative result here because it might be not a final one + // because we might ended up here because of `visitedSymbols.has(symbol)` check above + // while actually checking that `symbol` symbol and we will store all its "children" as `false` + // while in reality some of them might be `true` because of cross-references or using the same children symbols + return false; } private setUsageCacheValue(fromSymbol: ts.Symbol, toSymbol: ts.Symbol, value: boolean): boolean { diff --git a/tests/e2e/test-cases/recursive-types/config.ts b/tests/e2e/test-cases/recursive-types/config.ts new file mode 100644 index 00000000..f1fb1a8c --- /dev/null +++ b/tests/e2e/test-cases/recursive-types/config.ts @@ -0,0 +1,5 @@ +import { TestCaseConfig } from '../../test-cases/test-case-config'; + +const config: TestCaseConfig = {}; + +export = config; diff --git a/tests/e2e/test-cases/recursive-types/index.spec.js b/tests/e2e/test-cases/recursive-types/index.spec.js new file mode 100644 index 00000000..c015c268 --- /dev/null +++ b/tests/e2e/test-cases/recursive-types/index.spec.js @@ -0,0 +1 @@ +require('../run-test-case').runTestCase(__dirname); diff --git a/tests/e2e/test-cases/recursive-types/input.ts b/tests/e2e/test-cases/recursive-types/input.ts new file mode 100644 index 00000000..4baea076 --- /dev/null +++ b/tests/e2e/test-cases/recursive-types/input.ts @@ -0,0 +1,3 @@ +import { FirstType } from './types'; + +export type MyType = FirstType; diff --git a/tests/e2e/test-cases/recursive-types/output.d.ts b/tests/e2e/test-cases/recursive-types/output.d.ts new file mode 100644 index 00000000..312db248 --- /dev/null +++ b/tests/e2e/test-cases/recursive-types/output.d.ts @@ -0,0 +1,6 @@ +export type ThirdType = number; +export type SecondType = T extends any ? V : FirstType; +export type FirstType = SecondType; +export type MyType = FirstType; + +export {}; diff --git a/tests/e2e/test-cases/recursive-types/types.ts b/tests/e2e/test-cases/recursive-types/types.ts new file mode 100644 index 00000000..70c62f3b --- /dev/null +++ b/tests/e2e/test-cases/recursive-types/types.ts @@ -0,0 +1,5 @@ +type ThirdType = number; + +type SecondType = T extends any ? V : FirstType; + +export type FirstType = SecondType;