Skip to content

Commit 5719b44

Browse files
committed
TS: Add some documentation
1 parent a220268 commit 5719b44

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ private boolean verifyYarnInstallation() {
602602
}
603603
}
604604

605+
/**
606+
* Returns an existing file named <code>dir/stem.ext</code> where <code>ext</code> is any
607+
* of the given extensions, or <code>null</code> if no such file exists.
608+
*/
605609
private static Path tryResolveWithExtensions(Path dir, String stem, Iterable<String> extensions) {
606610
for (String ext : extensions) {
607611
Path path = dir.resolve(stem + ext);
@@ -612,12 +616,19 @@ private static Path tryResolveWithExtensions(Path dir, String stem, Iterable<Str
612616
return null;
613617
}
614618

619+
/**
620+
* Returns an existing file named <code>dir/stem.ext</code> where <code>ext</code> is any TypeScript or JavaScript extension,
621+
* or <code>null</code> if no such file exists.
622+
*/
615623
private static Path tryResolveTypeScriptOrJavaScriptFile(Path dir, String stem) {
616624
Path resolved = tryResolveWithExtensions(dir, stem, FileType.TYPESCRIPT.getExtensions());
617625
if (resolved != null) return resolved;
618626
return tryResolveWithExtensions(dir, stem, FileType.JS.getExtensions());
619627
}
620628

629+
/**
630+
* Gets a child of a JSON object as a string, or <code>null</code>.
631+
*/
621632
private String getChildAsString(JsonObject obj, String name) {
622633
JsonElement child = obj.get(name);
623634
if (child instanceof JsonPrimitive && ((JsonPrimitive)child).isString()) {
@@ -626,6 +637,25 @@ private String getChildAsString(JsonObject obj, String name) {
626637
return null;
627638
}
628639

640+
/**
641+
* Installs dependencies for use by the TypeScript type checker.
642+
* <p>
643+
* Some packages must be downloaded while others exist within the same repo ("monorepos")
644+
* but are not in a location where TypeScript would look for it.
645+
* <p>
646+
* Downloaded packages are intalled under {@link #scratchDir}, in a mirrored directory hierarchy
647+
* we call the "virtual source root".
648+
* Each <tt>package.json</tt> file is rewritten and copied to the virtual source root,
649+
* where <tt>yarn install</tt> is invoked.
650+
* <p>
651+
* Packages that exists within the repo are stripped from the dependencies
652+
* before installation, so they are not downloaded. Since they are part of the main source tree,
653+
* these packages are not mirrored under the virtual source root.
654+
* Instead, an explicit package location mapping is passed to the TypeScript parser wrapper.
655+
* <p>
656+
* The TypeScript parser wrapper then overrides module resolution so packages can be found
657+
* under the virtual source root and via that package location mapping.
658+
*/
629659
protected DependencyInstallationResult installDependencies(Set<Path> filesToExtract) {
630660
if (!verifyYarnInstallation()) {
631661
return DependencyInstallationResult.empty;

0 commit comments

Comments
 (0)