Skip to content

Commit

Permalink
Merge pull request #13 from JakeSidSmith/next-line-assertions
Browse files Browse the repository at this point in the history
Next line assertions
  • Loading branch information
JakeSidSmith authored Mar 6, 2020
2 parents 219fc84 + ee742a1 commit 2cbeb37
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You must be using TypeScript `3.7` or above. This is a peer dependency.

## The syntax

Simply add a comment with the following structure to the end of any variable declaration:
Simply add a comment with the following structure to the end of the line, or on the line above:

```ts
// @type: ExpectedTypeHere
Expand All @@ -30,19 +30,29 @@ Basic examples:
// Assert variable types
const myNumber = 1; // @type: number

// @type: number
const myOtherNumber = 2;

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

// @type: Promise<string>
sendMessage('Hello again');

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

// @type: MyClass<ABC>
new MyClass(abc);
```

Example in tests:

```ts
describe('my getter', () => {
it('should return undefined if any values in the path are nullable', () => {
const result = get(obj, ['a', 'b', 'c']); // @type: string | undefined
// @type: string | undefined
const result = get(obj, ['a', 'b', 'c']);

expect(result).toBe(undefined);
});
Expand Down
3 changes: 3 additions & 0 deletions assertions/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ class MyClass<T> {

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<ABC>

// @type: ABC
removeNull(abc); // @type: ABC
3 changes: 3 additions & 0 deletions assertions/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ class MyClass<T> {

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<ABC | null>

// @type: ABC
removeNull(abc);
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.2",
"version": "0.2.3",
"description": "Check TypeScript types against assertion comments",
"publishConfig": {
"access": "public"
Expand Down
23 changes: 19 additions & 4 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { indent, isTruthyString } from './utils';
import { version } from './version';

const MATCHES_GLOB = /(?:}|\)|\*+\/?|\.[t]sx?)$/;
const MATCHES_TRAILING_COMMENT = /\/\/\s?@type(?::|\s)\s*(.+)\s*?$/;
const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_TRAILING_COMMENT = /\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_NODE_MODULES = /^node_modules/;

const assert = (tree: Tree) => {
Expand Down Expand Up @@ -129,15 +130,29 @@ const assert = (tree: Tree) => {
node.getStart()
);
const line = lines[lineIndex];
let result = MATCHES_TRAILING_COMMENT.exec(line);

const result = MATCHES_TRAILING_COMMENT.exec(line);
const lineNumber = lineIndex + 1;
const fileLine = `${relativeFileName}:${lineNumber}: `;

if (lineIndex > 0) {
const previousLine = lines[lineIndex - 1];
const lonelyResult = MATCHES_LONELY_COMMENT.exec(previousLine);

if (lonelyResult) {
if (result) {
errors.push(`${fileLine}Found 2 type comments for the same line`);
return;
} else {
result = lonelyResult;
}
}
}

if (result) {
commentsChecked += 1;

const comment = result[1];
const lineNumber = lineIndex + 1;
const fileLine = `${relativeFileName}:${lineNumber}: `;

if (ts.isVariableDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name);
Expand Down

0 comments on commit 2cbeb37

Please sign in to comment.