Skip to content

Commit

Permalink
Merge pull request #12 from JakeSidSmith/assert-expressions
Browse files Browse the repository at this point in the history
Assert expressions
  • Loading branch information
JakeSidSmith authored Mar 6, 2020
2 parents 7bcb480 + 43beb0c commit 219fc84
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ Simply add a comment with the following structure to the end of any variable dec
// @type: ExpectedTypeHere
```

Basic examples:

```ts
// Assert variable types
const myNumber = 1; // @type: number

// Assert return type of function
sendMessage('Hello'); // @type: Promise<string>

// Assert type of class instance
new MyClass(abc); // @type: MyClass<ABC>
```

Example in tests:

```ts
Expand Down
15 changes: 14 additions & 1 deletion assertions/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ interface ABC {
d: ['a', 'b', 'c'];
}

export const abc = {} as ABC | null; // @type: ABC | null
export const abc = {} as ABC | null; // @type: ABC

export const result = removeNull(abc); // @type: ABC | null

removeNull(abc); // @type: ABC | null

export const c = abc?.a?.b.c; // @type string | number

export const d = abc?.d; // @type ['a', 'b', 'c'] | undefined

class MyClass<T> {
public input: T;

public constructor(input: T) {
this.input = input;
}
}

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<ABC>
13 changes: 13 additions & 0 deletions assertions/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export const abc = {} as ABC | null; // @type: ABC | null

export const result = removeNull(abc); // @type: ABC

removeNull(abc); // @type: ABC

export const c = abc?.a?.b.c; // @type string | number | undefined

export const d = abc?.d; // @type ["a", "b", "c"] | undefined

class MyClass<T> {
public input: T;

public constructor(input: T) {
this.input = input;
}
}

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<ABC | null>
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jakesidsmith/tsassert",
"version": "0.2.1",
"version": "0.2.2",
"description": "Check TypeScript types against assertion comments",
"publishConfig": {
"access": "public"
Expand Down
41 changes: 33 additions & 8 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,46 @@ const assert = (tree: Tree) => {

const result = MATCHES_TRAILING_COMMENT.exec(line);

if (result && ts.isVariableDeclaration(node)) {
if (result) {
commentsChecked += 1;

const comment = result[1];
const lineNumber = lineIndex + 1;
const symbol = checker.getSymbolAtLocation(node.name);
const fileLine = `${relativeFileName}:${lineNumber}: `;

if (ts.isVariableDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name);
const variableName = node.name.getText();

if (symbol) {
const typeNode = checker.getTypeOfSymbolAtLocation(symbol, node);
const type = checker.typeToString(typeNode);
if (symbol) {
const typeNode = checker.getTypeOfSymbolAtLocation(symbol, node);
const type = checker.typeToString(typeNode);

if (type !== comment) {
errors.push(
`${fileLine}Type of "${variableName}" - ${type} did not match type comment ${comment}`
);
}
}

if (type !== comment) {
errors.push(
`${relativeFileName}:${lineNumber}: Type ${type} did not match type comment ${comment}`
return;
} else if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
const signature = checker.getResolvedSignature(node);
const functionName = node.expression.getText();

if (signature) {
const type = checker.typeToString(
checker.getReturnTypeOfSignature(signature)
);

if (type !== comment) {
errors.push(
`${fileLine}Return type of "${functionName}" - ${type} did not match type comment ${comment}`
);
}
}

return;
}
}

Expand Down

0 comments on commit 219fc84

Please sign in to comment.