Skip to content

Commit 4ac9a26

Browse files
CopilotT-Gro
andcommitted
Re-implement XML doc comment position warning using token position tracking
- Add lastTokenEndLine and lastTokenEndColumn fields to LexArgs - Track token positions in KeywordOrIdentifierToken and IdentifierToken - Add updateLastTokenPos helper function in lexer - Check position when /// is lexed and warn if on same line as previous token - Add FSComp.txt entry 3879 for the warning message Co-authored-by: T-Gro <[email protected]>
1 parent 1f45f17 commit 4ac9a26

17 files changed

+94
-2
lines changed

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,4 +1813,5 @@ featureAllowLetOrUseBangTypeAnnotationWithoutParens,"Allow let! and use! type an
18131813
3876,lexWarnDirectivesMustMatch,"There is another %s for this warning already in line %d."
18141814
3877,lexLineDirectiveMappingIsNotUnique,"The file '%s' was also pointed to in a line directive in '%s'. Proper warn directive application may not be possible."
18151815
3878,tcAttributeIsNotValidForUnionCaseWithFields,"This attribute is not valid for use on union cases with fields."
1816+
3879,xmlDocNotFirstOnLine,"XML documentation comments should be the first non-whitespace text on a line."
18161817
featureReturnFromFinal,"Support for ReturnFromFinal/YieldFromFinal in computation expressions to enable tailcall optimization when available on the builder."

src/Compiler/SyntaxTree/LexHelpers.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ type LexArgs =
6363
mutable indentationSyntaxStatus: IndentationAwareSyntaxStatus
6464
mutable stringNest: LexerInterpolatedStringNesting
6565
mutable interpolationDelimiterLength: int
66+
/// Tracks the line number of the last non-whitespace, non-comment token
67+
mutable lastTokenEndLine: int
68+
/// Tracks the end column of the last non-whitespace, non-comment token
69+
mutable lastTokenEndColumn: int
6670
}
6771

6872
/// possible results of lexing a long Unicode escape sequence in a string literal, e.g. "\U0001F47D",
@@ -85,6 +89,8 @@ let mkLexargs
8589
stringNest = []
8690
pathMap = pathMap
8791
interpolationDelimiterLength = 0
92+
lastTokenEndLine = 0
93+
lastTokenEndColumn = 0
8894
}
8995

9096
/// Register the lexbuf and call the given function
@@ -445,9 +451,15 @@ module Keywords =
445451
if IsCompilerGeneratedName s then
446452
warning (Error(FSComp.SR.lexhlpIdentifiersContainingAtSymbolReserved (), lexbuf.LexemeRange))
447453

454+
// Track token position for XML doc comment checking
455+
args.lastTokenEndLine <- lexbuf.EndPos.Line
456+
args.lastTokenEndColumn <- lexbuf.EndPos.Column
448457
args.resourceManager.InternIdentifierToken s
449458

450459
let KeywordOrIdentifierToken args (lexbuf: Lexbuf) s =
460+
// Track token position for XML doc comment checking
461+
args.lastTokenEndLine <- lexbuf.EndPos.Line
462+
args.lastTokenEndColumn <- lexbuf.EndPos.Column
451463
match keywordTable.TryGetValue s with
452464
| true, v ->
453465
match v with

src/Compiler/SyntaxTree/LexHelpers.fsi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ type LexArgs =
3737
mutable ifdefStack: LexerIfdefStack
3838
mutable indentationSyntaxStatus: IndentationAwareSyntaxStatus
3939
mutable stringNest: LexerInterpolatedStringNesting
40-
mutable interpolationDelimiterLength: int }
40+
mutable interpolationDelimiterLength: int
41+
/// Tracks the line number of the last non-whitespace, non-comment token
42+
mutable lastTokenEndLine: int
43+
/// Tracks the end column of the last non-whitespace, non-comment token
44+
mutable lastTokenEndColumn: int }
4145

4246
type LongUnicodeLexResult =
4347
| SurrogatePair of uint16 * uint16

src/Compiler/lex.fsl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ let fail args (lexbuf:UnicodeLexing.Lexbuf) msg dflt =
5454
args.diagnosticsLogger.ErrorR(Error(msg,m))
5555
dflt
5656

57+
/// Update tracking of the last token position for XML doc comment position checking
58+
let updateLastTokenPos (args: LexArgs) (lexbuf: UnicodeLexing.Lexbuf) =
59+
let endPos = lexbuf.EndPos
60+
args.lastTokenEndLine <- endPos.Line
61+
args.lastTokenEndColumn <- endPos.Column
62+
5763
//--------------------------
5864
// Integer parsing
5965

@@ -740,6 +746,10 @@ rule token (args: LexArgs) (skip: bool) = parse
740746
| "///" op_char*
741747
{ // Match exactly 3 slash, 4+ slash caught by preceding rule
742748
let m = lexbuf.LexemeRange
749+
// Check if there was a token on this line before this /// comment
750+
// If lastTokenEndLine matches the current line, there's code before the comment
751+
if args.lastTokenEndLine = m.StartLine then
752+
warning (Error(FSComp.SR.xmlDocNotFirstOnLine(), m))
743753
let doc = lexemeTrimLeft lexbuf 3
744754
let sb = (new StringBuilder(100)).Append(doc)
745755
if not skip then LINE_COMMENT (LexCont.SingleLineComment(args.ifdefStack, args.stringNest, 1, m))
@@ -852,7 +862,7 @@ rule token (args: LexArgs) (skip: bool) = parse
852862

853863
| '(' { LPAREN }
854864

855-
| ')' { RPAREN }
865+
| ')' { updateLastTokenPos args lexbuf; RPAREN }
856866

857867
| '*' { STAR }
858868

src/Compiler/xlf/FSComp.txt.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)