Skip to content

Commit

Permalink
Support ellipsis in typescript function parameters (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihji authored Nov 22, 2024
1 parent 12e7143 commit 841dbe0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lang/semgrep-grammars/src/semgrep-typescript/common/semgrep-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit 841dbe0

Please sign in to comment.