Skip to content

Commit 308461a

Browse files
committed
JS: Pass around base folder in file resolution
1 parent e8d10b9 commit 308461a

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

javascript/ql/src/semmle/javascript/frameworks/Templating.qll

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ module Templating {
322322

323323
/** Gets the template file referenced by this node. */
324324
final TemplateFile getTemplateFile() {
325-
result = this.getValue().(TemplateFileReferenceString).getTemplateFile()
325+
result = this.getValue().(TemplateFileReferenceString).getTemplateFile(getFile().getParentContainer())
326326
}
327327
}
328328

@@ -361,19 +361,16 @@ module Templating {
361361
*/
362362
string getStem() { result = getBaseName().regexpCapture("(.*?)(?:\\.([^.]*))?", 1) }
363363

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
366368
exists(UpwardTraversalSuffix up |
367369
this = up.getOriginal() and
368-
result = up.(TemplateFileReferenceString).resolveUpwardTraversal()
370+
result = up.(TemplateFileReferenceString).getTemplateFile(baseFolder.getParentContainer()) and
371+
baseFolder = getContextFolder()
369372
)
370-
or
371-
not this = any(UpwardTraversalSuffix up).getOriginal() and
372-
result = this
373373
}
374-
375-
/** Gets the template file referenced by this string. */
376-
final TemplateFile getTemplateFile() { result = getBestMatchingTarget(this.resolveUpwardTraversal()) }
377374
}
378375

379376
/** The value of a template reference node, as a template reference string. */
@@ -470,7 +467,7 @@ module Templating {
470467
}
471468

472469
/**
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`.
474471
*
475472
* This is used to rank all the possible files that `ref` could refer to.
476473
* Picking the one with the highest rank ensures that the file most closely related
@@ -487,21 +484,22 @@ module Templating {
487484
* and vice versa in `B/components/foo.js`.
488485
*/
489486
pragma[nomagic]
490-
private int getRankOfMatchingTarget(TemplateFile file, TemplateFileReferenceString ref) {
487+
private int getRankOfMatchingTarget(TemplateFile file, Folder baseFolder, TemplateFileReferenceString ref) {
491488
file = getAMatchingTarget(ref) and
489+
baseFolder = ref.getContextFolder() and
492490
exists(string filePath, string refPath |
493491
// Pad each file name to ensure they differ at some index, in case one was a prefix of the other
494492
filePath = file.getRelativePath() + "!" and
495-
refPath = ref.getContextFolder().getRelativePath() + "@" and
493+
refPath = baseFolder.getRelativePath() + "@" and
496494
result = min(int i | filePath.charAt(i) != refPath.charAt(i))
497495
)
498496
}
499497

500498
/**
501-
* Gets the template file referred to by `ref`.
499+
* Gets the template file referred to by `ref` when resolved from `baseFolder`.
502500
*/
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))
505503
}
506504

507505
/**
@@ -677,7 +675,7 @@ module Templating {
677675

678676
/** Gets the file referenced by this inclusion tag. */
679677
TemplateFile getImportedFile() {
680-
result = getPath().(TemplateFileReferenceString).getTemplateFile()
678+
result = getPath().(TemplateFileReferenceString).getTemplateFile(getFile().getParentContainer())
681679
}
682680
}
683681

0 commit comments

Comments
 (0)