Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript file ends up malformed after running precise-commits #31

Open
andyexeter opened this issue Feb 23, 2019 · 1 comment
Open

Comments

@andyexeter
Copy link

When I enter the following code into the Prettier playground it formats correctly:

Click to expand
"use strict";

var PLUGIN_NAME = "groupRequiredTest";

Plugin.prototype = {
    /**
     * Returns the plugin instance's options object.
     *
     * @returns {object}
     */
    getOptions: function () {
        return this.options;
    },

    /**
     * Destroys the plugin instance.
     *
     * @returns {jQuery}
     */
    destroy: function () {
        // Reset each element's 'required' attribute.
        this.$els.each(function () {
            var origRequired = $(this).data("origRequired." + PLUGIN_NAME);

            if (origRequired) {
                $(this).attr("required", origRequired);
            } else {
                $(this).removeAttr("required");
            }
        });

        // Remove all events and data added by the plugin.
        return this.$els
            .off("." + this.options.namespace)
            .removeData([
                PLUGIN_NAME + ".plugin",
                "origRequired." + PLUGIN_NAME
            ]);
    }
};

/**
 *
 * @param {jQuery} $elements
 * @param {Object} options
 * @constructor
 */
function Plugin($elements, options) {
    this.$els = $elements;
    this.options = $.extend({}, $.fn[PLUGIN_NAME].defaults, options);

    var _this = this;

    this.$els
        .each(function () {
            $(this).data(
                "origRequired." + PLUGIN_NAME,
                $(this).attr("required")
            );
            setRequired.call(_this, $(this));
        })
        .on("input." + this.options.namespace + " change." +
            this.options.namespace,
            function (event) {
                setRequired.call(_this, $(this), event);
            }
        )
        .on("invalid." + this.options.namespace, function (event) {
            var errorMessage = _this.options.errorMessage;

            if ($.isFunction(errorMessage)) {
                errorMessage = errorMessage.call(this, _this, event);
            }

            this.setCustomValidity(errorMessage);
        });
}

/**
 * Sets the required property of all other elements in the group based on the value of the given
 * element and the custom required filter function.
 *
 * Used as a handler for the 'input' event AND to initialise the plugin.
 *
 * @param {jQuery} $element
 * @param {jQuery.Event} [event]
 */
function setRequired($element, event) {
    /* jshint validthis: true */

    var required = true;

    this.$els.each(function () {
        this.setCustomValidity("");
        required = required && !this.checkValidity();
    });

    if (this.options.requiredFilter) {
        required = this.options.requiredFilter.call(
            $element,
            required,
            this,
            event
        );
    }

    this.$els.prop("required", required);
}

$.fn[PLUGIN_NAME] = function (options) {
    var plugin = this.data(PLUGIN_NAME + ".plugin");

    if (!plugin) {
        plugin = new Plugin(this, options);
        this.data(PLUGIN_NAME + ".plugin", plugin);
    }

    if ($.isFunction(Plugin.prototype[options])) {
        return plugin[options].apply(
            plugin,
            Array.prototype.slice.call(arguments, 1)
        );
    }

    return this;
};

$.fn[PLUGIN_NAME].defaults = {
    namespace: "groupRequired",
    requiredFilter: null,
    errorMessage: ""
};

However, when I run precise-commits it ends up malformed:

Click to expand
"use strict";

var PLUGIN_NAME = "groupRequired";

