Skip to content

Commit 5c962fc

Browse files
alexeyr-cialexeyr
andauthored
TypeScript cleanup (#1651)
* Include type-check in CI * Clean up some TypeScript problems * Fix Store/StoreGenerator confusion * Update TypeScript * Add comment --------- Co-authored-by: Alexey Romanov <alexey.v.romanov@gmail.com>
1 parent bb9a8a2 commit 5c962fc

15 files changed

+100
-88
lines changed

.github/workflows/lint-js-and-ruby.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ jobs:
5353
run: yarn start lint
5454
- name: Check formatting
5555
run: yarn start format.listDifferent
56+
- name: Type-check TypeScript
57+
run: yarn run type-check

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ npm-debug.*
3232
yalc.lock
3333

3434
.byebug_history
35+
36+
# IDE
37+
.idea/
38+
39+
# TypeScript
40+
*.tsbuildinfo

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1818
### [Unreleased]
1919
Changes since the last non-beta release.
2020

21+
#### Fixed
22+
- Incorrect type and confusing name for `ReactOnRails.registerStore`, use `registerStoreGenerators` instead. [PR 1651](https://github.com/shakacode/react_on_rails/pull/1651) by [alexeyr-ci](https://github.com/alexeyr-ci).
23+
2124
### [14.0.5] - 2024-08-20
2225
#### Fixed
2326
- Should force load react-components which send over turbo-stream [PR #1620](https://github.com/shakacode/react_on_rails/pull/1620) by [theforestvn88](https://github.com/theforestvn88).

node_package/src/Authenticity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AuthenticityHeaders } from './types/index';
33
export default {
44
authenticityToken(): string | null {
55
const token = document.querySelector('meta[name="csrf-token"]');
6-
if (token && (token instanceof window.HTMLMetaElement)) {
6+
if (token instanceof HTMLMetaElement) {
77
return token.content;
88
}
99
return null;

node_package/src/ComponentRegistry.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RegisteredComponent, ReactComponentOrRenderFunction, RenderFunction } from './types/index';
22
import isRenderFunction from './isRenderFunction';
33

4-
const registeredComponents = new Map();
4+
const registeredComponents = new Map<string, RegisteredComponent>();
55

66
export default {
77
/**
@@ -35,8 +35,9 @@ export default {
3535
* @returns { name, component, isRenderFunction, isRenderer }
3636
*/
3737
get(name: string): RegisteredComponent {
38-
if (registeredComponents.has(name)) {
39-
return registeredComponents.get(name);
38+
const registeredComponent = registeredComponents.get(name);
39+
if (registeredComponent !== undefined) {
40+
return registeredComponent;
4041
}
4142

4243
const keys = Array.from(registeredComponents.keys()).join(', ');

node_package/src/ReactOnRails.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import type {
1717
ErrorOptions,
1818
ReactComponentOrRenderFunction,
1919
AuthenticityHeaders,
20+
Store,
2021
StoreGenerator,
2122
} from './types';
2223
import reactHydrateOrRender from './reactHydrateOrRender';
2324

24-
/* eslint-disable @typescript-eslint/no-explicit-any */
25-
type Store = any;
26-
2725
const ctx = context();
2826

2927
if (ctx === undefined) {
@@ -56,19 +54,23 @@ ctx.ReactOnRails = {
5654
ComponentRegistry.register(components);
5755
},
5856

57+
registerStore(stores: { [id: string]: StoreGenerator }): void {
58+
this.registerStoreGenerators(stores);
59+
},
60+
5961
/**
60-
* Allows registration of store generators to be used by multiple react components on one Rails
62+
* Allows registration of store generators to be used by multiple React components on one Rails
6163
* view. store generators are functions that take one arg, props, and return a store. Note that
6264
* the setStore API is different in that it's the actual store hydrated with props.
63-
* @param stores (keys are store names, values are the store generators)
65+
* @param storeGenerators (keys are store names, values are the store generators)
6466
*/
65-
registerStore(stores: { [id: string]: Store }): void {
66-
if (!stores) {
67-
throw new Error('Called ReactOnRails.registerStores with a null or undefined, rather than ' +
67+
registerStoreGenerators(storeGenerators: { [id: string]: StoreGenerator }): void {
68+
if (!storeGenerators) {
69+
throw new Error('Called ReactOnRails.registerStoreGenerators with a null or undefined, rather than ' +
6870
'an Object with keys being the store names and the values are the store generators.');
6971
}
7072

71-
StoreRegistry.register(stores);
73+
StoreRegistry.register(storeGenerators);
7274
},
7375

7476
/**
@@ -148,7 +150,7 @@ ctx.ReactOnRails = {
148150

149151
/**
150152
* Returns header with csrf authenticity token and XMLHttpRequest
151-
* @param {*} other headers
153+
* @param otherHeaders Other headers
152154
* @returns {*} header
153155
*/
154156

@@ -171,7 +173,7 @@ ctx.ReactOnRails = {
171173

172174
/**
173175
* Allows retrieval of the store generator by name. This is used internally by ReactOnRails after
174-
* a rails form loads to prepare stores.
176+
* a Rails form loads to prepare stores.
175177
* @param name
176178
* @returns Redux Store generator function
177179
*/

node_package/src/StoreRegistry.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import type { StoreGenerator } from './types';
1+
import type { Store, StoreGenerator } from './types';
22

3-
/* eslint-disable @typescript-eslint/no-explicit-any */
4-
type Store = any;
5-
6-
const registeredStoreGenerators = new Map();
7-
const hydratedStores = new Map();
3+
const registeredStoreGenerators = new Map<string, StoreGenerator>();
4+
const hydratedStores = new Map<string, Store>();
85

96
export default {
107
/**
118
* Register a store generator, a function that takes props and returns a store.
129
* @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
1310
*/
14-
register(storeGenerators: { [id: string]: Store }): void {
11+
register(storeGenerators: { [id: string]: StoreGenerator }): void {
1512
Object.keys(storeGenerators).forEach(name => {
1613
if (registeredStoreGenerators.has(name)) {
1714
console.warn('Called registerStore for store that is already registered', name);
@@ -66,8 +63,9 @@ This can happen if you are server rendering and either:
6663
* @returns storeCreator with given name
6764
*/
6865
getStoreGenerator(name: string): StoreGenerator {
69-
if (registeredStoreGenerators.has(name)) {
70-
return registeredStoreGenerators.get(name);
66+
const registeredStoreGenerator = registeredStoreGenerators.get(name);
67+
if (registeredStoreGenerator) {
68+
return registeredStoreGenerator;
7169
}
7270

7371
const storeKeys = Array.from(registeredStoreGenerators.keys()).join(', ');

node_package/src/buildConsoleReplay.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import RenderUtils from './RenderUtils';
22
import scriptSanitizedVal from './scriptSanitizedVal';
33

4-
/* eslint-disable @typescript-eslint/no-explicit-any */
5-
64
declare global {
75
interface Console {
86
history?: {
@@ -13,24 +11,29 @@ declare global {
1311

1412
export function consoleReplay(): string {
1513
// console.history is a global polyfill used in server rendering.
16-
// $FlowFixMe
1714
if (!(console.history instanceof Array)) {
1815
return '';
1916
}
2017

2118
const lines = console.history.map(msg => {
2219
const stringifiedList = msg.arguments.map(arg => {
23-
let val;
20+
let val: string;
2421
try {
25-
val = (typeof arg === 'string' || arg instanceof String) ? arg : JSON.stringify(arg);
22+
if (typeof arg === 'string') {
23+
val = arg;
24+
} else if (arg instanceof String) {
25+
val = String(arg);
26+
} else {
27+
val = JSON.stringify(arg);
28+
}
2629
if (val === undefined) {
2730
val = 'undefined';
2831
}
29-
} catch (e: any) {
30-
val = `${e.message}: ${arg}`;
32+
} catch (e) {
33+
val = `${(e as Error).message}: ${arg}`;
3134
}
3235

33-
return scriptSanitizedVal(val as string);
36+
return scriptSanitizedVal(val);
3437
});
3538

3639
return `console.${msg.level}.apply(console, ${JSON.stringify(stringifiedList)});`;

node_package/src/handleError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import ReactDOMServer from 'react-dom/server';
33
import type { ErrorOptions } from './types/index';
44

5-
function handleRenderFunctionIssue(options: {e: Error; name?: string}): string {
5+
function handleRenderFunctionIssue(options: ErrorOptions): string {
66
const { e, name } = options;
77

88
let msg = '';

node_package/src/isRenderFunction.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import { ReactComponentOrRenderFunction, RenderFunction } from "./types/index";
88
* @param component
99
* @returns {boolean}
1010
*/
11-
export default function isRenderFunction(component: ReactComponentOrRenderFunction): boolean {
11+
export default function isRenderFunction(component: ReactComponentOrRenderFunction): component is RenderFunction {
1212
// No for es5 or es6 React Component
13-
if (
14-
(component as RenderFunction).prototype &&
15-
(component as RenderFunction).prototype.isReactComponent) {
13+
if ((component as RenderFunction).prototype?.isReactComponent) {
1614
return false;
1715
}
1816

@@ -22,7 +20,7 @@ export default function isRenderFunction(component: ReactComponentOrRenderFuncti
2220

2321
// If zero or one args, then we know that this is a regular function that will
2422
// return a React component
25-
if (component.length >= 2) {
23+
if ((component as RenderFunction).length >= 2) {
2624
return true;
2725
}
2826

0 commit comments

Comments
 (0)