You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2019/11/07 16:06:50 UTC

[cordova-common] branch master updated: feat: Replace addProperty with ES6 getters (#96)

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

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-common.git


The following commit(s) were added to refs/heads/master by this push:
     new 40cb6ce  feat: Replace addProperty with ES6 getters (#96)
40cb6ce is described below

commit 40cb6ceef90f4d04727094c429748044f1dc0b38
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Thu Nov 7 17:06:45 2019 +0100

    feat: Replace addProperty with ES6 getters (#96)
    
    * Replace addProperty with ES6 getters in main module
    
    This allows static code analysis tools to better understand the module
    dependencies. For example, you can use VS Code's code navigation feature
    to jump to the module definition.
    
    There's a slight difference in implementation, as the getters installed
    by addProperty would only call `require` on first invocation and then
    replace themselves with the actual module. But since require does
    caching itself, I don't see how that would make a big difference in
    terms of runtime.
    
    * Replace addProperty with ES6 getters in ConfigFile
    
    * Remove private module util/addProperty
---
 cordova-common.js               | 53 +++++++++++++++++++----------------------
 src/ConfigChanges/ConfigFile.js | 17 +++++++------
 src/util/addProperty.js         | 31 ------------------------
 3 files changed, 33 insertions(+), 68 deletions(-)

diff --git a/cordova-common.js b/cordova-common.js
index 801d510..3fe53c9 100644
--- a/cordova-common.js
+++ b/cordova-common.js
@@ -17,31 +17,28 @@
     under the License.
 */
 
-var addProperty = require('./src/util/addProperty');
-
-module.exports = { };
-
-addProperty(module, 'events', './src/events');
-addProperty(module, 'superspawn', './src/superspawn');
-
-addProperty(module, 'ActionStack', './src/ActionStack');
-addProperty(module, 'CordovaError', './src/CordovaError/CordovaError');
-addProperty(module, 'CordovaLogger', './src/CordovaLogger');
-addProperty(module, 'CordovaCheck', './src/CordovaCheck');
-addProperty(module, 'CordovaExternalToolErrorContext', './src/CordovaError/CordovaExternalToolErrorContext');
-addProperty(module, 'PlatformJson', './src/PlatformJson');
-addProperty(module, 'ConfigParser', './src/ConfigParser/ConfigParser');
-addProperty(module, 'FileUpdater', './src/FileUpdater');
-
-addProperty(module, 'PluginInfo', './src/PluginInfo/PluginInfo');
-addProperty(module, 'PluginInfoProvider', './src/PluginInfo/PluginInfoProvider');
-
-addProperty(module, 'PluginManager', './src/PluginManager');
-
-addProperty(module, 'ConfigChanges', './src/ConfigChanges/ConfigChanges');
-addProperty(module, 'ConfigKeeper', './src/ConfigChanges/ConfigKeeper');
-addProperty(module, 'ConfigFile', './src/ConfigChanges/ConfigFile');
-addProperty(module, 'mungeUtil', './src/ConfigChanges/munge-util');
-
-addProperty(module, 'xmlHelpers', './src/util/xml-helpers');
-
+module.exports = {
+    get events () { return require('./src/events'); },
+    get superspawn () { return require('./src/superspawn'); },
+
+    get ActionStack () { return require('./src/ActionStack'); },
+    get CordovaError () { return require('./src/CordovaError/CordovaError'); },
+    get CordovaLogger () { return require('./src/CordovaLogger'); },
+    get CordovaCheck () { return require('./src/CordovaCheck'); },
+    get CordovaExternalToolErrorContext () { return require('./src/CordovaError/CordovaExternalToolErrorContext'); },
+    get PlatformJson () { return require('./src/PlatformJson'); },
+    get ConfigParser () { return require('./src/ConfigParser/ConfigParser'); },
+    get FileUpdater () { return require('./src/FileUpdater'); },
+
+    get PluginInfo () { return require('./src/PluginInfo/PluginInfo'); },
+    get PluginInfoProvider () { return require('./src/PluginInfo/PluginInfoProvider'); },
+
+    get PluginManager () { return require('./src/PluginManager'); },
+
+    get ConfigChanges () { return require('./src/ConfigChanges/ConfigChanges'); },
+    get ConfigKeeper () { return require('./src/ConfigChanges/ConfigKeeper'); },
+    get ConfigFile () { return require('./src/ConfigChanges/ConfigFile'); },
+    get mungeUtil () { return require('./src/ConfigChanges/munge-util'); },
+
+    get xmlHelpers () { return require('./src/util/xml-helpers'); }
+};
diff --git a/src/ConfigChanges/ConfigFile.js b/src/ConfigChanges/ConfigFile.js
index b346978..626e410 100644
--- a/src/ConfigChanges/ConfigFile.js
+++ b/src/ConfigChanges/ConfigFile.js
@@ -17,17 +17,16 @@
 var fs = require('fs-extra');
 var path = require('path');
 
-var modules = {};
-var addProperty = require('../util/addProperty');
-
 // Use delay loading to ensure plist and other node modules to not get loaded
 // on Android, Windows platforms
-addProperty(module, 'bplist', 'bplist-parser', modules);
-addProperty(module, 'et', 'elementtree', modules);
-addProperty(module, 'glob', 'glob', modules);
-addProperty(module, 'plist', 'plist', modules);
-addProperty(module, 'plist_helpers', '../util/plist-helpers', modules);
-addProperty(module, 'xml_helpers', '../util/xml-helpers', modules);
+const modules = {
+    get bplist () { return require('bplist-parser'); },
+    get et () { return require('elementtree'); },
+    get glob () { return require('glob'); },
+    get plist () { return require('plist'); },
+    get plist_helpers () { return require('../util/plist-helpers'); },
+    get xml_helpers () { return require('../util/xml-helpers'); }
+};
 
 /******************************************************************************
 * ConfigFile class
diff --git a/src/util/addProperty.js b/src/util/addProperty.js
deleted file mode 100644
index 6e058d3..0000000
--- a/src/util/addProperty.js
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
-*/
-
-module.exports = function addProperty (module, property, modulePath, obj) {
-    obj = obj || module.exports;
-    // Add properties as getter to delay load the modules on first invocation
-    Object.defineProperty(obj, property, {
-        configurable: true,
-        get: function () {
-            var delayLoadedModule = module.require(modulePath);
-            obj[property] = delayLoadedModule;
-            return delayLoadedModule;
-        }
-    });
-};


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org