js_regex 3.8.0 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8971658980813740deb03ece3c5af5bfdfd7412f0630fc4a3f172e4c06b11c52
4
- data.tar.gz: bdafa3639a230b1ec1ac4661828050d99339eb18ec1768fa2a6f1b5e69d95f1b
3
+ metadata.gz: 4710feb6c1da4ea2665ec125ab16bf92c35117ee028b2c6986ec9ebe61cc5eea
4
+ data.tar.gz: 7152f08cbea846150bc370cb2aeb2d9a99f7e7c18b8b448e080845a54fbbe05d
5
5
  SHA512:
6
- metadata.gz: 13abdf7b41485194f05cce79751ca60e8b1f9fc864b17f58294650df9f9e485a889b9571d847bf564aa12b709fd572c53f27a5a2900e3dc8bfa765f522b58e62
7
- data.tar.gz: 31941c0d7a4842fdea84d5f649f3df30c54e8da24e6dbf722cff3e48661ae89646117f66f33ee419c0fc0e393b11f284cb565751b4f77aa5eb0bbbc8d38d903d
6
+ metadata.gz: cee471265231e58c04d682a707184c5d8a468481eff5f2e0df498b7323fe75ee245b95e1d2ca69016fa83e9378faecaa95541e70af07711773bcd7cbe4bdae57
7
+ data.tar.gz: '0924f4a4f73f2786c8dcac578588f04fb3c6d86913ce6c8f1ac74169e8e226f6e4b993ee43a100e7e913ade4062cb18d6e53ac87573aeaabe3780c6f75beb0d0'
@@ -41,14 +41,19 @@ class JsRegex
41
41
  end
42
42
 
43
43
  def convert_call
44
- if expression.respond_to?(:number) && expression.number.equal?(0)
45
- return warn_of_unsupported_feature('whole-pattern recursion')
44
+ if context.recursions(expression) >= 5
45
+ warn_of("Recursion for '#{expression}' curtailed at 5 levels")
46
+ return ''
46
47
  end
48
+
49
+ context.count_recursion(expression)
47
50
  context.increment_local_capturing_group_count
48
51
  target_copy = expression.referenced_expression.unquantified_clone
49
52
  # avoid "Duplicate capture group name" error in JS
50
53
  target_copy.token = :capture if target_copy.is?(:named, :group)
51
- convert_expression(target_copy)
54
+ result = convert_expression(target_copy)
55
+ # wrap in group if it is a full-pattern recursion
56
+ expression.reference == 0 ? Node.new('(?:', result, ')') : result
52
57
  end
53
58
  end
54
59
  end
@@ -14,6 +14,7 @@ class JsRegex
14
14
  def initialize(case_insensitive_root: false, target: nil)
15
15
  self.added_capturing_groups_after_group = Hash.new(0)
16
16
  self.capturing_group_count = 0
17
+ self.recursions_per_expression = {}
17
18
  self.warnings = []
18
19
  self.required_options_hash = {}
19
20
 
@@ -62,6 +63,18 @@ class JsRegex
62
63
  capture_group
63
64
  end
64
65
 
66
+ def recursions(exp)
67
+ recursions_per_expression[recursion_id(exp)] || 0
68
+ end
69
+
70
+ def count_recursion(exp)
71
+ recursions_per_expression[recursion_id(exp)] = recursions(exp) + 1
72
+ end
73
+
74
+ def recursion_id(exp)
75
+ [exp.class, exp.starts_at]
76
+ end
77
+
65
78
  # takes and returns 1-indexed group positions.
66
79
  # new is different from old if capturing groups were added in between.
67
80
  def new_capturing_group_position(old_position)
@@ -79,6 +92,7 @@ class JsRegex
79
92
  private
80
93
 
81
94
  attr_accessor :added_capturing_groups_after_group,
95
+ :recursions_per_expression,
82
96
  :required_options_hash,
83
97
  :target
84
98
 
@@ -6,11 +6,12 @@ class JsRegex
6
6
  # Template class implementation.
7
7
  #
8
8
  class TypeConverter < JsRegex::Converter::Base
9
- HEX_EXPANSION = '[0-9A-Fa-f]'
10
- NONHEX_EXPANSION = '[^0-9A-Fa-f]'
11
- ES2018_HEX_EXPANSION = '\p{AHex}'
12
- ES2018_NONHEX_EXPANSION = '\P{AHex}'
13
- LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
9
+ HEX_EXPANSION = '[0-9A-Fa-f]'
10
+ NONHEX_EXPANSION = '[^0-9A-Fa-f]'
11
+ ES2018_HEX_EXPANSION = '\p{AHex}'
12
+ ES2018_NONHEX_EXPANSION = '\P{AHex}'
13
+ ES2018_XGRAPHEME_EXPANSION = '[\P{M}\P{Lm}](?:(?:[\u035C\u0361]\P{M}\p{M}*)|\u200d|\p{M}|\p{Lm}|\p{Emoji_Modifier})*'
14
+ LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
14
15
 
15
16
  def self.directly_compatible?(expression)
16
17
  case expression.token
@@ -28,6 +29,7 @@ class JsRegex
28
29
  when :hex then hex_expansion
29
30
  when :nonhex then nonhex_expansion
30
31
  when :linebreak then LINEBREAK_EXPANSION
32
+ when :xgrapheme then xgrapheme
31
33
  when :digit, :space, :word
32
34
  return pass_through if self.class.directly_compatible?(expression)
33
35
  set_substitution
@@ -68,6 +70,14 @@ class JsRegex
68
70
  def character_set
69
71
  CharacterSet.of_expression(expression)
70
72
  end
73
+
74
+ def xgrapheme
75
+ if context.es_2018_or_higher? && context.enable_u_option
76
+ ES2018_XGRAPHEME_EXPANSION
77
+ else
78
+ warn_of_unsupported_feature
79
+ end
80
+ end
71
81
  end
72
82
  end
73
83
  end
@@ -1,3 +1,3 @@
1
1
  class JsRegex
2
- VERSION = '3.8.0'
2
+ VERSION = '3.9.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_regex
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.0
4
+ version: 3.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janosch Müller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-25 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: character_set
@@ -28,16 +28,22 @@ dependencies:
28
28
  name: regexp_parser
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.2
34
+ - - "<"
32
35
  - !ruby/object:Gem::Version
33
- version: '2.5'
36
+ version: 3.0.0
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - "~>"
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.2
44
+ - - "<"
39
45
  - !ruby/object:Gem::Version
40
- version: '2.5'
46
+ version: 3.0.0
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: regexp_property_values
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -105,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
111
  - !ruby/object:Gem::Version
106
112
  version: '0'
107
113
  requirements: []
108
- rubygems_version: 3.4.0.dev
114
+ rubygems_version: 3.4.1
109
115
  signing_key:
110
116
  specification_version: 4
111
117
  summary: Converts Ruby regexes to JavaScript regexes.