From 0b587202f19e05c5157a24ce459385a4fd43ad36 Mon Sep 17 00:00:00 2001 From: Jake 'Sid' Smith Date: Sun, 8 Mar 2020 11:12:52 +0000 Subject: [PATCH 1/3] Change @type to @tsassert to avoid clashes with other tools --- README.md | 16 ++++++++-------- assertions/fail.ts | 16 ++++++++-------- assertions/pass.ts | 14 +++++++------- src/assert.ts | 4 ++-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ca7ee26..0a08db2 100644 --- a/README.md +++ b/README.md @@ -21,28 +21,28 @@ You must be using TypeScript 3 (this is a peer dependency). Simply add a comment with the following structure to the end of the line, or on the line above: ```ts -// @type: ExpectedTypeHere +// @tsassert: ExpectedTypeHere ``` Basic examples: ```ts // Assert variable types -const myNumber = 1; // @type: number +const myNumber = 1; // @tsassert: number -// @type: number +// @tsassert: number const myOtherNumber = 2; // Assert return type of function -sendMessage('Hello'); // @type: Promise +sendMessage('Hello'); // @tsassert: Promise -// @type: Promise +// @tsassert: Promise sendMessage('Hello again'); // Assert type of class instance -new MyClass(abc); // @type: MyClass +new MyClass(abc); // @tsassert: MyClass -// @type: MyClass +// @tsassert: MyClass new MyClass(abc); ``` @@ -51,7 +51,7 @@ Example in tests: ```ts describe('my getter', () => { it('should return undefined if any values in the path are nullable', () => { - // @type: string | undefined + // @tsassert: string | undefined const result = get(obj, ['a', 'b', 'c']); expect(result).toBe(undefined); diff --git a/assertions/fail.ts b/assertions/fail.ts index b090260..d887860 100644 --- a/assertions/fail.ts +++ b/assertions/fail.ts @@ -11,15 +11,15 @@ interface ABC { d: ['a', 'b', 'c']; } -export const abc = {} as ABC | null; // @type: ABC +export const abc = {} as ABC | null; // @tsassert: ABC -export const result = removeNull(abc); // @type: ABC | null +export const result = removeNull(abc); // @tsassert: ABC | null -removeNull(abc); // @type: ABC | null +removeNull(abc); // @tsassert: ABC | null -export const c = abc?.a?.b.c; // @type string | number +export const c = abc?.a?.b.c; // @tsassert string | number -export const d = abc?.d; // @type ['a', 'b', 'c'] | undefined +export const d = abc?.d; // @tsassert ['a', 'b', 'c'] | undefined class MyClass { public input: T; @@ -30,7 +30,7 @@ class MyClass { } // tslint:disable-next-line:no-unused-expression -new MyClass(abc); // @type: MyClass +new MyClass(abc); // @tsassert: MyClass -// @type: ABC -removeNull(abc); // @type: ABC +// @tsassert: ABC +removeNull(abc); // @tsassert: ABC diff --git a/assertions/pass.ts b/assertions/pass.ts index 03498ea..13b684c 100644 --- a/assertions/pass.ts +++ b/assertions/pass.ts @@ -11,15 +11,15 @@ interface ABC { d: ['a', 'b', 'c']; } -export const abc = {} as ABC | null; // @type: ABC | null +export const abc = {} as ABC | null; // @tsassert: ABC | null -export const result = removeNull(abc); // @type: ABC +export const result = removeNull(abc); // @tsassert: ABC -removeNull(abc); // @type: ABC +removeNull(abc); // @tsassert: ABC -export const c = abc?.a?.b.c; // @type string | number | undefined +export const c = abc?.a?.b.c; // @tsassert string | number | undefined -export const d = abc?.d; // @type ["a", "b", "c"] | undefined +export const d = abc?.d; // @tsassert ["a", "b", "c"] | undefined class MyClass { public input: T; @@ -30,7 +30,7 @@ class MyClass { } // tslint:disable-next-line:no-unused-expression -new MyClass(abc); // @type: MyClass +new MyClass(abc); // @tsassert: MyClass -// @type: ABC +// @tsassert: ABC removeNull(abc); diff --git a/src/assert.ts b/src/assert.ts index b06a1d4..8bd8256 100644 --- a/src/assert.ts +++ b/src/assert.ts @@ -10,8 +10,8 @@ import { indent, isTruthyString } from './utils'; import { version } from './version'; const MATCHES_GLOB = /(?:}|\)|\*+\/?|\.[t]sx?)$/; -const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/; -const MATCHES_TRAILING_COMMENT = /\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/; +const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/; +const MATCHES_TRAILING_COMMENT = /\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/; const MATCHES_NODE_MODULES = /^node_modules/; interface AssertionError { From 65213edd89e728aa1b3b2c7065402261d987c3bb Mon Sep 17 00:00:00 2001 From: Jake 'Sid' Smith Date: Sun, 8 Mar 2020 11:18:00 +0000 Subject: [PATCH 2/3] Output comment prefix in logs --- src/assert.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/assert.ts b/src/assert.ts index 8bd8256..f3f5134 100644 --- a/src/assert.ts +++ b/src/assert.ts @@ -9,6 +9,7 @@ import * as logger from './logger'; import { indent, isTruthyString } from './utils'; import { version } from './version'; +const COMMENT_PREFIX = '@tsassert'; const MATCHES_GLOB = /(?:}|\)|\*+\/?|\.[t]sx?)$/; const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/; const MATCHES_TRAILING_COMMENT = /\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/; @@ -25,7 +26,7 @@ const assert = (tree: Tree) => { return version(); } - logger.log('\nChecking type assertion comments...\n'); + logger.log(`\nChecking ${COMMENT_PREFIX} comments...\n`); const cwd = process.cwd(); @@ -149,7 +150,9 @@ const assert = (tree: Tree) => { if (lonelyResult) { if (result) { - errors.push(`${fileLine}Found 2 type comments for the same line`); + errors.push( + `${fileLine}Found 2 ${COMMENT_PREFIX} comments for the same line` + ); return; } else { result = lonelyResult; @@ -171,7 +174,7 @@ const assert = (tree: Tree) => { const type = checker.typeToString(typeNode); if (type !== comment) { - const message = `${fileLine}Type of "${variableName}" did not match type comment:`; + const message = `${fileLine}Type of "${variableName}" did not match ${COMMENT_PREFIX} comment:`; errors.push({ message, @@ -192,7 +195,7 @@ const assert = (tree: Tree) => { ); if (type !== comment) { - const message = `${fileLine}Return type of "${functionName}" did not match type comment:`; + const message = `${fileLine}Return type of "${functionName}" did not match ${COMMENT_PREFIX} comment:`; errors.push({ message, @@ -251,7 +254,10 @@ const assert = (tree: Tree) => { } }); - return logger.error('\nSome files failed tsassert checks.\n', true); + return logger.error( + `\nSome files failed ${COMMENT_PREFIX} checks.\n`, + true + ); } else { if (!filesChecked) { logger.warn( @@ -259,10 +265,10 @@ const assert = (tree: Tree) => { ); } else if (!commentsChecked) { logger.warn( - '\nCould not find any type assertions in matched files.\nRun with --verbose to see patterns that were checked.\n' + `\nCould not find any ${COMMENT_PREFIX} comments in matched files.\nRun with --verbose to see patterns that were checked.\n` ); } else { - logger.success('\nAll files passed tsassert checks.\n'); + logger.success(`\nAll files passed ${COMMENT_PREFIX} checks.\n`); } } }; From c890fa4d2d7ee651a001cb70840a6a33f6338776 Mon Sep 17 00:00:00 2001 From: Jake 'Sid' Smith Date: Sun, 8 Mar 2020 11:20:07 +0000 Subject: [PATCH 3/3] 0.3.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 99091be..0cb5f0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@jakesidsmith/tsassert", - "version": "0.2.5", + "version": "0.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f8dae99..358af63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jakesidsmith/tsassert", - "version": "0.2.5", + "version": "0.3.0", "description": "Check TypeScript types against assertion comments", "publishConfig": { "access": "public"