Skip to content

Commit

Permalink
Keep themed component registry up to date
Browse files Browse the repository at this point in the history
When the ESLint runs in an IDE service, registries used to remain fixed and rules could not respond to changes (e.g. if a new component is made themeable) until the IDE is restarted.
This commit introduces a file watcher to register/unregister components on the fly.
  • Loading branch information
ybnd committed Oct 3, 2024
1 parent 3d057bb commit 3482d92
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 58 deletions.
26 changes: 25 additions & 1 deletion lint/src/util/theme-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { TSESTree } from '@typescript-eslint/utils';
import chokidar from 'chokidar';
import { readFileSync } from 'fs';
import { basename } from 'path';
import ts, { Identifier } from 'typescript';
Expand Down Expand Up @@ -87,7 +88,7 @@ class ThemeableComponentRegistry {
this.byWrapperPath = new Map();
}

public initialize(prefix = '') {
public initialize(prefix = '', watch = true) {
if (this.entries.size > 0) {
return;
}
Expand Down Expand Up @@ -148,6 +149,20 @@ class ThemeableComponentRegistry {
traverse(source);
}

function unregisterWrapper(path: string) {
const entry = themeableComponents.byWrapperPath.get(path);

if (entry === undefined) {
return;
}

themeableComponents.entries.delete(entry);
themeableComponents.byWrapperPath.delete(entry.wrapperPath);
themeableComponents.byWrapperClass.delete(entry.wrapperClass);
themeableComponents.byBasePath.delete(entry.basePath);
themeableComponents.byBaseClass.delete(entry.baseClass);
}

const glob = require('glob');

// note: this outputs Unix-style paths on Windows
Expand All @@ -156,6 +171,15 @@ class ThemeableComponentRegistry {
for (const wrapper of wrappers) {
registerWrapper(wrapper);
}

// Continue watching files for changes
if (watch) {
chokidar
.watch(prefix + 'src/app/**/themed-*.component.ts')
.on('add', registerWrapper)
.on('change', registerWrapper)
.on('unlink', unregisterWrapper);
}
}

private add(entry: ThemeableComponentRegistryEntry) {
Expand Down
2 changes: 1 addition & 1 deletion lint/test/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {


// Register themed components from test fixture
themeableComponents.initialize(FIXTURE);
themeableComponents.initialize(FIXTURE, false);

TypeScriptRuleTester.itOnly = fit;
TypeScriptRuleTester.itSkip = xit;
Expand Down
110 changes: 54 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"axios": "^1.7.4",
"bootstrap": "^4.6.1",
"cerialize": "0.1.18",
"chokidar": "^3.6.0",
"cli-progress": "^3.12.0",
"colors": "^1.4.0",
"compression": "^1.7.4",
Expand Down

0 comments on commit 3482d92

Please sign in to comment.