Browse Source

prefer const

Evan You 10 years ago
parent
commit
3aa7503472

+ 1 - 0
.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,

+ 4 - 4
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)

+ 3 - 3
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 + '}'

+ 1 - 1
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

+ 1 - 1
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
   }

+ 1 - 2
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
     }

+ 1 - 1
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

+ 1 - 1
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 {

+ 5 - 5
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])

+ 1 - 1
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])
   }
 }

+ 3 - 3
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

+ 5 - 5
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] }
       }

+ 5 - 5
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 {

+ 2 - 2
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))

+ 4 - 4
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
           }

+ 3 - 3
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
           }

+ 1 - 1
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')))
     }

+ 2 - 2
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, ' ')
     }

+ 1 - 1
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])
       }

+ 1 - 1
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
     }

+ 1 - 1
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 || '') + '"'

+ 1 - 1
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)

+ 2 - 2
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
       }

+ 3 - 3
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
 }