Skip to content

Commit 9ec6c7d

Browse files
committed
JS: Avoid using global vars in documentation examples
1 parent 867bdcf commit 9ec6c7d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

docs/codeql/codeql-language-guides/using-flow-labels-for-precise-data-flow-analysis.rst

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,22 @@ For example, we would like to flag this code:
7070

7171
.. code-block:: javascript
7272
73-
var data = JSON.parse(str);
74-
if (data.length > 0) { // problematic: `data` may be `null`
75-
...
73+
function test(str) {
74+
var data = JSON.parse(str);
75+
if (data.length > 0) { // problematic: `data` may be `null`
76+
...
77+
}
7678
}
7779
7880
This code, on the other hand, should not be flagged:
7981

8082
.. code-block:: javascript
8183
82-
var data = JSON.parse(str);
83-
if (data && data.length > 0) { // unproblematic: `data` is first checked for nullness
84-
...
84+
function test(str) {
85+
var data = JSON.parse(str);
86+
if (data && data.length > 0) { // unproblematic: `data` is first checked for nullness
87+
...
88+
}
8589
}
8690
8791
We will first try to write a query to find this kind of problem without flow labels, and use the
@@ -168,11 +172,13 @@ checked for null-guardedness:
168172

169173
.. code-block:: javascript
170174
171-
var root = JSON.parse(str);
172-
if (root) {
173-
var payload = root.data; // unproblematic: `root` cannot be `null` here
174-
if (payload.length > 0) { // problematic: `payload` may be `null` here
175-
...
175+
function test(str) {
176+
var root = JSON.parse(str);
177+
if (root) {
178+
var payload = root.data; // unproblematic: `root` cannot be `null` here
179+
if (payload.length > 0) { // problematic: `payload` may be `null` here
180+
...
181+
}
176182
}
177183
}
178184

0 commit comments

Comments
 (0)