Question

I have a collection of methods which I'm calling through a variable name like so:

var some = factory[some_method](a, b, c);

My problem is that some of the methods I'm calling need a, b, c while some only need a or a, b. No problem for me, but JSLINT complains about unused a/b.

Question:
How can I keep my variables although being unused on a method specific scope? I don't want to set global unused:true? Can it be set locally for a single method, too?

Thanks!

EDIT:
I'm calling the above method like this:

// content_dict = determines which method to call (along with properties being set)
app.setContent = function (content_dict, url_dict, create, purge) {
    var i, skip, container, target, pass, spec, dependency;

    spec = url_dict || {};

    switch (content_dict.generate) {

    case "widget":
      return factory[content_dict.type](content_dict, spec, create);
    ...

Examples of content_dict and method call:

  //      {
  //        "generate": "widget",
  //        "type": "navbar",
  //        "property_dict": {
  //           "foo":"bar"
  //        }
  //      }
  factory.navbar = function (content_dict) {
    var navbar, controls, i, element, target, order;

    if (content_dict === undefined) {
      util.error({
        "error": "Generate Navbar: Missing Configuration"
      });
    } else {

      // navbar
      navbar = factory.element(
        "div",
        {"className": "navbar ui-navbar " + (content_dict.class_list || "")},
        {
          "data-role": "navbar",
          "role": "navigation",
          "data-enhanced": "true"
        },
        {"data-reference": content_dict.reference || null}
      );

      // controls
      controls = factory.element(
        "ul",
       {
          "className": "ui-grid-" +
            util.toLetter(content_dict.children.length - 1).toLowerCase()
        }
      );

      // children
      if (spec.children) {
        for (i = 0; i < content_dict.children.length; i += 1) {
          element = content_dict.children[i];
          target = factory.element(
            "li",
            {"className": "ui-block-" + util.toLetter(i + 1).toLowerCase()}
          );

          // class string
          // TODO: needed?
          if ((element.type !== "input" && element.type !== "select") &&
              element.direct) {
            order = i === 0 ? " ui-first-child " :
                (i === (content_dict.length - 1) ? " ui-last-child " : " ");

            element.direct.className = (element.direct.className || "") +
                order + (element.type === "a" ? (" ui-btn ui-shadow " +
                    factory.generateIconClassString(element)) : " ");
          }

          target.appendChild(app.setContent(element));
          controls.appendChild(target);
        }
      }

      // assemble
      navbar.appendChild(controls);

      return navbar;
    }
  };

So, the method above does not require the url_dict to be set, while this one does:

  //      {
  //        "generate": "widget",
  //        "type": "page",
  //        "property_dict": {
  //           "foo":"bar"
  //        }
  //      }
  factory.page = function (content_dict, url_dict, create) {
    var i, j, last, wrapper, split_url, promises, container, target,
      view, render;

    container = document.getElementById(url_dict.id);
    target = document.createDocumentFragment();
    view = (url_dict && url_dict.mode) ? url_dict.mode : "default";
    render = content_dict.layout[view];

    if (render) {
      promises = [];
      for (i = 0; i < render.length; i += 1) {
        promises[i] = app.setContent(render[i], url_dict, create);
      }
    } else {
      util.error({"error": "factory.page: Missing view"});
    }

    return RSVP.all(promises)
     .then(function (promises) {
        for (j = 0; j < promises.length; j += 1) {
         target.appendChild(promises[j]);
        }

        if (container === null || create === true) {
          last = document.body.lastChild;
          wrapper = factory.element("div", {"className": "ui-content"}, {});
          wrapper.setAttribute("data-bound", true);
          split_url = url_dict.url.split("#");

          container = factory.element(
           "div",
           {
             "id": url_dict.id,
             "className": " ui-page " + ("ui-page-theme-" +
                content_dict.theme || "") + " " + ((content_dict.fix &&
                    content_dict.fix === false) ? "" :
                        " ui-page-header-fixed ui-page-footer-fixed")
           },
           {
             "data-module": url_dict.id,
             "data-role": "page",
             "data-url": split_url[1] ? split_url[1].split("?")[0] :
                split_url[0],
             "data-external-page": true,
             "tabindex": 0,
             "data-enhanced": true
          }
        );

        wrapper.appendChild(app.breadcrumb(url_dict));
        wrapper.appendChild(target);
        container.appendChild(wrapper);

        if (util.testForString("ui-footer", last.className)) {
          document.body.insertBefore(container, last);
        } else {
          document.body.appendChild(container);
        }

        $(document).enhanceWithin();

        // trigger new transition to this page
        $.mobile.changePage("#" + url_dict.id);

      } else {
        factory.util.updatePageSection(url_dict.id, target);

        // also changePage in case we are not on the active
        // page (this will only happen deeplink > home
        if (url_dict.id !== util.getActivePage()) {
          $.mobile.changePage("#" + url_dict.id);
        }
      }

      // update header
      if (create !== false) {
        app.setPageTitle(
          content_dict.title_i18n,
          target.querySelectorAll("div.ui-header")
        );
      }
    })
    .fail(util.error);
};

Two examples for sake of completeness. I guess I could shuffle my parameters around and pass the url_dict last, which means quite a lot of changes in my method calls. So, still question remains Is there a way to set a JSLINT rule on a per-method basis?

Thanks.

Was it helpful?

Solution

You can always turn unparam on and off as you'd like.

/*jslint white:true, browser:true, sloppy: true */
function test1(p1)  {
    // param check on, no error
    window.alert(p1); 
}

/*jslint unparam: true */
function test2(p1, p2, p3)  {
    // param check off temporarily, two unused, no error
    window.alert(p2);
}
/*jslint unparam: false */

function test3(p1, p2, p3)  {
    // param check back on, one unused, error reported.
    window.alert(p3 + p2);
}

Though unless you're worried about catching unused params elsewhere, it might be best just to put unparam in your first jslint line.

/*jslint white:true, browser:true, sloppy: true, unparam:true */

Just remember that if /*jslint unparam: false */ is anywhere in the file, it gets turned back on for the balance of the file. The first jslint setting isn't global or anything like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top