diff --git a/.eslintrc.js b/.eslintrc.js index 32cf65b6..bf2e1bf9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,6 +2,7 @@ module.exports = { root: true, extends: 'standard', 'rules': { + 'prefer-const': 2, 'arrow-parens': [2, 'as-needed'], 'no-new-func': 0, 'no-new': 0, diff --git a/src/compiler/codegen.js b/src/compiler/codegen.js index 4108d93a..a252ead4 100644 --- a/src/compiler/codegen.js +++ b/src/compiler/codegen.js @@ -94,7 +94,7 @@ function genData (el) { // directives first. // directives may mutate the el's other properties before they are generated. if (el.directives) { - let dirs = genDirectives(el) + const dirs = genDirectives(el) if (dirs) data += dirs + ',' } // pre @@ -174,7 +174,7 @@ function genDirectives (el) { for (i = 0, l = dirs.length; i < l; i++) { dir = dirs[i] needRuntime = true - let gen = platformDirectives[dir.name] || baseDirectives[dir.name] + const gen = platformDirectives[dir.name] || baseDirectives[dir.name] if (gen) { // compile-time directive that manipulates AST. // returns true if it also needs a runtime counterpart. @@ -236,7 +236,7 @@ function genComponent (el) { function genProps (props) { let res = '' for (let i = 0; i < props.length; i++) { - let prop = props[i] + const prop = props[i] res += `"${prop.name}":${prop.value},` } return res.slice(0, -1) @@ -244,7 +244,7 @@ function genProps (props) { function genHooks (hooks) { let res = '' - for (let key in hooks) { + for (const key in hooks) { res += `"${key}":function(n1,n2){${hooks[key].join(';')}},` } return res.slice(0, -1) diff --git a/src/compiler/events.js b/src/compiler/events.js index 854d9d59..2c00b63a 100644 --- a/src/compiler/events.js +++ b/src/compiler/events.js @@ -22,7 +22,7 @@ const modifierCode = { export function genHandlers (events) { let res = 'on:{' - for (let name in events) { + for (const name in events) { res += `"${name}":${genHandler(events[name])},` } return res.slice(0, -1) + '}' @@ -39,10 +39,10 @@ function genHandler (handler) { : `function($event){${handler.value}}` } else { let code = 'function($event){' - for (let key in handler.modifiers) { + for (const key in handler.modifiers) { code += modifierCode[key] || genKeyFilter(key) } - let handlerCode = simplePathRE.test(handler.value) + const handlerCode = simplePathRE.test(handler.value) ? handler.value + '($event)' : handler.value return code + handlerCode + '}' diff --git a/src/compiler/optimizer.js b/src/compiler/optimizer.js index e8bac635..b9cc77a7 100644 --- a/src/compiler/optimizer.js +++ b/src/compiler/optimizer.js @@ -26,7 +26,7 @@ function markStatic (node) { node.static = isStatic(node) if (node.children) { for (let i = 0, l = node.children.length; i < l; i++) { - let child = node.children[i] + const child = node.children[i] markStatic(child) if (!child.static) { node.static = false diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index e06c3f37..d946fcb8 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -224,7 +224,7 @@ function processFor (el) { } function processIf (el) { - let exp = getAndRemoveAttr(el, 'v-if') + const exp = getAndRemoveAttr(el, 'v-if') if (exp) { el.if = exp } diff --git a/src/core/instance/events.js b/src/core/instance/events.js index 16c3db6f..24e38102 100644 --- a/src/core/instance/events.js +++ b/src/core/instance/events.js @@ -47,14 +47,13 @@ export function eventsMixin (Vue) { */ Vue.prototype.$off = function (event, fn) { - let cbs // all if (!arguments.length) { this._events = Object.create(null) return this } // specific event - cbs = this._events[event] + const cbs = this._events[event] if (!cbs) { return this } diff --git a/src/core/instance/lifecycle.js b/src/core/instance/lifecycle.js index 5fb4afb5..d489ab4c 100644 --- a/src/core/instance/lifecycle.js +++ b/src/core/instance/lifecycle.js @@ -89,7 +89,7 @@ export function lifecycleMixin (Vue) { observerState.shouldConvert = false const propKeys = this.$options.propKeys for (let i = 0; i < propKeys.length; i++) { - let key = propKeys[i] + const key = propKeys[i] this[key] = validateProp(this, key, propsData) } observerState.shouldConvert = true diff --git a/src/core/instance/render.js b/src/core/instance/render.js index 4c6f7abc..7158995b 100644 --- a/src/core/instance/render.js +++ b/src/core/instance/render.js @@ -105,7 +105,7 @@ function resolveSlots (vm, renderChildren) { while (i--) { child = children[i] if ((name = child.data && child.data.slot)) { - let slot = (slots[name] || (slots[name] = [])) + const slot = (slots[name] || (slots[name] = [])) if (child.tag === 'template') { slot.push.apply(slot, child.children) } else { diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 9cfaf9f5..cee31234 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -34,7 +34,7 @@ function initProps (vm) { // root instance props should be converted observerState.shouldConvert = isRoot for (let i = 0; i < keys.length; i++) { - let key = keys[i] + const key = keys[i] defineReactive(vm, key, validateProp(vm, key, propsData)) } observerState.shouldConvert = true @@ -68,7 +68,7 @@ function noop () {} function initComputed (vm) { const computed = vm.$options.computed if (computed) { - for (let key in computed) { + for (const key in computed) { const userDef = computed[key] const def = { enumerable: true, @@ -110,7 +110,7 @@ function makeComputedGetter (getter, owner) { function initMethods (vm) { const methods = vm.$options.methods if (methods) { - for (let key in methods) { + for (const key in methods) { vm[key] = bind(methods[key], vm) } } @@ -119,8 +119,8 @@ function initMethods (vm) { function initWatch (vm) { const watch = vm.$options.watch if (watch) { - for (let key in watch) { - let handler = watch[key] + for (const key in watch) { + const handler = watch[key] if (isArray(handler)) { for (let i = 0; i < handler.length; i++) { createWatcher(vm, key, handler[i]) diff --git a/src/core/observer/index.js b/src/core/observer/index.js index c804e5c1..505ab53b 100644 --- a/src/core/observer/index.js +++ b/src/core/observer/index.js @@ -60,7 +60,7 @@ export function Observer (value) { */ Observer.prototype.walk = function (obj) { - for (let key in obj) { + for (const key in obj) { this.convert(key, obj[key]) } } diff --git a/src/core/observer/watcher.js b/src/core/observer/watcher.js index 0561a08b..d579c24e 100644 --- a/src/core/observer/watcher.js +++ b/src/core/observer/watcher.js @@ -246,13 +246,13 @@ Watcher.prototype.teardown = function () { const seenObjects = new Set() function traverse (val, seen) { - let i, keys, isA, isO + let i, keys if (!seen) { seen = seenObjects seen.clear() } - isA = isArray(val) - isO = isObject(val) + const isA = isArray(val) + const isO = isObject(val) if (isA || isO) { if (val.__ob__) { const depId = val.__ob__.dep.id diff --git a/src/core/util/options.js b/src/core/util/options.js index b0fcc645..e2f868b3 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -165,9 +165,9 @@ strats.watch = function (parentVal, childVal) { if (!parentVal) return childVal const ret = {} extend(ret, parentVal) - for (let key in childVal) { + for (const key in childVal) { let parent = ret[key] - let child = childVal[key] + const child = childVal[key] if (parent && !isArray(parent)) { parent = [parent] } @@ -214,7 +214,7 @@ function guardComponents (options) { if (options.components) { const components = options.components let def - for (let key in components) { + for (const key in components) { if (isBuiltInTag(key) || config.isReservedTag(key)) { process.env.NODE_ENV !== 'production' && warn( 'Do not use built-in or reserved HTML elements as component ' + @@ -254,7 +254,7 @@ function guardProps (options) { } } } else if (isPlainObject(props)) { - for (let key in props) { + for (const key in props) { val = props[key] name = camelize(key) res[name] = isPlainObject(val) @@ -268,7 +268,7 @@ function guardProps (options) { function guardDirectives (options) { const dirs = options.directives if (dirs) { - for (let key in dirs) { + for (const key in dirs) { if (typeof dirs[key] === 'function') { dirs[key] = { update: dirs[key] } } diff --git a/src/core/vdom/create-component.js b/src/core/vdom/create-component.js index d599a913..fb002b2d 100644 --- a/src/core/vdom/create-component.js +++ b/src/core/vdom/create-component.js @@ -152,8 +152,8 @@ function extractProps (data, Ctor) { if (!attrs && !props) { return res } - for (let key in propOptions) { - let altKey = hyphenate(key) + for (const key in propOptions) { + const altKey = hyphenate(key) checkProp(res, attrs, key, altKey) || checkProp(res, props, key, altKey) || checkProp(res, staticAttrs, key, altKey) @@ -178,9 +178,9 @@ function checkProp (res, hash, key, altKey) { function mergeHooks (data) { if (data.hook) { for (let i = 0; i < hooksToMerge.length; i++) { - let key = hooksToMerge[i] - let fromParent = data.hook[key] - let ours = hooks[key] + const key = hooksToMerge[i] + const fromParent = data.hook[key] + const ours = hooks[key] data.hook[key] = fromParent ? mergeHook(ours, fromParent) : ours } } else { diff --git a/src/core/vdom/helpers.js b/src/core/vdom/helpers.js index 16f74af4..c699eee6 100644 --- a/src/core/vdom/helpers.js +++ b/src/core/vdom/helpers.js @@ -8,9 +8,9 @@ export function flatten (children) { return [VNode(undefined, undefined, undefined, children)] } if (isArray(children)) { - let res = [] + const res = [] for (let i = 0, l = children.length; i < l; i++) { - let c = children[i] + const c = children[i] // flatten nested if (isArray(c)) { res.push.apply(res, flatten(c)) diff --git a/src/core/vdom/modules/directives.js b/src/core/vdom/modules/directives.js index 3a2ab613..629e1841 100644 --- a/src/core/vdom/modules/directives.js +++ b/src/core/vdom/modules/directives.js @@ -16,13 +16,13 @@ function applyDirectives (oldVnode, vnode, hook, update) { const dirs = vnode.data.directives if (dirs) { for (let i = 0; i < dirs.length; i++) { - let dir = dirs[i] - let def = resolveAsset(vnode.context.$options, 'directives', dir.name, true) - let fn = def && def[hook] + const dir = dirs[i] + const def = resolveAsset(vnode.context.$options, 'directives', dir.name, true) + const fn = def && def[hook] if (fn) { // only call update if value has changed if (update) { - let oldValue = oldVnode.data.directives[i].value + const oldValue = oldVnode.data.directives[i].value if (oldValue === dir.value) { continue } diff --git a/src/core/vdom/patch.js b/src/core/vdom/patch.js index 9115e636..864b488e 100644 --- a/src/core/vdom/patch.js +++ b/src/core/vdom/patch.js @@ -136,7 +136,7 @@ export function createPatchFunction (backend) { function removeVnodes (parentElm, vnodes, startIdx, endIdx) { for (; startIdx <= endIdx; ++startIdx) { - let ch = vnodes[startIdx] + const ch = vnodes[startIdx] if (isDef(ch)) { if (isDef(ch.tag)) { invokeDestroyHook(ch) @@ -150,7 +150,7 @@ export function createPatchFunction (backend) { function removeAndInvokeRemoveHook (vnode, rm) { if (rm || isDef(vnode.data)) { - let listeners = cbs.remove.length + 1 + const listeners = cbs.remove.length + 1 if (!rm) { // directly removing rm = createRmCb(vnode.elm, listeners) @@ -309,7 +309,7 @@ export function createPatchFunction (backend) { if (isDef(children)) { const childNodes = elm.childNodes for (let i = 0; i < children.length; i++) { - let success = hydrate(childNodes[i], children[i], insertedVnodeQueue) + const success = hydrate(childNodes[i], children[i], insertedVnodeQueue) if (!success) { return false } diff --git a/src/platforms/web/compiler/directives/model.js b/src/platforms/web/compiler/directives/model.js index 1386cb22..eaf81b02 100644 --- a/src/platforms/web/compiler/directives/model.js +++ b/src/platforms/web/compiler/directives/model.js @@ -77,7 +77,7 @@ const getSelectedValueCode = function patchChildOptions (el, fn) { for (let i = 0; i < el.children.length; i++) { - let c = el.children[i] + const c = el.children[i] if (c.tag === 'option') { addProp(c, 'selected', fn(getBindingAttr(c, 'value'))) } diff --git a/src/platforms/web/runtime/class-util.js b/src/platforms/web/runtime/class-util.js index 7dec5bee..42f220e8 100644 --- a/src/platforms/web/runtime/class-util.js +++ b/src/platforms/web/runtime/class-util.js @@ -36,7 +36,7 @@ export function addClass (el, cls) { el.classList.add(cls) } } else { - let cur = ' ' + getClass(el) + ' ' + const cur = ' ' + getClass(el) + ' ' if (cur.indexOf(' ' + cls + ' ') < 0) { setClass(el, (cur + cls).trim()) } @@ -59,7 +59,7 @@ export function removeClass (el, cls) { } } else { let cur = ' ' + getClass(el) + ' ' - let tar = ' ' + cls + ' ' + const tar = ' ' + cls + ' ' while (cur.indexOf(tar) >= 0) { cur = cur.replace(tar, ' ') } diff --git a/src/platforms/web/runtime/modules/attrs.js b/src/platforms/web/runtime/modules/attrs.js index 9596fa4c..c9d4e3ac 100644 --- a/src/platforms/web/runtime/modules/attrs.js +++ b/src/platforms/web/runtime/modules/attrs.js @@ -47,7 +47,7 @@ export default { create: function (_, vnode) { const attrs = vnode.data.staticAttrs if (attrs) { - for (let key in attrs) { + for (const key in attrs) { if (!vnode.elm) debugger setAttr(vnode.elm, key, attrs[key]) } diff --git a/src/platforms/web/runtime/modules/style.js b/src/platforms/web/runtime/modules/style.js index 9bc45781..86d43527 100644 --- a/src/platforms/web/runtime/modules/style.js +++ b/src/platforms/web/runtime/modules/style.js @@ -11,7 +11,7 @@ const normalize = cached(function (prop) { } const upper = prop.charAt(0).toUpperCase() + prop.slice(1) for (let i = 0; i < prefixes.length; i++) { - let prefixed = prefixes[i] + upper + const prefixed = prefixes[i] + upper if (prefixed in testEl.style) { return prefixed } diff --git a/src/platforms/web/server/modules/style.js b/src/platforms/web/server/modules/style.js index f85816cb..a5a41603 100644 --- a/src/platforms/web/server/modules/style.js +++ b/src/platforms/web/server/modules/style.js @@ -5,7 +5,7 @@ export default function renderStyle (node) { if (node.data.style || staticStyle) { const styles = node.data.style let res = ' style="' - for (let key in styles) { + for (const key in styles) { res += `${hyphenate(key)}:${styles[key]};` } return res + (staticStyle || '') + '"' diff --git a/src/platforms/web/util/class.js b/src/platforms/web/util/class.js index 82800d2e..d8cdb0f0 100644 --- a/src/platforms/web/util/class.js +++ b/src/platforms/web/util/class.js @@ -49,7 +49,7 @@ export function stringifyClass (value) { } if (isObject(value)) { let res = '' - for (let key in value) { + for (const key in value) { if (value[key]) res += key + ' ' } return res.slice(0, -1) diff --git a/src/server/render-starting-tag.js b/src/server/render-starting-tag.js index 0e192ccc..1f33e882 100644 --- a/src/server/render-starting-tag.js +++ b/src/server/render-starting-tag.js @@ -5,7 +5,7 @@ export function renderStartingTag (node, modules, directives) { const dirs = node.data.directives if (dirs) { for (let i = 0; i < dirs.length; i++) { - let dirRenderer = directives[dirs[i].name] + const dirRenderer = directives[dirs[i].name] if (dirRenderer) { // directives mutate the node's data // which then gets rendered by modules @@ -15,7 +15,7 @@ export function renderStartingTag (node, modules, directives) { } // apply other modules for (let i = 0; i < modules.length; i++) { - let res = modules[i](node) + const res = modules[i](node) if (res) { markup += res } diff --git a/src/shared/util.js b/src/shared/util.js index dd557242..4d9bf7d4 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -167,9 +167,9 @@ export function toArray (list, start) { * @param {Object} from */ -export function extend (to, from) { - for (let key in from) { - to[key] = from[key] +export function extend (to, _from) { + for (const key in _from) { + to[key] = _from[key] } return to }