From 28ac09c1e2472d04c0d7fdcd5dba3078ed13362a Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 00:06:57 +0545 Subject: [PATCH 01/11] Convert Exception to a class --- types/index.d.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 00e04d427..2e226f033 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -66,7 +66,6 @@ declare namespace Handlebars { export function K(): void; export function createFrame(object: any): any; export function blockParams(obj: any[], ids: any[]): any[]; - export function Exception(message: string): void; export function log(level: number, obj: any): void; export function parse(input: string, options?: ParseOptions): hbs.AST.Program; export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program; @@ -87,6 +86,20 @@ declare namespace Handlebars { export function noConflict(): typeof Handlebars; + export class Exception { + constructor(message: string); + description: string; + fileName: string; + lineNumber?: string; + endLineNumber?: string; + message: string; + name: string; + number: number; + stack?: string; + column?: any; + endColumn?: any; + } + export class SafeString { constructor(str: string); toString(): string; From a3ea4bfecd0ba8ec2919512ce0a8251c586ad5dc Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 00:17:06 +0545 Subject: [PATCH 02/11] Add node param type declaration --- types/index.d.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 2e226f033..22292e7d5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -86,8 +86,15 @@ declare namespace Handlebars { export function noConflict(): typeof Handlebars; + export interface ExceptionNode { + loc?: { + start: { line: any; column: any; }; + end: { line: any; column: any; }; + } + } + export class Exception { - constructor(message: string); + constructor(message: string, node?: ExceptionNode); description: string; fileName: string; lineNumber?: string; From e39f01580437540312cd53a29981d10627e38c3c Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 21:52:53 +0545 Subject: [PATCH 03/11] Change line numbers to any type --- types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 22292e7d5..98293e7d9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -97,8 +97,8 @@ declare namespace Handlebars { constructor(message: string, node?: ExceptionNode); description: string; fileName: string; - lineNumber?: string; - endLineNumber?: string; + lineNumber?: any; + endLineNumber?: any; message: string; name: string; number: number; From fcc6d082e0f88879d62fe47b922fbf9fe5b50ce8 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 21:53:12 +0545 Subject: [PATCH 04/11] Test the types --- types/test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/types/test.ts b/types/test.ts index cef0d5981..07debaf58 100644 --- a/types/test.ts +++ b/types/test.ts @@ -201,3 +201,41 @@ function testParseWithoutProcessing() { const parsedTemplateWithoutOptions: hbs.AST.Program = Handlebars.parseWithoutProcessing('

Hello, my name is {{name}}.

'); } + +// Test exception constructor with just message. +try { + const exp = new Handlebars.Exception('Just a test message.'); + + // Assert + exp.message === 'Just a test message.' ? 'Passed': 'Failed'; + + throw exp; +} catch(e) { + // Catch exception here. +} + +try { + const node: Handlebars.ExceptionNode = { + loc: { + start: {line: 1, column: 5}, + end: {line: 10, column: 2} + } + }; + + const exp = new Handlebars.Exception('Just a test message.', node); + + // Assert + exp.message === 'Just a test message.' ? 'Passed': 'Failed'; + exp.lineNumber === 1 ? 'Passed': 'Failed'; + exp.column === 5 ? 'Passed': 'Failed'; + exp.endLineNumber === 10 ? 'Passed': 'Failed'; + exp.endColumn === 2 ? 'Passed': 'Failed'; + exp.description; // Could be a string value. + exp.name; // Could be a string value. + exp.fileName; // Could be a string value. + exp.stack; // Could be a string value if not undefined. + + throw exp; +} catch(e) { + // Catch exception here. +} From 53ad003e94f49657064abede19b448db8c0760c7 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 21:54:58 +0545 Subject: [PATCH 05/11] Format object literal --- types/test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/test.ts b/types/test.ts index 07debaf58..bc3ccf8df 100644 --- a/types/test.ts +++ b/types/test.ts @@ -217,8 +217,8 @@ try { try { const node: Handlebars.ExceptionNode = { loc: { - start: {line: 1, column: 5}, - end: {line: 10, column: 2} + start: { line: 1, column: 5 }, + end: { line: 10, column: 2 } } }; From 168d4f93bbfc57abec1f5a637a77aeb2eef86a26 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Fri, 25 Oct 2019 21:55:58 +0545 Subject: [PATCH 06/11] Add a comment --- types/test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/test.ts b/types/test.ts index bc3ccf8df..e7c870c70 100644 --- a/types/test.ts +++ b/types/test.ts @@ -214,6 +214,7 @@ try { // Catch exception here. } +// Test exception constructor with message and node. try { const node: Handlebars.ExceptionNode = { loc: { From 4c464ed852840d65c504907c8e781a1aba4aed86 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Mon, 28 Oct 2019 11:56:14 +0545 Subject: [PATCH 07/11] Wrap the exception tests within a function --- types/test.ts | 74 ++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/types/test.ts b/types/test.ts index e7c870c70..97533019b 100644 --- a/types/test.ts +++ b/types/test.ts @@ -202,41 +202,43 @@ function testParseWithoutProcessing() { const parsedTemplateWithoutOptions: hbs.AST.Program = Handlebars.parseWithoutProcessing('

Hello, my name is {{name}}.

'); } -// Test exception constructor with just message. -try { - const exp = new Handlebars.Exception('Just a test message.'); - - // Assert - exp.message === 'Just a test message.' ? 'Passed': 'Failed'; - - throw exp; -} catch(e) { - // Catch exception here. -} - -// Test exception constructor with message and node. -try { - const node: Handlebars.ExceptionNode = { - loc: { - start: { line: 1, column: 5 }, - end: { line: 10, column: 2 } - } - }; - - const exp = new Handlebars.Exception('Just a test message.', node); - - // Assert - exp.message === 'Just a test message.' ? 'Passed': 'Failed'; - exp.lineNumber === 1 ? 'Passed': 'Failed'; - exp.column === 5 ? 'Passed': 'Failed'; - exp.endLineNumber === 10 ? 'Passed': 'Failed'; - exp.endColumn === 2 ? 'Passed': 'Failed'; - exp.description; // Could be a string value. - exp.name; // Could be a string value. - exp.fileName; // Could be a string value. - exp.stack; // Could be a string value if not undefined. +function testExceptionTypings() { + // Test exception constructor with just message. + try { + const exp = new Handlebars.Exception('Just a test message.'); + + // Assert + exp.message === 'Just a test message.' ? 'Passed': 'Failed'; - throw exp; -} catch(e) { - // Catch exception here. + throw exp; + } catch(e) { + // Catch exception here. + } + + // Test exception constructor with message and node. + try { + const node: Handlebars.ExceptionNode = { + loc: { + start: { line: 1, column: 5 }, + end: { line: 10, column: 2 } + } + }; + + const exp = new Handlebars.Exception('Just a test message.', node); + + // Assert + exp.message === 'Just a test message.' ? 'Passed': 'Failed'; + exp.lineNumber === 1 ? 'Passed': 'Failed'; + exp.column === 5 ? 'Passed': 'Failed'; + exp.endLineNumber === 10 ? 'Passed': 'Failed'; + exp.endColumn === 2 ? 'Passed': 'Failed'; + exp.description; // Could be a string value. + exp.name; // Could be a string value. + exp.fileName; // Could be a string value. + exp.stack; // Could be a string value if not undefined. + + throw exp; + } catch(e) { + // Catch exception here. + } } From e96c7464967c18b37db4ac0b9ac69a371c1799df Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Mon, 28 Oct 2019 11:58:10 +0545 Subject: [PATCH 08/11] Use existing type hbs.AST.Node --- types/index.d.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 98293e7d9..bb0656a51 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -86,15 +86,8 @@ declare namespace Handlebars { export function noConflict(): typeof Handlebars; - export interface ExceptionNode { - loc?: { - start: { line: any; column: any; }; - end: { line: any; column: any; }; - } - } - export class Exception { - constructor(message: string, node?: ExceptionNode); + constructor(message: string, node?: hbs.AST.Node); description: string; fileName: string; lineNumber?: any; From c30d0e8a4a8d5c613e07d3211022b7c552f7d41c Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Mon, 28 Oct 2019 12:46:28 +0545 Subject: [PATCH 09/11] Update exception typing tests --- types/test.ts | 74 +++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/types/test.ts b/types/test.ts index 97533019b..613281817 100644 --- a/types/test.ts +++ b/types/test.ts @@ -203,42 +203,40 @@ function testParseWithoutProcessing() { } function testExceptionTypings() { - // Test exception constructor with just message. - try { - const exp = new Handlebars.Exception('Just a test message.'); - - // Assert - exp.message === 'Just a test message.' ? 'Passed': 'Failed'; - - throw exp; - } catch(e) { - // Catch exception here. - } - - // Test exception constructor with message and node. - try { - const node: Handlebars.ExceptionNode = { - loc: { - start: { line: 1, column: 5 }, - end: { line: 10, column: 2 } - } - }; - - const exp = new Handlebars.Exception('Just a test message.', node); - - // Assert - exp.message === 'Just a test message.' ? 'Passed': 'Failed'; - exp.lineNumber === 1 ? 'Passed': 'Failed'; - exp.column === 5 ? 'Passed': 'Failed'; - exp.endLineNumber === 10 ? 'Passed': 'Failed'; - exp.endColumn === 2 ? 'Passed': 'Failed'; - exp.description; // Could be a string value. - exp.name; // Could be a string value. - exp.fileName; // Could be a string value. - exp.stack; // Could be a string value if not undefined. - - throw exp; - } catch(e) { - // Catch exception here. - } + // Test exception constructor with a single argument - message. + let exception: Handlebars.Exception = new Handlebars.Exception('message'); + + // Fields + let message: string = exception.message; + let lineNumber: number = exception.lineNumber; + let column: number = exception.column; + let endLineNumber: number = exception.endLineNumber; + let endColumn: number = exception.endColumn; + let description = exception.description; + let name: string = exception.name; + let fileName: string = exception.fileName; + let stack: string | undefined = exception.stack; +} + +function testExceptionWithNodeTypings() { + // Test exception constructor with both arguments. + const exception = new Handlebars.Exception('Just a test message.', { + type: 'MustacheStatement', + loc: { + source: 'source', + start: { line: 1, column: 5 }, + end: { line: 10, column: 2 } + } + }); + + // Fields + let message: string = exception.message; + let lineNumber: number = exception.lineNumber; + let column: number = exception.column; + let endLineNumber: number = exception.endLineNumber; + let endColumn: number = exception.endColumn; + let description = exception.description; + let name: string = exception.name; + let fileName: string = exception.fileName; + let stack: string | undefined = exception.stack; } From 9684fb796598d0abcb65176d6fe990cfec9644cd Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Mon, 28 Oct 2019 12:48:36 +0545 Subject: [PATCH 10/11] Add type for variable declaration --- types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/test.ts b/types/test.ts index 613281817..d558b4f3d 100644 --- a/types/test.ts +++ b/types/test.ts @@ -220,7 +220,7 @@ function testExceptionTypings() { function testExceptionWithNodeTypings() { // Test exception constructor with both arguments. - const exception = new Handlebars.Exception('Just a test message.', { + const exception: Handlebars.Exception = new Handlebars.Exception('Just a test message.', { type: 'MustacheStatement', loc: { source: 'source', From 0bd4cf4469eb3df2925ecefb1253b51dd0ed5b40 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Mon, 28 Oct 2019 12:56:03 +0545 Subject: [PATCH 11/11] Update message --- types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/test.ts b/types/test.ts index d558b4f3d..fa9832683 100644 --- a/types/test.ts +++ b/types/test.ts @@ -220,7 +220,7 @@ function testExceptionTypings() { function testExceptionWithNodeTypings() { // Test exception constructor with both arguments. - const exception: Handlebars.Exception = new Handlebars.Exception('Just a test message.', { + const exception: Handlebars.Exception = new Handlebars.Exception('message', { type: 'MustacheStatement', loc: { source: 'source',