Skip to content

Commit 2cbeb37

Browse files
authored
Merge pull request #13 from JakeSidSmith/next-line-assertions
Next line assertions
2 parents 219fc84 + ee742a1 commit 2cbeb37

6 files changed

Lines changed: 39 additions & 8 deletions

File tree

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You must be using TypeScript `3.7` or above. This is a peer dependency.
1818

1919
## The syntax
2020

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

2323
```ts
2424
// @type: ExpectedTypeHere
@@ -30,19 +30,29 @@ Basic examples:
3030
// Assert variable types
3131
const myNumber = 1; // @type: number
3232

33+
// @type: number
34+
const myOtherNumber = 2;
35+
3336
// Assert return type of function
3437
sendMessage('Hello'); // @type: Promise<string>
3538

39+
// @type: Promise<string>
40+
sendMessage('Hello again');
41+
3642
// Assert type of class instance
3743
new MyClass(abc); // @type: MyClass<ABC>
44+
45+
// @type: MyClass<ABC>
46+
new MyClass(abc);
3847
```
3948

4049
Example in tests:
4150

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

4757
expect(result).toBe(undefined);
4858
});

assertions/fail.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ class MyClass<T> {
3131

3232
// tslint:disable-next-line:no-unused-expression
3333
new MyClass(abc); // @type: MyClass<ABC>
34+
35+
// @type: ABC
36+
removeNull(abc); // @type: ABC

assertions/pass.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ class MyClass<T> {
3131

3232
// tslint:disable-next-line:no-unused-expression
3333
new MyClass(abc); // @type: MyClass<ABC | null>
34+
35+
// @type: ABC
36+
removeNull(abc);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jakesidsmith/tsassert",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Check TypeScript types against assertion comments",
55
"publishConfig": {
66
"access": "public"

src/assert.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { indent, isTruthyString } from './utils';
88
import { version } from './version';
99

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

1415
const assert = (tree: Tree) => {
@@ -129,15 +130,29 @@ const assert = (tree: Tree) => {
129130
node.getStart()
130131
);
131132
const line = lines[lineIndex];
133+
let result = MATCHES_TRAILING_COMMENT.exec(line);
132134

133-
const result = MATCHES_TRAILING_COMMENT.exec(line);
135+
const lineNumber = lineIndex + 1;
136+
const fileLine = `${relativeFileName}:${lineNumber}: `;
137+
138+
if (lineIndex > 0) {
139+
const previousLine = lines[lineIndex - 1];
140+
const lonelyResult = MATCHES_LONELY_COMMENT.exec(previousLine);
141+
142+
if (lonelyResult) {
143+
if (result) {
144+
errors.push(`${fileLine}Found 2 type comments for the same line`);
145+
return;
146+
} else {
147+
result = lonelyResult;
148+
}
149+
}
150+
}
134151

135152
if (result) {
136153
commentsChecked += 1;
137154

138155
const comment = result[1];
139-
const lineNumber = lineIndex + 1;
140-
const fileLine = `${relativeFileName}:${lineNumber}: `;
141156

142157
if (ts.isVariableDeclaration(node)) {
143158
const symbol = checker.getSymbolAtLocation(node.name);

0 commit comments

Comments
 (0)