Skip to content

Commit

Permalink
Merge pull request #2352 from halfnibble/halfnibble/typings-dep-maps
Browse files Browse the repository at this point in the history
[typings-generator] Dependency Maps
  • Loading branch information
octogonz authored Nov 12, 2020
2 parents ed0474d + 8300b58 commit a2f0d08
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
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

0 comments on commit a2f0d08

Please sign in to comment.