Add a test case for record access syntax within guards

This improvement was mentioned in the v0.31.0 release notes. This is
already supported so all I've done is remove the comment about how the
behavior does not yet exist upstream and add a test case.
pull/844/head
Michael Davis 2023-09-26 11:03:01 +07:00
parent 297031dce6
commit 32c8f1e32a
No known key found for this signature in database
2 changed files with 78 additions and 4 deletions

@ -142,10 +142,8 @@ module.exports = grammar({
optional(seq(field("label", $.label), ":")),
field("value", $._constant_value)
),
// This is definitely a misnomer at this time as field access are actually
// not allowed as constant values (yet). This rule exists to parse remote
// function references which are generally indistinguishable from field
// accesses and so share an AST node.
// This rule exists to parse remote function references which are generally
// indistinguishable from field accesses and so share an AST node.
constant_field_access: ($) =>
seq(field("record", $.identifier), ".", field("field", $.label)),

@ -32,3 +32,79 @@ case value {}
(case
(case_subjects
(identifier))))
================================================================================
Case examples
================================================================================
// From https://gleam.run/news/v0.31-keeping-dependencies-explicit/#quality-of-life-improvements
pub fn listed(names: List(String), person: Person) -> String {
case names {
[name, ..names] if name == person.name -> True
[_, ..names] -> listed(names, person)
[] -> False
}
}
--------------------------------------------------------------------------------
(source_file
(comment)
(function
(visibility_modifier)
(identifier)
(function_parameters
(function_parameter
(identifier)
(type
(type_identifier)
(type_arguments
(type_argument
(type
(type_identifier))))))
(function_parameter
(identifier)
(type
(type_identifier))))
(type
(type_identifier))
(function_body
(case
(case_subjects
(identifier))
(case_clauses
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern
(identifier)
(list_pattern_tail
(identifier)))))
(case_clause_guard
(binary_expression
(identifier)
(field_access
(identifier)
(label))))
(record
(constructor_name)))
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern
(discard)
(list_pattern_tail
(identifier)))))
(function_call
(identifier)
(arguments
(argument
(identifier))
(argument
(identifier)))))
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern)))
(record
(constructor_name))))))))