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

First approach of rule SA1140 MaximumLineLength #2302

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

pkess
Copy link
Contributor

@pkess pkess commented Feb 23, 2017

Hi,
i just created a rule to meet #1808.
The rule will not raise any warnings or errors unless a maximum line length is configured in stylecop.json

Please leave comments

@codecov
Copy link

codecov bot commented Feb 23, 2017

Codecov Report

Merging #2302 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master    #2302      +/-   ##
==========================================
+ Coverage   96.84%   96.84%   +<.01%     
==========================================
  Files         598      600       +2     
  Lines       84061    84159      +98     
  Branches     3200     3210      +10     
==========================================
+ Hits        81406    81504      +98     
  Misses       1790     1790              
  Partials      865      865
Impacted Files Coverage Δ
...Analyzers.Test/ReadabilityRules/SA1140UnitTests.cs 100% <100%> (ø)
...lyzers/ReadabilityRules/SA1140MaximumLineLength.cs 100% <100%> (ø)
...lyzers/Settings/ObjectModel/ReadabilitySettings.cs 100% <100%> (ø) ⬆️
...alyzers.Test/DocumentationRules/SA1623UnitTests.cs 100% <0%> (ø) ⬆️
...alyzers.Test/DocumentationRules/SA1612UnitTests.cs 100% <0%> (ø) ⬆️
...Analyzers.Test/ReadabilityRules/SA1125UnitTests.cs 100% <0%> (ø) ⬆️
...eCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs 100% <0%> (ø) ⬆️
...alyzers.Test/DocumentationRules/SA1624UnitTests.cs 100% <0%> (ø) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08e960e...871be1b. Read the comment docs.

@pkess
Copy link
Contributor Author

pkess commented Feb 23, 2017

Corrected some copy paste errors and added example for documentation

@pkess
Copy link
Contributor Author

pkess commented Mar 2, 2017

Any news here?

@vweijsters
Copy link
Contributor

I'll have a look this week. Marked it 'do not merge' to signify that the discussion in #1808 has not come to a conclusion yet.


this.SetLineLengthSettings(41);
await this.VerifyCSharpDiagnosticAsync(testCode, new[] { diagnosticLine4 }, CancellationToken.None).ConfigureAwait(false);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a test case where all lines are shorter than the maximum line length

/// than allowed in stylecop.json</para>
/// </remarks>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class SA1140MaximumLineLength : DiagnosticAnalyzer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be marked with [NoCodeFix]

}

public int LineLength =>
this.lineLength;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LineLength should be named MaximumLineLength to provide more clarity to someone reading the stylecop.json file.

@@ -39,3 +39,4 @@ Identifier | Name | Description
[SA1134](SA1134.md) | AttributesMustNotShareLine | An attribute is placed on the same line of code as another attribute or element.
[SA1136](SA1136.md) | EnumValuesShouldBeOnSeparateLines | Multiple enum values are placed on the same line of code.
[SA1139](SA1139.md) | UseLiteralsSuffixNotationInsteadOfCasting | Use literal suffix notation instead of casting.
[SA1140](SA1140.md) | SA1140MaximumLineLength | Code should be wrapped to a maximum line length.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SA1140MaximumLineLength should be MaximumLineLength, to be inline with the other entries

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

</table>

:memo: This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a remark that this was added in 1.1.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1140.md";

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have AnalyzerConstants.DisabledByDefault

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am i right that the AnalyzerConstants.DisabledByDefault will set the Severity Level to None by default?
I would like this rule to check the line length as soon as a user defined a maximum in his config. Thats the way it works at the moment.

@pkess
Copy link
Contributor Author

pkess commented Mar 19, 2017

Ok, i think finally i implemented all of your requests exept the DisabledByDefault constant.
I am sorry for all those commits but i did not had visual studio installed on the pc i used to implement all those changes.

Please give feedback

@pkess
Copy link
Contributor Author

pkess commented Mar 30, 2017

Hey guys,

Did you already checkout my last changes?

@cristobalito
Copy link

apologies for unsolicited commenting on this PR - I've nothing to do with project but was looking for this sort of functionality as a user

@pkess - nice work. It would be great to see this merged into the project. While i like your approach w.r.t. to the outstanding issue on this PR, esp. for its convenience, it strikes me as a non-standard way to configure the rule. As a user, I would expect that the value specified in my ruleset ultimately governs whether or not the rule is on. One major benefit of this, is that there is only one obvious place I have to go in order to disable the rule, should I so desire.

In order to break the impasse here, would you be willing to compromise here to get the majority of the benefit into the codebase?

@pkess
Copy link
Contributor Author

pkess commented Jun 19, 2017

Hi,

Thank you for your comment.
In fact I think the way of activating the rule by specification of a line length is very clear. There is another way to specific set the severity level of rules. I don't like to implement a separate way of activation.

Additionally I don't have a machine with Windows and visual studio running now (and will not have any in the next month's) so I am not able to fix it quite now. Feel free to fork my branch and implement it as you like. I would be excited!

@sharwell
Copy link
Member

sharwell commented Jun 19, 2017

@pkess I'm won't hide that I'm not a fan of this rule (unrelated to your implementation of it), but after I get some of the other blocking issues (e.g. C# 7) taken care of I will take a serious look at this PR and see if we can get it included as a disabled-by-default rule.

@LordBenjamin
Copy link

@sharwell - is there any timeline for including this functionality? It would be useful to know before we decide whether to reference another third-party analyzer or write our own equivalent.

@sharwell
Copy link
Member

@LordBenjamin Not yet. Feel free to keep reminding me.

@cristobalito
Copy link

@sharwell If this rule wasn't enabled by default (as per original PR), would you be happy to merge it? If so, I'd be happy to take a look at doing that tonight.

@sharwell
Copy link
Member

@cristobalito I haven't had a chance to do the code review yet. If the only issues are the merge conflicts and changing it to disabled by default, then those are items we can quickly clean up when we get ready to merge. Thank you though. 😄

@promontis
Copy link

@sharwell is it possible to merge this?

@PeterRockstars
Copy link

Any progress on this? Would love to see this rule implemented and it seems that we are on the home stretch. Anyone care to take us the last few yards?

Copy link
Member

@pdelvo pdelvo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few minor comments on this. This is still pending @sharwell's take on this. All the comments I made could also be addressed in a later PR.

<value>Code should be wrapped to a maximum line length. See stylecop.json</value>
</data>
<data name="SA1140MessageFormat" xml:space="preserve">
<value>Maximum line length exceeded</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to change this to "Maximum line length of {0} exceeded" and make it include the set line length.

{
if ((line.End - line.Start) > readabilityRules.MaximumLineLength)
{
var location = root.SyntaxTree.GetLocation(line.Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only report the diagnostic on the bit that is too long so the user knows how much the line is too long.


## Known issues

Tabs in source file are not expanded to spaces for evaluation of the line length. Only use this rule if spaces are used for indention.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do something about this? We could count the number of tabs in the line and use the configured tab width for that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time this was first implemented, tab width might not have been included in stylecop.json. It's now available there so the feature could be implemented in this rule too.

using StyleCop.Analyzers.Helpers;

/// <summary>
/// Maximum line length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All(?) other rules contain the documentation as part of the xml documentation

@danpetitt
Copy link

Any news on this? My new fellow devs (company merged) prefer to operate a max line length style and I want something to pre-emptively warn me

@dave-q
Copy link

dave-q commented May 3, 2019

Would be very happy to see this as a feature. Any news on progress?

@admsugar
Copy link

Any update on this?

@jnm2
Copy link
Contributor

jnm2 commented Jun 17, 2020

I believe all updates will be seen in this thread or the proposal issue, if that's what you're asking.

@jhoy1020
Copy link

Is this change going to be checked in? Trying to get my team to standardize on a max line length is impossible without some kind of preventative check. Per documentation, it is recommended to have a max line length of 120 https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions. It would be really helpful to have this change to help enforce this rule.

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

Successfully merging this pull request may close these issues.