Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ string getAnArrayContent() {
// Values stored at an unknown index
result = "ArrayElement[?]"
}

/**
* Gets an argument position up to a certain limit.
*
* This can be used to generate flow summaries that should preserve such positions.
*/
int getAnArgumentPosition() { result = [0 .. 10] }
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,29 @@ private class PromiseWithResolversLike extends SummarizedCallable {
)
}
}

class PromiseTry extends DataFlow::SummarizedCallable {
PromiseTry() { this = "Promise.try()" }

override DataFlow::CallNode getACallSimple() {
result = promiseConstructorRef().getAMemberCall(["try", "attempt"])
or
result = DataFlow::moduleImport(["p-try", "es6-promise-try"]).getACall()
}

override predicate propagatesFlow(string input, string output, boolean preservesValue) {
preservesValue = true and
(
exists(int i | i = getAnArgumentPosition() |
input = "Argument[" + (i + 1) + "]" and
output = "Argument[0].Parameter[" + i + "]"
)
Comment thread
Napalys marked this conversation as resolved.
Comment on lines +384 to +387
Copy link
Copy Markdown
Contributor

@Napalys Napalys Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick question for my understanding. We would not have flow if a lambda would have more then 10 arguments?
Also what happens if we have ...args(arbitrary number of arguments), will it work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct on both accounts

or
input = "Argument[0].ReturnValue" and
output = "ReturnValue.Awaited"
or
input = "Argument[0].ReturnValue[exception]" and
output = "ReturnValue.Awaited[error]"
)
}
}
29 changes: 29 additions & 0 deletions javascript/ql/test/library-tests/TripleDot/promise-try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
async function t1() {
const promise = Promise.try(() => {
return source('try.1');
});
sink(await promise); // $ hasValueFlow=try.1
}

async function t2() {
const promise = Promise.try((x) => {
return x
}, source('try.2'));
sink(await promise); // $ hasValueFlow=try.2
}

async function t3() {
const promise = Promise.try((x) => {
throw x;
}, source('try.3'));
promise.catch(err => {
sink(err); // $ hasValueFlow=try.3
});
}

async function t4() {
const promise = Promise.try((x, y) => {
return y;
}, source('try.4.1'), source('try.4.2'));
sink(await promise); // $ hasValueFlow=try.4.2
}