Skip to content

Commit 03d7416

Browse files
authored
Merge pull request finale-lua#743 from asherber/end-regex
Improve inject() in bundler
2 parents 9c2dea5 + 61dad90 commit 03d7416

File tree

3 files changed

+77
-20
lines changed

3 files changed

+77
-20
lines changed

.github/actions/bundle/dist/index.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -6975,17 +6975,22 @@ const injectExtras = (name, contents) => {
69756975
return parts.prolog + parts.plugindef + parts.epilog;
69766976
};
69776977
exports.injectExtras = injectExtras;
6978-
const inject = (contents, injection) => {
6978+
const inject = (plugindef, injection) => {
69796979
if (injection) {
6980-
const returnRegex = /^(\s*)return/gm;
6981-
const endRegex = /^(\s*)*end/gm;
6982-
const returnMatch = returnRegex.exec(contents);
6983-
const endMatch = endRegex.exec(contents);
6984-
const index = Math.min(returnMatch ? returnMatch.index : Infinity, endMatch ? endMatch.index : Infinity);
6985-
return contents.slice(0, index) + injection + '\n' + contents.slice(index);
6980+
const getLastIndex = (regex) => {
6981+
let match, result = Infinity;
6982+
while ((match = regex.exec(plugindef))) {
6983+
result = match.index;
6984+
}
6985+
return result;
6986+
};
6987+
const endIndex = getLastIndex(/^(\s*)end(\s*)$/gm);
6988+
const returnIndex = getLastIndex(/^(\s*)return/gm);
6989+
const index = Math.min(endIndex, returnIndex);
6990+
return plugindef.slice(0, index) + injection + '\n' + plugindef.slice(index);
69866991
}
69876992
else {
6988-
return contents;
6993+
return plugindef;
69896994
}
69906995
};
69916996
const getHashURL = (name) => {

.github/actions/bundle/src/inject-extras.test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,56 @@ return true
347347
expect(result).toEqual(expected);
348348
});
349349

350+
it('should find the last occurrence of end', () => {
351+
const name = 'test.lua';
352+
const contents = `
353+
function plugindef()
354+
finaleplugin.Foo = [[
355+
end
356+
]]
357+
end
358+
`;
359+
360+
const expected = `
361+
function plugindef()
362+
finaleplugin.Foo = [[
363+
end
364+
]]
365+
finaleplugin.HashURL = "https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/test.hash"
366+
end
367+
`;
368+
369+
const result = injectExtras(name, contents);
370+
expect(result).toEqual(expected);
371+
});
372+
373+
374+
it('should find the last occurrence of return', () => {
375+
const name = 'test.lua';
376+
const contents = `
377+
function plugindef()
378+
finaleplugin.Foo = [[
379+
return
380+
]]
381+
return "Foo"
382+
end
383+
`;
384+
385+
const expected = `
386+
function plugindef()
387+
finaleplugin.Foo = [[
388+
return
389+
]]
390+
finaleplugin.HashURL = "https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/test.hash"
391+
return "Foo"
392+
end
393+
`;
394+
395+
const result = injectExtras(name, contents);
396+
expect(result).toEqual(expected);
397+
});
398+
399+
350400
it('should return the unaltered contents if the search criteria is not found', () => {
351401
const name = 'test.lua';
352402
const contents = `

.github/actions/bundle/src/inject-extras.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@ export const injectExtras = (name: string, contents: string): string => {
1515
return parts.prolog + parts.plugindef + parts.epilog;
1616
}
1717

18-
const inject = (contents: string, injection: string): string => {
18+
const inject = (plugindef: string, injection: string): string => {
1919
if (injection) {
20-
const returnRegex = /^(\s*)return/gm;
21-
const endRegex = /^(\s*)*end/gm;
20+
const getLastIndex = (regex: RegExp): number => {
21+
let match, result = Infinity;
22+
while ((match = regex.exec(plugindef))) {
23+
result = match.index;
24+
}
25+
return result;
26+
}
2227

23-
const returnMatch = returnRegex.exec(contents);
24-
const endMatch = endRegex.exec(contents);
25-
26-
const index = Math.min(
27-
returnMatch ? returnMatch.index : Infinity,
28-
endMatch ? endMatch.index : Infinity
29-
);
30-
return contents.slice(0, index) + injection + '\n' + contents.slice(index);
28+
const endIndex = getLastIndex(/^(\s*)end(\s*)$/gm);
29+
const returnIndex = getLastIndex(/^(\s*)return/gm);
30+
31+
const index = Math.min(endIndex, returnIndex);
32+
return plugindef.slice(0, index) + injection + '\n' + plugindef.slice(index);
3133
} else {
32-
return contents;
34+
return plugindef;
3335
}
3436
}
3537

0 commit comments

Comments
 (0)