Skip to content

Commit 529b6a5

Browse files
authored
Merge pull request #19730 from owen-mc/update-qhelp-style-guide-for-markdown-format
Update qhelp style guide for markdown format
2 parents cdd6245 + 5bc974a commit 529b6a5

File tree

1 file changed

+137
-21
lines changed

1 file changed

+137
-21
lines changed

docs/query-help-style-guide.md

Lines changed: 137 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,33 @@ When you contribute a new [supported query](supported-queries.md) to this reposi
1616

1717
### Location and file name
1818

19-
Query help files must have the same base name as the query they describe and must be located in the same directory.
19+
Query help files must have the same base name as the query they describe and must be located in the same directory.
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. There are a few minor differences, noted in the section `Differences between XML and markdown formats` below.
24+
25+
#### Markdown query help files
26+
27+
A Markdown query help file should use the following structure and section order (note that the `Implementation notes` section is optional):
28+
29+
```
30+
## Overview
31+
32+
## Recommendation
33+
34+
## Example
35+
36+
## Implementation notes
37+
38+
## References
39+
```
40+
41+
Each section should be clearly marked with the appropriate heading. See the other Markdown files in this repository for examples.
42+
43+
#### XML query help files
44+
45+
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:
2446

2547
```xml
2648
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
@@ -33,7 +55,7 @@ The header and single top-level `<qhelp>` element are both mandatory.
3355

3456
### Section-level elements
3557

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:
58+
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:
3759

3860
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.
3961
2. `recommendation`—information on how to fix the issue highlighted by the query.
@@ -42,10 +64,9 @@ Section-level elements are used to group the information within the query help f
4264

4365
For further information about the other section-level, block, list and table elements supported by query help files, see [Query help files](https://codeql.github.com/docs/writing-codeql-queries/query-help-files/) on codeql.github.com.
4466

45-
4667
## English style
4768

48-
You should write the overview and recommendation elements in simple English that is easy to follow. You should:
69+
You should write the overview and recommendation sections in simple English that is easy to follow. You should:
4970

5071
* Use simple sentence structures and avoid complex or academic language.
5172
* Avoid colloquialisms and contractions.
@@ -57,10 +78,11 @@ You should write the overview and recommendation elements in simple English that
5778
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:
5879

5980
* 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.
60-
* Put the code example after the recommendation element where possible. Only include an example in the description element if absolutely necessary.
81+
* Put the code example after the recommendation section where possible. Only include an example in the description section if absolutely necessary.
6182
* 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.
6283
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
63-
* Define the code examples in `src` files. The language is inferred from the file extension:
84+
* For Markdown files, use fenced code blocks with the appropriate language identifier (for example, <code> ```java </code>).
85+
* For XML files, define the code examples in `src` files. The language is inferred from the file extension:
6486

6587
```xml
6688
<example>
@@ -74,11 +96,11 @@ Whenever possible, you should include a code example that helps to explain the i
7496
</example>
7597
```
7698

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

79101
## Including references
80102

81-
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:
103+
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:
82104

83105
### Books
84106

@@ -90,15 +112,14 @@ For example:
90112

91113
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
92114
93-
Note, & symbols need to be replaced by \&amp;. The symbol will be displayed correctly in the HTML files generated from the query help files.
115+
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.
94116

95117
### Academic papers
96118

97119
If you are citing an academic paper, we recommend adopting the reference style of the journal that you are citing. For example:
98120

99121
>S. R. Chidamber and C. F. Kemerer, _A metrics suite for object-oriented design_. IEEE Transactions on Software Engineering, 20(6):476-493, 1994.
100122
101-
102123
### Websites
103124

104125
If you are citing a website, please use the following format, without breadcrumb trails:
@@ -111,28 +132,123 @@ For example:
111132
112133
### Referencing potential security weaknesses
113134

114-
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.
135+
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 in a query help file in the custom XML format, 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.
115136

116-
## Validating qhelp files
137+
## Validating query help files
117138

118-
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:
139+
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:
119140

120141
```bash
121142
# codeql generate query-help <path_to_your_qhelp_file> --format=<format>
122143
# For example:
123144
codeql generate query-help ./myCustomQuery.qhelp --format=markdown
145+
codeql generate query-help ./myCustomQuery.md --format=markdown
124146
```
125147

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

127-
Please include the `.qhelp` files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
150+
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)
128151

129-
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)
152+
## Differences between XML and markdown formats
153+
154+
1. The XML format allows for the contents of other files to be included in the output generated by processing the file, as mentioned in the section `Code examples`. This is not possible with the Markdown format.
155+
2. When using the XML format, references are added to the output HTML file based on CWE tags, as mentioned in the section `Referencing potential security weaknesses`.
156+
3. For custom queries and custom query packs, only the Markdown format is supported.
130157

131158
## Query help example
132159

133-
The following example is a query help file for a query from the standard query suite for Java:
160+
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.
134161

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

157-
<p>You should also ensure that the indentation of the code is consistent with the actual flow of
273+
<p>You should also ensure that the indentation of the code is consistent with the actual flow of
158274
control, so that it does not confuse programmers.</p>
159275

160276
</recommendation>
161277
<example>
162278

163-
<p>In the example below, the original version of <code>Cart</code> is missing braces. This means
279+
<p>In the example below, the original version of <code>Cart</code> is missing braces. This means
164280
that the code triggers a <code>NullPointerException</code> at runtime if <code>i</code>
165281
is <code>null</code>.</p>
166282

@@ -198,4 +314,4 @@ tab width settings cannot be taken into account.
198314

199315
</references>
200316
</qhelp>
201-
```
317+
````

0 commit comments

Comments
 (0)