Skip to content

Commit

Permalink
Merge tsdjs#154
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Sep 13, 2022
1 parent aaad782 commit 9a5d505
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
@@ -1,7 +1,17 @@
import {CallExpression, TypeChecker} from '@tsd/typescript';
import {CallExpression, TypeChecker, TypeFormatFlags} from '@tsd/typescript';
import {Diagnostic} from '../../interfaces';
import {makeDiagnostic, tsutils} 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 printTypeWarning = (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/informational/print-type/index.d.ts
@@ -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;
}
@@ -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/informational.ts
Expand Up @@ -14,6 +14,7 @@ test('print type', 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 9a5d505

Please sign in to comment.