Validate asciidoc ifdef/endif (#3311)

Fix kotlin:S6511
This commit is contained in:
Marco Borgeaud 2023-10-18 11:43:40 +02:00 committed by GitHub
parent d7138f5ef4
commit cd424756a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 226 additions and 82 deletions

View File

@ -0,0 +1,105 @@
# Validate the asciidoc environment directives in the given file(s).
# Errors are printed to the standard output stream.
#
# "ifdef" commands has to start the line without any leading spaces,
# as per asciidoc format.
#
# Only one form is allowed:
# ifdef::env-github,rspecator-view[]
#
# The closing command is:
# endif::env-github,rspecator-view[]
#
# It must be in the same file.
# Only one such environment is allowed per file.
from pathlib import Path
import sys
VALID_IFDEF = "ifdef::env-github,rspecator-view[]"
VALID_ENDIF = "endif::env-github,rspecator-view[]"
class Checker:
def __init__(self, file: Path):
assert file.exists()
assert file.is_file()
self._file = file
self._is_env_open = False
self._has_env = False
self._is_valid = True
def process(self) -> bool:
content = self._file.read_text(encoding="utf-8")
lines = content.splitlines(keepends=False)
for line_index, line in enumerate(lines):
line_number = line_index + 1
if line.startswith("ifdef::"):
self._process_open(line_number, line)
if line.startswith("endif::"):
self._process_close(line_number, line)
if self._is_env_open:
self._on_error(len(lines), "The ifdef command is not closed.")
return self._is_valid
def _process_open(self, line_number: int, line: str):
if self._has_env:
self._on_error(line_number, "Only one ifdef command is allowed per file.")
if self._is_env_open:
self._on_error(line_number, "The previous ifdef command was not closed.")
self._has_env = True
self._is_env_open = True
# IDEs should be configured to properly display the description,
# not the other way around.
# "env-vscode" was used in the passed. Instead, user should be able to
# toggle the rspecator view based on their needs. Help these users migrate.
if "vscode" in line:
self._on_error(
line_number,
"Configure VS Code to display rspecator-view by setting the asciidoctor attribute.",
)
if line != VALID_IFDEF:
self._on_error(
line_number,
f'Incorrect asciidoc environment. "{VALID_IFDEF}" should be used instead.',
)
def _process_close(self, line_number: int, line: str):
if not self._is_env_open:
self._on_error(line_number, "Unexpected endif command.")
self._is_env_open = False
if line != VALID_ENDIF:
self._on_error(
line_number,
f'Incorrect endif command. "{VALID_ENDIF}" should be used instead.',
)
def _on_error(self, line_number: int, message: str):
print(f"{self._file}:{line_number} {message}")
self._is_valid = False
def main():
files = sys.argv[1:]
if not files:
sys.exit("Missing input files")
valid = True
for file in files:
if not Checker(Path(file)).process():
valid = False
if not valid:
sys.exit(1)
if __name__ == "__main__":
main()

View File

@ -43,6 +43,7 @@ fi
# * Only valid languages can be used as subdirectories in rule directories,
# with the exception of ALLOWED_RULE_SUB_FOLDERS.
# * Asciidoc files are free or errors and warnings.
# * ifdef/endif are used appropriatedly.
#
# [properties validated always on all rules]
# * Rule descriptions can include other asciidoc files from the same rule
@ -85,6 +86,17 @@ do
fi
rm -f stuck
# Validate modified files' ifdef/endif commands.
find "${dir}" -name '*.adoc' \
-exec python3 "./ci/asciidoc_validation/validate_environment.py" '{}' '+' \
>validate_env_commands 2>&1
if [ -s validate_env_commands ]; then
echo "ERROR: Some ifdef/endif commands are misused."
cat validate_env_commands
exit_code=1
fi
rm -f validate_env_commands
for language in "${dir}"/*/
do
language=${language%*/}

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -21,4 +21,4 @@ The 'throw' statement.
=== on 1 Mar 2018, 17:03:27 Valeri Hristov wrote:
Throwing in IDisposable.Dispose is covered in RSPEC-3877
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -35,3 +35,5 @@ ifdef::env-github,rspecator-view[]
include::../message.adoc[]
'''
endif::env-github,rspecator-view[]

View File

@ -49,4 +49,4 @@ ifdef::env-github,rspecator-view[]
Use ".Any()" to test whether this "IEnumerable(Of XXX)" is empty or not.
endif::env-github,rspecator-view[]

View File

@ -91,3 +91,5 @@ include::../message.adoc[]
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]

View File

@ -67,3 +67,5 @@ include::../message.adoc[]
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]

View File

@ -52,7 +52,7 @@ class MyClass
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2[Dictionary<TKey,TValue> Class]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -74,4 +74,4 @@ The [class|struct] name.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -16,4 +16,4 @@ include::parameters.adoc[]
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -43,7 +43,7 @@ public class Choice {
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1717[Compiler Warning (level 3) CS1717]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -57,4 +57,4 @@ include::../message.adoc[]
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -17,4 +17,4 @@ include::highlighting.adoc[]
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -52,7 +52,7 @@ else if (param == 3)
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-if-statement[The if statement]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -76,4 +76,4 @@ include::../highlighting.adoc[]
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -41,7 +41,7 @@ End If
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If...Then...Else Statement]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -59,4 +59,4 @@ include::../highlighting.adoc[]
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -28,4 +28,4 @@ include::../message.adoc[]
include::../comments-and-links.adoc[]
gndif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]

View File

@ -31,4 +31,4 @@ include::../message.adoc[]
include::../comments-and-links.adoc[]
gndif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]

View File

@ -30,4 +30,4 @@ include::../message.adoc[]
include::../comments-and-links.adoc[]
gndif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]

View File

@ -32,4 +32,4 @@ include::../message.adoc[]
include::../comments-and-links.adoc[]
gndif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -18,4 +18,4 @@ Remove this useless shift (multiple of 32/64).
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -173,7 +173,7 @@ int Pow(int num, int exponent)
* Edsger Dijkstra - https://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html[A Case against the GO TO Statement]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -189,5 +189,5 @@ Add a way to break out of this \[[method|property|property accessor]'s recursion
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -12,4 +12,4 @@ include::message.adoc[]
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -19,4 +19,4 @@ In fact, it's not an infinite loop because of integer overflow. On my computer,
In JavaScript, moving the counter in the wrong direction causes an infinite loop because there is no integer type. All numbers are 64 bit floating point numbers.
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -24,4 +24,4 @@ To me the 2 title versions are six-of-one-half-dozen-of-the-other. But given tha
=== on 2 Dec 2014, 15:02:37 Pierre-Yves Nicolas wrote:
No, I don't care much.
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -14,4 +14,4 @@ include::./highlighting.adoc[]
include::./comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -28,4 +28,4 @@ Double-check my edits please [~tamas.vajk]
=== on 6 Jul 2015, 09:33:41 Tamas Vajk wrote:
\[~ann.campbell.2] Looks good.
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -12,4 +12,4 @@ include::message.adoc[]
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -43,7 +43,7 @@ if (!double.IsNaN(a))
----
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -60,4 +60,4 @@ Use [double|float].IsNaN() instead.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -85,7 +85,7 @@ class PointManager<T1, T2>
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct[Structure types (C# reference)]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -118,4 +118,4 @@ Looks good, I adjusted some wording in the first sentence of the description
=== on 22 May 2015, 12:10:14 Ann Campbell wrote:
Thanks [~tamas.vajk]. Looks good
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -83,7 +83,7 @@ static class MyClass
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct[Structure types (C# reference)]
* S3898 - Value types should implement "IEquatable<T>"
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -103,4 +103,4 @@ Use a different kind of comparison for these value types.
=== on 8 Jun 2015, 09:32:21 Tamas Vajk wrote:
LGTM, I've changed the `object` to `Object` just to conform to the title, but there is no difference, because `object` is just an alias for `System.Object`
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -98,7 +98,7 @@ public void Method()
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.eventhandler[`EventHandler` Delegate]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -134,4 +134,4 @@ A method with `async` keyword returning a `Task` is like a non `async` method wi
=== on 1 Jul 2015, 11:59:39 Ann Campbell wrote:
Okay, thanks [~tamas.vajk]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -27,4 +27,4 @@ updated. See what you think [~tamas.vajk]
=== on 20 Jul 2015, 11:44:56 Tamas Vajk wrote:
\[~ann.campbell.2] Looks good.
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -52,7 +52,7 @@ void TraceMessage(string message = null,
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute[CallerArgumentExpressionAttribute Class]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments[Named and Optional Arguments]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -76,4 +76,4 @@ And also modified the last sentence. It is possible to not specify all parameter
=== on 18 Nov 2015, 19:55:28 Ann Campbell wrote:
Okay, thanks [~tamas.vajk]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -23,4 +23,4 @@ class name
done [~tamas.vajk]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -14,4 +14,4 @@ include::highlighting.adoc[]
include::comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -32,7 +32,7 @@ interface IMyService
include::../resources.adoc[]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -42,4 +42,4 @@ include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -30,7 +30,7 @@ End Interface
include::../resources.adoc[]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -40,4 +40,4 @@ include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -45,7 +45,7 @@ void DoChecks<T>(Nullable<T> value) where T : struct
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException Class]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -103,4 +103,4 @@ ____
But if C#ers will understand, I'm good with it.
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -12,4 +12,4 @@ Provide an 'AssemblyVersion' attribute for assembly 'xxx'.
Assembly declaration
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -191,7 +191,7 @@ public class UnsealedFoo : Foo
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.iserializable.getobjectdata[`ISerializable.GetObjectData` Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.exception[`Exception` Class]
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -228,4 +228,4 @@ The following check is not implemented because it is difficult to know exactly w
We find mostly classes that derive from Exception in the projects we test and that's why they might not be a good source for checking issues and false positives (statistically).
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -1,4 +1,4 @@
ifdef::env-github,rspecator-view,env-vscode[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
@ -29,4 +29,4 @@ member declaration
=== on 22 May 2017, 15:10:29 Ann Campbell wrote:
Okay, then RSPEC-3434 is the related RSpec, altho it's scope is different. I've added it as 'related'
endif::env-github,rspecator-view,env-vscode[]
endif::env-github,rspecator-view[]

View File

@ -85,3 +85,5 @@ False
Enable issues on unread attributes with a single underscore prefix
****
endif::env-github,rspecator-view[]

View File

@ -106,3 +106,5 @@ ifdef::env-github,rspecator-view[]
(visible only on this page)
'''
endif::env-github,rspecator-view[]

