Bug 1350646: Part 21 - Remove SDK api-utils and dependent modules. r?Mossop draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 05 Aug 2017 22:54:47 -0700
changeset 641202 3fb2d28e6d8c8d2a0b0ce05b0c6ccb5df77b281e
parent 641201 631f69a287e93b1df7609acb0ce818116ab2f9f0
child 724746 b7363bbf69d9ff3043df34d476d62d76a0d23627
push id72469
push usermaglione.k@gmail.com
push dateSun, 06 Aug 2017 07:23:41 +0000
reviewersMossop
bugs1350646
milestone57.0a1
Bug 1350646: Part 21 - Remove SDK api-utils and dependent modules. r?Mossop MozReview-Commit-ID: L6tBQ3YdgEI
addon-sdk/moz.build
addon-sdk/source/lib/sdk/clipboard.js
addon-sdk/source/lib/sdk/deprecated/api-utils.js
addon-sdk/source/lib/sdk/util/contract.js
--- a/addon-sdk/moz.build
+++ b/addon-sdk/moz.build
@@ -34,17 +34,16 @@ modules = [
     'sdk/console/plain-text.js',
     'sdk/console/traceback.js',
     'sdk/core/disposable.js',
     'sdk/core/heritage.js',
     'sdk/core/namespace.js',
     'sdk/core/observer.js',
     'sdk/core/promise.js',
     'sdk/core/reference.js',
-    'sdk/deprecated/api-utils.js',
     'sdk/deprecated/unit-test-finder.js',
     'sdk/deprecated/unit-test.js',
     'sdk/deprecated/window-utils.js',
     'sdk/event/chrome.js',
     'sdk/event/core.js',
     'sdk/event/dom.js',
     'sdk/event/target.js',
     'sdk/event/utils.js',
@@ -80,17 +79,16 @@ modules = [
     'sdk/test/options.js',
     'sdk/test/utils.js',
     'sdk/timers.js',
     'sdk/uri/resource.js',
     'sdk/url.js',
     'sdk/url/utils.js',
     'sdk/util/array.js',
     'sdk/util/collection.js',
-    'sdk/util/contract.js',
     'sdk/util/deprecate.js',
     'sdk/util/dispatcher.js',
     'sdk/util/list.js',
     'sdk/util/object.js',
     'sdk/util/sequence.js',
     'sdk/util/uuid.js',
     'sdk/window/utils.js',
     'sdk/zip/utils.js',
--- a/addon-sdk/source/lib/sdk/clipboard.js
+++ b/addon-sdk/source/lib/sdk/clipboard.js
@@ -11,17 +11,16 @@ module.metadata = {
     "Firefox": "*",
     "SeaMonkey": "*",
     "Thunderbird": "*"
   }
 };
 
 const { Cc, Ci } = require("chrome");
 const { DataURL } = require("./url");
-const apiUtils = require("./deprecated/api-utils");
 /*
 While these data flavors resemble Internet media types, they do
 no directly map to them.
 */
 const kAllowableFlavors = [
   "text/unicode",
   "text/html",
   "image/png"
@@ -85,25 +84,16 @@ exports.set = function(aData, aDataType)
     catch (e) {
       // Ignore invalid URIs
       if (e.name !== "URIError") {
         throw e;
       }
     }
   }
 
-  options = apiUtils.validateOptions(options, {
-    data: {
-      is: ["string"]
-    },
-    datatype: {
-      is: ["string"]
-    }
-  });
-
   let flavor = fromJetpackFlavor(options.datatype);
 
   if (!flavor)
     throw new Error("Invalid flavor for " + options.datatype);
 
   // Additional checks for using the simple case
   if (flavor == "text/unicode") {
     clipboardHelper.copyString(options.data);
@@ -203,22 +193,16 @@ exports.get = function(aDataType) {
   // Figure out the best data type for the clipboard's data, if omitted
   if (!aDataType) {
     if (~currentFlavors().indexOf("image"))
       options.datatype = "image";
     else
       options.datatype = "text";
   }
 
-  options = apiUtils.validateOptions(options, {
-    datatype: {
-      is: ["string"]
-    }
-  });
-
   var xferable = Cc["@mozilla.org/widget/transferable;1"].
                  createInstance(Ci.nsITransferable);
   if (!xferable)
     throw new Error("Couldn't set the clipboard due to an internal error " +
                     "(couldn't create a Transferable object).");
   // Bug 769440: Starting with FF16, transferable have to be inited
   if ("init" in xferable)
     xferable.init(null);
deleted file mode 100644
--- a/addon-sdk/source/lib/sdk/deprecated/api-utils.js
+++ /dev/null
@@ -1,197 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-"use strict";
-
-module.metadata = {
-  "stability": "deprecated"
-};
-
-const { merge } = require("../util/object");
-const { union } = require("../util/array");
-const { isNil, isRegExp } = require("../lang/type");
-
-// The possible return values of getTypeOf.
-const VALID_TYPES = [
-  "array",
-  "boolean",
-  "function",
-  "null",
-  "number",
-  "object",
-  "string",
-  "undefined",
-  "regexp"
-];
-
-const { isArray } = Array;
-
-/**
- * Returns a validated options dictionary given some requirements.  If any of
- * the requirements are not met, an exception is thrown.
- *
- * @param  options
- *         An object, the options dictionary to validate.  It's not modified.
- *         If it's null or otherwise falsey, an empty object is assumed.
- * @param  requirements
- *         An object whose keys are the expected keys in options.  Any key in
- *         options that is not present in requirements is ignored.  Each value
- *         in requirements is itself an object describing the requirements of
- *         its key.  There are four optional keys in this object:
- *           map: A function that's passed the value of the key in options.
- *                map's return value is taken as the key's value in the final
- *                validated options, is, and ok.  If map throws an exception
- *                it's caught and discarded, and the key's value is its value in
- *                options.
- *           is:  An array containing any number of the typeof type names.  If
- *                the key's value is none of these types, it fails validation.
- *                Arrays, null and regexps are identified by the special type names
- *                "array", "null", "regexp"; "object" will not match either.  No type
- *                coercion is done.
- *           ok:  A function that's passed the key's value.  If it returns
- *                false, the value fails validation.
- *           msg: If the key's value fails validation, an exception is thrown.
- *                This string will be used as its message.  If undefined, a
- *                generic message is used, unless is is defined, in which case
- *                the message will state that the value needs to be one of the
- *                given types.
- * @return An object whose keys are those keys in requirements that are also in
- *         options and whose values are the corresponding return values of map
- *         or the corresponding values in options.  Note that any keys not
- *         shared by both requirements and options are not in the returned
- *         object.
- */
-exports.validateOptions = function validateOptions(options, requirements) {
-  options = options || {};
-  let validatedOptions = {};
-
-  for (let key in requirements) {
-    let isOptional = false;
-    let mapThrew = false;
-    let req = requirements[key];
-    let [optsVal, keyInOpts] = (key in options) ?
-                               [options[key], true] :
-                               [undefined, false];
-    if (req.map) {
-      try {
-        optsVal = req.map(optsVal);
-      }
-      catch (err) {
-        if (err instanceof RequirementError)
-          throw err;
-
-        mapThrew = true;
-      }
-    }
-    if (req.is) {
-      let types = req.is;
-
-      if (!isArray(types) && isArray(types.is))
-        types = types.is;
-
-      if (isArray(types)) {
-        isOptional = ['undefined', 'null'].every(v => ~types.indexOf(v));
-
-        // Sanity check the caller's type names.
-        types.forEach(function (typ) {
-          if (VALID_TYPES.indexOf(typ) < 0) {
-            let msg = 'Internal error: invalid requirement type "' + typ + '".';
-            throw new Error(msg);
-          }
-        });
-        if (types.indexOf(getTypeOf(optsVal)) < 0)
-          throw new RequirementError(key, req);
-      }
-    }
-
-    if (req.ok && ((!isOptional || !isNil(optsVal)) && !req.ok(optsVal)))
-      throw new RequirementError(key, req);
-
-    if (keyInOpts || (req.map && !mapThrew && optsVal !== undefined))
-      validatedOptions[key] = optsVal;
-  }
-
-  return validatedOptions;
-};
-
-exports.addIterator = function addIterator(obj, keysValsGenerator) {
-  obj.__iterator__ = function(keysOnly, keysVals) {
-    let keysValsIterator = keysValsGenerator.call(this);
-
-    // "for (.. in ..)" gets only keys, "for each (.. in ..)" gets values,
-    // and "for (.. in Iterator(..))" gets [key, value] pairs.
-    let index = keysOnly ? 0 : 1;
-    while (true)
-      yield keysVals ? keysValsIterator.next() : keysValsIterator.next()[index];
-  };
-};
-
-// Similar to typeof, except arrays, null and regexps are identified by "array" and
-// "null" and "regexp", not "object".
-var getTypeOf = exports.getTypeOf = function getTypeOf(val) {
-  let typ = typeof(val);
-  if (typ === "object") {
-    if (!val)
-      return "null";
-    if (isArray(val))
-      return "array";
-    if (isRegExp(val))
-      return "regexp";
-  }
-  return typ;
-}
-
-function RequirementError(key, requirement) {
-  Error.call(this);
-
-  this.name = "RequirementError";
-
-  let msg = requirement.msg;
-  if (!msg) {
-    msg = 'The option "' + key + '" ';
-    msg += requirement.is ?
-           "must be one of the following types: " + requirement.is.join(", ") :
-           "is invalid.";
-  }
-
-  this.message = msg;
-}
-RequirementError.prototype = Object.create(Error.prototype);
-
-var string = { is: ['string', 'undefined', 'null'] };
-exports.string = string;
-
-var number = { is: ['number', 'undefined', 'null'] };
-exports.number = number;
-
-var boolean = { is: ['boolean', 'undefined', 'null'] };
-exports.boolean = boolean;
-
-var object = { is: ['object', 'undefined', 'null'] };
-exports.object = object;
-
-var array = { is: ['array', 'undefined', 'null'] };
-exports.array = array;
-
-var isTruthyType = type => !(type === 'undefined' || type === 'null');
-var findTypes = v => { while (!isArray(v) && v.is) v = v.is; return v };
-
-function required(req) {
-  let types = (findTypes(req) || VALID_TYPES).filter(isTruthyType);
-
-  return merge({}, req, {is: types});
-}
-exports.required = required;
-
-function optional(req) {
-  req = merge({is: []}, req);
-  req.is = findTypes(req).filter(isTruthyType).concat('undefined', 'null');
-
-  return req;
-}
-exports.optional = optional;
-
-function either(...types) {
-  return union.apply(null, types.map(findTypes));
-}
-exports.either = either;
deleted file mode 100644
--- a/addon-sdk/source/lib/sdk/util/contract.js
+++ /dev/null
@@ -1,55 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-"use strict";
-
-module.metadata = {
-  "stability": "unstable"
-};
-
-const { validateOptions: valid } = require("../deprecated/api-utils");
-const method = require("method/core");
-
-// Function takes property validation rules and returns function that given
-// an `options` object will return validated / normalized options back. If
-// option(s) are invalid validator will throw exception described by rules.
-// Returned will also have contain `rules` property with a given validation
-// rules and `properties` function that can be used to generate validated
-// property getter and setters can be mixed into prototype. For more details
-// see `properties` function below.
-function contract(rules) {
-  const validator = (instance, options) => {
-    return valid(options || instance || {}, rules);
-  };
-  validator.rules = rules
-  validator.properties = function(modelFor) {
-    return properties(modelFor, rules);
-  }
-  return validator;
-}
-exports.contract = contract
-
-// Function takes `modelFor` instance state model accessor functions and
-// a property validation rules and generates object with getters and setters
-// that can be mixed into prototype. Property accessors update model for the
-// given instance. If you wish to react to property updates you can always
-// override setters to put specific logic.
-function properties(modelFor, rules) {
-  let descriptor = Object.keys(rules).reduce(function(descriptor, name) {
-    descriptor[name] = {
-      get: function() { return modelFor(this)[name] },
-      set: function(value) {
-        let change = {};
-        change[name] = value;
-        modelFor(this)[name] = valid(change, rules)[name];
-      }
-    }
-    return descriptor
-  }, {});
-  return Object.create(Object.prototype, descriptor);
-}
-exports.properties = properties;
-
-const validate = method("contract/validate");
-exports.validate = validate;