Skip to content

Commit f169251

Browse files
authored
Merge pull request #20668 from knewbury01/knewbury01/add-react-useref-step
Add React missing useRef related constructs
2 parents 47d0863 + 806fcb2 commit f169251

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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.

javascript/ql/lib/semmle/javascript/frameworks/React.qll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)