Plugin.prototype = {
/**
 * Returns the plugin instance's options object.
      * @returns {object}
 */
  getOptions: function() {   return this.options;
},

 /*
 * Destroys the plugin instance.
 *
  * @returns {jQuery}
   *
  destroy: function() {    // Reset each element's 'required' attribute.
   this.$els.each(function() {      var origRequired = $(this).data(
          "origRequired." + PLUGIN_NAME
         );

 (origRequired) {
 $(this).attr("required", origRequired);
} else {
$(this).removeAttr("required");
    ;

// Remove all events and data added by the plugin.
return this.$els
   .off("." + this.options.namespace)
.removeData([PLUGIN_NAME + ".plugin", "origRequired." + PLUGIN_NAME]);
  }
};

/**
 *
 * @param {jQuery} $elements
 * @param {Object} options
 * @constructor
 */
function Plugin($elements, options) {
    this.$els = $elements;
    this.options = $.extend({}, $.fn[PLUGIN_NAME].defaults, options);

    var _this = this;

    this.$els
      .each(function() {      $(this).data(
"origRequired." + PLUGIN_NAME,
          $(this).attr("required")
        setRequired.call(_this, $(this));
      })
      .on(
    "input." +
      this.options.namespace +
       " change." +
      this.options.namespace,
    functi(event) {
          setRequired.call(_this, $(this), event);
      }
    )     .on("invalid." + this.options.namespace, function(event) {
var errorMessage = _this.options.errorMessage;

        if ($.isFunction(errorMessage)) {
    errorMessage = errorMessage.call(this, _this, event);
  }
     this.setCustomValidity(errorMessage);
    });
}

/**
 * Sets the required property of all other elements in the group based on the value of the given
 * element and the custom required filter function.
 *
 * Used as a handler for the 'input' event AND to initialise the plugin.
 *
 * @param {jQuery} $element
 * @param {jQuery.Event} [event]
 */
function setRequired($element, event) {
    /* jshint validthis: true */

    var required = true;

    this.$els.each(function() {
      this.setCustomValidity("");
      required = required && !this.checkValidity();
    });

    if (this.options.requiredFilter) {
        required = this.options.requiredFilter.call(
            $element,
            required,
            this,
            event
        );
    }

    this.$els.prop("required", required);
}

$.fn[PLUGIN_NAME] = function(options) {
  var plugin = this.data(PLUGIN_NAME + ".plugin");

  if (!plugin) {
    plugin = new Plugin(this, options);
    this.data(PLUGIN_NAME + ".plugin", plugin);
  }

  if ($.isFunction(Plugin.prototype[options])) {
    return plugin[options].apply(
      plugin,
      Array.prototype.slice.call(arguments, 1)
    );
  }

  return this;
};

$.fn[PLUGIN_NAME].defaults = {
    namespace: "groupRequired",
    requiredFilter: null,
    errorMessage: ""
};

I'm aware precise-commits runs on changed lines, so the diff for the file is as follows:
diff --git a/src/plugin.js b/src/plugin.js
index 8c39844..1a37eb4 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -8,7 +8,7 @@ Plugin.prototype = {
      *
      * @returns {object}
      */
-    getOptions: function() {
+    getOptions: function () {
         return this.options;
     },
 
@@ -17,9 +17,9 @@ Plugin.prototype = {
      *
      * @returns {jQuery}
      */
-    destroy: function() {
+    destroy: function () {
         // Reset each element's 'required' attribute.
-        this.$els.each(function() {
+        this.$els.each(function () {
             var origRequired = $(this).data("origRequired." + PLUGIN_NAME);
 
             if (origRequired) {
@@ -52,7 +52,7 @@ function Plugin($elements, options) {
     var _this = this;
 
     this.$els
-        .each(function() {
+        .each(function () {
             $(this).data(
                 "origRequired." + PLUGIN_NAME,
                 $(this).attr("required")
@@ -61,14 +61,14 @@ function Plugin($elements, options) {
         })
         .on(
             "input." +
-                this.options.namespace +
-                " change." +
-                this.options.namespace,
-            function(event) {
+            this.options.namespace +
+            " change." +
+            this.options.namespace,
+            function (event) {
                 setRequired.call(_this, $(this), event);
             }
         )
-        .on("invalid." + this.options.namespace, function(event) {
+        .on("invalid." + this.options.namespace, function (event) {
             var errorMessage = _this.options.errorMessage;
 
             if ($.isFunction(errorMessage)) {
@@ -93,7 +93,7 @@ function setRequired($element, event) {
 
     var required = true;
 
-    this.$els.each(function() {
+    this.$els.each(function () {
         this.setCustomValidity("");
         required = required && !this.checkValidity();
     });
@@ -110,7 +110,7 @@ function setRequired($element, event) {
     this.$els.prop("required", required);
 }
 
-$.fn[PLUGIN_NAME] = function(options) {
+$.fn[PLUGIN_NAME] = function (options) {
     var plugin = this.data(PLUGIN_NAME + ".plugin");
 
     if (!plugin) {
@rahulbhadhoriya
Copy link

anyone found any solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants