GeezerVison is showing github.com — Implement `printType` helper to print the type of the passed expressi… · tsdjs/tsd@6480ca9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: readme.md +6Lines changed: 6 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -189,6 +189,12 @@ Check that `value` is marked a [`@deprecated`](https://jsdoc.app/tags-deprecated | 189 | 189 |
| 190 | 190 | Check that `value` is not marked a [`@deprecated`](https://jsdoc.app/tags-deprecated.html). | 191 | 191 |
| | 192 | +### printType(value) | | 193 | + | | 194 | +Print the type of `value` as a warning. | | 195 | + | | 196 | +Useful if you don't know the exact type of the expression passed to `printType()` or the type is too complex to write out by hand. | | 197 | + | 192 | 198 |
| 193 | 199 | ## Programmatic API | 194 | 200 |
| |
Collapse file Copy file name to clipboardExpand all lines: source/lib/assertions/assert.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,3 +68,13 @@ export const expectDeprecated = (expression: any) => { // tslint:disable-line:n | 68 | 68 | export const expectNotDeprecated = (expression: any) => { // tslint:disable-line:no-unused | 69 | 69 | // Do nothing, the TypeScript compiler handles this for us | 70 | 70 | }; | | 71 | + | | 72 | +/** | | 73 | + * Will print a warning with the type of the expression passed as argument. | | 74 | + * | | 75 | + * @param expression - Expression whose type should be printed as a warning. | | 76 | + */ | | 77 | +// @ts-ignore | | 78 | +export const printType = (expression: any) => { // tslint:disable-line:no-unused | | 79 | + // Do nothing, the TypeScript compiler handles this for us | | 80 | +}; |
Collapse file Copy file name to clipboardExpand all lines: source/lib/assertions/handlers/identicality.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,7 @@ export const isIdentical = (checker: TypeChecker, nodes: Set<CallExpression>): D | 55 | 55 | * Verifies that the argument of the assertion is not identical to the generic type of the assertion. | 56 | 56 | * | 57 | 57 | * @param checker - The TypeScript type checker. | 58 | | - * @param nodes - The `expectType` AST nodes. | | 58 | + * @param nodes - The `expectNotType` AST nodes. | 59 | 59 | * @return List of custom diagnostics. | 60 | 60 | */ | 61 | 61 | export const isNotIdentical = (checker: TypeChecker, nodes: Set<CallExpression>): Diagnostic[] => { | |
Collapse file Copy file name to clipboardExpand all lines: source/lib/assertions/handlers/index.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,3 +4,4 @@ export {Handler} from './handler'; | 4 | 4 | export {isIdentical, isNotIdentical} from './identicality'; | 5 | 5 | export {isNotAssignable} from './assignability'; | 6 | 6 | export {expectDeprecated, expectNotDeprecated} from './expect-deprecated'; | | 7 | +export {prinTypeWarning} from './informational'; |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import {CallExpression, TypeChecker} from '@tsd/typescript'; | | 2 | +import {Diagnostic} from '../../interfaces'; | | 3 | +import {makeDiagnostic} from '../../utils'; | | 4 | + | | 5 | +/** | | 6 | + * Emits a warning diagnostic for every call experession encountered containing the type of the first argument. | | 7 | + * | | 8 | + * @param checker - The TypeScript type checker. | | 9 | + * @param nodes - The `printType` AST nodes. | | 10 | + * @return List of warning diagnostics containing the type of the first argument. | | 11 | + */ | | 12 | +export const prinTypeWarning = (checker: TypeChecker, nodes: Set<CallExpression>): Diagnostic[] => { | | 13 | + const diagnostics: Diagnostic[] = []; | | 14 | + | | 15 | + if (!nodes) { | | 16 | + return diagnostics; | | 17 | + } | | 18 | + | | 19 | + for (const node of nodes) { | | 20 | + const argumentType = checker.getTypeAtLocation(node.arguments[0]); | | 21 | + const argumentExpression = node.arguments[0].getText(); | | 22 | + | | 23 | + diagnostics.push(makeDiagnostic(node, `Type for expression \`${argumentExpression}\` is: \`${checker.typeToString(argumentType)}\``, 'warning')); | | 24 | + } | | 25 | + | | 26 | + return diagnostics; | | 27 | +}; |
Collapse file Copy file name to clipboardExpand all lines: source/lib/assertions/index.ts +13-3Lines changed: 13 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import {CallExpression, TypeChecker} from '@tsd/typescript'; | 2 | 2 | import {Diagnostic} from '../interfaces'; | 3 | | -import {Handler, isIdentical, isNotIdentical, isNotAssignable, expectDeprecated, expectNotDeprecated} from './handlers'; | | 3 | +import { | | 4 | + Handler, | | 5 | + isIdentical, | | 6 | + isNotIdentical, | | 7 | + isNotAssignable, | | 8 | + expectDeprecated, | | 9 | + expectNotDeprecated, | | 10 | + prinTypeWarning, | | 11 | +} from './handlers'; | 4 | 12 |
| 5 | 13 | export enum Assertion { | 6 | 14 | EXPECT_TYPE = 'expectType', | @@ -9,7 +17,8 @@ export enum Assertion { | 9 | 17 | EXPECT_ASSIGNABLE = 'expectAssignable', | 10 | 18 | EXPECT_NOT_ASSIGNABLE = 'expectNotAssignable', | 11 | 19 | EXPECT_DEPRECATED = 'expectDeprecated', | 12 | | - EXPECT_NOT_DEPRECATED = 'expectNotDeprecated' | | 20 | + EXPECT_NOT_DEPRECATED = 'expectNotDeprecated', | | 21 | + PRINT_TYPE = 'printType', | 13 | 22 | } | 14 | 23 |
| 15 | 24 | // List of diagnostic handlers attached to the assertion | @@ -18,7 +27,8 @@ const assertionHandlers = new Map<Assertion, Handler>([ | 18 | 27 | [Assertion.EXPECT_NOT_TYPE, isNotIdentical], | 19 | 28 | [Assertion.EXPECT_NOT_ASSIGNABLE, isNotAssignable], | 20 | 29 | [Assertion.EXPECT_DEPRECATED, expectDeprecated], | 21 | | - [Assertion.EXPECT_NOT_DEPRECATED, expectNotDeprecated] | | 30 | + [Assertion.EXPECT_NOT_DEPRECATED, expectNotDeprecated], | | 31 | + [Assertion.PRINT_TYPE, prinTypeWarning] | 22 | 32 | ]); | 23 | 33 |
| 24 | 34 | /** | |
Collapse file Copy file name to clipboardExpand all lines: source/lib/parser.ts +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,8 +2,7 @@ import {Program, Node, CallExpression, forEachChild, isCallExpression, Identifie | 2 | 2 | import {Assertion} from './assertions'; | 3 | 3 | import {Location, Diagnostic} from './interfaces'; | 4 | 4 |
| 5 | | -// TODO: Use Object.values() when targetting Node.js >= 8 | 6 | | -const assertionTypes = new Set<string>(Object.keys(Assertion).map(key => Assertion[key])); | | 5 | +const assertionFnNames = new Set<string>(Object.values(Assertion)); | 7 | 6 |
| 8 | 7 | /** | 9 | 8 | * Extract all assertions. | @@ -18,11 +17,11 @@ export const extractAssertions = (program: Program): Map<Assertion, Set<CallExpr | 18 | 17 | */ | 19 | 18 | function walkNodes(node: Node) { | 20 | 19 | if (isCallExpression(node)) { | 21 | | - const text = (node.expression as Identifier).getText(); | | 20 | + const identifier = (node.expression as Identifier).getText(); | 22 | 21 |
| 23 | 22 | // Check if the call type is a valid assertion | 24 | | - if (assertionTypes.has(text)) { | 25 | | - const assertion = text as Assertion; | | 23 | + if (assertionFnNames.has(identifier)) { | | 24 | + const assertion = identifier as Assertion; | 26 | 25 |
| 27 | 26 | const nodes = assertions.get(assertion) || new Set<CallExpression>(); | 28 | 27 |
| |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export default function (foo: number): number | null; |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +module.exports.default = foo => { | | 2 | + return foo > 0 ? foo : null; | | 3 | +}; |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import {printType} from '../../..'; | | 2 | +import aboveZero from '.'; | | 3 | + | | 4 | +printType(aboveZero); | | 5 | +printType(null); | | 6 | +printType(undefined); | | 7 | +printType(null as any); | | 8 | +printType(null as never); | | 9 | +printType(null as unknown); | | 10 | +printType('foo'); |
|