Skip to content

Commit dceb211

Browse files
committed
JS: Pass source root to Node.js process
1 parent aaf1417 commit dceb211

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

javascript/extractor/lib/typescript/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface ParseCommand {
4848
interface OpenProjectCommand {
4949
command: "open-project";
5050
tsConfig: string;
51+
sourceRoot: string | null;
5152
virtualSourceRoot: string | null;
5253
packageEntryPoints: [string, string][];
5354
packageJsonFiles: [string, string][];
@@ -370,7 +371,7 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
370371

371372
let packageEntryPoints = new Map(command.packageEntryPoints);
372373
let packageJsonFiles = new Map(command.packageJsonFiles);
373-
let virtualSourceRoot = new VirtualSourceRoot(process.cwd(), command.virtualSourceRoot);
374+
let virtualSourceRoot = new VirtualSourceRoot(command.sourceRoot, command.virtualSourceRoot);
374375

375376
/**
376377
* Rewrites path segments of form `node_modules/PACK/suffix` to be relative to
@@ -720,6 +721,7 @@ if (process.argv.length > 2) {
720721
tsConfig: argument,
721722
packageEntryPoints: [],
722723
packageJsonFiles: [],
724+
sourceRoot: null,
723725
virtualSourceRoot: null,
724726
});
725727
for (let sf of state.project.program.getSourceFiles()) {

javascript/extractor/lib/typescript/src/virtual_source_root.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import * as ts from "./typescript";
77
*/
88
export class VirtualSourceRoot {
99
constructor(
10-
private sourceRoot: string,
10+
private sourceRoot: string | null,
1111

1212
/**
1313
* Directory whose folder structure mirrors the real source root, but with `node_modules` installed,
1414
* or undefined if no virtual source root exists.
1515
*/
16-
private virtualSourceRoot: string,
16+
private virtualSourceRoot: string | null,
1717
) {}
1818

1919
/**
2020
* Maps a path under the real source root to the corresponding path in the virtual source root.
2121
*/
2222
public toVirtualPath(path: string) {
23-
if (!this.virtualSourceRoot) return null;
23+
if (!this.virtualSourceRoot || !this.sourceRoot) return null;
2424
let relative = pathlib.relative(this.sourceRoot, path);
2525
if (relative.startsWith('..') || pathlib.isAbsolute(relative)) return null;
2626
return pathlib.join(this.virtualSourceRoot, relative);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ protected DependencyInstallationResult installDependencies(Set<Path> filesToExtr
837837
}
838838
}
839839

840-
return new DependencyInstallationResult(virtualSourceRoot, packageMainFile, packagesInRepo);
840+
return new DependencyInstallationResult(sourceRoot, virtualSourceRoot, packageMainFile, packagesInRepo);
841841
}
842842

843843
/**

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,34 @@
66

77
/** Contains the results of installing dependencies. */
88
public class DependencyInstallationResult {
9+
private Path sourceRoot;
910
private Path virtualSourceRoot;
1011
private Map<String, Path> packageEntryPoints;
1112
private Map<String, Path> packageJsonFiles;
1213

1314
public static final DependencyInstallationResult empty =
14-
new DependencyInstallationResult(null, Collections.emptyMap(), Collections.emptyMap());
15+
new DependencyInstallationResult(null, null, Collections.emptyMap(), Collections.emptyMap());
1516

1617
public DependencyInstallationResult(
18+
Path sourceRoot,
1719
Path virtualSourceRoot,
1820
Map<String, Path> packageEntryPoints,
1921
Map<String, Path> packageJsonFiles) {
2022
this.packageEntryPoints = packageEntryPoints;
2123
this.packageJsonFiles = packageJsonFiles;
2224
}
2325

26+
/**
27+
* Returns the source root mirrored by {@link #getVirtualSourceRoot()} or <code>null</code>
28+
* if no virtual source root exists.
29+
* <p/>
30+
* When invoked from the AutoBuilder, this corresponds to the source root. When invoked
31+
* from ODASA, there is no notion of source root, so this is always <code>null</code> in that context.
32+
*/
33+
public Path getSourceRoot() {
34+
return sourceRoot;
35+
}
36+
2437
/**
2538
* Returns the virtual source root or <code>null</code> if no virtual source root exists.
2639
*

javascript/extractor/src/com/semmle/js/parser/TypeScriptParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ public ParsedProject openProject(File tsConfigFile, DependencyInstallationResult
502502
request.add("tsConfig", new JsonPrimitive(tsConfigFile.getPath()));
503503
request.add("packageEntryPoints", mapToArray(deps.getPackageEntryPoints()));
504504
request.add("packageJsonFiles", mapToArray(deps.getPackageJsonFiles()));
505+
request.add("sourceRoot", deps.getSourceRoot() == null
506+
? JsonNull.INSTANCE
507+
: new JsonPrimitive(deps.getSourceRoot().toString()));
505508
request.add("virtualSourceRoot", deps.getVirtualSourceRoot() == null
506509
? JsonNull.INSTANCE
507510
: new JsonPrimitive(deps.getVirtualSourceRoot().toString()));

0 commit comments

Comments
 (0)