Add GHA for Dead Code Analysis (#9052)
This commit is contained in:
parent
ed8ccb5d80
commit
a866005bea
2 changed files with 18 additions and 52 deletions
18
.github/workflows/static_analysis.yaml
vendored
18
.github/workflows/static_analysis.yaml
vendored
|
@ -74,3 +74,21 @@ jobs:
|
||||||
|
|
||||||
- name: Run Linter
|
- name: Run Linter
|
||||||
run: "yarn run lint:style"
|
run: "yarn run lint:style"
|
||||||
|
|
||||||
|
analyse_dead_code:
|
||||||
|
name: "Analyse Dead Code"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
cache: 'yarn'
|
||||||
|
|
||||||
|
- name: Install Deps
|
||||||
|
run: "scripts/ci/layered.sh"
|
||||||
|
|
||||||
|
- name: Dead Code Analysis
|
||||||
|
run: |
|
||||||
|
cd element-web
|
||||||
|
yarn run analyse:unused-exports
|
||||||
|
|
|
@ -68,43 +68,6 @@ export function useEventEmitter(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Hook to wrap an EventTarget addEventListener and removeEventListener in hook
|
|
||||||
* lifecycle
|
|
||||||
*/
|
|
||||||
export function useEventTarget(
|
|
||||||
emitter: EventTarget | undefined,
|
|
||||||
eventName: string,
|
|
||||||
handler: Handler,
|
|
||||||
): void {
|
|
||||||
// Create a ref that stores handler
|
|
||||||
const savedHandler = useRef(handler);
|
|
||||||
|
|
||||||
// Update ref.current value if handler changes.
|
|
||||||
useEffect(() => {
|
|
||||||
savedHandler.current = handler;
|
|
||||||
}, [handler]);
|
|
||||||
|
|
||||||
useEffect(
|
|
||||||
() => {
|
|
||||||
// allow disabling this hook by passing a falsy emitter
|
|
||||||
if (!emitter) return;
|
|
||||||
|
|
||||||
// Create event listener that calls handler function stored in ref
|
|
||||||
const eventListener = (...args) => savedHandler.current(...args);
|
|
||||||
|
|
||||||
// Add event listener
|
|
||||||
emitter.addEventListener(eventName, eventListener);
|
|
||||||
|
|
||||||
// Remove event listener on cleanup
|
|
||||||
return () => {
|
|
||||||
emitter.removeEventListener(eventName, eventListener);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
[eventName, emitter], // Re-run if eventName or emitter changes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type Mapper<T> = (...args: any[]) => T;
|
type Mapper<T> = (...args: any[]) => T;
|
||||||
|
|
||||||
export function useTypedEventEmitterState<
|
export function useTypedEventEmitterState<
|
||||||
|
@ -133,18 +96,3 @@ export function useEventEmitterState<T>(
|
||||||
useEventEmitter(emitter, eventName, handler);
|
useEventEmitter(emitter, eventName, handler);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useEventTargetState<T>(
|
|
||||||
target: EventTarget | undefined,
|
|
||||||
eventName: string,
|
|
||||||
fn: Mapper<T>,
|
|
||||||
): T {
|
|
||||||
const [value, setValue] = useState<T>(fn());
|
|
||||||
const handler = useCallback((...args: any[]) => {
|
|
||||||
setValue(fn(...args));
|
|
||||||
}, [fn]);
|
|
||||||
// re-run when the emitter changes
|
|
||||||
useEffect(handler, [target]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
||||||
useEventTarget(target, eventName, handler);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue