Skip to content

Commit 25adcc8

Browse files
committed
Add Markdown format to guide
1 parent c8a5418 commit 25adcc8

File tree

1 file changed

+122
-16
lines changed

1 file changed

+122
-16
lines changed

docs/query-help-style-guide.md

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,23 @@ Query help files must have the same base name as the query they describe and mus
2020

2121
### File structure and layout
2222

23-
Query help files are written using a custom XML format, and stored in a file with a `.qhelp` extension. The basic structure is as follows:
23+
Query help files can be written in either a custom XML format (with a `.qhelp` extension) or in Markdown (with a `.md` extension). Both formats are supported by the CodeQL documentation tooling.
24+
25+
#### Markdown query help files
26+
27+
A Markdown query help file should use the following structure and section order:
28+
29+
1. **Overview** (level 2 heading, `## Overview`)
30+
2. **Recommendation** (level 2 heading, `## Recommendation`)
31+
3. **Example** (level 2 heading, `## Example`)
32+
4. **Implementation notes** (optional, level 2 heading, `## Implementation notes`)
33+
5. **References** (level 2 heading, `## References`)
34+
35+
Each section should be clearly marked with the appropriate heading. See the other Markdown files in this repository for examples.
36+
37+
#### XML query help files
38+
39+
Query help files can also be written using a custom XML format, and stored in a file with a `.qhelp` extension. The basic structure is as follows:
2440

2541
```xml
2642
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
@@ -33,7 +49,7 @@ The header and single top-level `<qhelp>` element are both mandatory.
3349

3450
### Section-level elements
3551

36-
Section-level elements are used to group the information within the query help file. All query help files should include at least the following section elements, in the order specified:
52+
Section-level elements are used to group the information within the query help file. For both Markdown and XML formats, the following sections should be included, in the order specified:
3753

3854
1. `overview`—a short summary of the issue that the query identifies, including an explanation of how it could affect the behavior of the program.
3955
2. `recommendation`—information on how to fix the issue highlighted by the query.
@@ -44,7 +60,7 @@ For further information about the other section-level, block, list and table ele
4460

4561
## English style
4662

47-
You should write the overview and recommendation elements in simple English that is easy to follow. You should:
63+
You should write the overview and recommendation sections in simple English that is easy to follow. You should:
4864

4965
* Use simple sentence structures and avoid complex or academic language.
5066
* Avoid colloquialisms and contractions.
@@ -56,10 +72,11 @@ You should write the overview and recommendation elements in simple English that
5672
Whenever possible, you should include a code example that helps to explain the issue you are highlighting. Any code examples that you include should adhere to the following guidelines:
5773

5874
* The example should be less than 20 lines, but it should still clearly illustrate the issue that the query identifies. If appropriate, then the example may also be runnable.
59-
* Put the code example after the recommendation element where possible. Only include an example in the description element if absolutely necessary.
75+
* Put the code example after the recommendation section where possible. Only include an example in the description section if absolutely necessary.
6076
* If you are using an example to illustrate the solution to a problem, and the change required is minor, avoid repeating the whole example. It is preferable to either describe the change required or to include a smaller snippet of the corrected code.
6177
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
62-
* Define the code examples in `src` files. The language is inferred from the file extension:
78+
* For Markdown files, use fenced code blocks with the appropriate language identifier (for example, <code> ```java </code>).
79+
* For XML files, define the code examples in `src` files. The language is inferred from the file extension:
6380

6481
```xml
6582
<example>
@@ -73,11 +90,11 @@ Whenever possible, you should include a code example that helps to explain the i
7390
</example>
7491
```
7592

76-
Note, if any code words are included in the `overview` and `recommendation` sections, they should be formatted with `<code> ... </code>` for emphasis.
93+
Note, if any code words are included in the `overview` and `recommendation` sections, in Markdown they should be formatted with backticks (<code>`...`</code>) and in XML they should be formatted with`<code> ... </code>` for emphasis.
7794

7895
## Including references
7996

80-
You should include one or more references, list formatted with `<li> ... </li>` for each item, to provide further information about the problem that your query is designed to find. References can be of the following types:
97+
You should include one or more references, formatted as an unordered list (`- ...` or `* ...`) in Markdown or with `<li> ... </li>` for each item in XML, to provide further information about the problem that your query is designed to find. References can be of the following types:
8198

8299
### Books
83100

@@ -89,7 +106,7 @@ For example:
89106

90107
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
91108
92-
Note, & symbols need to be replaced by \&amp;. The symbol will be displayed correctly in the HTML files generated from the query help files.
109+
Note, & symbols need to be replaced by \&amp; in XML. The symbol will be displayed correctly in the HTML files generated from the query help files.
93110

94111
### Academic papers
95112

@@ -111,26 +128,115 @@ For example:
111128

