From 0a55355515b4879d80f01aa611d01292b8063e8e Mon Sep 17 00:00:00 2001 From: jsus Date: Sat, 30 Sep 2023 15:29:05 +0300 Subject: [PATCH 1/2] Fix ordering of declares --- src/metadata/cleanQuery.ts | 3 ++- src/metadata/templateSections.ts | 7 +++---- tests/metadata/basicTemplates/templates/connection.sql | 3 ++- tests/metadata/compileView.test.ts | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/metadata/cleanQuery.ts b/src/metadata/cleanQuery.ts index 76c9e5f..bdc5cfe 100644 --- a/src/metadata/cleanQuery.ts +++ b/src/metadata/cleanQuery.ts @@ -4,7 +4,8 @@ const cleanQuery = (str: string) => { .replace(/\/\*[^]*?\*\//g, '') // remove multi-line comments .replace(/\s{1,}/g, ' ') // minify .trim() - if (cleaned[cleaned.length - 1] !== ';') cleaned += ';' + // FIXME This check needs to be checked + if (cleaned[cleaned.length - 1] !== ';' && cleaned.length > 2) cleaned += ';' return cleaned } diff --git a/src/metadata/templateSections.ts b/src/metadata/templateSections.ts index e657052..40c5523 100644 --- a/src/metadata/templateSections.ts +++ b/src/metadata/templateSections.ts @@ -1,15 +1,14 @@ import cleanQuery from './cleanQuery' export const extractSections = (view: string): TemplateSections => { - const declaresRegex = - /declare \$(?[\w\d]+) as (?[\w\d]+)( = (?[\w\d]+));?\s*/g + const declaresRegex = /declare \$(?[\w\d]+) as (?[\w\d]+);?\s*/g const declares = new Map() let match while ((match = declaresRegex.exec(view)) !== null) - if (match.groups) declares.set(match[1], match.groups as Declare) + if (match.groups) declares.set(match.groups['key'], match.groups as Declare) - const body = cleanQuery(view.replace(declaresRegex, '').trim()) + const body = cleanQuery(view.replaceAll(/declare[^;]*;/g, '').trim()) return { declares, body } } diff --git a/tests/metadata/basicTemplates/templates/connection.sql b/tests/metadata/basicTemplates/templates/connection.sql index 1f61aac..683a693 100644 --- a/tests/metadata/basicTemplates/templates/connection.sql +++ b/tests/metadata/basicTemplates/templates/connection.sql @@ -1 +1,2 @@ -declare $connectionId as Utf8; \ No newline at end of file +declare $connectionId as Utf8; +select $connectionId \ No newline at end of file diff --git a/tests/metadata/compileView.test.ts b/tests/metadata/compileView.test.ts index 93b8192..66ca940 100644 --- a/tests/metadata/compileView.test.ts +++ b/tests/metadata/compileView.test.ts @@ -35,7 +35,7 @@ describe('compile view', () => { for await (const { result } of compileViews(viewsPath, templatesPath)) views.push(result) const targetViews = [ - 'declare $connectionId as Utf8;declare $role as Utf8;select $role;' + 'declare $connectionId as Utf8;declare $role as Utf8;select $connectionId;select $role;' ] expect(views).toEqual(targetViews) }) From ca6c8a6c70d336ca2b28861e1ad37529dbafcdb5 Mon Sep 17 00:00:00 2001 From: jsus Date: Sat, 30 Sep 2023 15:30:52 +0300 Subject: [PATCH 2/2] Version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2fcd0db..4dd47c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@esseswann/lev", - "version": "0.0.8", + "version": "0.0.9", "repository": { "urL": "git+https://github.com/esseswann/lev.git", "type": "git"