Skip to content

Commit 3b61da7

Browse files
Ignore errors from libs in node_modules folders (#110)
Fixes #45 Fixes #76
1 parent fc871a1 commit 3b61da7

File tree

7 files changed

+70
-8
lines changed

7 files changed

+70
-8
lines changed

source/lib/assertions/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum Assertion {
1313
}
1414

1515
// List of diagnostic handlers attached to the assertion
16-
const assertionHandlers = new Map<string, Handler | Handler[]>([
16+
const assertionHandlers = new Map<Assertion, Handler>([
1717
[Assertion.EXPECT_TYPE, isIdentical],
1818
[Assertion.EXPECT_NOT_TYPE, isNotIdentical],
1919
[Assertion.EXPECT_NOT_ASSIGNABLE, isNotAssignable],
@@ -39,12 +39,7 @@ export const handle = (typeChecker: TypeChecker, assertions: Map<Assertion, Set<
3939
continue;
4040
}
4141

42-
const handlers = Array.isArray(handler) ? handler : [handler];
43-
44-
// Iterate over the handlers and invoke them
45-
for (const fn of handlers) {
46-
diagnostics.push(...fn(typeChecker, nodes));
47-
}
42+
diagnostics.push(...handler(typeChecker, nodes));
4843
}
4944

5045
return diagnostics;

source/lib/compiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
9999
const expectedErrorsLocationsWithFoundDiagnostics: Location[] = [];
100100

101101
for (const diagnostic of tsDiagnostics) {
102-
if (!diagnostic.file) {
102+
/* Filter out all diagnostic messages without a file or from node_modules directories, files under
103+
* node_modules are most definitely not under test.
104+
*/
105+
if (!diagnostic.file || /[/\\]node_modules[/\\]/.test(diagnostic.file.fileName)) {
103106
continue;
104107
}
105108

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function (foo: number): number | null;
2+
3+
export type Foo = Bar;
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports.default = foo => {
2+
return foo > 0 ? foo : null;
3+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {expectType} from '../../..';
2+
import aboveZero from '.';
3+
4+
expectType<number | null>(aboveZero(1));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "foo",
3+
"tsd": {
4+
"compilerOptions": {
5+
"noLib": true
6+
}
7+
}
8+
}

source/test/test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from 'path';
22
import test from 'ava';
33
import {verify} from './fixtures/utils';
44
import tsd from '..';
5+
import {Diagnostic} from '../lib/interfaces';
56

67
test('throw if no type definition was found', async t => {
78
await t.throwsAsync(tsd({cwd: path.join(__dirname, 'fixtures/no-tsd')}), {message: 'The type definition `index.d.ts` does not exist. Create one and try again.'});
@@ -313,3 +314,47 @@ test('includes extended config files along with found ones', async t => {
313314
[6, 64, 'error', 'Not all code paths return a value.'],
314315
]);
315316
});
317+
318+
test('errors in libs from node_modules are not reported', async t => {
319+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/exclude-node-modules')});
320+
321+
const [nodeModuleDiagnostics, testFileDiagnostics, otherDiagnostics] = diagnostics.reduce(
322+
([nodeModuleDiags, testFileDiags, otherDiags], diagnostic) => {
323+
if (/[/\\]node_modules[/\\]/.test(diagnostic.fileName)) {
324+
nodeModuleDiags.push(diagnostic);
325+
} else if (/[/\\]fixtures[/\\]exclude-node-modules[/\\]/.test(diagnostic.fileName)) {
326+
testFileDiags.push(diagnostic);
327+
} else {
328+
otherDiags.push(diagnostic);
329+
}
330+
return [nodeModuleDiags, testFileDiags, otherDiags];
331+
},
332+
[[], [], []] as Diagnostic[][]
333+
);
334+
335+
t.deepEqual(
336+
nodeModuleDiagnostics.length,
337+
0,
338+
'There must be no errors from node_modules folders when standard lib is not available (option `"noLib": true`).',
339+
);
340+
341+
t.true(
342+
otherDiagnostics.length > 0,
343+
'There must be errors from tsd lib when standard lib is not available (option `"noLib": true`).',
344+
);
345+
346+
const alloweOtherFileFailures = [
347+
/[/\\]lib[/\\]index.d.ts$/,
348+
/[/\\]lib[/\\]interfaces.d.ts$/,
349+
];
350+
otherDiagnostics.forEach(diagnostic => {
351+
t.true(
352+
alloweOtherFileFailures.some(allowedFileRe => allowedFileRe.test(diagnostic.fileName)),
353+
`Found diagnostic from an unexpected file: ${diagnostic.fileName} - ${diagnostic.message}`,
354+
);
355+
});
356+
357+
verify(t, testFileDiagnostics, [
358+
[3, 18, 'error', 'Cannot find name \'Bar\'.']
359+
]);
360+
});

0 commit comments

Comments
 (0)