File: property_converter.rb

package info (click to toggle)
ruby-js-regex 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: ruby: 1,002; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_relative 'base'
require 'character_set'

class JsRegex
  module Converter
    #
    # Template class implementation.
    #
    # Uses the `character_set` and `regexp_property_values` gems to get the
    # codepoints matched by the property and build a set string from them.
    #
    class PropertyConverter < JsRegex::Converter::Base
      # A map of normalized Ruby property names to names supported by ES2018+.
      def self.map
        @map ||= File.read("#{__dir__}/property_map.csv").scan(/(.+),(.+)/).to_h
      end

      private

      def convert_data
        if context.es_2018_or_higher? &&
            (prop_name_in_js = self.class.map[subtype.to_s.tr('_', '')])
          context.enable_u_option
          "\\#{expression.negative? ? 'P' : 'p'}{#{prop_name_in_js}}"
        else
          build_character_set
        end
      end

      def build_character_set
        content = CharacterSet.of_expression(expression)

        if expression.case_insensitive? && !context.case_insensitive_root
          content = content.case_insensitive
        elsif !expression.case_insensitive? && context.case_insensitive_root
          warn_of_unsupported_feature('nested case-sensitive property')
        end

        content.to_s_with_surrogate_ranges
      end
    end
  end
end