precise-commits icon indicating copy to clipboard operation
precise-commits copied to clipboard

JavaScript file ends up malformed after running precise-commits

Open andyexeter opened this issue 5 years ago • 1 comments

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) {

andyexeter avatar Feb 23 '19 13:02 andyexeter

anyone found any solution for this?

rahulbhadhoriya avatar May 06 '20 12:05 rahulbhadhoriya