From 09c0d17365c7585dd8d77f4c0d454d63d0c58e72 Mon Sep 17 00:00:00 2001 From: Tommy Date: Sat, 17 Sep 2022 13:29:34 -0500 Subject: [PATCH] Add regression test for #161 (#164) --- source/lib/parser.ts | 54 ++++++++++++------- .../test/fixtures/undefined-symbol/index.d.ts | 1 + .../fixtures/undefined-symbol/index.test-d.ts | 7 +++ .../fixtures/undefined-symbol/package.json | 3 ++ source/test/test.ts | 6 +++ 5 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 source/test/fixtures/undefined-symbol/index.d.ts create mode 100644 source/test/fixtures/undefined-symbol/index.test-d.ts create mode 100644 source/test/fixtures/undefined-symbol/package.json diff --git a/source/lib/parser.ts b/source/lib/parser.ts index 5b72a06c..8ad952f4 100644 --- a/source/lib/parser.ts +++ b/source/lib/parser.ts @@ -14,33 +14,47 @@ export const extractAssertions = (program: Program): Map(); - const nodes = assertions.get(assertion) ?? new Set(); + nodes.add(node); - nodes.add(node); + assertions.set(assertion, nodes); + } + } - assertions.set(assertion, nodes); - } - } + /** + * Recursively loop over all the nodes and extract all the assertions out of the source files. + */ + function walkNodes(node: Node) { + if (isCallExpression(node)) { + handleNode(node); } forEachChild(node, walkNodes); diff --git a/source/test/fixtures/undefined-symbol/index.d.ts b/source/test/fixtures/undefined-symbol/index.d.ts new file mode 100644 index 00000000..336ce12b --- /dev/null +++ b/source/test/fixtures/undefined-symbol/index.d.ts @@ -0,0 +1 @@ +export {} diff --git a/source/test/fixtures/undefined-symbol/index.test-d.ts b/source/test/fixtures/undefined-symbol/index.test-d.ts new file mode 100644 index 00000000..810a76b8 --- /dev/null +++ b/source/test/fixtures/undefined-symbol/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from '../../../..'; + +// Identifier `bar` has no Symbol +const anyCall = (foo: any) => foo.bar(); + +// Fails with `Cannot read properties of undefined (reading 'flags')` in 0.24.0 +expectType(anyCall('foo')); diff --git a/source/test/fixtures/undefined-symbol/package.json b/source/test/fixtures/undefined-symbol/package.json new file mode 100644 index 00000000..de6dc1db --- /dev/null +++ b/source/test/fixtures/undefined-symbol/package.json @@ -0,0 +1,3 @@ +{ + "name": "foo" +} diff --git a/source/test/test.ts b/source/test/test.ts index 21ad2930..b29f9063 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -466,3 +466,9 @@ test('assertions should be identified if aliased', async t => { verify(t, diagnostics, []); }); + +test('parsing undefined symbol should not fail', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/undefined-symbol')}); + + verify(t, diagnostics, []); +});