mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-16 15:44:16 -06:00
Add api to get the dependencies of the file
This commit is contained in:
parent
3c5a6e1ae7
commit
c9a17f325b
@ -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,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user