Skip to content

Commit 4d50543

Browse files
authored
Merge pull request github#11141 from aibaars/docs-update
Ruby: expand explanation of desugaring
2 parents 9d55cd7 + 33b1c84 commit 4d50543

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

docs/codeql/codeql-language-guides/abstract-syntax-tree-classes-for-working-with-ruby-programs.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,15 @@ All classes in this subsection are subclasses of VariableAccess_.
418418
| ``self`` | SelfVariableReadAccess_ | VariableReadAccess_, SelfVariableAccess_ | |
419419
+----------------------------+------------------------------+-----------------------------------------------+------------------+
420420

421-
Desugaring
422-
~~~~~~~~~~
421+
Syntactic sugar and desugaring
422+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
423423

424-
Certain Ruby language features are implemented using syntactic sugar. For example, supposing that ``x`` is an object with an attribute ``foo``, the assignment::
424+
Certain Ruby language features are shorthands for common operations that could also be expressed in other, more verbose, forms.
425+
Such language features are typically referred to as "syntactic sugar", and make it easier for programmers to write and read code. This is
426+
great for programmers. For source code analyzers, however, this leads to additional work as they need to understand the shorthand notation as well as the long form. To make analysis easier, CodeQL automatically "desugars" Ruby code, effectively rewriting
427+
rich syntactic constructs into equivalent code that uses simpler syntactic constructs.
428+
429+
For example, supposing that ``x`` is an object with an attribute ``foo``, the assignment::
425430

426431
x.foo = y
427432

@@ -432,9 +437,9 @@ is desugared to code similar to::
432437

433438
In other words, there is effectively a call to the SetterMethodCall_ ``foo=`` on ``x`` with argument ``__synth_0 = y``, followed by a read of the ``__synth_0`` variable.
434439

435-
In CodeQL, this is implemented by syntheisizing AstNode_ instances corresponding to this desugared version of the code.
440+
In CodeQL, this is implemented by synthesizing AstNode_ instances corresponding to this desugared version of the code.
436441

437-
Note that both the original AssignExpr_ and the desugared SetterMethodCall_ versions are both available to CodeQL queries, and it is usually not necessary to be aware of any desugaring that may take place. However, if a codebase explicitly uses ``x.foo=(y)`` SetterMethodCall_ syntax, then this will not be found by a query for AssignExpr_ instances.
442+
Note that the original AssignExpr_ and the desugared SetterMethodCall_ versions are both available to use in CodeQL queries, and you do not usually need to be aware of any desugaring that may take place. However, if a codebase explicitly uses ``x.foo=(y)`` SetterMethodCall_ syntax, you cannot find this syntax by searching for instances of AssignExpr_ .
438443

439444
Other synthesized AstNode_ instances exist, see the isSynthesized_ and getDesugared_ predicates for details.
440445

0 commit comments

Comments
 (0)