You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- :doc:`Basic query for JavaScript code <basic-query-for-javascript-code>`: Learn to write and run a simple CodeQL query using LGTM.
21
+
- :doc:`Basic query for JavaScript code <basic-query-for-javascript-code>`: Learn to write and run a simple CodeQL query.
22
22
23
23
- :doc:`CodeQL library for JavaScript <codeql-library-for-javascript>`: When you're analyzing a JavaScript program, you can make use of the large collection of classes in the CodeQL library for JavaScript.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/codeql-library-for-javascript.rst
+8-30Lines changed: 8 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ Textual level
43
43
44
44
At its most basic level, a JavaScript code base can simply be viewed as a collection of files organized into folders, where each file is composed of zero or more lines of text.
45
45
46
-
Note that the textual content of a program is not included in the CodeQL database unless you specifically request it during extraction. In particular, databases on LGTM (also known as "snapshots") do not normally include textual information.
46
+
Note that the textual content of a program is not included in the CodeQL database unless you specifically request it during extraction.
47
47
48
48
Files and folders
49
49
^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ For example, the following query computes, for each folder, the number of JavaSc
77
77
from Folder d
78
78
select d.getRelativePath(), count(File f | f = d.getAFile() and f.getExtension() = "js")
79
79
80
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1506075865985/>`__. When you run the query on most projects, the results include folders that contain files with a ``js`` extension and folders that don't.
80
+
When you run the query on most projects, the results include folders that contain files with a ``js`` extension and folders that don't.
81
81
82
82
Locations
83
83
^^^^^^^^^
@@ -138,7 +138,7 @@ As an example of a query operating entirely on the lexical level, consider the f
138
138
where comma.getNextToken() instanceof CommaToken
139
139
select comma, "Omitted array elements are bad style."
140
140
141
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/659662177/>`__. If the query returns no results, this pattern isn't used in the projects that you analyzed.
141
+
If the query returns no results, this pattern isn't used in the projects that you analyzed.
142
142
143
143
You can use predicate ``Locatable.getFirstToken()`` and ``Locatable.getLastToken()`` to access the first and last token (if any) belonging to an element with a source location.
144
144
@@ -179,8 +179,6 @@ As an example of a query using only lexical information, consider the following
179
179
from HtmlLineComment c
180
180
select c, "Do not use HTML comments."
181
181
182
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/686330023/>`__. When we ran this query on the *mozilla/pdf.js* project in LGTM.com, we found three HTML comments.
183
-
184
182
Syntactic level
185
183
~~~~~~~~~~~~~~~
186
184
@@ -230,7 +228,7 @@ The `TopLevel <https://codeql.github.com/codeql-standard-libraries/javascript/se
230
228
231
229
Note
232
230
233
-
By default, LGTM filters out alerts in minified top-levels, since they are often hard to interpret. When writing your own queries in the LGTM query console, this filtering is *not* done automatically, so you may want to explicitly add a condition of the form ``and not e.getTopLevel().isMinified()`` or similar to your query to exclude results in minified code.
231
+
By default, GitHub code scanning filters out alerts in minified top-levels, since they are often hard to interpret. When you write your own queries in Visual Studio Code, this filtering is *not* done automatically, so you may want to explicitly add a condition of the form ``and not e.getTopLevel().isMinified()`` or similar to your query to exclude results in minified code.
234
232
235
233
Statements and expressions
236
234
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -351,8 +349,6 @@ As an example of how to use expression AST nodes, here is a query that finds exp
351
349
where add = shift.getAnOperand()
352
350
select add, "This expression should be bracketed to clarify precedence rules."
353
351
354
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/690010024/>`__. When we ran this query on the *meteor/meteor* project in LGTM.com, we found many results where precedence could be clarified using brackets.
355
-
356
352
Functions
357
353
^^^^^^^^^
358
354
@@ -373,8 +369,6 @@ As an example, here is a query that finds all expression closures:
373
369
where fe.getBody() instanceof Expr
374
370
select fe, "Use arrow expressions instead of expression closures."
375
371
376
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/668510056/>`__. None of the LGTM.com demo projects uses expression closures, but you may find this query gets results on other projects.
377
-
378
372
As another example, this query finds functions that have two parameters that bind the same variable:
379
373
380
374
.. code-block:: ql
@@ -388,8 +382,6 @@ As another example, this query finds functions that have two parameters that bin
388
382
p.getAVariable() = q.getAVariable()
389
383
select fun, "This function has two parameters that bind the same variable."
390
384
391
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/673860037/>`__. None of the LGTM.com demo projects has functions where two parameters bind the same variable.
392
-
393
385
Classes
394
386
^^^^^^^
395
387
@@ -444,7 +436,7 @@ Here is an example of a query to find declaration statements that declare the sa
444
436
not ds.getTopLevel().isMinified()
445
437
select ds, "Variable " + v.getName() + " is declared both $@ and $@.", d1, "here", d2, "here"
446
438
447
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/668700496/>`__. This is not a common problem, so you may not find any results in your own projects. The *angular/angular.js* project on LGTM.com has one instance of this problem at the time of writing.
439
+
This is not a common problem, so you may not find any results in your own projects.
448
440
449
441
Notice the use of ``not ... isMinified()`` here and in the next few queries. This excludes any results found in minified code. If you delete ``and not ds.getTopLevel().isMinified()`` and re-run the query, two results in minified code in the *meteor/meteor* project are reported.
450
442
@@ -471,8 +463,6 @@ As an example of a query involving properties, consider the following query that
471
463
not oe.getTopLevel().isMinified()
472
464
select oe, "Property " + p1.getName() + " is defined both $@ and $@.", p1, "here", p2, "here"
473
465
474
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/660700064/>`__. Many projects have a few instances of object expressions with two identically named properties.
475
-
476
466
Modules
477
467
^^^^^^^
478
468
@@ -537,7 +527,7 @@ As an example, consider the following query which finds distinct function declar
537
527
not g.getTopLevel().isMinified()
538
528
select f, g
539
529
540
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/667290067/>`__. Some projects declare conflicting functions of the same name and rely on platform-specific behavior to disambiguate the two declarations.
530
+
Some projects declare conflicting functions of the same name and rely on platform-specific behavior to disambiguate the two declarations.
541
531
542
532
Control flow
543
533
~~~~~~~~~~~~
@@ -574,7 +564,7 @@ As an example of an analysis using basic blocks, ``BasicBlock.isLiveAtEntry(v, u
574
564
not f.getStartBB().isLiveAtEntry(gv, _)
575
565
select f, "This function uses " + gv + " like a local variable."
576
566
577
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/686320048/>`__. Many projects have some variables which look as if they were intended to be local.
567
+
Many projects have some variables which look as if they were intended to be local.
578
568
579
569
Data flow
580
570
~~~~~~~~~
@@ -599,8 +589,6 @@ As an example, the following query finds definitions of local variables that are
599
589
not exists (VarUse use | def = use.getADef())
600
590
select def, "Dead store of local variable."
601
591
602
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2086440429/>`__. Many projects have some examples of useless assignments to local variables.
603
-
604
592
SSA
605
593
^^^
606
594
@@ -642,8 +630,6 @@ For example, here is a query that finds all invocations of a method called ``sen
642
630
send.getMethodName() = "send"
643
631
select send
644
632
645
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1506058347056/>`__. The query finds HTTP response sends in the `AMP HTML <https://lgtm.com/projects/g/ampproject/amphtml>`__ project.
646
-
647
633
Note that the data flow modeling in this library is intraprocedural, that is, flow across function calls and returns is *not* modeled. Likewise, flow through object properties and global variables is not modeled.
648
634
649
635
Type inference
@@ -707,8 +693,6 @@ As an example of a call-graph-based query, here is a query to find invocations f
707
693
not exists(invk.getACallee())
708
694
select invk, "Unable to find a callee for this invocation."
709
695
710
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/3260345690335671362/>`__
711
-
712
696
Inter-procedural data flow
713
697
~~~~~~~~~~~~~~~~~~~~~~~~~~
714
698
@@ -843,7 +827,7 @@ As an example of the use of these classes, here is a query that counts for every
843
827
from NodeModule m
844
828
select m, count(m.getAnImportedModule())
845
829
846
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/659662207/>`__. When you analyze a project, for each module you can see how many other modules it imports.
830
+
When you analyze a project, for each module you can see how many other modules it imports.
847
831
848
832
NPM
849
833
^^^
@@ -872,8 +856,6 @@ As an example of the use of these classes, here is a query that identifies unuse
872
856
not exists (Require req | req.getTopLevel() = pkg.getAModule() | name = req.getImportedPath().getValue())
873
857
select deps, "Unused dependency '" + name + "'."
874
858
875
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/666680077/>`__. It is not uncommon for projects to have some unused dependencies.
876
-
877
859
React
878
860
^^^^^
879
861
@@ -899,8 +881,6 @@ For example, here is a query to find SQL queries that use string concatenation (
899
881
where ss instanceof AddExpr
900
882
select ss, "Use templating instead of string concatenation."
901
883
902
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1506076336224/>`__, showing two (benign) results on `strong-arc <https://lgtm.com/projects/g/strongloop/strong-arc/>`__.
903
-
904
884
Miscellaneous
905
885
~~~~~~~~~~~~~
906
886
@@ -965,8 +945,6 @@ As an example, here is a query that finds ``@param`` tags that do not specify th
965
945
not exists(t.getName())
966
946
select t, "@param tag is missing name."
967
947
968
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/673060054/>`__. Of the LGTM.com demo projects analyzed, only *Semantic-Org/Semantic-UI* has an example where the ``@param`` tag omits the name.
969
-
970
948
For full details on these and other classes representing JSDoc comments and type expressions, see `the API documentation <https://codeql.github.com/codeql-standard-libraries/javascript/semmle/javascript/JSDoc.qll/module.JSDoc.html>`__.
0 commit comments