Skip to content

Commit 1c54642

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

File tree

1 file changed

+123
-14
lines changed

1 file changed

+123
-14
lines changed

docs/query-help-style-guide.md

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,25 @@ 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. Title (level 1 heading, `#`)
30+
2. Short summary (optional, can be included after the title)
31+
3. **Overview** (level 2 heading, `## Overview`)
32+
4. **Recommendation** (level 2 heading, `## Recommendation`)
33+
5. **Example** (level 2 heading, `## Example`)
34+
6. **Implementation notes** (optional, level 2 heading, `## Implementation notes`)
35+
7. **References** (level 2 heading, `## References`)
36+
37+
Each section should be clearly marked with the appropriate heading. See the other Markdown files in this repository for examples.
38+
39+
#### XML query help files
40+
41+
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:
2442

2543
```xml
2644
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
@@ -33,7 +51,7 @@ The header and single top-level `<qhelp>` element are both mandatory.
3351

3452
### Section-level elements
3553

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:
54+
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:
3755

3856
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.
3957
2. `recommendation`—information on how to fix the issue highlighted by the query.
@@ -44,7 +62,7 @@ For further information about the other section-level, block, list and table ele
4462

4563
## English style
4664

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

4967
* Use simple sentence structures and avoid complex or academic language.
5068
* Avoid colloquialisms and contractions.
@@ -56,10 +74,11 @@ You should write the overview and recommendation elements in simple English that
5674
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:
5775

5876
* 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.
77+
* Put the code example after the recommendation section where possible. Only include an example in the description section if absolutely necessary.
6078
* 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.
6179
* 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:
80+
* For Markdown files, use fenced code blocks with the appropriate language identifier (for example, <code> ```java </code>).
81+
* For XML files, define the code examples in `src` files. The language is inferred from the file extension:
6382

6483
```xml
6584
<example>
@@ -73,11 +92,11 @@ Whenever possible, you should include a code example that helps to explain the i
7392
</example>
7493
```
7594

76-
Note, if any code words are included in the `overview` and `recommendation` sections, they should be formatted with `<code> ... </code>` for emphasis.
95+
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.
7796

7897
## Including references
7998

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:
99+
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:
81100

82101
### Books
83102

@@ -89,7 +108,7 @@ For example:
89108

90109
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
91110
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.
111+
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.
93112

94113
### Academic papers
95114

@@ -111,24 +130,114 @@ For example:
111130

112131
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.
113132

114-
## Validating qhelp files
133+
## Validating query help files
115134

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:
135+
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:
117136

118137
```bash
119138
# codeql generate query-help <path_to_your_qhelp_file> --format=<format>
120139
# For example:
121140
codeql generate query-help ./myCustomQuery.qhelp --format=markdown
141+
codeql generate query-help ./myCustomQuery.md --format=markdown
122142
```
123143

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

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)
146+
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)
128147

129148
## Query help example
130149

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

0 commit comments

Comments
 (0)