From 841dbe02704e19183c223ec72d2b39d676bd9140 Mon Sep 17 00:00:00 2001 From: Heejong Lee Date: Fri, 22 Nov 2024 01:44:28 -0800 Subject: [PATCH] Support ellipsis in typescript function parameters (#518) --- .../semgrep-typescript/common/semgrep-ext.js | 12 ++++ .../typescript/corpus/semgrep-ext.txt | 64 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/lang/semgrep-grammars/src/semgrep-typescript/common/semgrep-ext.js b/lang/semgrep-grammars/src/semgrep-typescript/common/semgrep-ext.js index 9f289042..78b0426d 100644 --- a/lang/semgrep-grammars/src/semgrep-typescript/common/semgrep-ext.js +++ b/lang/semgrep-grammars/src/semgrep-typescript/common/semgrep-ext.js @@ -13,6 +13,7 @@ module.exports = { [$.semgrep_expression_ellipsis, $.rest_type], [$.semgrep_expression_ellipsis, $.rest_type, $.spread_element, $.rest_pattern], [$.semgrep_expression_ellipsis, $.spread_element, $.rest_pattern], + [$.semgrep_expression_ellipsis, $.semgrep_ellipsis], ]), rules: { @@ -28,6 +29,16 @@ module.exports = { */ semgrep_ellipsis: $ => '...', + + /* ellipsis in function parameters + * e.g. function foo(..., x, ...) + */ + _formal_parameter: $ => choice( + $.semgrep_ellipsis, + $.required_parameter, + $.optional_parameter, + ), + /* TODO: restore this when the changes are made in semgrep. Remove the XXXXXXX when uncommenting. @@ -48,6 +59,7 @@ module.exports = { previous, $.semgrep_expression_ellipsis, ), + /* TODO: restore this when the changes are made in semgrep. _jsx_attribute: ($, previous) => choice( previous, diff --git a/lang/semgrep-grammars/src/semgrep-typescript/typescript/corpus/semgrep-ext.txt b/lang/semgrep-grammars/src/semgrep-typescript/typescript/corpus/semgrep-ext.txt index a08e7d70..5abb4a9b 100644 --- a/lang/semgrep-grammars/src/semgrep-typescript/typescript/corpus/semgrep-ext.txt +++ b/lang/semgrep-grammars/src/semgrep-typescript/typescript/corpus/semgrep-ext.txt @@ -52,3 +52,67 @@ case "Foo": (string_fragment)) (expression_statement (semgrep_expression_ellipsis)))) + +================================== +Ellipsis in function params +================================== + +function foo(...) { ... } +function foo(..., a) { ... } +function foo(a, ...) { ... } +function foo(..., a, ...) { ... } +function foo(..., @Bar(...) $X, ...) { ... } + +--- + +(program + (function_declaration + (identifier) + (formal_parameters + (semgrep_ellipsis)) + (statement_block + (expression_statement + (semgrep_expression_ellipsis)))) + (function_declaration + (identifier) + (formal_parameters + (semgrep_ellipsis) + (required_parameter + (identifier))) + (statement_block + (expression_statement + (semgrep_expression_ellipsis)))) + (function_declaration + (identifier) + (formal_parameters + (required_parameter + (identifier)) + (semgrep_ellipsis)) + (statement_block + (expression_statement + (semgrep_expression_ellipsis)))) + (function_declaration + (identifier) + (formal_parameters + (semgrep_ellipsis) + (required_parameter + (identifier)) + (semgrep_ellipsis)) + (statement_block + (expression_statement + (semgrep_expression_ellipsis)))) + (function_declaration + (identifier) + (formal_parameters + (semgrep_ellipsis) + (required_parameter + (decorator + (call_expression + (identifier) + (arguments + (semgrep_expression_ellipsis)))) + (identifier)) + (semgrep_ellipsis)) + (statement_block + (expression_statement + (semgrep_expression_ellipsis)))))