@@ -52,7 +52,8 @@ function collectSafetyCommentLines(sourceFile: ts.SourceFile, source: string) {
5252 sourceFile . languageVariant ,
5353 source ,
5454 ) ;
55- const lines = new Set < number > ( ) ;
55+ const sameLine = new Set < number > ( ) ;
56+ const standalone = new Set < number > ( ) ;
5657 for ( let token = scanner . scan ( ) ; token !== ts . SyntaxKind . EndOfFileToken ; token = scanner . scan ( ) ) {
5758 if ( token !== ts . SyntaxKind . SingleLineCommentTrivia ) {
5859 continue ;
@@ -61,9 +62,15 @@ function collectSafetyCommentLines(sourceFile: ts.SourceFile, source: string) {
6162 if ( ! / ^ \/ \/ \s * S A F E T Y : \s * \S / u. test ( comment ) ) {
6263 continue ;
6364 }
64- lines . add ( sourceFile . getLineAndCharacterOfPosition ( scanner . getTokenPos ( ) ) . line ) ;
65+ const position = scanner . getTokenPos ( ) ;
66+ const line = sourceFile . getLineAndCharacterOfPosition ( position ) . line ;
67+ sameLine . add ( line ) ;
68+ const lineStart = sourceFile . getPositionOfLineAndCharacter ( line , 0 ) ;
69+ if ( source . slice ( lineStart , position ) . trim ( ) === "" ) {
70+ standalone . add ( line ) ;
71+ }
6572 }
66- return lines ;
73+ return { sameLine , standalone } ;
6774}
6875
6976function assertionOperatorPosition ( sourceFile : ts . SourceFile , node : AssertionNode ) {
@@ -118,7 +125,10 @@ export function countUnsafeAssertions(source: string, filePath = "src/source.ts"
118125 const operatorLine = sourceFile . getLineAndCharacterOfPosition (
119126 assertionOperatorPosition ( sourceFile , node ) ,
120127 ) . line ;
121- if ( ! safetyCommentLines . has ( operatorLine ) && ! safetyCommentLines . has ( operatorLine - 1 ) ) {
128+ if (
129+ ! safetyCommentLines . sameLine . has ( operatorLine ) &&
130+ ! safetyCommentLines . standalone . has ( operatorLine - 1 )
131+ ) {
122132 count += 1 ;
123133 }
124134 }
0 commit comments