From 5664d90285ee242f2c47c829b82923536de505c9 Mon Sep 17 00:00:00 2001 From: Asger F Date: Tue, 4 Feb 2025 10:42:28 +0100 Subject: [PATCH 1/2] JS: Make the function decl ID belong to the outer container --- .../src/com/semmle/js/extractor/ASTExtractor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java b/javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java index 0dc00f79ab03..5f48e4ef6d1d 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java +++ b/javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java @@ -1052,10 +1052,6 @@ public Label visit(AFunctionExpression nd, Context c) { } private void extractFunction(IFunction nd, Label key) { - // If the function has no body we process it like any other function, it - // just won't contain any statements. - contextManager.enterContainer(key); - boolean bodyIsStrict = isStrict; if (!isStrict && nd.getBody() instanceof BlockStatement) bodyIsStrict = hasUseStrict(((BlockStatement) nd.getBody()).getBody()); @@ -1065,6 +1061,10 @@ private void extractFunction(IFunction nd, Label key) { visit(nd.getId(), key, -1, IdContext.VAR_DECL); } + // If the function has no body we process it like any other function, it + // just won't contain any statements. + contextManager.enterContainer(key); + DeclaredNames locals = scopeManager.collectDeclaredNames(nd.getBody(), bodyIsStrict, false, DeclKind.none); From 0d266fd513e503b44a471c91ed57e5710d02ea96 Mon Sep 17 00:00:00 2001 From: Asger F Date: Tue, 4 Feb 2025 12:32:31 +0100 Subject: [PATCH 2/2] JS: Test output changes --- .../ql/test/library-tests/Expr/tests.expected | 20 +++++++++---------- .../test/library-tests/stmts/tests.expected | 3 +++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/javascript/ql/test/library-tests/Expr/tests.expected b/javascript/ql/test/library-tests/Expr/tests.expected index 3e48ec37a20f..986274b8841f 100644 --- a/javascript/ql/test/library-tests/Expr/tests.expected +++ b/javascript/ql/test/library-tests/Expr/tests.expected @@ -1869,7 +1869,7 @@ test_getContainer | es2015.js:6:5:6:9 | Array | es2015.js:1:1:29:0 | | | es2015.js:6:11:6:17 | ...elts | es2015.js:1:1:29:0 | | | es2015.js:6:14:6:17 | elts | es2015.js:1:1:29:0 | | -| es2015.js:8:10:8:10 | f | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | +| es2015.js:8:10:8:10 | f | es2015.js:1:1:29:0 | | | es2015.js:8:12:8:17 | [x, y] | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:8:13:8:13 | x | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:8:16:8:16 | y | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | @@ -1888,7 +1888,7 @@ test_getContainer | es2015.js:13:3:13:16 | console.log(d) | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:13:11:13:13 | log | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:13:15:13:15 | d | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | -| es2015.js:17:10:17:10 | g | es2015.js:17:1:20:1 | functio ... rn w;\\n} | +| es2015.js:17:10:17:10 | g | es2015.js:1:1:29:0 | | | es2015.js:17:12:17:22 | { x, y: z } | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:17:14:17:14 | x | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:17:14:17:14 | x | es2015.js:17:1:20:1 | functio ... rn w;\\n} | @@ -1900,7 +1900,7 @@ test_getContainer | es2015.js:18:13:18:13 | w | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:18:19:18:19 | z | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:19:9:19:9 | w | es2015.js:17:1:20:1 | functio ... rn w;\\n} | -| es2015.js:22:11:22:13 | foo | es2015.js:22:1:26:1 | functio ... o(1);\\n} | +| es2015.js:22:11:22:13 | foo | es2015.js:1:1:29:0 | | | es2015.js:22:15:22:15 | n | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:23:8:23:8 | n | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:23:8:23:10 | n-- | es2015.js:22:1:26:1 | functio ... o(1);\\n} | @@ -1912,13 +1912,13 @@ test_getContainer | es2015.js:25:9:25:11 | foo | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:25:9:25:14 | foo(1) | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:25:13:25:13 | 1 | es2015.js:22:1:26:1 | functio ... o(1);\\n} | -| es2015.js:28:10:28:12 | bar | es2015.js:28:1:28:26 | functio ... +19) {} | +| es2015.js:28:10:28:12 | bar | es2015.js:1:1:29:0 | | | es2015.js:28:14:28:14 | x | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:17:28:17 | y | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:19:28:19 | x | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:19:28:22 | x+19 | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:21:28:22 | 19 | es2015.js:28:1:28:26 | functio ... +19) {} | -| functions.js:1:10:1:10 | f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | +| functions.js:1:10:1:10 | f | functions.js:1:1:10:7 | | | functions.js:2:2:2:5 | in_f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:3:9:3:17 | also_in_f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:4:3:6:4 | (functi ... f;\\n\\t\\t}) | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | @@ -2349,6 +2349,7 @@ test_getEnclosingStmt | es2015.js:6:5:6:9 | Array | es2015.js:6:1:6:19 | new Array(...elts); | | es2015.js:6:11:6:17 | ...elts | es2015.js:6:1:6:19 | new Array(...elts); | | es2015.js:6:14:6:17 | elts | es2015.js:6:1:6:19 | new Array(...elts); | +| es2015.js:8:10:8:10 | f | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:9:6:9:15 | [a, [, c]] | es2015.js:9:2:9:20 | var [a, [, c]] = x; | | es2015.js:9:6:9:19 | [a, [, c]] = x | es2015.js:9:2:9:20 | var [a, [, c]] = x; | | es2015.js:9:7:9:7 | a | es2015.js:9:2:9:20 | var [a, [, c]] = x; | @@ -2364,12 +2365,14 @@ test_getEnclosingStmt | es2015.js:13:3:13:16 | console.log(d) | es2015.js:13:3:13:17 | console.log(d); | | es2015.js:13:11:13:13 | log | es2015.js:13:3:13:17 | console.log(d); | | es2015.js:13:15:13:15 | d | es2015.js:13:3:13:17 | console.log(d); | +| es2015.js:17:10:17:10 | g | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:18:6:18:15 | { [x]: w } | es2015.js:18:2:18:20 | var { [x]: w } = z; | | es2015.js:18:6:18:19 | { [x]: w } = z | es2015.js:18:2:18:20 | var { [x]: w } = z; | | es2015.js:18:9:18:9 | x | es2015.js:18:2:18:20 | var { [x]: w } = z; | | es2015.js:18:13:18:13 | w | es2015.js:18:2:18:20 | var { [x]: w } = z; | | es2015.js:18:19:18:19 | z | es2015.js:18:2:18:20 | var { [x]: w } = z; | | es2015.js:19:9:19:9 | w | es2015.js:19:2:19:10 | return w; | +| es2015.js:22:11:22:13 | foo | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:23:8:23:8 | n | es2015.js:23:2:24:10 | while(n ... ield n; | | es2015.js:23:8:23:10 | n-- | es2015.js:23:2:24:10 | while(n ... ield n; | | es2015.js:23:8:23:12 | n-->0 | es2015.js:23:2:24:10 | while(n ... ield n; | @@ -2380,6 +2383,8 @@ test_getEnclosingStmt | es2015.js:25:9:25:11 | foo | es2015.js:25:2:25:15 | yield* foo(1); | | es2015.js:25:9:25:14 | foo(1) | es2015.js:25:2:25:15 | yield* foo(1); | | es2015.js:25:13:25:13 | 1 | es2015.js:25:2:25:15 | yield* foo(1); | +| es2015.js:28:10:28:12 | bar | es2015.js:28:1:28:26 | functio ... +19) {} | +| functions.js:1:10:1:10 | f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:2:2:2:5 | in_f | functions.js:2:2:2:6 | in_f; | | functions.js:3:9:3:17 | also_in_f | functions.js:3:2:8:5 | while ( ... n\\n\\t\\t}); | | functions.js:4:3:6:4 | (functi ... f;\\n\\t\\t}) | functions.js:4:3:8:5 | (functi ... n\\n\\t\\t}); | @@ -2653,7 +2658,6 @@ test_getEnclosingFunction | es2015.js:3:20:3:24 | alert | es2015.js:3:12:3:39 | () => { ... p!"); } | | es2015.js:3:20:3:36 | alert("Wake up!") | es2015.js:3:12:3:39 | () => { ... p!"); } | | es2015.js:3:26:3:35 | "Wake up!" | es2015.js:3:12:3:39 | () => { ... p!"); } | -| es2015.js:8:10:8:10 | f | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:8:12:8:17 | [x, y] | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:8:13:8:13 | x | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:8:16:8:16 | y | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | @@ -2672,7 +2676,6 @@ test_getEnclosingFunction | es2015.js:13:3:13:16 | console.log(d) | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:13:11:13:13 | log | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | | es2015.js:13:15:13:15 | d | es2015.js:8:1:15:1 | functio ... );\\n\\t}\\n} | -| es2015.js:17:10:17:10 | g | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:17:12:17:22 | { x, y: z } | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:17:14:17:14 | x | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:17:14:17:14 | x | es2015.js:17:1:20:1 | functio ... rn w;\\n} | @@ -2684,7 +2687,6 @@ test_getEnclosingFunction | es2015.js:18:13:18:13 | w | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:18:19:18:19 | z | es2015.js:17:1:20:1 | functio ... rn w;\\n} | | es2015.js:19:9:19:9 | w | es2015.js:17:1:20:1 | functio ... rn w;\\n} | -| es2015.js:22:11:22:13 | foo | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:22:15:22:15 | n | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:23:8:23:8 | n | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:23:8:23:10 | n-- | es2015.js:22:1:26:1 | functio ... o(1);\\n} | @@ -2696,13 +2698,11 @@ test_getEnclosingFunction | es2015.js:25:9:25:11 | foo | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:25:9:25:14 | foo(1) | es2015.js:22:1:26:1 | functio ... o(1);\\n} | | es2015.js:25:13:25:13 | 1 | es2015.js:22:1:26:1 | functio ... o(1);\\n} | -| es2015.js:28:10:28:12 | bar | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:14:28:14 | x | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:17:28:17 | y | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:19:28:19 | x | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:19:28:22 | x+19 | es2015.js:28:1:28:26 | functio ... +19) {} | | es2015.js:28:21:28:22 | 19 | es2015.js:28:1:28:26 | functio ... +19) {} | -| functions.js:1:10:1:10 | f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:2:2:2:5 | in_f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:3:9:3:17 | also_in_f | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | | functions.js:4:3:6:4 | (functi ... f;\\n\\t\\t}) | functions.js:1:1:9:1 | functio ... \\t\\t});\\n} | diff --git a/javascript/ql/test/library-tests/stmts/tests.expected b/javascript/ql/test/library-tests/stmts/tests.expected index 99171999eb8c..f090c05c6b0e 100644 --- a/javascript/ql/test/library-tests/stmts/tests.expected +++ b/javascript/ql/test/library-tests/stmts/tests.expected @@ -140,12 +140,15 @@ test_EnclosingStmt | foreach.js:8:1:8:16 | console.log(sum) | foreach.js:8:1:8:17 | console.log(sum); | | foreach.js:8:9:8:11 | log | foreach.js:8:1:8:17 | console.log(sum); | | foreach.js:8:13:8:15 | sum | foreach.js:8:1:8:17 | console.log(sum); | +| functions.js:1:10:1:10 | g | functions.js:1:1:3:1 | functio ... x+y;\\n} | | functions.js:2:12:2:12 | x | functions.js:2:5:2:15 | return x+y; | | functions.js:2:12:2:14 | x+y | functions.js:2:5:2:15 | return x+y; | | functions.js:2:14:2:14 | y | functions.js:2:5:2:15 | return x+y; | +| functions.js:5:10:5:10 | h | functions.js:5:1:5:15 | function h() {} | | functions.js:7:1:7:1 | k | functions.js:7:1:9:1 | k = fun ... turn;\\n} | | functions.js:7:1:9:1 | k = fun ... turn;\\n} | functions.js:7:1:9:1 | k = fun ... turn;\\n} | | functions.js:7:5:9:1 | functio ... turn;\\n} | functions.js:7:1:9:1 | k = fun ... turn;\\n} | +| guardedCatch.js:1:10:1:10 | f | guardedCatch.js:1:1:9:1 | functio ... );\\n\\t}\\n} | | guardedCatch.js:3:3:3:3 | g | guardedCatch.js:3:3:3:6 | g(); | | guardedCatch.js:3:3:3:5 | g() | guardedCatch.js:3:3:3:6 | g(); | | guardedCatch.js:4:11:4:11 | e | guardedCatch.js:4:4:6:2 | catch ( ... !");\\n\\t} |