Skip to content

Commit d8e7ebf

Browse files
committed
Format TypeScript
1 parent 00a570e commit d8e7ebf

19 files changed

+264
-174
lines changed

.prettierrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ semi: true
55
singleQuote: true
66
trailingComma: all
77
bracketSpacing: true
8-
jsxBracketSameLine: false
9-
parser: flow
8+
bracketSameLine: false
9+
parser: typescript
1010

1111
overrides:
1212
- files: "*.@(css|scss)"

node_package/src/Authenticity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
return null;
1010
},
1111

12-
authenticityHeaders(otherHeaders: {[id: string]: string} = {}): AuthenticityHeaders {
12+
authenticityHeaders(otherHeaders: { [id: string]: string } = {}): AuthenticityHeaders {
1313
return Object.assign(otherHeaders, {
1414
'X-CSRF-Token': this.authenticityToken(),
1515
'X-Requested-With': 'XMLHttpRequest',

node_package/src/ComponentRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
* @param components { component1: component1, component2: component2, etc. }
99
*/
1010
register(components: { [id: string]: ReactComponentOrRenderFunction }): void {
11-
Object.keys(components).forEach(name => {
11+
Object.keys(components).forEach((name) => {
1212
if (registeredComponents.has(name)) {
1313
console.warn('Called register for component that is already registered', name);
1414
}

node_package/src/ReactOnRails.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ ctx.ReactOnRails = {
6666
*/
6767
registerStoreGenerators(storeGenerators: { [id: string]: StoreGenerator }): void {
6868
if (!storeGenerators) {
69-
throw new Error('Called ReactOnRails.registerStoreGenerators with a null or undefined, rather than ' +
70-
'an Object with keys being the store names and the values are the store generators.');
69+
throw new Error(
70+
'Called ReactOnRails.registerStoreGenerators with a null or undefined, rather than ' +
71+
'an Object with keys being the store names and the values are the store generators.',
72+
);
7173
}
7274

7375
StoreRegistry.register(storeGenerators);
@@ -103,7 +105,7 @@ ctx.ReactOnRails = {
103105
* `traceTurbolinks: true|false Gives you debugging messages on Turbolinks events
104106
* `turbo: true|false Turbo (the follower of Turbolinks) events will be registered, if set to true.
105107
*/
106-
setOptions(newOptions: {traceTurbolinks?: boolean, turbo?: boolean }): void {
108+
setOptions(newOptions: { traceTurbolinks?: boolean; turbo?: boolean }): void {
107109
if (typeof newOptions.traceTurbolinks !== 'undefined') {
108110
this.options.traceTurbolinks = newOptions.traceTurbolinks;
109111

@@ -119,9 +121,7 @@ ctx.ReactOnRails = {
119121
}
120122

121123
if (Object.keys(newOptions).length > 0) {
122-
throw new Error(
123-
`Invalid options passed to ReactOnRails.options: ${JSON.stringify(newOptions)}`,
124-
);
124+
throw new Error(`Invalid options passed to ReactOnRails.options: ${JSON.stringify(newOptions)}`);
125125
}
126126
},
127127

@@ -227,7 +227,11 @@ ctx.ReactOnRails = {
227227
const componentObj = ComponentRegistry.get(name);
228228
const reactElement = createReactOutput({ componentObj, props, domNodeId });
229229

230-
return reactHydrateOrRender(document.getElementById(domNodeId) as Element, reactElement as ReactElement, hydrate);
230+
return reactHydrateOrRender(
231+
document.getElementById(domNodeId) as Element,
232+
reactElement as ReactElement,
233+
hydrate,
234+
);
231235
},
232236

233237
/**
@@ -252,7 +256,9 @@ ctx.ReactOnRails = {
252256
* @param options
253257
*/
254258
streamServerRenderedReactComponent() {
255-
throw new Error('streamServerRenderedReactComponent is only supported when using a bundle built for Node.js environments');
259+
throw new Error(
260+
'streamServerRenderedReactComponent is only supported when using a bundle built for Node.js environments',
261+
);
256262
},
257263

258264
/**
@@ -303,5 +309,5 @@ ctx.ReactOnRails.resetOptions();
303309

304310
ClientStartup.clientStartup(ctx);
305311

306-
export * from "./types";
312+
export * from './types';
307313
export default ctx.ReactOnRails;

node_package/src/StoreRegistry.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ export default {
99
* @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
1010
*/
1111
register(storeGenerators: { [id: string]: StoreGenerator }): void {
12-
Object.keys(storeGenerators).forEach(name => {
12+
Object.keys(storeGenerators).forEach((name) => {
1313
if (registeredStoreGenerators.has(name)) {
1414
console.warn('Called registerStore for store that is already registered', name);
1515
}
1616

1717
const store = storeGenerators[name];
1818
if (!store) {
19-
throw new Error('Called ReactOnRails.registerStores with a null or undefined as a value ' +
20-
`for the store generator with key ${name}.`);
19+
throw new Error(
20+
'Called ReactOnRails.registerStores with a null or undefined as a value ' +
21+
`for the store generator with key ${name}.`,
22+
);
2123
}
2224

2325
registeredStoreGenerators.set(name, store);
@@ -39,8 +41,7 @@ export default {
3941
const storeKeys = Array.from(hydratedStores.keys()).join(', ');
4042

4143
if (storeKeys.length === 0) {
42-
const msg =
43-
`There are no stores hydrated and you are requesting the store ${name}.
44+
const msg = `There are no stores hydrated and you are requesting the store ${name}.
4445
This can happen if you are server rendering and either:
4546
1. You do not call redux_store near the top of your controller action's view (not the layout)
4647
and before any call to react_component.
@@ -50,8 +51,10 @@ This can happen if you are server rendering and either:
5051

5152
if (throwIfMissing) {
5253
console.log('storeKeys', storeKeys);
53-
throw new Error(`Could not find hydrated store with name '${name}'. ` +
54-
`Hydrated store names include [${storeKeys}].`);
54+
throw new Error(
55+
`Could not find hydrated store with name '${name}'. ` +
56+
`Hydrated store names include [${storeKeys}].`,
57+
);
5558
}
5659

5760
return undefined;
@@ -69,8 +72,10 @@ This can happen if you are server rendering and either:
6972
}
7073

7174
const storeKeys = Array.from(registeredStoreGenerators.keys()).join(', ');
72-
throw new Error(`Could not find store registered with name '${name}'. Registered store ` +
73-
`names include [ ${storeKeys} ]. Maybe you forgot to register the store?`);
75+
throw new Error(
76+
`Could not find store registered with name '${name}'. Registered store ` +
77+
`names include [ ${storeKeys} ]. Maybe you forgot to register the store?`,
78+
);
7479
},
7580

7681
/**

node_package/src/buildConsoleReplay.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@ import scriptSanitizedVal from './scriptSanitizedVal';
44
declare global {
55
interface Console {
66
history?: {
7-
arguments: Array<string | Record<string, string>>; level: "error" | "log" | "debug";
7+
arguments: Array<string | Record<string, string>>;
8+
level: 'error' | 'log' | 'debug';
89
}[];
910
}
1011
}
1112

1213
/** @internal Exported only for tests */
13-
export function consoleReplay(customConsoleHistory: typeof console['history'] | undefined = undefined, numberOfMessagesToSkip: number = 0): string {
14+
// Mismatch between Prettier locally and in CI
15+
// prettier-ignore
16+
export function consoleReplay(
17+
customConsoleHistory: typeof console['history'] | undefined = undefined,
18+
numberOfMessagesToSkip: number = 0,
19+
): string {
1420
// console.history is a global polyfill used in server rendering.
1521
const consoleHistory = customConsoleHistory ?? console.history;
1622

17-
if (!(Array.isArray(consoleHistory))) {
23+
if (!Array.isArray(consoleHistory)) {
1824
return '';
1925
}
2026

21-
const lines = consoleHistory.slice(numberOfMessagesToSkip).map(msg => {
22-
const stringifiedList = msg.arguments.map(arg => {
27+
const lines = consoleHistory.slice(numberOfMessagesToSkip).map((msg) => {
28+
const stringifiedList = msg.arguments.map((arg) => {
2329
let val: string;
2430
try {
2531
if (typeof arg === 'string') {
@@ -45,6 +51,13 @@ export function consoleReplay(customConsoleHistory: typeof console['history'] |
4551
return lines.join('\n');
4652
}
4753

48-
export default function buildConsoleReplay(customConsoleHistory: typeof console['history'] | undefined = undefined, numberOfMessagesToSkip: number = 0): string {
49-
return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay(customConsoleHistory, numberOfMessagesToSkip));
54+
// prettier-ignore
55+
export default function buildConsoleReplay(
56+
customConsoleHistory: typeof console['history'] | undefined = undefined,
57+
numberOfMessagesToSkip: number = 0,
58+
): string {
59+
return RenderUtils.wrapInScriptTags(
60+
'consoleReplayLog',
61+
consoleReplay(customConsoleHistory, numberOfMessagesToSkip),
62+
);
5063
}

node_package/src/clientStartup.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function debugTurbolinks(...msg: string[]): void {
6565
}
6666

6767
function turbolinksInstalled(): boolean {
68-
return (typeof Turbolinks !== 'undefined');
68+
return typeof Turbolinks !== 'undefined';
6969
}
7070

7171
function turboInstalled() {
@@ -82,7 +82,7 @@ function reactOnRailsHtmlElements(): HTMLCollectionOf<Element> {
8282

8383
function initializeStore(el: Element, context: Context, railsContext: RailsContext): void {
8484
const name = el.getAttribute(REACT_ON_RAILS_STORE_ATTRIBUTE) || '';
85-
const props = (el.textContent !== null) ? JSON.parse(el.textContent) : {};
85+
const props = el.textContent !== null ? JSON.parse(el.textContent) : {};
8686
const storeGenerator = context.ReactOnRails.getStoreGenerator(name);
8787
const store = storeGenerator(props, railsContext);
8888
context.ReactOnRails.setStore(name, store);
@@ -96,7 +96,7 @@ function forEachStore(context: Context, railsContext: RailsContext): void {
9696
}
9797

9898
function turbolinksVersion5(): boolean {
99-
return (typeof Turbolinks.controller !== 'undefined');
99+
return typeof Turbolinks.controller !== 'undefined';
100100
}
101101

102102
function turbolinksSupported(): boolean {
@@ -114,9 +114,12 @@ function delegateToRenderer(
114114

115115
if (isRenderer) {
116116
if (trace) {
117-
console.log(`\
117+
console.log(
118+
`\
118119
DELEGATING TO RENDERER ${name} for dom node with id: ${domNodeId} with props, railsContext:`,
119-
props, railsContext);
120+
props,
121+
railsContext,
122+
);
120123
}
121124

122125
(component as RenderFunction)(props, railsContext, domNodeId);
@@ -138,7 +141,7 @@ function render(el: Element, context: Context, railsContext: RailsContext): void
138141
// This must match lib/react_on_rails/helper.rb
139142
const name = el.getAttribute('data-component-name') || '';
140143
const domNodeId = domNodeIdForEl(el);
141-
const props = (el.textContent !== null) ? JSON.parse(el.textContent) : {};
144+
const props = el.textContent !== null ? JSON.parse(el.textContent) : {};
142145
const trace = el.getAttribute('data-trace') === 'true';
143146

144147
try {
@@ -167,15 +170,19 @@ function render(el: Element, context: Context, railsContext: RailsContext): void
167170
You returned a server side type of react-router error: ${JSON.stringify(reactElementOrRouterResult)}
168171
You should return a React.Component always for the client side entry point.`);
169172
} else {
170-
const rootOrElement = reactHydrateOrRender(domNode, reactElementOrRouterResult as ReactElement, shouldHydrate);
173+
const rootOrElement = reactHydrateOrRender(
174+
domNode,
175+
reactElementOrRouterResult as ReactElement,
176+
shouldHydrate,
177+
);
171178
if (supportsRootApi) {
172179
context.roots.push(rootOrElement as Root);
173180
}
174181
}
175182
}
176183
} catch (e: any) {
177184
console.error(e.message);
178-
e.message = `ReactOnRails encountered an error while rendering component: ${name}. See above error message.`
185+
e.message = `ReactOnRails encountered an error while rendering component: ${name}. See above error message.`;
179186
throw e;
180187
}
181188
}
@@ -196,7 +203,7 @@ function parseRailsContext(): RailsContext | null {
196203
}
197204

198205
if (!el.textContent) {
199-
throw new Error('The HTML element with ID \'js-react-on-rails-context\' has no textContent');
206+
throw new Error("The HTML element with ID 'js-react-on-rails-context' has no textContent");
200207
}
201208

202209
return JSON.parse(el.textContent);
@@ -246,8 +253,7 @@ function unmount(el: Element): void {
246253
try {
247254
ReactDOM.unmountComponentAtNode(domNode);
248255
} catch (e: any) {
249-
console.info(`Caught error calling unmountComponentAtNode: ${e.message} for domNode`,
250-
domNode, e);
256+
console.info(`Caught error calling unmountComponentAtNode: ${e.message} for domNode`, domNode, e);
251257
}
252258
}
253259

@@ -281,23 +287,19 @@ function renderInit(): void {
281287
}
282288

283289
if (turboInstalled()) {
284-
debugTurbolinks(
285-
'USING TURBO: document added event listeners ' +
286-
'turbo:before-render and turbo:render.');
290+
debugTurbolinks('USING TURBO: document added event listeners turbo:before-render and turbo:render.');
287291
document.addEventListener('turbo:before-render', reactOnRailsPageUnloaded);
288292
document.addEventListener('turbo:render', reactOnRailsPageLoaded);
289293
reactOnRailsPageLoaded();
290294
} else if (turbolinksVersion5()) {
291295
debugTurbolinks(
292-
'USING TURBOLINKS 5: document added event listeners ' +
293-
'turbolinks:before-render and turbolinks:render.');
296+
'USING TURBOLINKS 5: document added event listeners turbolinks:before-render and turbolinks:render.',
297+
);
294298
document.addEventListener('turbolinks:before-render', reactOnRailsPageUnloaded);
295299
document.addEventListener('turbolinks:render', reactOnRailsPageLoaded);
296300
reactOnRailsPageLoaded();
297301
} else {
298-
debugTurbolinks(
299-
'USING TURBOLINKS 2: document added event listeners page:before-unload and ' +
300-
'page:change.');
302+
debugTurbolinks('USING TURBOLINKS 2: document added event listeners page:before-unload and page:change.');
301303
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
302304
document.addEventListener('page:change', reactOnRailsPageLoaded);
303305
}
@@ -308,12 +310,12 @@ function isWindow(context: Context): context is Window {
308310
}
309311

310312
function onPageReady(callback: () => void) {
311-
if (document.readyState === "complete") {
313+
if (document.readyState === 'complete') {
312314
callback();
313315
} else {
314-
document.addEventListener("readystatechange", function onReadyStateChange() {
315-
onPageReady(callback);
316-
document.removeEventListener("readystatechange", onReadyStateChange);
316+
document.addEventListener('readystatechange', function onReadyStateChange() {
317+
onPageReady(callback);
318+
document.removeEventListener('readystatechange', onReadyStateChange);
317319
});
318320
}
319321
}

node_package/src/context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ export type Context = Window | typeof globalThis;
44
* Get the context, be it window or global
55
*/
66
export default function context(this: void): Context | void {
7-
return ((typeof window !== 'undefined') && window) ||
8-
((typeof global !== 'undefined') && global) ||
9-
this;
7+
return (typeof window !== 'undefined' && window) || (typeof global !== 'undefined' && global) || this;
108
}

0 commit comments

Comments
 (0)