소스 검색

strict mode

Evan You 10 년 전
부모
커밋
17e4b33b7a
3개의 변경된 파일80개의 추가작업 그리고 55개의 파일을 삭제
  1. 55 55
      src/util/options.js
  2. 2 0
      test/unit/lib/util.js
  3. 23 0
      test/unit/specs/misc_spec.js

+ 55 - 55
src/util/options.js

@@ -206,61 +206,6 @@ var defaultStrat = function (parentVal, childVal) {
     : childVal
 }
 
-/**
- * Merge two option objects into a new one.
- * Core utility used in both instantiation and inheritance.
- *
- * @param {Object} parent
- * @param {Object} child
- * @param {Vue} [vm] - if vm is present, indicates this is
- *                     an instantiation merge.
- */
-
-exports.mergeOptions = function merge (parent, child, vm) {
-  guardComponents(child)
-  guardProps(child)
-  var options = {}
-  var key
-  if (child.mixins) {
-    for (var i = 0, l = child.mixins.length; i < l; i++) {
-      parent = merge(parent, child.mixins[i], vm)
-    }
-  }
-  for (key in parent) {
-    mergeField(key)
-  }
-  for (key in child) {
-    if (!(parent.hasOwnProperty(key))) {
-      mergeField(key)
-    }
-  }
-  function mergeField (key) {
-    var strat = strats[key] || defaultStrat
-    options[key] = strat(parent[key], child[key], vm, key)
-  }
-  return options
-}
-
-/**
- * Resolve an asset.
- * This function is used because child instances need access
- * to assets defined in its ancestor chain.
- *
- * @param {Object} options
- * @param {String} type
- * @param {String} id
- * @return {Object|Function}
- */
-
-exports.resolveAsset = function resolve (options, type, id) {
-  var asset = options[type][id]
-  while (!asset && options._parent) {
-    options = options._parent.$options
-    asset = options[type][id]
-  }
-  return asset
-}
-
 /**
  * Make sure component options get converted to actual
  * constructors.
@@ -346,3 +291,58 @@ function guardArrayAssets (assets) {
   }
   return assets
 }
+
+/**
+ * Merge two option objects into a new one.
+ * Core utility used in both instantiation and inheritance.
+ *
+ * @param {Object} parent
+ * @param {Object} child
+ * @param {Vue} [vm] - if vm is present, indicates this is
+ *                     an instantiation merge.
+ */
+
+exports.mergeOptions = function merge (parent, child, vm) {
+  guardComponents(child)
+  guardProps(child)
+  var options = {}
+  var key
+  if (child.mixins) {
+    for (var i = 0, l = child.mixins.length; i < l; i++) {
+      parent = merge(parent, child.mixins[i], vm)
+    }
+  }
+  for (key in parent) {
+    mergeField(key)
+  }
+  for (key in child) {
+    if (!(parent.hasOwnProperty(key))) {
+      mergeField(key)
+    }
+  }
+  function mergeField (key) {
+    var strat = strats[key] || defaultStrat
+    options[key] = strat(parent[key], child[key], vm, key)
+  }
+  return options
+}
+
+/**
+ * Resolve an asset.
+ * This function is used because child instances need access
+ * to assets defined in its ancestor chain.
+ *
+ * @param {Object} options
+ * @param {String} type
+ * @param {String} id
+ * @return {Object|Function}
+ */
+
+exports.resolveAsset = function resolve (options, type, id) {
+  var asset = options[type][id]
+  while (!config.strict && !asset && options._parent) {
+    options = options._parent.$options
+    asset = options[type][id]
+  }
+  return asset
+}

+ 2 - 0
test/unit/lib/util.js

@@ -11,6 +11,8 @@ scope.hasWarned = function (_, msg) {
     }
   }
 
+  console.warn('[test] "' + msg + '" was never warned.')
+
   function containsMsg (arg) {
     return arg.indexOf(msg) > -1
   }

+ 23 - 0
test/unit/specs/misc_spec.js

@@ -1,8 +1,13 @@
 // test cases for edge cases & bug fixes
 var Vue = require('../../../src/vue')
+var _ = require('../../../src/util/debug')
 
 describe('Misc', function () {
 
+  beforeEach(function () {
+    spyOn(_, 'warn')
+  })
+
   it('should handle directive.bind() altering its childNode structure', function () {
     var vm = new Vue({
       el: document.createElement('div'),
@@ -221,4 +226,22 @@ describe('Misc', function () {
     })
   })
 
+  it('strict mode', function () {
+    Vue.config.strict = true
+    new Vue({
+      el: document.createElement('div'),
+      template: '<test></test>',
+      components: {
+        test: {
+          template: '<div v-strict>hi</div>'
+        }
+      },
+      directives: {
+        strict: function () {}
+      }
+    })
+    expect(hasWarned(_, 'Failed to resolve directive: strict')).toBe(true)
+    Vue.config.strict = false
+  })
+
 })