You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by do...@apache.org on 2019/03/13 03:02:37 UTC

[incubator-weex] branch master updated: [jsfm] add type check for register modules and components

This is an automated email from the ASF dual-hosted git repository.

dongyayun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new 16d0cf5  [jsfm] add type check for register modules and components
     new 826a681  Merge pull request #2206 from Hanks10100/jsfm-type-check
16d0cf5 is described below

commit 16d0cf5a2b4708615f32c1af156e570cfff3deeb
Author: Hanks <zh...@gmail.com>
AuthorDate: Wed Mar 6 17:29:20 2019 +0800

    [jsfm] add type check for register modules and components
---
 runtime/api/component.js                  |  3 ++-
 runtime/api/module.js                     | 24 ++++++++++++++----------
 runtime/frameworks/legacy/app/register.js | 20 +++++++++++---------
 runtime/shared/utils.js                   |  8 ++++++++
 runtime/vdom/WeexElement.js               |  2 +-
 5 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/runtime/api/component.js b/runtime/api/component.js
index 8a56961..ae03a31 100644
--- a/runtime/api/component.js
+++ b/runtime/api/component.js
@@ -17,6 +17,7 @@
  * under the License.
  */
 
+import { isPlainObject } from '../shared/utils'
 import { registerElement } from '../vdom/WeexElement'
 
 const weexComponents = {}
@@ -34,7 +35,7 @@ export function registerComponents (newComponents) {
       if (typeof component === 'string') {
         weexComponents[component] = true
       }
-      else if (typeof component === 'object' && typeof component.type === 'string') {
+      else if (isPlainObject(component) && typeof component.type === 'string') {
         weexComponents[component.type] = component
         registerElement(component.type, component.methods)
       }
diff --git a/runtime/api/module.js b/runtime/api/module.js
index df26b92..f3c2d1d 100644
--- a/runtime/api/module.js
+++ b/runtime/api/module.js
@@ -17,25 +17,29 @@
  * under the License.
  */
 
+import { hasOwn, isPlainObject } from '../shared/utils'
+
 const weexModules = {}
 
 /**
  * Register native modules information.
  * @param {object} newModules
  */
-export function registerModules (newModules) {
+export function registerModules (newModules = {}) {
   for (const name in newModules) {
-    if (!weexModules[name]) {
+    if (!hasOwn(weexModules, name)) {
       weexModules[name] = {}
     }
-    newModules[name].forEach(method => {
-      if (typeof method === 'string') {
-        weexModules[name][method] = true
-      }
-      else {
-        weexModules[name][method.name] = method.args
-      }
-    })
+    if (Array.isArray(newModules[name])) {
+      newModules[name].forEach(method => {
+        if (typeof method === 'string') {
+          weexModules[name][method] = true
+        }
+        else if (isPlainObject(method) && typeof method.name === 'string') {
+          weexModules[name][method.name] = method.args || []
+        }
+      })
+    }
   }
 }
 
diff --git a/runtime/frameworks/legacy/app/register.js b/runtime/frameworks/legacy/app/register.js
index bd0302e..ca3d0b8 100644
--- a/runtime/frameworks/legacy/app/register.js
+++ b/runtime/frameworks/legacy/app/register.js
@@ -50,17 +50,19 @@ export function initModules (modules, ifReplace) {
     }
 
     // push each non-existed new method
-    modules[moduleName].forEach(function (method) {
-      if (typeof method === 'string') {
-        method = {
-          name: method
+    if (Array.isArray(modules[moduleName])) {
+      modules[moduleName].forEach(function (method) {
+        if (typeof method === 'string') {
+          method = {
+            name: method
+          }
         }
-      }
 
-      if (!methods[method.name] || ifReplace) {
-        methods[method.name] = method
-      }
-    })
+        if (!methods[method.name] || ifReplace) {
+          methods[method.name] = method
+        }
+      })
+    }
   }
 }
 
diff --git a/runtime/shared/utils.js b/runtime/shared/utils.js
index 6dd42d1..595e2fd 100644
--- a/runtime/shared/utils.js
+++ b/runtime/shared/utils.js
@@ -30,6 +30,14 @@ export function typof (v) {
   return s.substring(8, s.length - 1)
 }
 
+export function isPlainObject (value) {
+  return Object.prototype.toString.call(value) === '[object Object]'
+}
+
+export function hasOwn (object, key) {
+  return isPlainObject(object) && Object.prototype.hasOwnProperty.call(object, key)
+}
+
 export function bufferToBase64 (buffer) {
   if (typeof btoa !== 'function') {
     return ''
diff --git a/runtime/vdom/WeexElement.js b/runtime/vdom/WeexElement.js
index 455e69e..f1f5aeb 100644
--- a/runtime/vdom/WeexElement.js
+++ b/runtime/vdom/WeexElement.js
@@ -37,7 +37,7 @@ const registeredElements = {}
  */
 export function registerElement (type, methods) {
   // Skip when no special component methods.
-  if (!methods || !methods.length) {
+  if (!Array.isArray(methods) || !methods.length) {
     return
   }