question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unstable indentation of comment around `else`

See original GitHub issue

Seems close to issue #1161.

Prettier 1.19.1 Playground link

--parser babel
--use-tabs

Input:

// UI.js v3.4.0
/* eslint-env browser */
/* exported ui, _doc, getEL, createEL */

/* ui.js Locals */
var _doc = document;
function getEL(id){ return _doc.getElementById(id);}
function createEL(tagName){ return _doc.createElement(tagName);}

//---------------------------------------------------------------------------
// ★uiオブジェクト UserInterface側のオブジェクト
//---------------------------------------------------------------------------
/* extern */
window.ui = {
        version : '<%= git.hash %>',

        /* このサイトで使用するパズルのオブジェクト */
        puzzle    : null,

        /* どの種類のパズルのメニューを表示しているか */
        currentpid : '',

        customAttr : function(el, name){
                var value = "";
                if(el.dataset!==void 0){ value = el.dataset[name];}
                /* IE10, Firefox5, Chrome7, Safari5.1以下のフォールバック */
                else{
                        var lowername = "data-";
                        for(var i=0;i<name.length;i++){
                                var ch = name[i] || name.charAt(i);
                                lowername += ((ch>="A" && ch<="Z") ? ("-" + ch.toLowerCase()) : ch);
                        }
                        value = el[lowername] || el.getAttribute(lowername) || "";
                }
                return value;
        }
};

Output:

// UI.js v3.4.0
/* eslint-env browser */
/* exported ui, _doc, getEL, createEL */

/* ui.js Locals */
var _doc = document;
function getEL(id) {
	return _doc.getElementById(id);
}
function createEL(tagName) {
	return _doc.createElement(tagName);
}

//---------------------------------------------------------------------------
// ★uiオブジェクト UserInterface側のオブジェクト
//---------------------------------------------------------------------------
/* extern */
window.ui = {
	version: "<%= git.hash %>",

	/* このサイトで使用するパズルのオブジェクト */
	puzzle: null,

	/* どの種類のパズルのメニューを表示しているか */
	currentpid: "",

	customAttr: function(el, name) {
		var value = "";
		if (el.dataset !== void 0) {
			value = el.dataset[name];
		} else {
		/* IE10, Firefox5, Chrome7, Safari5.1以下のフォールバック */
			var lowername = "data-";
			for (var i = 0; i < name.length; i++) {
				var ch = name[i] || name.charAt(i);
				lowername += ch >= "A" && ch <= "Z" ? "-" + ch.toLowerCase() : ch;
			}
			value = el[lowername] || el.getAttribute(lowername) || "";
		}
		return value;
	}
};

Iterated Output: (comment after else indented further)

// UI.js v3.4.0
/* eslint-env browser */
/* exported ui, _doc, getEL, createEL */

/* ui.js Locals */
var _doc = document;
function getEL(id) {
	return _doc.getElementById(id);
}
function createEL(tagName) {
	return _doc.createElement(tagName);
}

//---------------------------------------------------------------------------
// ★uiオブジェクト UserInterface側のオブジェクト
//---------------------------------------------------------------------------
/* extern */
window.ui = {
	version: "<%= git.hash %>",

	/* このサイトで使用するパズルのオブジェクト */
	puzzle: null,

	/* どの種類のパズルのメニューを表示しているか */
	currentpid: "",

	customAttr: function(el, name) {
		var value = "";
		if (el.dataset !== void 0) {
			value = el.dataset[name];
		} else {
			/* IE10, Firefox5, Chrome7, Safari5.1以下のフォールバック */
			var lowername = "data-";
			for (var i = 0; i < name.length; i++) {
				var ch = name[i] || name.charAt(i);
				lowername += ch >= "A" && ch <= "Z" ? "-" + ch.toLowerCase() : ch;
			}
			value = el[lowername] || el.getAttribute(lowername) || "";
		}
		return value;
	}
};

Expected behavior: Probably get the second output directly.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fiskercommented, Mar 31, 2020

@sosukesuzuki You link broken, playground

1reaction
sosukesuzukicommented, Mar 29, 2020

a minimal example to reproduce: Playground Input:

if (cond) {}
/* comment */
else {
  return;
}

Output:

if (cond) {
} else {
/* comment */
  return;
}

Second Output:

if (cond) {
} else {
  /* comment */
  return;
}

Expected(Same as Input):

if (cond) {}
/* comment */
else {
  return;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Shouldn't change the indentation of comments #16 - GitHub
There's a workaround if your parser doesn't support comments the way you want: transform the comments into strings with a unique random prefix, ......
Read more >
CLion inconsistent auto indentation for line comment
It seems like whenever I use = to initialize an variable, and then add comment at the end of the line, it will...
Read more >
clang-format doesn't indent comments in front of an #ifdef
The comment belongs to the #if[def] usually but it's indented according to the local scope. Anything else would be just wrong and so...
Read more >
List Indentation Very Unstable - Unpredictable Changes - Evernote ...
Hi, I primarily use Evernote for managing schedules and thus use the list formatting a lot for to-do-lists and similar types. Since the...
Read more >
Preserve indentation of comments not working (since VS 2015)
Inside C++ code, Visual try to indent unrelated comments (~with an empty line in between) even if the option "Preserve indentation of comments"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found