Add api to get the dependencies of the file

This commit is contained in:
Sheetal Nandi 2017-11-09 13:35:56 -08:00
parent 3c5a6e1ae7
commit c9a17f325b
3 changed files with 61 additions and 1 deletions

View File

@ -93,6 +93,11 @@ namespace ts {
* Note that it is assumed that the when asked about semantic diagnostics, the file has been taken out of affected files
*/
getSemanticDiagnostics(programOfThisState: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
/**
* Get all the dependencies of the file
*/
getAllDependencies(programOfThisState: Program, sourceFile: SourceFile): string[];
}
/**
@ -190,7 +195,8 @@ namespace ts {
canCreateNewStateFrom,
getFilesAffectedBy,
emitNextAffectedFile,
getSemanticDiagnostics
getSemanticDiagnostics,
getAllDependencies
};
/**
@ -306,6 +312,52 @@ namespace ts {
return diagnostics;
}
/**
* Get all the dependencies of the sourceFile
*/
function getAllDependencies(programOfThisState: Program, sourceFile: SourceFile): string[] {
const compilerOptions = programOfThisState.getCompilerOptions();
// With --out or --outFile all outputs go into single file, all files depend on each other
if (compilerOptions.outFile || compilerOptions.out) {
return programOfThisState.getSourceFiles().map(getFileName);
}
// If this is non module emit, or its a global file, it depends on all the source files
if (!isModuleEmit || (!isExternalModule(sourceFile) && !containsOnlyAmbientModules(sourceFile))) {
return programOfThisState.getSourceFiles().map(getFileName);
}
// Get the references, traversing deep from the referenceMap
Debug.assert(!!referencedMap);
const seenMap = createMap<true>();
const queue = [sourceFile.path];
while (queue.length) {
const path = queue.pop();
if (!seenMap.has(path)) {
seenMap.set(path, true);
const references = referencedMap.get(path);
if (references) {
const iterator = references.keys();
for (let { value, done } = iterator.next(); !done; { value, done } = iterator.next()) {
queue.push(value as Path);
}
}
}
}
return flatMapIter(seenMap.keys(), path => {
const file = programOfThisState.getSourceFileByPath(path as Path);
if (file) {
return file.fileName;
}
return path;
});
}
function getFileName(sourceFile: SourceFile) {
return sourceFile.fileName;
}
/**
* For script files that contains only ambient external modules, although they are not actually external module files,
* they can only be consumed via importing elements from them. Regular script files cannot consume them. Therefore,

View File

@ -3815,6 +3815,10 @@ declare namespace ts {
* Note that it is assumed that the when asked about semantic diagnostics, the file has been taken out of affected files
*/
getSemanticDiagnostics(programOfThisState: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
/**
* Get all the dependencies of the file
*/
getAllDependencies(programOfThisState: Program, sourceFile: SourceFile): string[];
}
/**
* Information about the source file: Its version and optional signature from last emit

View File

@ -3762,6 +3762,10 @@ declare namespace ts {
* Note that it is assumed that the when asked about semantic diagnostics, the file has been taken out of affected files
*/
getSemanticDiagnostics(programOfThisState: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
/**
* Get all the dependencies of the file
*/
getAllDependencies(programOfThisState: Program, sourceFile: SourceFile): string[];
}
/**
* Information about the source file: Its version and optional signature from last emit