@@ -322,7 +322,7 @@ module Templating {
322
322
323
323
/** Gets the template file referenced by this node. */
324
324
final TemplateFile getTemplateFile ( ) {
325
- result = this .getValue ( ) .( TemplateFileReferenceString ) .getTemplateFile ( )
325
+ result = this .getValue ( ) .( TemplateFileReferenceString ) .getTemplateFile ( getFile ( ) . getParentContainer ( ) )
326
326
}
327
327
}
328
328
@@ -361,19 +361,16 @@ module Templating {
361
361
*/
362
362
string getStem ( ) { result = getBaseName ( ) .regexpCapture ( "(.*?)(?:\\.([^.]*))?" , 1 ) }
363
363
364
- /** Gets a template string with all leading `../` sequences resolved. */
365
- private TemplateFileReferenceString resolveUpwardTraversal ( ) {
364
+ /** Gets the template file referenced by this string when resolved from `baseFolder`. */
365
+ final TemplateFile getTemplateFile ( Folder baseFolder ) {
366
+ result = getBestMatchingTarget ( baseFolder , this )
367
+ or
366
368
exists ( UpwardTraversalSuffix up |
367
369
this = up .getOriginal ( ) and
368
- result = up .( TemplateFileReferenceString ) .resolveUpwardTraversal ( )
370
+ result = up .( TemplateFileReferenceString ) .getTemplateFile ( baseFolder .getParentContainer ( ) ) and
371
+ baseFolder = getContextFolder ( )
369
372
)
370
- or
371
- not this = any ( UpwardTraversalSuffix up ) .getOriginal ( ) and
372
- result = this
373
373
}
374
-
375
- /** Gets the template file referenced by this string. */
376
- final TemplateFile getTemplateFile ( ) { result = getBestMatchingTarget ( this .resolveUpwardTraversal ( ) ) }
377
374
}
378
375
379
376
/** The value of a template reference node, as a template reference string. */
@@ -470,7 +467,7 @@ module Templating {
470
467
}
471
468
472
469
/**
473
- * Gets the length of the longest common prefix between `file` and `ref`.
470
+ * Gets the length of the longest common prefix between `file` and the `baseFolder` of `ref`.
474
471
*
475
472
* This is used to rank all the possible files that `ref` could refer to.
476
473
* Picking the one with the highest rank ensures that the file most closely related
@@ -487,21 +484,22 @@ module Templating {
487
484
* and vice versa in `B/components/foo.js`.
488
485
*/
489
486
pragma [ nomagic]
490
- private int getRankOfMatchingTarget ( TemplateFile file , TemplateFileReferenceString ref ) {
487
+ private int getRankOfMatchingTarget ( TemplateFile file , Folder baseFolder , TemplateFileReferenceString ref ) {
491
488
file = getAMatchingTarget ( ref ) and
489
+ baseFolder = ref .getContextFolder ( ) and
492
490
exists ( string filePath , string refPath |
493
491
// Pad each file name to ensure they differ at some index, in case one was a prefix of the other
494
492
filePath = file .getRelativePath ( ) + "!" and
495
- refPath = ref . getContextFolder ( ) .getRelativePath ( ) + "@" and
493
+ refPath = baseFolder .getRelativePath ( ) + "@" and
496
494
result = min ( int i | filePath .charAt ( i ) != refPath .charAt ( i ) )
497
495
)
498
496
}
499
497
500
498
/**
501
- * Gets the template file referred to by `ref`.
499
+ * Gets the template file referred to by `ref` when resolved from `baseFolder` .
502
500
*/
503
- private TemplateFile getBestMatchingTarget ( TemplateFileReferenceString ref ) {
504
- result = max ( getAMatchingTarget ( ref ) as f order by getRankOfMatchingTarget ( f , ref ) )
501
+ private TemplateFile getBestMatchingTarget ( Folder baseFolder , TemplateFileReferenceString ref ) {
502
+ result = max ( getAMatchingTarget ( ref ) as f order by getRankOfMatchingTarget ( f , baseFolder , ref ) )
505
503
}
506
504
507
505
/**
@@ -677,7 +675,7 @@ module Templating {
677
675
678
676
/** Gets the file referenced by this inclusion tag. */
679
677
TemplateFile getImportedFile ( ) {
680
- result = getPath ( ) .( TemplateFileReferenceString ) .getTemplateFile ( )
678
+ result = getPath ( ) .( TemplateFileReferenceString ) .getTemplateFile ( getFile ( ) . getParentContainer ( ) )
681
679
}
682
680
}
683
681
0 commit comments