torture.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Torture fixture — exercises every TS/TSX extraction path the kernel ports.
  3. */
  4. import React, { forwardRef, memo, useState as useStateAlias } from 'react';
  5. import * as NS from './namespace-module';
  6. import DefaultThing from './default-module';
  7. import './side-effect';
  8. import { helperFn, CONFIG_TABLE } from './helpers';
  9. export { reExported, orig as aliased } from './barrel-source';
  10. export * from './star-source';
  11. // A documented constant table (value-ref target).
  12. const RETRY_LIMITS = { a: 1, b: 2 };
  13. export const API_BASE = 'https://example.test';
  14. let plainLet = 42;
  15. var oldVar = 'x';
  16. /** Class docs.
  17. * Multi-line.
  18. */
  19. @Injectable()
  20. @scoped.Registry<Config>('name')
  21. export abstract class BaseService extends EventTarget implements Disposable, Serializable {
  22. static instances = 0;
  23. private readonly cache: Map<string, Config> = new Map();
  24. public fonts: FontConfig;
  25. count = 0;
  26. onScroll = throttle((e: Event) => {
  27. this.handleScroll(e);
  28. }, 100);
  29. handleClick = (ev: MouseEvent): void => {
  30. emitTelemetry(ev);
  31. new AbortController();
  32. };
  33. @Get('/list')
  34. async list(query: QueryOpts): Promise<Result<Item>> {
  35. const local: ItemMapper = makeMapper();
  36. const limit = RETRY_LIMITS;
  37. return this.cache.get(query.key) ?? helperFn(query);
  38. }
  39. private static compute(n: number): number {
  40. return n * RETRY_LIMITS.a;
  41. }
  42. get size(): number {
  43. return this.count;
  44. }
  45. protected dispose(): void {
  46. listeners.forEach((l) => unregister(l));
  47. }
  48. }
  49. class Plain {
  50. constructor(private svc: BaseService) {
  51. register(this.onEvent);
  52. btn.on('click', this.handleClick);
  53. }
  54. onEvent() {}
  55. handleClick() {}
  56. }
  57. export interface Shape extends Named, Sized {
  58. area: number;
  59. resize(f: number): Shape;
  60. onchange: (s: Shape) => void;
  61. }
  62. export enum Direction {
  63. Up,
  64. Down = 2,
  65. Left,
  66. }
  67. export type Handle = {
  68. stop: () => void;
  69. id: string;
  70. refresh(force: boolean): Promise<void>;
  71. };
  72. export type ServiceList = [
  73. Service<'query_apply_record', Req, Resp>,
  74. Service<'apply_confirm', Req, Resp>,
  75. ];
  76. export type MaybeShape = Shape | null;
  77. export function topLevel(a: ShapeConfig): Shape {
  78. const inner = () => reallyDeep();
  79. function namedInner(): void {
  80. chained(a).value;
  81. }
  82. namedInner();
  83. return makeShape(a);
  84. }
  85. async function* generatorFn(items: Item[]) {
  86. yield* items.map((i) => transform(i));
  87. }
  88. export const arrowConst = async (x: number) => {
  89. return x + plainLet;
  90. };
  91. const AnonClassHolder = class {
  92. method() {}
  93. };
  94. // React components (#841).
  95. export const Button = forwardRef((props: ButtonProps, ref) => {
  96. const [state, setState] = useStateAlias(0);
  97. useEffect(() => {
  98. trackRender();
  99. });
  100. return <BaseButton ref={ref} onClick={() => setState(state + 1)} {...props} />;
  101. });
  102. export const MemoRow = memo(function Row(props: RowProps) {
  103. return <tr className={props.cls}>{props.children}</tr>;
  104. });
  105. export const Wrapped = React.memo(ImportedComponent);
  106. export const Styled = styled.button`
  107. color: red;
  108. `;
  109. const memoCache = memo(computeThing); // lowercase — stays a constant
  110. export function App() {
  111. const nodes: GraphNode[] = [];
  112. return (
  113. <div>
  114. <Button label={CONFIG_TABLE.label} />
  115. <NS.Panel />
  116. {nodes.map((n) => (
  117. <MemoRow key={n.id} cls={n.cls} />
  118. ))}
  119. </div>
  120. );
  121. }
  122. // Object-of-functions (SvelteKit-style actions map).
  123. export const actionsMap = {
  124. create: async (input: CreateInput) => {
  125. return persistNew(input);
  126. },
  127. update(input: UpdateInput) {
  128. return persistExisting(input);
  129. },
  130. };
  131. // Zustand-style store through middleware wrappers.
  132. export const useStore = create(
  133. persist(
  134. (set, get) => ({
  135. fetchUser: async (id: string) => {
  136. const user = await api.load(id);
  137. set({ user });
  138. },
  139. reset: () => set({ user: null }),
  140. }),
  141. { name: 'store' }
  142. )
  143. );
  144. // RTK Query.
  145. export const widgetApi = createApi({
  146. reducerPath: 'widgets',
  147. endpoints: (build) => ({
  148. getWidget: build.query({
  149. query: (id: string) => fetchWidget(id),
  150. }),
  151. updateWidget: build.mutation({
  152. queryFn: async (patch) => {
  153. return applyPatch(patch);
  154. },
  155. }),
  156. prebuilt: build.query(makeEndpointConfig()),
  157. }),
  158. });
  159. export const { useGetWidgetQuery, useUpdateWidgetMutation } = widgetApi;
  160. const { notAHook } = widgetApi;
  161. // Value-ref shadowing: SHADOWED_LIMIT re-bound locally must be pruned.
  162. const SHADOWED_LIMIT = 10;
  163. export function readsShadowed() {
  164. const SHADOWED_LIMIT = 20;
  165. return SHADOWED_LIMIT;
  166. }
  167. export function readsTable() {
  168. return RETRY_LIMITS.b + API_BASE.length;
  169. }
  170. // Fn-ref registrations.
  171. registerHandler(helperFn);
  172. queueMicrotask(topLevel);
  173. const routeTable = { home: topLevel, missing: notDefinedAnywhere };
  174. const handlerList = [helperFn, DefaultThing];
  175. target.cb = topLevel;
  176. const aliasFn = topLevel;
  177. // Calls with interesting callees.
  178. ;(topLevel)(cfg);
  179. NS.helper.deep(1);
  180. "literal".includes('x');
  181. [1, 2].map(String);
  182. obj?.optMethod?.(3);
  183. import('./dynamic-module');
  184. new NS.Widget(makeArg());
  185. new Map<string, number>();
  186. super_weird?.();