Skip to content

Commit

Permalink
Merge pull request #399 from netux/feat/editor-compile-subroutines-in…
Browse files Browse the repository at this point in the history
…-actions

Include subroutines used in actions in the compiled output of the Editor
  • Loading branch information
Mitcheljager authored Jan 22, 2024
2 parents 475a01b + 000373a commit 0ef2288
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/javascript/src/utils/compiler/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ ${ subroutines.map((v, i) => ` ${ i }: ${ v }`).join("\n") }
}

export function getSubroutines(joinedItems) {
let subroutines = joinedItems.match(/Subroutine;[\r\n]+([^\r\n;]+)/g) || []
subroutines = subroutines.map(s => s.replace("Subroutine;\n", "").replace("Call Subroutine", "").replace(/[\())\s]/g, ""))
return [...new Set(subroutines)]
const declaredSubroutines = Array.from(joinedItems.matchAll(/Subroutine;[\r\n]+([^\r\n;]+)/g))
.map((match) => match[1].trim())
const usedSubroutines = Array.from(joinedItems.matchAll(/(?:Call Subroutine|Start Rule)\s*\(([^,\)]+)/g))
.map((match) => match[1].trim())
return [...new Set([...declaredSubroutines, ...usedSubroutines])]
}
23 changes: 20 additions & 3 deletions spec/javascript/utils/compiler/subroutines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { disregardWhitespace } from "../../helpers/text"

describe("subroutines.js", () => {
describe("getSubroutines", () => {
test("Should extract subroutines", () => {
test("Should extract subroutines declared from rules", () => {
const input = `
Subroutine;
someSubroutine1;
Expand All @@ -15,13 +15,27 @@ describe("subroutines.js", () => {
expect(getSubroutines(input)).toEqual(expectedOutput)
})

test("Should extract subroutines used in actions", () => {
const input = `
Call Subroutine(someSubroutine1);
Start Rule(someSubroutine2, OtherParamIdk);
`
const expectedOutput = ["someSubroutine1", "someSubroutine2"]
expect(getSubroutines(input)).toEqual(expectedOutput)
})

test("Should handle duplicate subroutines", () => {
const input = `
Subroutine;
someSubroutine;
Subroutine;
someSubroutine;
Call Subroutine(someSubroutine);
Start Rule(someSubroutine, OtherParamIdk);
`
const expectedOutput = ["someSubroutine"]
expect(getSubroutines(input)).toEqual(expectedOutput)
Expand All @@ -32,11 +46,14 @@ describe("subroutines.js", () => {
test("Should compile subroutines", () => {
const input = `
Subroutine;
someSubroutine;
someSubroutine1;
Call Subroutine(someSubroutine2);
`
const expectedOutput = `
subroutines {
0: someSubroutine
0: someSubroutine1
1: someSubroutine2
}\n\n
`
expect(disregardWhitespace(compileSubroutines(input))).toBe(disregardWhitespace(expectedOutput))
Expand Down

0 comments on commit 0ef2288

Please sign in to comment.