View File

@ -64,3 +64,5 @@ ifdef::env-github,rspecator-view[]
(visible only on this page)
'''
endif::env-github,rspecator-view[]

View File

@ -204,3 +204,5 @@ In any other case, when a dangerous peer definition is identified:
'''
endif::env-github,rspecator-view[]

View File

@ -250,3 +250,5 @@ In any other case, when a dangerous peer definition is identified:
'''
endif::env-github,rspecator-view[]

View File

@ -135,3 +135,5 @@ ifdef::env-github,rspecator-view[]
(visible only on this page)
'''
endif::env-github,rspecator-view[]

View File

@ -63,3 +63,5 @@ ifdef::env-github,rspecator-view[]
(visible only on this page)
The implementation should be common with S4462. When implementing, should make sure S4462 will ignore Azure Functions.
endif::env-github,rspecator-view[]

View File

@ -119,3 +119,4 @@ For `wget`:
* Highlight the `wget` command and the URL.
endif::env-github,rspecator-view[]

View File

@ -82,6 +82,18 @@ fun compare(a: Int, b: Int): Int {
}
----
== Resources
=== Documentation
* https://kotlinlang.org/docs/control-flow.html#when-expression[Kotlin Docs, When expression]
=== Articles & blog posts
* https://www.baeldung.com/kotlin/when[Baeldung, Guide to the “when{}” Block in Kotlin]
* https://superkotlin.com/kotlin-when-statement[Kotlin when: A switch with Superpowers]
* https://www.sonarsource.com/resources/cognitive-complexity[G. Ann Campbell, Cognitive Complexity]
ifdef::env-github,rspecator-view[]
== Implementation Specification
@ -99,16 +111,4 @@ ifdef::env-github,rspecator-view[]
Number of "if" after which the chain should be replaced by a "when" statement.
****
endif::[]
== Resources
=== Documentation
* https://kotlinlang.org/docs/control-flow.html#when-expression[Kotlin Docs, When expression]
=== Articles & blog posts
* https://www.baeldung.com/kotlin/when[Baeldung, Guide to the “when{}” Block in Kotlin]
* https://superkotlin.com/kotlin-when-statement[Kotlin when: A switch with Superpowers]
* https://www.sonarsource.com/resources/cognitive-complexity[G. Ann Campbell, Cognitive Complexity]
endif::env-github,rspecator-view[]

View File

@ -37,3 +37,5 @@ ifdef::env-github,rspecator-view[]
include::../common/message.adoc[]
'''
endif::env-github,rspecator-view[]

View File

@ -62,3 +62,5 @@ ifdef::env-github,rspecator-view[]
include::../common/message.adoc[]
'''
endif::env-github,rspecator-view[]

View File

@ -40,3 +40,5 @@ include::../common/message.adoc[]
include::../common/highlighting.adoc[]
'''
endif::env-github,rspecator-view[]