From 9cc7c6b46af23c5219bc67a92b4dc8877ce84c4b Mon Sep 17 00:00:00 2001 From: Alec Aivazis Date: Fri, 25 Jun 2021 19:42:48 -0700 Subject: [PATCH] add line number to parser errors --- packages/houdini-common/src/parse.test.ts | 28 +++++++++++++----- packages/houdini-common/src/parse.ts | 35 +++++++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/houdini-common/src/parse.test.ts b/packages/houdini-common/src/parse.test.ts index 009431c15d..90fdbd48a2 100644 --- a/packages/houdini-common/src/parse.test.ts +++ b/packages/houdini-common/src/parse.test.ts @@ -341,8 +341,8 @@ describe('parser tests', () => { hello {:else if foo < 4} -
&&
@@ -364,8 +364,8 @@ describe('parser tests', () => { expect(result.instance?.end).toMatchInlineSnapshot(`undefined`) expect(result.module?.content).toMatchInlineSnapshot(`console.log("hello");`) - expect(result.module?.start).toMatchInlineSnapshot(`306`) - expect(result.module?.end).toMatchInlineSnapshot(`368`) + expect(result.module?.start).toMatchInlineSnapshot(`304`) + expect(result.module?.end).toMatchInlineSnapshot(`366`) checkScriptBounds(doc, result) }) @@ -374,11 +374,11 @@ describe('parser tests', () => { const doc = ` - +
- + { const doc = ` - +
hello
` @@ -446,6 +446,20 @@ describe('parser tests', () => { checkScriptBounds(doc, result) }) + + test('error messages include line number', () => { + try { + // parse the string + parseFile(` + { + return { + message, + lineNumber, + } + } + const pop = () => { const head = content.slice(0, 1) content = content.slice(1, content.length) index++ + + // if we found a newline, increment the line count + if (head === '\n') { + lineNumber++ + } + return head } @@ -105,7 +128,7 @@ function parse(str: string): { instance: StackElement | null; module: StackEleme // if the last character is not what we were looking for if (tail !== char) { - throw new Error('could not find ' + char) + throw ErrorWithLineNumber('could not find ' + char) } return acc @@ -131,7 +154,7 @@ function parse(str: string): { instance: StackElement | null; module: StackEleme // if the last character we saw was not the finishing character, there was a problem if (head !== finish) { - throw new Error(`did not encounter matching ${finish}.`) + throw ErrorWithLineNumber(`did not encounter matching ${finish}.`) } } @@ -166,7 +189,7 @@ function parse(str: string): { instance: StackElement | null; module: StackEleme const innerElement = stack.pop() const tagName = tag.substr(1) if (!innerElement || innerElement.tag !== parseTag(tagName).tag) { - throw new Error( + throw ErrorWithLineNumber( `unexpected closing tag ${parseTag(tagName).tag}, expected ${ innerElement?.tag }.`