From 5a539559ef7cfb835a022efa1db3bb0c48069fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20van=20Br=C3=BCgge?= Date: Thu, 25 Feb 2021 15:25:03 +0100 Subject: [PATCH] chore(refactor): apply eslint hints --- .eslintrc.js | 1 + karma.conf.js | 2 +- src/h.ts | 6 +----- src/modules/hero.ts | 35 +++++++++-------------------------- src/thunk.ts | 10 +++++----- test/tsconfig.json | 2 +- test/unit/core.ts | 3 +-- test/unit/eventlisteners.ts | 4 ++-- test/unit/thunk.ts | 28 ++++++++++++++-------------- 9 files changed, 35 insertions(+), 56 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index abcbee8..d1ffbd7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -30,6 +30,7 @@ module.exports = { "@typescript-eslint/no-unsafe-return": "off", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/explicit-module-boundary-types": "off", + "@typescript-eslint/explicit-module-boundary-types": "off", }, }, { diff --git a/karma.conf.js b/karma.conf.js index d48e489..373df41 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -29,7 +29,7 @@ module.exports = function (config) { plugins: [ "karma-mocha", "karma-typescript", - require("karma-mocha-reporter"), + "karma-mocha-reporter", require("./karma-benchmark-reporter.cjs"), "karma-chrome-launcher", "karma-firefox-launcher", diff --git a/src/h.ts b/src/h.ts index 7bf80d3..736b012 100644 --- a/src/h.ts +++ b/src/h.ts @@ -16,11 +16,7 @@ function addNS( for (let i = 0; i < children.length; ++i) { const childData = children[i].data; if (childData !== undefined) { - addNS( - childData, - (children[i] as VNode).children as VNodes, - children[i].sel - ); + addNS(childData, children[i].children as VNodes, children[i].sel); } } } diff --git a/src/modules/hero.ts b/src/modules/hero.ts index c9e1b9b..738cae3 100755 --- a/src/modules/hero.ts +++ b/src/modules/hero.ts @@ -41,7 +41,7 @@ function calcTransformOrigin( textRect.left + textRect.width / 2 - boundingRect.left; const relativeCenterY = textRect.top + textRect.height / 2 - boundingRect.top; - return relativeCenterX + "px " + relativeCenterY + "px"; + return `${relativeCenterX}px ${relativeCenterY}px`; } } return "0 0"; // top left @@ -168,18 +168,9 @@ function post() { newRect ); newStyle.opacity = "0"; - newStyle.transform = - origTransform + - "translate(" + - dx + - "px, " + - dy + - "px) " + - "scale(" + - 1 / wRatio + - ", " + - 1 / hRatio + - ")"; + newStyle.transform = `${origTransform}translate(${dx}px, ${dy}px) scale(${ + 1 / wRatio + }, ${1 / hRatio})`; setNextFrame(newStyle, "transition", origTransition); setNextFrame(newStyle, "transform", origTransform); setNextFrame(newStyle, "opacity", "1"); @@ -197,10 +188,10 @@ function post() { } } oldStyle.position = "absolute"; - oldStyle.top = oldRect.top + "px"; // start at existing position - oldStyle.left = oldRect.left + "px"; - oldStyle.width = oldRect.width + "px"; // Needed for elements who were sized relative to their parents - oldStyle.height = oldRect.height + "px"; // Needed for elements who were sized relative to their parents + oldStyle.top = `${oldRect.top}px`; // start at existing position + oldStyle.left = `${oldRect.left}px`; + oldStyle.width = `${oldRect.width}px`; // Needed for elements who were sized relative to their parents + oldStyle.height = `${oldRect.height}px`; // Needed for elements who were sized relative to their parents oldStyle.margin = "0"; // Margin on hero element leads to incorrect positioning oldStyle.transformOrigin = calcTransformOrigin( isTextNode, @@ -213,15 +204,7 @@ function post() { setNextFrame( oldStyle, "transform", - "translate(" + - -dx + - "px, " + - -dy + - "px) scale(" + - wRatio + - ", " + - hRatio + - ")" + `translate(${-dx}px, ${-dy}px) scale(${wRatio}, ${hRatio})` ); // scale must be on far right for translate to be correct setNextFrame(oldStyle, "opacity", "0"); oldElm.addEventListener("transitionend", function (ev: TransitionEvent) { diff --git a/src/thunk.ts b/src/thunk.ts index ec7373d..dcdefb5 100644 --- a/src/thunk.ts +++ b/src/thunk.ts @@ -11,8 +11,8 @@ export interface Thunk extends VNode { } export interface ThunkFn { - (sel: string, fn: Function, args: any[]): Thunk; - (sel: string, key: any, fn: Function, args: any[]): Thunk; + (sel: string, fn: (...args: any[]) => any, args: any[]): Thunk; + (sel: string, key: any, fn: (...args: any[]) => any, args: any[]): Thunk; } function copyToThunk(vnode: VNode, thunk: VNode): void { @@ -26,7 +26,7 @@ function copyToThunk(vnode: VNode, thunk: VNode): void { function init(thunk: VNode): void { const cur = thunk.data as VNodeData; - const vnode = (cur.fn as any).apply(undefined, cur.args); + const vnode = (cur.fn as any)(...cur.args); copyToThunk(vnode, thunk); } @@ -37,12 +37,12 @@ function prepatch(oldVnode: VNode, thunk: VNode): void { const oldArgs = old.args; const args = cur.args; if (old.fn !== cur.fn || (oldArgs as any).length !== (args as any).length) { - copyToThunk((cur.fn as any).apply(undefined, args), thunk); + copyToThunk((cur.fn as any)(...args), thunk); return; } for (i = 0; i < (args as any).length; ++i) { if ((oldArgs as any)[i] !== (args as any)[i]) { - copyToThunk((cur.fn as any).apply(undefined, args), thunk); + copyToThunk((cur.fn as any)(...args), thunk); return; } } diff --git a/test/tsconfig.json b/test/tsconfig.json index a762a6c..eb755c1 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -3,5 +3,5 @@ "compilerOptions": { "esModuleInterop": true }, - "include": ["./unit/*.ts"] + "include": ["./**/*.ts"] } diff --git a/test/unit/core.ts b/test/unit/core.ts index 6ea02da..0b84930 100644 --- a/test/unit/core.ts +++ b/test/unit/core.ts @@ -769,8 +769,7 @@ describe("snabbdom", function () { elm.children[i].innerHTML, shufArr[i].toString() ); - const opacity = (elm.children[i] as HTMLSpanElement).style - .opacity as string; + const opacity = (elm.children[i] as HTMLSpanElement).style.opacity; assert.strictEqual(opacities[i].indexOf(opacity), 0); } } diff --git a/test/unit/eventlisteners.ts b/test/unit/eventlisteners.ts index 33fbe2a..4e55df3 100644 --- a/test/unit/eventlisteners.ts +++ b/test/unit/eventlisteners.ts @@ -29,7 +29,7 @@ describe("event listeners", function () { "div", { on: { - click: function (ev) { + click: function () { result.push(1); }, }, @@ -40,7 +40,7 @@ describe("event listeners", function () { "div", { on: { - click: function (ev) { + click: function () { result.push(2); }, }, diff --git a/test/unit/thunk.ts b/test/unit/thunk.ts index cd85d12..dbece0c 100644 --- a/test/unit/thunk.ts +++ b/test/unit/thunk.ts @@ -11,7 +11,7 @@ describe("thunk", function () { }); it("returns vnode with data and render function", function () { function numberInSpan(n: number) { - return h("span", "Number is " + n); + return h("span", `Number is ${n}`); } const vnode = thunk("span", "num", numberInSpan, [22]); assert.deepEqual(vnode.sel, "span"); @@ -22,7 +22,7 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("span", "num", numberInSpan, [2])]); @@ -35,7 +35,7 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("span", "num", numberInSpan, [1])]); @@ -48,7 +48,7 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("span", "num", numberInSpan, [1, 2])]); @@ -61,11 +61,11 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } function numberInSpan2(n: number) { called++; - return h("span", { key: "num" }, "Number really is " + n); + return h("span", { key: "num" }, `Number really is ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("span", "num", numberInSpan2, [1])]); @@ -78,7 +78,7 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("span", "num", numberInSpan, [1])]); @@ -106,7 +106,7 @@ describe("thunk", function () { let called = 0; function numberInSpan(n: number) { called++; - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } const vnode1 = thunk("span", "num", numberInSpan, [1]); const vnode2 = thunk("span", "num", numberInSpan, [1]); @@ -127,11 +127,11 @@ describe("thunk", function () { }); it("can be replaced and removed", function () { function numberInSpan(n: number) { - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } function oddEven(n: number): VNode { const prefix = n % 2 === 0 ? "Even" : "Odd"; - return h("div", { key: oddEven as any }, prefix + ": " + n); + return h("div", { key: oddEven as any }, `${prefix}: ${n}`); } const vnode1 = h("div", [thunk("span", "num", numberInSpan, [1])]); const vnode2 = h("div", [thunk("div", "oddEven", oddEven, [4])]); @@ -146,11 +146,11 @@ describe("thunk", function () { }); it("can be replaced and removed when root", function () { function numberInSpan(n: number) { - return h("span", { key: "num" }, "Number is " + n); + return h("span", { key: "num" }, `Number is ${n}`); } function oddEven(n: number): VNode { const prefix = n % 2 === 0 ? "Even" : "Odd"; - return h("div", { key: oddEven as any }, prefix + ": " + n); + return h("div", { key: oddEven as any }, `${prefix}: ${n}`); } const vnode1 = thunk("span", "num", numberInSpan, [1]); const vnode2 = thunk("div", "oddEven", oddEven, [4]); @@ -172,7 +172,7 @@ describe("thunk", function () { return h( "span", { key: "num", hook: { destroy: destroyHook } }, - "Number is " + n + `Number is ${n}` ); } const vnode1 = h("div", [ @@ -194,7 +194,7 @@ describe("thunk", function () { return h( "span", { key: "num", hook: { remove: hook } }, - "Number is " + n + `Number is ${n}` ); } const vnode1 = h("div", [