Skip to content

Commit 8239611

Browse files
committed
JS: Add root dir to tsconfig model
1 parent bcd3a32 commit 8239611

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

javascript/ql/lib/semmle/javascript/TSConfig.qll

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,49 @@ class TSConfig extends JsonObject {
8282
result = this.getAnIncludedContainer().getAChildContainer()
8383
}
8484

85+
/** Gets the path mentioned in the `rootDir` property. */
86+
string getRootDirPath() { result = this.getCompilerOptions().getPropStringValue("rootDir") }
87+
88+
private Container getOwnRootDir() {
89+
result = Resolver::resolve(this.getFolder(), this.getRootDirPath())
90+
}
91+
92+
/** Gets the file or folder referenced by the `rootDir` property. */
93+
Container getRootDir() {
94+
result = this.getOwnRootDir()
95+
or
96+
not exists(this.getRootDirPath()) and
97+
result = this.getExtendedTSConfig().getOwnRootDir()
98+
}
99+
100+
private string getATopLevelIncludePath() {
101+
result = this.getAnIncludePath().(FilePath).getComponent(0)
102+
}
103+
104+
private string getUniqueTopLevelIncludePath() {
105+
result = unique( | | this.getATopLevelIncludePath())
106+
}
107+
108+
/**
109+
* Gets the folder referred to by the `rootDir` property, or if absent, an effective root dir
110+
* derived from `include` paths.
111+
*/
112+
Container getEffectiveRootDir() {
113+
result = this.getRootDir()
114+
or
115+
not exists(this.getRootDir()) and
116+
(
117+
result = this.getFolder().getFolder(this.getUniqueTopLevelIncludePath())
118+
or
119+
not exists(this.getUniqueTopLevelIncludePath()) and
120+
exists(this.getATopLevelIncludePath()) and
121+
result = this.getFolder()
122+
or
123+
not exists(this.getATopLevelIncludePath()) and
124+
result = this.getExtendedTSConfig().getEffectiveRootDir()
125+
)
126+
}
127+
85128
private JsonObject getPathMappings() { result = this.getCompilerOptions().getPropValue("paths") }
86129

87130
/**
@@ -127,7 +170,8 @@ private module ResolverConfig implements Folder::ResolveSig {
127170
predicate shouldResolve(Container base, string path) {
128171
exists(TSConfig cfg |
129172
base = cfg.getFolder() and
130-
path = [cfg.getExtendsPath(), cfg.getBaseUrlPath(), cfg.getAnIncludePath()]
173+
path =
174+
[cfg.getExtendsPath(), cfg.getBaseUrlPath(), cfg.getRootDirPath(), cfg.getAnIncludePath()]
131175
)
132176
}
133177

0 commit comments

Comments
 (0)