Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typings-generator] Dependency Maps #2352

Merged
merged 7 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export class SassTypingsGenerator extends StringValuesTypingsGenerator {
indentedSyntax: path.extname(filePath).toLowerCase() === '.sass'
});

// Register any @import files as dependencies.
const target: string = result.stats.entry;
for (const dependency of result.stats.includedFiles) {
this.registerDependency(target, dependency);
}

return result.css.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@rushstack/heft",
"comment": "Update Sass typings generation to update in watch mode when a dependency changes.",
"type": "patch"
}
],
"packageName": "@rushstack/heft",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@rushstack/typings-generator",
"comment": "Add register dependency feature for typings generation. ",
"type": "patch"
}
],
"packageName": "@rushstack/typings-generator",
"email": "[email protected]"
}
1 change: 1 addition & 0 deletions common/reviews/api/typings-generator.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class TypingsGenerator {
generateTypingsAsync(): Promise<void>;
// (undocumented)
protected _options: ITypingsGeneratorOptions;
registerDependency(target: string, dependency: string): void;
// (undocumented)
runWatcherAsync(): Promise<void>;
}
Expand Down
54 changes: 54 additions & 0 deletions libraries/typings-generator/src/TypingsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> {
* @public
*/
export class TypingsGenerator {
// Map of target file path -> Set<dependency file path>
private _targetMap: Map<string, Set<string>>;

// Map of dependency file path -> Set<target file path>
private _dependencyMap: Map<string, Set<string>>;

protected _options: ITypingsGeneratorOptions;

public constructor(options: ITypingsGeneratorOptions) {
Expand Down Expand Up @@ -70,6 +76,10 @@ export class TypingsGenerator {
}

this._options.fileExtensions = this._normalizeFileExtensions(this._options.fileExtensions);

this._targetMap = new Map();

this._dependencyMap = new Map();
}

public async generateTypingsAsync(): Promise<void> {
Expand Down Expand Up @@ -121,7 +131,37 @@ export class TypingsGenerator {
});
}

/**
* Register file dependencies that may effect the typings of a target file.
* Note: This feature is only useful in watch mode.
* The registerDependency method must be called in the body of parseAndGenerateTypings every
* time because the registry for a file is cleared at the beginning of processing.
*/
public registerDependency(target: string, dependency: string): void {
let targetDependencySet: Set<string> | undefined = this._targetMap.get(target);
if (!targetDependencySet) {
targetDependencySet = new Set();
this._targetMap.set(target, targetDependencySet);
}
targetDependencySet.add(dependency);

let dependencyTargetSet: Set<string> | undefined = this._dependencyMap.get(dependency);
if (!dependencyTargetSet) {
dependencyTargetSet = new Set();
this._dependencyMap.set(dependency, dependencyTargetSet);
}
dependencyTargetSet.add(target);
}

private async _parseFileAndGenerateTypingsAsync(locFilePath: string): Promise<void> {
// Clear registered dependencies prior to reprocessing.
this._clearDependencies(locFilePath);

// Check for targets that register this file as a dependency, and reprocess them too.
for (const target of this._getDependencyTargets(locFilePath)) {
await this._parseFileAndGenerateTypingsAsync(target);
}

try {
const fileContents: string = await FileSystem.readFileAsync(locFilePath);
const typingsData: string | undefined = await this._options.parseAndGenerateTypings(
Expand Down Expand Up @@ -152,6 +192,20 @@ export class TypingsGenerator {
}
}

private _clearDependencies(target: string): void {
const targetDependencySet: Set<string> | undefined = this._targetMap.get(target);
if (targetDependencySet) {
for (const dependency of targetDependencySet) {
this._dependencyMap.get(dependency)!.delete(target);
}
targetDependencySet.clear();
}
}

private _getDependencyTargets(dependency: string): string[] {
return [...(this._dependencyMap.get(dependency)?.keys() || [])];
}

private _getTypingsFilePath(locFilePath: string): string {
return path.resolve(
this._options.generatedTsFolder,
Expand Down