From 86488cb8ca0d7e451107e4511cc3da087be69c84 Mon Sep 17 00:00:00 2001 From: "Shahar Or (mightyiam)" Date: Thu, 20 Feb 2020 22:03:59 +0700 Subject: [PATCH] style: enable eslint rule one-var --- .eslintrc.js | 1 - src/h.ts | 5 ++++- src/helpers/attachto.ts | 3 ++- src/modules/attributes.ts | 7 ++++--- src/modules/class.ts | 8 +++++--- src/modules/dataset.ts | 8 ++++---- src/modules/eventlisteners.ts | 16 ++++++++-------- src/modules/props.ts | 9 ++++++--- src/modules/style.ts | 22 ++++++++++++++++------ src/snabbdom.ts | 17 ++++++++++++----- src/test/core.ts | 20 +++++++++++++++++--- src/thunk.ts | 7 +++++-- 12 files changed, 83 insertions(+), 40 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 494575b..f959dbf 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -22,7 +22,6 @@ module.exports = { 'no-unneeded-ternary': 'off', 'no-unused-expressions': 'off', 'no-unused-vars': 'off', - 'one-var': 'off', 'prefer-const': 'off', eqeqeq: 'off', semi: 'off' diff --git a/src/h.ts b/src/h.ts index ca1b545..b6fe19b 100644 --- a/src/h.ts +++ b/src/h.ts @@ -23,7 +23,10 @@ export function h(sel: string, data: VNodeData | null): VNode; export function h(sel: string, children: VNodeChildren): VNode; export function h(sel: string, data: VNodeData | null, children: VNodeChildren): VNode; export function h (sel: any, b?: any, c?: any): VNode { - var data: VNodeData = {}, children: any, text: any, i: number; + var data: VNodeData = {}; + var children: any; + var text: any; + var i: number; if (c !== undefined) { if (b !== null) { data = b; diff --git a/src/helpers/attachto.ts b/src/helpers/attachto.ts index af00563..bcb8f66 100755 --- a/src/helpers/attachto.ts +++ b/src/helpers/attachto.ts @@ -39,7 +39,8 @@ function destroy (vnode: VNodeWithAttachData): void { } function create (_: any, vnode: VNodeWithAttachData): void { - const real = vnode.elm, attachData = vnode.data.attachData; + const real = vnode.elm; + const attachData = vnode.data.attachData; const placeholder = document.createElement('span'); // Replace actual element with dummy placeholder // Snabbdom will then insert placeholder instead diff --git a/src/modules/attributes.ts b/src/modules/attributes.ts index 9ca8426..490491b 100755 --- a/src/modules/attributes.ts +++ b/src/modules/attributes.ts @@ -17,9 +17,10 @@ const colonChar = 58; const xChar = 120; function updateAttrs (oldVnode: VNode, vnode: VNode): void { - var key: string, elm: Element = vnode.elm as Element, - oldAttrs = (oldVnode.data as VNodeData).attrs, - attrs = (vnode.data as VNodeData).attrs; + var key: string; + var elm: Element = vnode.elm as Element; + var oldAttrs = (oldVnode.data as VNodeData).attrs; + var attrs = (vnode.data as VNodeData).attrs; if (!oldAttrs && !attrs) return; if (oldAttrs === attrs) return; diff --git a/src/modules/class.ts b/src/modules/class.ts index 0d3f365..0ee01da 100755 --- a/src/modules/class.ts +++ b/src/modules/class.ts @@ -4,9 +4,11 @@ import { Module } from './module'; export type Classes = Record function updateClass (oldVnode: VNode, vnode: VNode): void { - var cur: any, name: string, elm: Element = vnode.elm as Element, - oldClass = (oldVnode.data as VNodeData).class, - klass = (vnode.data as VNodeData).class; + var cur: any; + var name: string; + var elm: Element = vnode.elm as Element; + var oldClass = (oldVnode.data as VNodeData).class; + var klass = (vnode.data as VNodeData).class; if (!oldClass && !klass) return; if (oldClass === klass) return; diff --git a/src/modules/dataset.ts b/src/modules/dataset.ts index b5958a1..69e2cf4 100755 --- a/src/modules/dataset.ts +++ b/src/modules/dataset.ts @@ -6,10 +6,10 @@ export type Dataset = Record; const CAPS_REGEX = /[A-Z]/g; function updateDataset (oldVnode: VNode, vnode: VNode): void { - let elm: HTMLElement = vnode.elm as HTMLElement, - oldDataset = (oldVnode.data as VNodeData).dataset, - dataset = (vnode.data as VNodeData).dataset, - key: string; + let elm: HTMLElement = vnode.elm as HTMLElement; + let oldDataset = (oldVnode.data as VNodeData).dataset; + let dataset = (vnode.data as VNodeData).dataset; + let key: string; if (!oldDataset && !dataset) return; if (oldDataset === dataset) return; diff --git a/src/modules/eventlisteners.ts b/src/modules/eventlisteners.ts index cf1ff17..ceb2ddf 100755 --- a/src/modules/eventlisteners.ts +++ b/src/modules/eventlisteners.ts @@ -33,8 +33,8 @@ function invokeHandler (handler: any, vnode?: VNode, event?: Event): void { } function handleEvent (event: Event, vnode: VNode) { - var name = event.type, - on = (vnode.data as VNodeData).on; + var name = event.type; + var on = (vnode.data as VNodeData).on; // call event handler(s) if exists if (on && on[name]) { @@ -49,12 +49,12 @@ function createListener () { } function updateEventListeners (oldVnode: VNode, vnode?: VNode): void { - var oldOn = (oldVnode.data as VNodeData).on, - oldListener = (oldVnode as any).listener, - oldElm: Element = oldVnode.elm as Element, - on = vnode && (vnode.data as VNodeData).on, - elm: Element = (vnode && vnode.elm) as Element, - name: string; + var oldOn = (oldVnode.data as VNodeData).on; + var oldListener = (oldVnode as any).listener; + var oldElm: Element = oldVnode.elm as Element; + var on = vnode && (vnode.data as VNodeData).on; + var elm: Element = (vnode && vnode.elm) as Element; + var name: string; // optimization for reused immutable handlers if (oldOn === on) { diff --git a/src/modules/props.ts b/src/modules/props.ts index 3a4ee64..819993a 100755 --- a/src/modules/props.ts +++ b/src/modules/props.ts @@ -4,9 +4,12 @@ import { Module } from './module'; export type Props = Record; function updateProps (oldVnode: VNode, vnode: VNode): void { - var key: string, cur: any, old: any, elm = vnode.elm, - oldProps = (oldVnode.data as VNodeData).props, - props = (vnode.data as VNodeData).props; + var key: string; + var cur: any; + var old: any; + var elm = vnode.elm; + var oldProps = (oldVnode.data as VNodeData).props; + var props = (vnode.data as VNodeData).props; if (!oldProps && !props) return; if (oldProps === props) return; diff --git a/src/modules/style.ts b/src/modules/style.ts index dc6b359..b90293e 100755 --- a/src/modules/style.ts +++ b/src/modules/style.ts @@ -22,9 +22,11 @@ function setNextFrame (obj: any, prop: string, val: any): void { } function updateStyle (oldVnode: VNode, vnode: VNode): void { - var cur: any, name: string, elm = vnode.elm, - oldStyle = (oldVnode.data as VNodeData).style, - style = (vnode.data as VNodeData).style; + var cur: any; + var name: string; + var elm = vnode.elm; + var oldStyle = (oldVnode.data as VNodeData).style; + var style = (vnode.data as VNodeData).style; if (!oldStyle && !style) return; if (oldStyle === style) return; @@ -61,7 +63,10 @@ function updateStyle (oldVnode: VNode, vnode: VNode): void { } function applyDestroyStyle (vnode: VNode): void { - var style: any, name: string, elm = vnode.elm, s = (vnode.data as VNodeData).style; + var style: any; + var name: string; + var elm = vnode.elm; + var s = (vnode.data as VNodeData).style; if (!s || !(style = s.destroy)) return; for (name in style) { (elm as any).style[name] = style[name]; @@ -78,8 +83,13 @@ function applyRemoveStyle (vnode: VNode, rm: () => void): void { (vnode.elm as any).offsetLeft; reflowForced = true; } - var name: string, elm = vnode.elm, i = 0, compStyle: CSSStyleDeclaration, - style = s.remove, amount = 0, applied: string[] = []; + var name: string; + var elm = vnode.elm; + var i = 0; + var compStyle: CSSStyleDeclaration; + var style = s.remove; + var amount = 0; + var applied: string[] = []; for (name in style) { applied.push(name); (elm as any).style[name] = style[name]; diff --git a/src/snabbdom.ts b/src/snabbdom.ts index 913c37b..70e7e84 100644 --- a/src/snabbdom.ts +++ b/src/snabbdom.ts @@ -50,7 +50,9 @@ export { h } from './h'; export { thunk } from './thunk'; export function init (modules: Array>, domApi?: DOMAPI) { - let i: number, j: number, cbs = ({} as ModuleHooks); + let i: number; + let j: number; + let cbs = ({} as ModuleHooks); const api: DOMAPI = domApi !== undefined ? domApi : htmlDomApi; @@ -80,7 +82,8 @@ export function init (modules: Array>, domApi?: DOMAPI) { } function createElm (vnode: VNode, insertedVnodeQueue: VNodeQueue): Node { - let i: any, data = vnode.data; + let i: any; + let data = vnode.data; if (data !== undefined) { const init = data.hook?.init; if (isDef(init)) { @@ -88,7 +91,8 @@ export function init (modules: Array>, domApi?: DOMAPI) { data = vnode.data; } } - let children = vnode.children, sel = vnode.sel; + let children = vnode.children; + let sel = vnode.sel; if (sel === '!') { if (isUndef(vnode.text)) { vnode.text = ''; @@ -167,7 +171,9 @@ export function init (modules: Array>, domApi?: DOMAPI) { startIdx: number, endIdx: number): void { for (; startIdx <= endIdx; ++startIdx) { - let listeners: number, rm: () => void, ch = vnodes[startIdx]; + let listeners: number; + let rm: () => void; + let ch = vnodes[startIdx]; if (ch != null) { if (isDef(ch.sel)) { invokeDestroyHook(ch); @@ -191,7 +197,8 @@ export function init (modules: Array>, domApi?: DOMAPI) { oldCh: VNode[], newCh: VNode[], insertedVnodeQueue: VNodeQueue) { - let oldStartIdx = 0, newStartIdx = 0; + let oldStartIdx = 0; + let newStartIdx = 0; let oldEndIdx = oldCh.length - 1; let oldStartVnode = oldCh[0]; let oldEndVnode = oldCh[oldEndIdx]; diff --git a/src/test/core.ts b/src/test/core.ts index bdc2f96..0e5201d 100644 --- a/src/test/core.ts +++ b/src/test/core.ts @@ -608,7 +608,12 @@ describe('snabbdom', function () { assert.deepEqual(map(inner, elm.children), ['4', '3', '2', '1', '5', '0']); }); it('handles random shuffles', function () { - var n, i, arr = [], opacities: string[] = [], elms = 14, samples = 5; + var n; + var i; + var arr = []; + var opacities: string[] = []; + var elms = 14; + var samples = 5; function spanNumWithOpacity (n: number, o: string) { return h('span', { key: n, style: { opacity: o } }, n.toString()); } @@ -656,7 +661,15 @@ describe('snabbdom', function () { assert.deepEqual(map(inner, elm.children), ['5', '4', '3', '2', '1', '0']); }); it('handles random shuffles with null/undefined children', function () { - var i, j, r, len, arr, maxArrLen = 15, samples = 5, vnode1 = vnode0, vnode2; + var i; + var j; + var r; + var len; + var arr; + var maxArrLen = 15; + var samples = 5; + var vnode1 = vnode0; + var vnode2; for (i = 0; i < samples; ++i, vnode1 = vnode2) { len = Math.floor(Math.random() * maxArrLen); arr = []; @@ -872,7 +885,8 @@ describe('snabbdom', function () { assert.strictEqual(result.length, 1); }); it('calls `postpatch` after `prepatch` listener', function () { - var pre = 0, post = 0; + var pre = 0; + var post = 0; function preCb () { pre++ } diff --git a/src/thunk.ts b/src/thunk.ts index aa2eb2a..da445ff 100644 --- a/src/thunk.ts +++ b/src/thunk.ts @@ -32,8 +32,11 @@ function init (thunk: VNode): void { } function prepatch (oldVnode: VNode, thunk: VNode): void { - let i: number, old = oldVnode.data as VNodeData, cur = thunk.data as VNodeData; - const oldArgs = old.args, args = cur.args; + let i: number; + let old = oldVnode.data as VNodeData; + let cur = thunk.data as VNodeData; + 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); return;