File tree 3 files changed +77
-20
lines changed
3 files changed +77
-20
lines changed Original file line number Diff line number Diff line change @@ -6975,17 +6975,22 @@ const injectExtras = (name, contents) => {
6975
6975
return parts . prolog + parts . plugindef + parts . epilog ;
6976
6976
} ;
6977
6977
exports . injectExtras = injectExtras ;
6978
- const inject = ( contents , injection ) => {
6978
+ const inject = ( plugindef , injection ) => {
6979
6979
if ( injection ) {
6980
- const returnRegex = / ^ ( \s * ) r e t u r n / gm;
6981
- const endRegex = / ^ ( \s * ) * e n d / 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 * ) e n d ( \s * ) $ / gm) ;
6988
+ const returnIndex = getLastIndex ( / ^ ( \s * ) r e t u r n / gm) ;
6989
+ const index = Math . min ( endIndex , returnIndex ) ;
6990
+ return plugindef . slice ( 0 , index ) + injection + '\n' + plugindef . slice ( index ) ;
6986
6991
}
6987
6992
else {
6988
- return contents ;
6993
+ return plugindef ;
6989
6994
}
6990
6995
} ;
6991
6996
const getHashURL = ( name ) => {
Original file line number Diff line number Diff line change @@ -347,6 +347,56 @@ return true
347
347
expect ( result ) . toEqual ( expected ) ;
348
348
} ) ;
349
349
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
+
350
400
it ( 'should return the unaltered contents if the search criteria is not found' , ( ) => {
351
401
const name = 'test.lua' ;
352
402
const contents = `
Original file line number Diff line number Diff line change @@ -15,21 +15,23 @@ export const injectExtras = (name: string, contents: string): string => {
15
15
return parts . prolog + parts . plugindef + parts . epilog ;
16
16
}
17
17
18
- const inject = ( contents : string , injection : string ) : string => {
18
+ const inject = ( plugindef : string , injection : string ) : string => {
19
19
if ( injection ) {
20
- const returnRegex = / ^ ( \s * ) r e t u r n / gm;
21
- const endRegex = / ^ ( \s * ) * e n d / 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
+ }
22
27
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 * ) e n d ( \s * ) $ / gm) ;
29
+ const returnIndex = getLastIndex ( / ^ ( \s * ) r e t u r n / gm) ;
30
+
31
+ const index = Math . min ( endIndex , returnIndex ) ;
32
+ return plugindef . slice ( 0 , index ) + injection + '\n' + plugindef . slice ( index ) ;
31
33
} else {
32
- return contents ;
34
+ return plugindef ;
33
35
}
34
36
}
35
37
You can’t perform that action at this time.
0 commit comments