Skip to content

Commit

Permalink
fix(swift): allow ellipses in function parameters (#517)
Browse files Browse the repository at this point in the history
* allow metavariables in function parameters

* ellipsis metavariables too
  • Loading branch information
brandonspark authored Nov 12, 2024
1 parent a55e4a6 commit 12e7143
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lang/semgrep-grammars/src/semgrep-swift/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ module.exports = grammar(base_grammar, {
field("suffix", $.semgrep_ellipsis),
),
),

parameter: ($, previous) => choice (
previous,
$.semgrep_ellipsis,
$.semgrep_ellipsis_metavar
)
}
});
118 changes: 101 additions & 17 deletions lang/semgrep-grammars/src/semgrep-swift/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,60 @@ Unbounded range operator

(source_file
(additive_expression
(tuple_expression (fully_open_range))
(tuple_expression
(fully_open_range))
(integer_literal)))

================================================================================
Parameter Ellipsis
================================================================================

func $F(..., $X: $T, ...) {
...
}

--------------------------------------------------------------------------------

(source_file
(function_declaration
(simple_identifier)
(parameter
(semgrep_ellipsis))
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(parameter
(semgrep_ellipsis))
(function_body
(statements
(fully_open_range)))))

================================================================================
Parameter Metavariable Ellipsis
================================================================================

func $F($...ARGS1, $X: $T, $...ARGS2) {
...
}

--------------------------------------------------------------------------------

(source_file
(function_declaration
(simple_identifier)
(parameter
(semgrep_ellipsis_metavar))
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(parameter
(semgrep_ellipsis_metavar))
(function_body
(statements
(fully_open_range)))))

================================================================================
Argument Ellipsis
================================================================================
Expand All @@ -37,14 +88,19 @@ foo(..., 1, ...);
(call_expression
(simple_identifier)
(call_suffix
(value_arguments (value_argument (fully_open_range)))))
(value_arguments
(value_argument
(fully_open_range)))))
(call_expression
(simple_identifier)
(call_suffix
(value_arguments
(value_argument (fully_open_range))
(value_argument (integer_literal))
(value_argument (fully_open_range))))))
(value_argument
(fully_open_range))
(value_argument
(integer_literal))
(value_argument
(fully_open_range))))))

================================================================================
Ellipsis Metavariable
Expand All @@ -60,14 +116,19 @@ $...FOO + 1;
(call_expression
(simple_identifier)
(call_suffix
(value_arguments (value_argument (semgrep_ellipsis_metavar)))))
(value_arguments
(value_argument
(semgrep_ellipsis_metavar)))))
(call_expression
(simple_identifier)
(call_suffix
(value_arguments
(value_argument (semgrep_ellipsis_metavar))
(value_argument (integer_literal))
(value_argument (semgrep_ellipsis_metavar)))))
(value_argument
(semgrep_ellipsis_metavar))
(value_argument
(integer_literal))
(value_argument
(semgrep_ellipsis_metavar)))))
(additive_expression
(semgrep_ellipsis_metavar)
(integer_literal)))
Expand Down Expand Up @@ -98,13 +159,16 @@ func foo<T, ...>() { }
(function_declaration
(simple_identifier)
(type_parameters
(type_parameter (semgrep_ellipsis)))
(type_parameter
(semgrep_ellipsis)))
(function_body))
(function_declaration
(simple_identifier)
(type_parameters
(type_parameter (type_identifier))
(type_parameter (semgrep_ellipsis)))
(type_parameter
(type_identifier))
(type_parameter
(semgrep_ellipsis)))
(function_body)))

================================================================================
Expand All @@ -115,7 +179,10 @@ Deep Ellipsis

--------------------------------------------------------------------------------

(source_file (semgrep_deep_ellipsis (integer_literal) (custom_operator)))
(source_file
(semgrep_deep_ellipsis
(integer_literal)
(custom_operator)))

================================================================================
Deep Ellipsis with custom operator (should not parse, but does because of a hack
Expand All @@ -126,7 +193,10 @@ necessary to work around custom operators getting scanned by the custom scanner)

--------------------------------------------------------------------------------

(source_file (semgrep_deep_ellipsis (integer_literal) (custom_operator)))
(source_file
(semgrep_deep_ellipsis
(integer_literal)
(custom_operator)))

================================================================================
Deep Ellipsis with custom operator (this parses due to parens)
Expand Down Expand Up @@ -155,10 +225,14 @@ class ClassName {

--------------------------------------------------------------------------------

(source_file (class_declaration (type_identifier) (class_body (semgrep_ellipsis))))
(source_file
(class_declaration
(type_identifier)
(class_body
(semgrep_ellipsis))))

================================================================================
Class Ellipsis Sandwiched
Class Ellipsis Sandwiched
================================================================================

class ClassName {
Expand All @@ -169,7 +243,17 @@ class ClassName {

--------------------------------------------------------------------------------

(source_file (class_declaration (type_identifier) (class_body (semgrep_ellipsis) (property_declaration (value_binding_pattern) (pattern (simple_identifier)) (integer_literal)) (semgrep_ellipsis))))
(source_file
(class_declaration
(type_identifier)
(class_body
(semgrep_ellipsis)
(property_declaration
(value_binding_pattern)
(pattern
(simple_identifier))
(integer_literal))
(semgrep_ellipsis))))

================================================================================
Statement Ellipsis with semicolon
Expand Down

0 comments on commit 12e7143

Please sign in to comment.