From 0afff999eec9fed6ca765a779ea1ffc69b780088 Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Thu, 21 Sep 2023 12:12:37 -0400 Subject: [PATCH] [rust] add `ParseResult::frozen_string_literals` --- rust/yarp/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust/yarp/src/lib.rs b/rust/yarp/src/lib.rs index a5865c1e56e..e6bcb956e3f 100644 --- a/rust/yarp/src/lib.rs +++ b/rust/yarp/src/lib.rs @@ -134,6 +134,14 @@ impl<'pr> ParseResult<'pr> { self.source } + /// Returns whether we found a `frozen_string_literal` magic comment with a true value. + #[must_use] + pub fn frozen_string_literals(&self) -> bool { + unsafe { + (*self.parser.as_ptr()).frozen_string_literal + } + } + /// Returns a slice of the source string that was parsed using the given /// location range. #[must_use] @@ -391,4 +399,18 @@ end let closing_loc = call.closing_loc(); assert!(closing_loc.is_none()); } + + #[test] + fn frozen_strings_test() { + let source = r#" +# frozen_string_literal: true +"foo" +"#; + let result = parse(source.as_ref()); + assert!(result.frozen_string_literals()); + + let source = "3"; + let result = parse(source.as_ref()); + assert!(!result.frozen_string_literals()); + } }