-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
daisy-chain indentation leads to over-indentation #482
Comments
Could you give us the full input and expected output, along with your config? I'm having difficulty correlating your expected fragment (which looks incorrect, honestly) to the closing blocks. Thanks! |
As many things in this area, this is of course a matter of personal preference and habit rather than an objective truth. After processing, you may see something like this:
I would like this:
This is my configuration:
|
+1 |
The issue you're talking about is this one indent: angular
.module('module', []) I'm going to take a minute to feel a touch of awesome that we're so close to what you want to see. In times past, we wouldn't have gotten anywhere close. 😄 The indenting there is meant to keep clarity what elements are part of a particular statement. In the general case, the indenting is basically correct. In this specific case, you have is one very long statement, but per #200 the beautifier doesn't know that that are no significant statements after that one. The beautifier is not meant to be a fully configurable formatter - it is meant to address the general case To add some depth to this discussion, please take a look at these examples and tell me what the formatting should look like. alpha
.cooker(function() {
some
.thing()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thenclose()
beta(zeta);
omega
.cage(function() {
random
.things()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thendie() |
Absolutely! =) In regard to indentation, I think your example should look like this: alpha
.cooker(function() {
some
.thing()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thenclose()
beta(zeta);
omega
.cage(function() {
random
.things()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thendie() The maxim here is the same as for curly braces: start and end should be at the same indentation. Additionally, Douglas Crockford's code conventions prescribe the |
Except that js-beautify doesn't follow crockford by default, and if you run the above through jslint, it will complain that In your example, it seems to me like it is far too easy for I'm going to leave this open - the example you provide appears to be AngularJS-based, so this idiom may gain wider acceptance over time. But it is not something we'll be able to incorporate any time soon. |
I'm terribly sorry: I botched the indentation. None of them were supposed to be indented. And for alpha
.cooker(function() {
some
.thing()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thenclose();
beta(zeta);
omega
.cage(function() {
random
.things()
.should()
.happen();
elsewhere
.some_other_thing()
.should()
.happen();
})
.thendie(); As I said at the outset, I think it's a matter of personal taste. And specifically with single-line chains I am less inclined to reduce the indentation. But I find the multi-line case very bad and having mixed styles would be horrible, so I would definitely stick to the strategy of less indentation, always. |
You might look at #485. With this up coming fix the following will now remain unchanged when it goes through the beautifier: (function () {
'use strict';
angular
.module('module', [])
.directive('appVersion', ['version', function (version) {
return function (scope, elm, attrs) {
elm.text(version);
};
}])
.directive('foo', [function () {
return {};
}])
.directive('bar', [function () {
return {};
}]);
})(); Still not what you'd like, but the beautifier will no longer force the function declaration to a newline (while still respecting a newline if you include it). |
I have to +1 this request, this is especially handy when working with Promises. Promise.resolve()
.then(function() {
return foo.bar()
})
.then(function() {
return foo.baz();
})
.then(function() {
//...
}) //...
//... This chaining can continue for a while, especially when writing a more involved api end point, and by the time you're looking at the bottom you're constantly thrown off guard with how the indentation finishes relative to something close by. I believe it's important that all closing indentation be one level of depth difference from the next nearest. |
👍
To me this is the clincher that proves that extra indentation is misleading and ultimately a mistake. I realize some may feel that there is some sense in which
seems to reflect that the |
It may be nice to have the visual indication that indentation provides, but in this case it is overloading the meaning of indentation—not just indicating a new scope, but also indicating a chained method. However we already have the So it's not really _just_ a matter of personal preference—it's a matter of benefits & drawbacks of both approaches. (Personal preference is of course involved, because some may not care about certain drawbacks or about certain benefits, but the discussion can be more fruitful if we discuss what those benefits and drawbacks are, rather than just saying "I prefer x" or "I prefer y".) I think the case is strong that the drawbacks of extra indentation are significant, while the benefits can be had another way. Drawbacks of extra indentation for chained methods:
Benefits:
|
+1 |
+1 This is leading to Example: gulp.task('changelog', function () {
return gulp.src('CHANGELOG.md', {
buffer: false
})
.pipe(conventionalChangelog({
preset: 'angular' // Or to any other commit message convention you use.
}))
.pipe(gulp.dest('./'));
}); Errors:
Correct way (for jslint): gulp.task('changelog', function () {
return gulp.src('CHANGELOG.md', {
buffer: false
})
.pipe(conventionalChangelog({
preset: 'angular' // Or to any other commit message convention you use.
}))
.pipe(gulp.dest('./'));
}); |
I would very much prefer to have this as an option, primarily to avoid unnecessary extra indentation in promise chaining:
I think the extra indentation is the equivalent of:
It makes sense if the the child token which wraps around a block of indentation is on a new line from the initial variable:
vs.
But if it can all fit on one line then the double indentation shouldnt happen. (As it stands, the above would be linted/expected as:)
|
I've marked this as an enhancement. All you "+1"-ers and those who have commented, feel free to contribute a pull request. |
+1 want |
I've opened a PR for this if the continuous integration would finally update the PR status, it should be ready to merge. |
+1 This is boring as hell |
undindent-chained-methods option. Resolves #482
Using this via https://github.com/enginespot/js-beautify-sublime
Expected:
Actual:
To illustrate the problem: the current indenting can lead to EOFs like this:
That looks like an error to me, and will prompt me to look for the cause – or worse, make me blind to the real ones =(
The text was updated successfully, but these errors were encountered: