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

Add Analyzer for long lines (> 72 chars) #546

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/analyzer-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The following configurations can be set in a `.editorconfig` file to configure p
| `natls.style.discourage_inlineparameters` | `true`, `false` | [`NL031`](../tools/ruletranslator/src/main/resources/rules/NL031)|
| `natls.style.discourage_hiddentransactions` | `true`, `false` | [`NL032`](../tools/ruletranslator/src/main/resources/rules/NL032)|
| `natls.style.discourage_hiddenworkfiles` | `true`, `false` | [`NL033`](../tools/ruletranslator/src/main/resources/rules/NL033)|
| `natls.style.mark_mainframelongline` | `true`, `false` | [`NL034`](../tools/ruletranslator/src/main/resources/rules/NL034)|

# Example

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.amshove.natlint.analyzers;

import org.amshove.natlint.api.AbstractAnalyzer;
import org.amshove.natlint.api.DiagnosticDescription;
import org.amshove.natlint.api.IAnalyzeContext;
import org.amshove.natlint.api.ILinterContext;
import org.amshove.natparse.DiagnosticSeverity;
import org.amshove.natparse.ReadOnlyList;
import org.amshove.natparse.infrastructure.ActualFilesystem;
import org.amshove.natparse.lexing.PlainPosition;
import org.amshove.natparse.natural.INaturalModule;
import org.amshove.natparse.lexing.text.SourceTextScanner;

public class LongLinesAnalyzer extends AbstractAnalyzer
{
public static final DiagnosticDescription MAINFRAME_LONG_LINE = DiagnosticDescription.create(
"NL034",
"Long line found, it will not be visible immediately on Mainframes",
DiagnosticSeverity.INFO
);

private boolean isMainframeLongLineOff;

@Override
public ReadOnlyList<DiagnosticDescription> getDiagnosticDescriptions()
{
return ReadOnlyList.of(MAINFRAME_LONG_LINE);
}

@Override
public void initialize(ILinterContext context)
{
context.registerModuleAnalyzer(this::analyzeModule);
}

@Override
public void beforeAnalyzing(IAnalyzeContext context)
{
isMainframeLongLineOff = !context.getConfiguration(context.getModule().file(), "natls.style.mark_mainframelongline", OPTION_FALSE).equalsIgnoreCase(OPTION_TRUE);
}

private void analyzeModule(INaturalModule module, IAnalyzeContext context)
{
if (isMainframeLongLineOff)
{
return;
}

var fileContent = new ActualFilesystem().readFile(module.file().getPath());
var scanner = new SourceTextScanner(fileContent);
scanner.start();
int lineCnt = 0;
int offset = 0;

while (!scanner.isAtEnd())
{
int advanceBy = 0;
if (scanner.peek() == '\r' && scanner.peek(1) == '\n')
{
advanceBy = 2;
}
else
if (scanner.peek() == '\n')
{
advanceBy = 1;
}

if (advanceBy > 0)
{
if (scanner.lexemeLength() > 72)
{
context.report(MAINFRAME_LONG_LINE.createDiagnostic(new PlainPosition(offset, 0, lineCnt, 73, module.file().getPath())));
break;
}
lineCnt++;
offset += advanceBy;
scanner.advance(advanceBy);
scanner.start();
}
else
{
offset++;
scanner.advance();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.amshove.natlint.analyzers;

import org.amshove.natlint.linter.AbstractAnalyzerTest;
import org.junit.jupiter.api.Test;

class LongLinesAnalyzerShould extends AbstractAnalyzerTest
{
protected LongLinesAnalyzerShould()
{
super(new LongLinesAnalyzer());
}

@Test
void reportDiagnosticIfLongLine()
{
configureEditorConfig("""
[*]
natls.style.mark_mainframelongline=true
""");

var source = """
DEFINE DATA LOCAL
END-DEFINE
* This is a very long line. The fact that it is a comment does not matter, it should still be split in two lines because of the good, old Mainframe.
END
""";
testDiagnostics(source, expectDiagnostic(2, LongLinesAnalyzer.MAINFRAME_LONG_LINE));
}

@Test
void reportNoDiagnosticIfNoLongLine()
{
configureEditorConfig("""
[*]
natls.style.mark_mainframelongline=true
""");

var source = """
DEFINE DATA LOCAL
END-DEFINE

PERFORM MY-SUB

DEFINE SUBROUTINE MY-SUB
PRINT 'HELLO'
END-SUBROUTINE
END
""";
testDiagnostics(source, expectNoDiagnosticOfType(LongLinesAnalyzer.MAINFRAME_LONG_LINE));
}
}
6 changes: 6 additions & 0 deletions tools/ruletranslator/src/main/resources/rules/NL034
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Long lines will not be visible immediately on Mainframe systems
priority: MINOR
tags: confusing
type: CODE_SMELL
description:
Long lines will not be visible immediately on Mainframe systems, split them to make the readability better.