Skip to content

Commit 237b19b

Browse files
authored
[Blazor] Add more eslint rules and apply auto fixes (#9004)
1 parent facb001 commit 237b19b

19 files changed

+173
-57
lines changed

src/Components/Browser.JS/src/.eslintrc.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,37 @@ module.exports = {
3333
}],
3434
"comma-style": ["error", "last"],
3535
"comma-spacing": ["error", { "after": true }],
36-
"no-trailing-spaces": ["error"]
36+
"no-trailing-spaces": ["error"],
37+
"curly": ["error"],
38+
"dot-location": ["error", "property"],
39+
"eqeqeq": ["error", "always"],
40+
"no-eq-null": ["error"],
41+
"no-multi-spaces": ["error"],
42+
"no-unused-labels": ["error"],
43+
"require-await": ["error"],
44+
"array-bracket-newline": ["error", { "multiline": true, "minItems": 4 }],
45+
"array-bracket-spacing": ["error", "never"],
46+
"array-element-newline": ["error", { "minItems": 3 }],
47+
"block-spacing": ["error"],
48+
"func-call-spacing": ["error", "never"],
49+
"function-paren-newline": ["error", "multiline"],
50+
"key-spacing": ["error", { "mode": "strict" }],
51+
"keyword-spacing": ["error", { "before": true }],
52+
"lines-between-class-members": ["error", "always"],
53+
"new-parens": ["error"],
54+
"no-multi-assign": ["error"],
55+
"no-multiple-empty-lines": ["error"],
56+
"no-unneeded-ternary": ["error"],
57+
"no-whitespace-before-property": ["error"],
58+
"one-var": ["error", "never"],
59+
"space-before-function-paren": ["error", {
60+
"anonymous": "never",
61+
"named": "never",
62+
"asyncArrow": "always"
63+
}],
64+
"space-in-parens": ["error", "never"],
65+
"space-infix-ops": ["error"]
66+
3767
},
3868
globals: {
3969
DotNet: "readonly"

src/Components/Browser.JS/src/GlobalExports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ window['Blazor'] = {
99
_internal: {
1010
attachRootComponentToElement,
1111
http: httpInternalFunctions,
12-
uriHelper: uriHelperInternalFunctions
13-
}
14-
};
12+
uriHelper: uriHelperInternalFunctions,
13+
},
14+
};

src/Components/Browser.JS/src/Platform/Circuits/AutoReconnectCircuitHandler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { ReconnectDisplay } from './ReconnectDisplay';
55
import { ILogger, LogLevel } from '../Logging/ILogger';
66
export class AutoReconnectCircuitHandler implements CircuitHandler {
77
public static readonly MaxRetries = 5;
8+
89
public static readonly RetryInterval = 3000;
10+
911
public static readonly DialogId = 'components-reconnect-modal';
12+
1013
public reconnectDisplay: ReconnectDisplay;
14+
1115
public logger: ILogger;
1216

1317
public constructor(logger: ILogger) {
@@ -20,6 +24,7 @@ export class AutoReconnectCircuitHandler implements CircuitHandler {
2024
}
2125
});
2226
}
27+
2328
public onConnectionUp(): void {
2429
this.reconnectDisplay.hide();
2530
}

src/Components/Browser.JS/src/Platform/Circuits/CircuitManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ComponentDescriptor, MarkupRegistrationTags, StartComponentComment, End
33

44
export class CircuitDescriptor {
55
public circuitId: string;
6+
67
public components: ComponentDescriptor[];
78

89
public constructor(circuitId: string, components: ComponentDescriptor[]) {

src/Components/Browser.JS/src/Platform/Circuits/ComponentDescriptor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ export interface MarkupRegistrationTags {
2323

2424
export class ComponentDescriptor {
2525
public registrationTags: MarkupRegistrationTags;
26+
2627
public componentId: number;
28+
2729
public circuitId: string;
30+
2831
public rendererId: number;
2932

3033
public constructor(componentId: number, circuitId: string, rendererId: number, descriptor: MarkupRegistrationTags) {

src/Components/Browser.JS/src/Platform/Circuits/DefaultReconnectDisplay.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { ReconnectDisplay } from './ReconnectDisplay';
22
import { AutoReconnectCircuitHandler } from './AutoReconnectCircuitHandler';
33
export class DefaultReconnectDisplay implements ReconnectDisplay {
44
modal: HTMLDivElement;
5+
56
message: HTMLHeadingElement;
7+
68
button: HTMLButtonElement;
9+
710
addedToDom: boolean = false;
11+
812
constructor(private document: Document) {
913
this.modal = this.document.createElement('div');
1014
this.modal.id = AutoReconnectCircuitHandler.DialogId;
@@ -21,7 +25,7 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
2125
'background-color: #fff',
2226
'opacity: 0.8',
2327
'text-align: center',
24-
'font-weight: bold'
28+
'font-weight: bold',
2529
];
2630

2731
this.modal.style.cssText = modalStyles.join(';');
@@ -31,6 +35,7 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
3135

3236
this.button.addEventListener('click', () => window['Blazor'].reconnect());
3337
}
38+
3439
show(): void {
3540
if (!this.addedToDom) {
3641
this.addedToDom = true;
@@ -40,9 +45,11 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
4045
this.button.style.display = 'none';
4146
this.message.textContent = 'Attempting to reconnect to the server...';
4247
}
48+
4349
hide(): void {
4450
this.modal.style.display = 'none';
4551
}
52+
4653
failed(): void {
4754
this.button.style.display = 'block';
4855
this.message.textContent = 'Failed to reconnect to the server.';

src/Components/Browser.JS/src/Platform/Circuits/RenderQueue.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default class RenderQueue {
77
private static renderQueues = new Map<number, RenderQueue>();
88

99
private nextBatchId = 2;
10+
1011
public browserRendererId: number;
12+
1113
public logger: ILogger;
1214

1315
public constructor(browserRendererId: number, logger: ILogger) {

src/Components/Browser.JS/src/Platform/Circuits/UserSpecifiedDisplay.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { ReconnectDisplay } from "./ReconnectDisplay";
1+
import { ReconnectDisplay } from './ReconnectDisplay';
22
export class UserSpecifiedDisplay implements ReconnectDisplay {
33
static readonly ShowClassName = 'components-reconnect-show';
4+
45
static readonly HideClassName = 'components-reconnect-hide';
6+
57
static readonly FailedClassName = 'components-reconnect-failed';
8+
69
constructor(private dialog: HTMLElement) {
710
}
11+
812
show(): void {
913
this.removeClasses();
1014
this.dialog.classList.add(UserSpecifiedDisplay.ShowClassName);
1115
}
16+
1217
hide(): void {
1318
this.removeClasses();
1419
this.dialog.classList.add(UserSpecifiedDisplay.HideClassName);
1520
}
21+
1622
failed(): void {
1723
this.removeClasses();
1824
this.dialog.classList.add(UserSpecifiedDisplay.FailedClassName);
1925
}
26+
2027
private removeClasses() {
2128
this.dialog.classList.remove(UserSpecifiedDisplay.ShowClassName, UserSpecifiedDisplay.HideClassName, UserSpecifiedDisplay.FailedClassName);
2229
}

src/Components/Browser.JS/src/Platform/Mono/MonoPlatform.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const monoPlatform: Platform = {
2121

2222
// mono.js assumes the existence of this
2323
window['Browser'] = {
24-
init: () => { }
24+
init: () => { },
2525
};
2626
// Emscripten works by expecting the module config to be a global
2727
window['Module'] = createEmscriptenModuleInstance(loadAssemblyUrls, resolve, reject);
@@ -60,7 +60,7 @@ export const monoPlatform: Platform = {
6060
try {
6161
const argsBuffer = Module.stackAlloc(args.length);
6262
const exceptionFlagManagedInt = Module.stackAlloc(4);
63-
for (var i = 0; i < args.length; ++i) {
63+
for (let i = 0; i < args.length; ++i) {
6464
Module.setValue(argsBuffer + i * 4, args[i], 'i32');
6565
}
6666
Module.setValue(exceptionFlagManagedInt, 0, 'i32');
@@ -80,8 +80,8 @@ export const monoPlatform: Platform = {
8080

8181
toJavaScriptString: function toJavaScriptString(managedString: System_String) {
8282
// Comments from original Mono sample:
83-
//FIXME this is wastefull, we could remove the temp malloc by going the UTF16 route
84-
//FIXME this is unsafe, cuz raw objects could be GC'd.
83+
// FIXME this is wastefull, we could remove the temp malloc by going the UTF16 route
84+
// FIXME this is unsafe, cuz raw objects could be GC'd.
8585

8686
const utf8 = mono_string_get_utf8(managedString);
8787
const res = Module.UTF8ToString(utf8);
@@ -206,11 +206,27 @@ function createEmscriptenModuleInstance(loadAssemblyUrls: string[], onReady: ()
206206

207207
module.preRun.push(() => {
208208
// By now, emscripten should be initialised enough that we can capture these methods for later use
209-
const mono_wasm_add_assembly = Module.cwrap('mono_wasm_add_assembly', null, ['string', 'number', 'number']);
209+
const mono_wasm_add_assembly = Module.cwrap('mono_wasm_add_assembly', null, [
210+
'string',
211+
'number',
212+
'number',
213+
]);
210214
assembly_load = Module.cwrap('mono_wasm_assembly_load', 'number', ['string']);
211-
find_class = Module.cwrap('mono_wasm_assembly_find_class', 'number', ['number', 'string', 'string']);
212-
find_method = Module.cwrap('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number']);
213-
invoke_method = Module.cwrap('mono_wasm_invoke_method', 'number', ['number', 'number', 'number']);
215+
find_class = Module.cwrap('mono_wasm_assembly_find_class', 'number', [
216+
'number',
217+
'string',
218+
'string',
219+
]);
220+
find_method = Module.cwrap('mono_wasm_assembly_find_method', 'number', [
221+
'number',
222+
'string',
223+
'number',
224+
]);
225+
invoke_method = Module.cwrap('mono_wasm_invoke_method', 'number', [
226+
'number',
227+
'number',
228+
'number',
229+
]);
214230
mono_string_get_utf8 = Module.cwrap('mono_wasm_string_get_utf8', 'number', ['number']);
215231
mono_string = Module.cwrap('mono_wasm_string_from_js', 'number', ['string']);
216232

@@ -264,12 +280,12 @@ function toAbsoluteUrl(possiblyRelativeUrl: string) {
264280

265281
function asyncLoad(url) {
266282
return new Promise<Uint8Array>((resolve, reject) => {
267-
var xhr = new XMLHttpRequest;
283+
const xhr = new XMLHttpRequest();
268284
xhr.open('GET', url, /* async: */ true);
269285
xhr.responseType = 'arraybuffer';
270286
xhr.onload = function xhr_onload() {
271287
if (xhr.status == 200 || xhr.status == 0 && xhr.response) {
272-
var asm = new Uint8Array(xhr.response);
288+
const asm = new Uint8Array(xhr.response);
273289
resolve(asm);
274290
} else {
275291
reject(xhr);
@@ -295,12 +311,12 @@ function attachInteropInvoker() {
295311
const assemblyNameOrDotNetObjectId = dotNetObjectId
296312
? dotNetObjectId.toString()
297313
: assemblyName;
298-
314+
299315
monoPlatform.callMethod(dotNetDispatcherBeginInvokeMethodHandle, null, [
300316
callId ? monoPlatform.toDotNetString(callId.toString()) : null,
301317
monoPlatform.toDotNetString(assemblyNameOrDotNetObjectId!),
302318
monoPlatform.toDotNetString(methodIdentifier),
303-
monoPlatform.toDotNetString(argsJson)
319+
monoPlatform.toDotNetString(argsJson),
304320
]);
305321
},
306322

@@ -309,7 +325,7 @@ function attachInteropInvoker() {
309325
assemblyName ? monoPlatform.toDotNetString(assemblyName) : null,
310326
monoPlatform.toDotNetString(methodIdentifier),
311327
dotNetObjectId ? monoPlatform.toDotNetString(dotNetObjectId.toString()) : null,
312-
monoPlatform.toDotNetString(argsJson)
328+
monoPlatform.toDotNetString(argsJson),
313329
]) as System_String;
314330
return resultJsonStringPtr
315331
? monoPlatform.toJavaScriptString(resultJsonStringPtr)

src/Components/Browser.JS/src/Platform/Platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export interface Platform {
2424
// We don't actually instantiate any of these at runtime. For perf it's preferable to
2525
// use the original 'number' instances without any boxing. The definitions are just
2626
// for compile-time checking, since TypeScript doesn't support nominal types.
27-
export interface MethodHandle { MethodHandle__DO_NOT_IMPLEMENT: any };
28-
export interface System_Object { System_Object__DO_NOT_IMPLEMENT: any };
27+
export interface MethodHandle { MethodHandle__DO_NOT_IMPLEMENT: any }
28+
export interface System_Object { System_Object__DO_NOT_IMPLEMENT: any }
2929
export interface System_String extends System_Object { System_String__DO_NOT_IMPLEMENT: any }
3030
export interface System_Array<T> extends System_Object { System_Array__DO_NOT_IMPLEMENT: any }
3131
export interface Pointer { Pointer__DO_NOT_IMPLEMENT: any }

src/Components/Browser.JS/src/Rendering/BrowserRenderer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const rootComponentsPendingFirstRender: { [componentId: number]: LogicalElement
1111

1212
export class BrowserRenderer {
1313
private eventDelegator: EventDelegator;
14+
1415
private childComponentLocations: { [componentId: number]: LogicalElement } = {};
16+
1517
private browserRendererId: number;
1618

1719
public constructor(browserRendererId: number) {
@@ -388,7 +390,8 @@ function raiseEvent(event: Event, browserRendererId: number, eventHandlerId: num
388390
'Microsoft.AspNetCore.Components.Browser',
389391
'DispatchEvent',
390392
eventDescriptor,
391-
JSON.stringify(eventArgs.data));
393+
JSON.stringify(eventArgs.data)
394+
);
392395
}
393396

394397
function clearElement(element: Element) {
@@ -400,7 +403,7 @@ function clearElement(element: Element) {
400403

401404
function clearBetween(start: Node, end: Node): void {
402405
const logicalParent = getLogicalParent(start as unknown as LogicalElement);
403-
if(!logicalParent){
406+
if (!logicalParent){
404407
throw new Error("Can't clear between nodes. The start node does not have a logical parent.");
405408
}
406409
const children = getLogicalChildrenArray(logicalParent);

src/Components/Browser.JS/src/Rendering/EventDelegator.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
import { EventForDotNet, UIEventArgs } from './EventForDotNet';
1+
import { EventForDotNet, UIEventArgs } from './EventForDotNet';
22

33
const nonBubblingEvents = toLookup([
4-
'abort', 'blur', 'change', 'error', 'focus', 'load', 'loadend', 'loadstart', 'mouseenter', 'mouseleave',
5-
'progress', 'reset', 'scroll', 'submit', 'unload', 'DOMNodeInsertedIntoDocument', 'DOMNodeRemovedFromDocument'
4+
'abort',
5+
'blur',
6+
'change',
7+
'error',
8+
'focus',
9+
'load',
10+
'loadend',
11+
'loadstart',
12+
'mouseenter',
13+
'mouseleave',
14+
'progress',
15+
'reset',
16+
'scroll',
17+
'submit',
18+
'unload',
19+
'DOMNodeInsertedIntoDocument',
20+
'DOMNodeRemovedFromDocument',
621
]);
722

823
export interface OnEventCallback {
@@ -14,7 +29,9 @@ export interface OnEventCallback {
1429
// event listeners as required (and also maps actual events back to the given callback).
1530
export class EventDelegator {
1631
private static nextEventDelegatorId = 0;
32+
1733
private eventsCollectionKey: string;
34+
1835
private eventInfoStore: EventInfoStore;
1936

2037
constructor(private onEvent: OnEventCallback) {
@@ -93,6 +110,7 @@ export class EventDelegator {
93110
// for a given event name changes between zero and nonzero
94111
class EventInfoStore {
95112
private infosByEventHandlerId: { [eventHandlerId: number]: EventHandlerInfo } = {};
113+
96114
private countByEventName: { [eventName: string]: number } = {};
97115

98116
constructor(private globalListener: EventListener) {
@@ -155,7 +173,7 @@ interface EventHandlerInfosForElement {
155173
// can only have one attribute with a given name, hence only one event handler with
156174
// that name at any one time.
157175
// So to keep things simple, only track one EventHandlerInfo per (element, eventName)
158-
[eventName: string]: EventHandlerInfo
176+
[eventName: string]: EventHandlerInfo;
159177
}
160178

161179
interface EventHandlerInfo {
@@ -166,6 +184,8 @@ interface EventHandlerInfo {
166184

167185
function toLookup(items: string[]): { [key: string]: boolean } {
168186
const result = {};
169-
items.forEach(value => { result[value] = true; });
187+
items.forEach(value => {
188+
result[value] = true;
189+
});
170190
return result;
171191
}

0 commit comments

Comments
 (0)