Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swift): allow ellipses in function parameters #517

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading