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

Syntax tree analysis instead of raw text analysis in spacing analyzers #1367

Closed
alxru opened this issue Sep 2, 2015 · 2 comments
Closed

Comments

@alxru
Copy link

alxru commented Sep 2, 2015

Hi all,

I was researching StyleCop.Analyzers code as a reference for implementing my own custom analyzer and there is something that is not obvious to me.

Is there any reason that the complex token-level processing is used in spacing analyzers instead of simple parsing of raw text lines? For example, the analyzing code in SA1028CodeMustNotContainTrailingWhitespace class might look like the following:

SourceText sourceText = context.Tree.GetText(context.CancellationToken);
foreach (TextLine line in sourceText.Lines)
{
    if (line.Span.IsEmpty)
        continue;

    string text = line.ToString();
    int wsStart = FindTrailingWhitespace(text);

    if (wsStart >= 0)
    {
        Location diagnosticLocation = Location.Create(
            context.Tree, TextSpan.FromBounds(line.Start + wsStart, line.End));

        // TODO: report diagnostic for the location above
    }
}

This seems more simple and reliable than the current implementation.

@sharwell
Copy link
Member

sharwell commented Sep 2, 2015

We don't examine text inside disabled text #if false, or within verbatim string literals. Also many of the spacing rules have special exceptions depending on where you are in the syntax tree (see #1191 for example).

@alxru
Copy link
Author

alxru commented Sep 2, 2015

It makes sense to walk the syntax tree then. Thanks for the answer.

@alxru alxru closed this as completed Sep 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants