Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compile sections #10

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/metadata/cleanQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 3 additions & 4 deletions src/metadata/templateSections.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import cleanQuery from './cleanQuery'

export const extractSections = (view: string): TemplateSections => {
const declaresRegex =
/declare \$(?<varName>[\w\d]+) as (?<type>[\w\d]+)( = (?<defaultValue>[\w\d]+));?\s*/g
const declaresRegex = /declare \$(?<key>[\w\d]+) as (?<type>[\w\d]+);?\s*/g

const declares = new Map<string, Declare>()
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 }
}
Expand Down
3 changes: 2 additions & 1 deletion tests/metadata/basicTemplates/templates/connection.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare $connectionId as Utf8;
declare $connectionId as Utf8;
select $connectionId
2 changes: 1 addition & 1 deletion tests/metadata/compileView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down