Skip to content

Commit 68acb5b

Browse files
authoredSep 13, 2022
Don't truncate output of printType (#154)
1 parent ae6189b commit 68acb5b

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed
 

‎source/lib/assertions/handlers/informational.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import {CallExpression, TypeChecker} from '@tsd/typescript';
1+
import {CallExpression, TypeChecker, TypeFormatFlags} from '@tsd/typescript';
22
import {Diagnostic} from '../../interfaces';
33
import {makeDiagnostic} from '../../utils';
44

5+
/**
6+
* Default formatting flags set by TS plus the {@link TypeFormatFlags.NoTruncation NoTruncation} flag.
7+
*
8+
* @see {@link https://github.dev/microsoft/TypeScript/blob/b975dfa1027d1f3073fa7cbe6f7045bf4c882785/src/compiler/checker.ts#L4717 TypeChecker.typeToString}
9+
*/
10+
const typeToStringFormatFlags =
11+
TypeFormatFlags.AllowUniqueESSymbolType |
12+
TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |
13+
TypeFormatFlags.NoTruncation;
14+
515
/**
616
* Prints the type of the argument of the assertion as a warning.
717
*
@@ -19,8 +29,9 @@ export const prinTypeWarning = (checker: TypeChecker, nodes: Set<CallExpression>
1929
for (const node of nodes) {
2030
const argumentType = checker.getTypeAtLocation(node.arguments[0]);
2131
const argumentExpression = node.arguments[0].getText();
32+
const typeString = checker.typeToString(argumentType, node, typeToStringFormatFlags);
2233

23-
diagnostics.push(makeDiagnostic(node, `Type for expression \`${argumentExpression}\` is: \`${checker.typeToString(argumentType)}\``, 'warning'));
34+
diagnostics.push(makeDiagnostic(node, `Type for expression \`${argumentExpression}\` is: \`${typeString}\``, 'warning'));
2435
}
2536

2637
return diagnostics;
+17-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
export default function (foo: number): number | null;
1+
export function aboveZero(foo: number): number | null;
2+
3+
type SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace = {
4+
life: 42
5+
};
6+
7+
export const bigType: {
8+
prop1: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
9+
prop2: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
10+
prop3: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
11+
prop4: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
12+
prop5: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
13+
prop6: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
14+
prop7: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
15+
prop8: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
16+
prop9: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {printType} from '../../..';
2-
import aboveZero from '.';
2+
import {aboveZero, bigType} from '.';
33

44
printType(aboveZero);
55
printType(null);
@@ -8,3 +8,4 @@ printType(null as any);
88
printType(null as never);
99
printType(null as unknown);
1010
printType('foo');
11+
printType(bigType);

‎source/test/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ test('prints the types of expressions passed to `printType` helper', async t =>
453453
[8, 0, 'warning', 'Type for expression `null as never` is: `never`'],
454454
[9, 0, 'warning', 'Type for expression `null as unknown` is: `unknown`'],
455455
[10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'],
456+
[11, 0, 'warning', 'Type for expression `bigType` is: `{ prop1: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop2: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop3: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop4: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop5: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop6: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop7: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop8: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop9: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; }`']
456457
]);
457458
});
458459

0 commit comments

Comments
 (0)
Please sign in to comment.