瀏覽代碼

refactor: warn about wrong usage in dev mode

tophf 3 年之前
父節點
當前提交
ee4cd2176b
共有 3 個文件被更改,包括 14 次插入1 次删除
  1. 1 1
      scripts/webpack.conf.js
  2. 10 0
      src/common/index.js
  3. 3 0
      src/common/storage.js

+ 1 - 1
scripts/webpack.conf.js

@@ -86,7 +86,7 @@ const defsObj = {
   'process.env.HANDSHAKE_ID': HANDSHAKE_ID,
   'process.env.HANDSHAKE_ACK': '1',
   'process.env.CODEMIRROR_THEMES': JSON.stringify(getCodeMirrorThemes()),
-  'process.env.DEV': JSON.stringify(process.env.NODE_ENV === 'development'),
+  'process.env.DEV': JSON.stringify(!isProd),
 };
 // avoid running webpack bootstrap in a potentially hacked environment
 // after documentElement was replaced which triggered reinjection of content scripts

+ 10 - 0
src/common/index.js

@@ -7,6 +7,16 @@ import { blob2base64, i18n, isDataUri, noop } from './util';
 export { normalizeKeys } from './object';
 export * from './util';
 
+if (process.env.DEV && process.env.IS_INJECTED !== 'injected-web') {
+  const get = () => {
+    throw 'Do not use `for-of` with Map/Set. Use forEach or for-of with a [...copy]'
+    + '\n(not supported due to our config of @babel/plugin-transform-for-of).';
+  };
+  for (const obj of [Map, Set, WeakMap, WeakSet]) {
+    Object.defineProperty(obj.prototype, 'length', { get, configurable: true });
+  }
+}
+
 export const defaultImage = '/public/images/icon128.png';
 
 export function initHooks() {

+ 3 - 0
src/common/storage.js

@@ -56,6 +56,9 @@ class StorageArea {
    * @return {Promise<Object>} same object
    */
   async set(data) {
+    if (process.env.DEV && (!data || typeof data !== 'object')) {
+      throw 'StorageArea.set: data is not an object';
+    }
     await api.set(this.prefix
       ? data::mapEntry(null, this.toKey, this)
       : data);