Skip to content

Commit

Permalink
Don't truncate output of printType (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 authored Sep 13, 2022
1 parent ae6189b commit 68acb5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
15 changes: 13 additions & 2 deletions source/lib/assertions/handlers/informational.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import {CallExpression, TypeChecker} from '@tsd/typescript';
import {CallExpression, TypeChecker, TypeFormatFlags} from '@tsd/typescript';
import {Diagnostic} from '../../interfaces';
import {makeDiagnostic} from '../../utils';

/**
* Default formatting flags set by TS plus the {@link TypeFormatFlags.NoTruncation NoTruncation} flag.
*
* @see {@link https://github.dev/microsoft/TypeScript/blob/b975dfa1027d1f3073fa7cbe6f7045bf4c882785/src/compiler/checker.ts#L4717 TypeChecker.typeToString}
*/
const typeToStringFormatFlags =
TypeFormatFlags.AllowUniqueESSymbolType |
TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |
TypeFormatFlags.NoTruncation;

/**
* Prints the type of the argument of the assertion as a warning.
*
Expand All @@ -19,8 +29,9 @@ export const prinTypeWarning = (checker: TypeChecker, nodes: Set<CallExpression>
for (const node of nodes) {
const argumentType = checker.getTypeAtLocation(node.arguments[0]);
const argumentExpression = node.arguments[0].getText();
const typeString = checker.typeToString(argumentType, node, typeToStringFormatFlags);

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

return diagnostics;
Expand Down
18 changes: 17 additions & 1 deletion source/test/fixtures/print-type/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export default function (foo: number): number | null;
export function aboveZero(foo: number): number | null;

type SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace = {
life: 42
};

export const bigType: {
prop1: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop2: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop3: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop4: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop5: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop6: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop7: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop8: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
prop9: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace;
}
3 changes: 2 additions & 1 deletion source/test/fixtures/print-type/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {printType} from '../../..';
import aboveZero from '.';
import {aboveZero, bigType} from '.';

printType(aboveZero);
printType(null);
Expand All @@ -8,3 +8,4 @@ printType(null as any);
printType(null as never);
printType(null as unknown);
printType('foo');
printType(bigType);
1 change: 1 addition & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ test('prints the types of expressions passed to `printType` helper', async t =>
[8, 0, 'warning', 'Type for expression `null as never` is: `never`'],
[9, 0, 'warning', 'Type for expression `null as unknown` is: `unknown`'],
[10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'],
[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; }`']
]);
});

Expand Down

0 comments on commit 68acb5b

Please sign in to comment.