112129
If your query checks code for a CWE weakness, you should use the `@tags` element in the query file to reference the associated CWEs, as explained [here](query-metadata-style-guide.md). When you use these tags, a link to the appropriate entry from the [MITRE.org](https://cwe.mitre.org/scoring/index.html) site will automatically appear as a reference in the output HTML file.
113130

114-
## Validating qhelp files
131+
## Validating query help files
115132

116-
Before making a pull request, please ensure the `.qhelp` files are well-formed and can be generated without errors. This can be done locally with the CodeQL CLI, as shown in the following example:
133+
Before making a pull request, please ensure the `.qhelp` or `.md` files are well-formed and can be generated without errors. This can be done locally with the CodeQL CLI, as shown in the following example:
117134

118135
```bash
119136
# codeql generate query-help <path_to_your_qhelp_file> --format=<format>
120137
# For example:
121138
codeql generate query-help ./myCustomQuery.qhelp --format=markdown
139+
codeql generate query-help ./myCustomQuery.md --format=markdown
122140
```
123141

142+
Please include the query help files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
124143

125-
Please include the `.qhelp` files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
126-
127-
More information on how to test your `.qhelp` files can be found [within the documentation](https://docs.github.com/en/code-security/codeql-cli/using-the-codeql-cli/testing-query-help-files)
144+
More information on how to test your query help files can be found [within the documentation](https://docs.github.com/en/code-security/codeql-cli/using-the-codeql-cli/testing-query-help-files)
128145

129146
## Query help example
130147

131-
The following example is a query help file for a query from the standard query suite for Java:
148+
The following example is a query help file for a query from the standard query suite for Java, shown in both Markdown and XML formats.
132149

133-
```xml
150+
### Markdown example
151+
152+
````markdown
153+
# Overview
154+
155+
A control structure (an `if` statement or a loop) has a body that is either a block
156+
of statements surrounded by curly braces or a single statement.
157+
158+
If you omit braces, it is particularly important to ensure that the indentation of the code
159+
matches the control flow of the code.
160+
161+
## Recommendation
162+
163+
It is usually considered good practice to include braces for all control
164+
structures in Java. This is because it makes it easier to maintain the code
165+
later. For example, it's easy to see at a glance which part of the code is in the
166+
scope of an `if` statement, and adding more statements to the body of the `if`
167+
statement is less error-prone.
168+
169+
You should also ensure that the indentation of the code is consistent with the actual flow of
170+
control, so that it does not confuse programmers.
171+
172+
## Example
173+
174+
In the example below, the original version of `Cart` is missing braces. This means
175+
that the code triggers a `NullPointerException` at runtime if `i`
176+
is `null`.
177+
178+
```java
179+
class Cart {
180+
Map<Integer, Integer> items = ...
181+
public void addItem(Item i) {
182+
// No braces and misleading indentation.
183+
if (i != null)
184+
log("Adding item: " + i);
185+
// Indentation suggests that the following statements
186+
// are in the body of the 'if'.
187+
Integer curQuantity = items.get(i.getID());
188+
if (curQuantity == null) curQuantity = 0;
189+
items.put(i.getID(), curQuantity+1);
190+
}
191+
}
192+
```
193+
194+
The corrected version of `Cart` does include braces, so
195+
that the code executes as the indentation suggests.
196+
197+
```java
198+
class Cart {
199+
Map<Integer, Integer> items = ...
200+
public void addItem(Item i) {
201+
// Braces included.
202+
if (i != null) {
203+
log("Adding item: " + i);
204+
Integer curQuantity = items.get(i.getID());
205+
if (curQuantity == null) curQuantity = 0;
206+
items.put(i.getID(), curQuantity+1);
207+
}
208+
}
209+
}
210+
```
211+
212+
In the following example the indentation may or may not be misleading depending on your tab width
213+
settings. As such, mixing tabs and spaces in this way is not recommended, since what looks fine in
214+
one context can be very misleading in another.
215+
216+
```java
217+
// Tab width 8
218+
if (b) // Indentation: 1 tab
219+
f(); // Indentation: 2 tabs
220+
g(); // Indentation: 8 spaces
221+
222+
// Tab width 4
223+
if (b) // Indentation: 1 tab
224+
f(); // Indentation: 2 tabs
225+
g(); // Indentation: 8 spaces
226+
```
227+
228+
If you mix tabs and spaces in this way, then you might get seemingly false positives, since your
229+
tab width settings cannot be taken into account.
230+
231+
## References
232+
233+
* Java SE Documentation: [Compound Statements](https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#15395)
234+
* Wikipedia: [Indentation style](https://en.wikipedia.org/wiki/Indentation_style)
235+
````
236+
237+
### XML example
238+
239+
````xml
134240
<!DOCTYPE qhelp PUBLIC
135241
"-//Semmle//qhelp//EN"
136242
"qhelp.dtd">
@@ -196,4 +302,4 @@ tab width settings cannot be taken into account.
196302

197303
</references>
198304
</qhelp>
199-
```
305+
````

0 commit comments

Comments
 (0)