Module: SyntaxTree::Quotes
- Defined in:
- lib/syntax_tree.rb
Overview
Responsible for providing information about quotes to be used for strings and dynamic symbols.
Constant Summary collapse
- PAIRS =
The matching pairs of quotes that can be used with % literals.
{ "(" => ")", "[" => "]", "{" => "}", "<" => ">" }.freeze
Class Method Summary collapse
-
.locked?(node) ⇒ Boolean
If there is some part of this string that matches an escape sequence or that contains the interpolation pattern (“#{”), then we are locked into whichever quote the user chose. (If they chose single quotes, then double quoting would activate the escape sequence, and if they chose double quotes, then single quotes would deactivate it.).
-
.matching(quote) ⇒ Object
Find the matching closing quote for the given opening quote.
-
.normalize(content, enclosing) ⇒ Object
Escape and unescape single and double quotes as needed to be able to enclose
content
withenclosing
.
Class Method Details
.locked?(node) ⇒ Boolean
If there is some part of this string that matches an escape sequence or that contains the interpolation pattern (“#{”), then we are locked into whichever quote the user chose. (If they chose single quotes, then double quoting would activate the escape sequence, and if they chose double quotes, then single quotes would deactivate it.)
5104 5105 5106 5107 5108 |
# File 'lib/syntax_tree.rb', line 5104 def self.locked?(node) node.parts.any? do |part| part.is_a?(TStringContent) && part.value.match?(/#[@${]|[\\]/) end end |
.matching(quote) ⇒ Object
Find the matching closing quote for the given opening quote.
5111 5112 5113 |
# File 'lib/syntax_tree.rb', line 5111 def self.matching(quote) PAIRS.fetch(quote) { quote } end |
.normalize(content, enclosing) ⇒ Object
Escape and unescape single and double quotes as needed to be able to enclose content
with enclosing
.
5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 |
# File 'lib/syntax_tree.rb', line 5117 def self.normalize(content, enclosing) return content if enclosing != "\"" && enclosing != "'" content.gsub(/\\([\s\S])|(['"])/) do _match, escaped, quote = Regexp.last_match.to_a if quote == enclosing "\\#{quote}" elsif quote quote else "\\#{escaped}" end end end |