File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
semmle/javascript/frameworks Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ category : minorAnalysis
3+ ---
4+ * Added ` PreCallGraphStep ` flow model for React's ` useRef ` hook.
5+ * Added a ` DomValueSource ` that uses the ` current ` property off the object returned by React's ` useRef ` hook.
Original file line number Diff line number Diff line change @@ -612,6 +612,25 @@ private class UseStateStep extends PreCallGraphStep {
612612 }
613613}
614614
615+ /**
616+ * Step through a `useRef` call.
617+ *
618+ * It returns an object with a single property (`current`) initialized to the initial value.
619+ *
620+ * For example:
621+ * ```js
622+ * const inputRef1 = useRef(initialValue);
623+ * ```
624+ */
625+ private class UseRefStep extends PreCallGraphStep {
626+ override predicate step ( DataFlow:: Node pred , DataFlow:: Node succ ) {
627+ exists ( DataFlow:: CallNode call | call = react ( ) .getAMemberCall ( "useRef" ) |
628+ pred = call .getArgument ( 0 ) and // initial state
629+ succ = call .getAPropertyRead ( "current" )
630+ )
631+ }
632+ }
633+
615634/**
616635 * A step through a React context object.
617636 *
@@ -785,6 +804,17 @@ private class ReactRouterLocationSource extends DOM::LocationSource::Range {
785804 }
786805}
787806
807+ private class UseRefDomValueSource extends DOM:: DomValueSource:: Range {
808+ UseRefDomValueSource ( ) {
809+ this =
810+ any ( JsxAttribute attrib | attrib .getName ( ) = "ref" )
811+ .getValue ( )
812+ .flow ( )
813+ .getALocalSource ( )
814+ .getAPropertyRead ( "current" )
815+ }
816+ }
817+
788818/**
789819 * Gets a reference to a function which, if called with a React component, returns wrapped
790820 * version of that component, which we model as a direct reference to the underlying component.
You can’t perform that action at this time.
0 commit comments