diff options
author | brandjon <brandjon@google.com> | 2017-11-02 09:50:21 -0400 |
---|---|---|
committer | John Cater <jcater@google.com> | 2017-11-02 10:04:29 -0400 |
commit | 901cc6c7474e2a6cc8c3812d156177f44dcb76ed (patch) | |
tree | 88fc285191acba5becb97bdcb27fce7f616fa892 /src/tools/skylark/java/com | |
parent | ef469dad55378c8320dbb30aaad4eab5fef5abd5 (diff) |
Linter: Tolerate whitespace-only lines and complain about misaligned closing quotes
RELNOTES: None
PiperOrigin-RevId: 174319420
Diffstat (limited to 'src/tools/skylark/java/com')
-rw-r--r-- | src/tools/skylark/java/com/google/devtools/skylark/skylint/DocstringUtils.java | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/tools/skylark/java/com/google/devtools/skylark/skylint/DocstringUtils.java b/src/tools/skylark/java/com/google/devtools/skylark/skylint/DocstringUtils.java index 1d37bc6389..1863bccb1c 100644 --- a/src/tools/skylark/java/com/google/devtools/skylark/skylint/DocstringUtils.java +++ b/src/tools/skylark/java/com/google/devtools/skylark/skylint/DocstringUtils.java @@ -219,7 +219,7 @@ public final class DocstringUtils { /** Start offset of the current line. */ private int startOfLineOffset = 0; /** End offset of the current line. */ - private int endOfLineOffset = -1; + private int endOfLineOffset = 0; /** Current line number within the docstring. */ private int lineNumber = 0; /** @@ -261,12 +261,19 @@ public final class DocstringUtils { if (startOfLineOffset >= docstring.length()) { return false; } - blankLineBefore = line.isEmpty(); - startOfLineOffset = endOfLineOffset + 1; + blankLineBefore = line.trim().isEmpty(); + startOfLineOffset = endOfLineOffset; if (startOfLineOffset >= docstring.length()) { + // Previous line was the last; previous line had no trailing newline character. line = ""; return false; } + // If not the first line, advance start past the newline character. In the case where there is + // no more content, then the previous line was the second-to-last line and this last line is + // empty. + if (docstring.charAt(startOfLineOffset) == '\n') { + startOfLineOffset += 1; + } lineNumber++; endOfLineOffset = docstring.indexOf('\n', startOfLineOffset); if (endOfLineOffset < 0) { @@ -274,9 +281,18 @@ public final class DocstringUtils { } String originalLine = docstring.substring(startOfLineOffset, endOfLineOffset); originalLines.add(originalLine); - line = originalLine; - int indentation = getIndentation(line); - if (!line.isEmpty()) { + int indentation = getIndentation(originalLine); + if (endOfLineOffset == docstring.length() && startOfLineOffset != 0) { + if (!originalLine.trim().isEmpty()) { + error("closing docstring quote should be on its own line, indented the same as the " + + "opening quote"); + } else if (indentation != baselineIndentation) { + error("closing docstring quote should be indented the same as the opening quote"); + } + } + if (originalLine.trim().isEmpty()) { + line = ""; + } else { if (indentation < baselineIndentation) { error( "line indented too little (here: " @@ -288,11 +304,21 @@ public final class DocstringUtils { } else { startOfLineOffset += baselineIndentation; } + line = docstring.substring(startOfLineOffset, endOfLineOffset); } - line = docstring.substring(startOfLineOffset, endOfLineOffset); return true; } + /** + * Returns whether the current line is the last one in the docstring. + * + * <p>It is possible for both this function and {@link #eof} to return true if all content has + * been exhausted, or if the last line is empty. + */ + private boolean onLastLine() { + return endOfLineOffset >= docstring.length(); + } + private boolean eof() { return startOfLineOffset >= docstring.length(); } @@ -360,7 +386,9 @@ public final class DocstringUtils { if (deprecated.isEmpty() && nonStandardDeprecation.isEmpty()) { nonStandardDeprecation = checkForNonStandardDeprecation(line); } - longDescriptionLines.add(line); + if (!(onLastLine() && line.trim().isEmpty())) { + longDescriptionLines.add(line); + } nextLine(); } } |