summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/RegExp/prototype/Symbol.replace/result-coerce-groups-prop.js
blob: 4a2f731c760972239d988d72f0339b94d37c20a0 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (C) 2020 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-regexp.prototype-@@replace
description: >
  String coercion of "groups" object values returned by RegExpExec.
info: |
  RegExp.prototype [ @@replace ] ( string, replaceValue )

  [...]
  14. For each result in results, do
    [...]
    j. Let namedCaptures be ? Get(result, "groups").
    k. If functionalReplace is true, then
      [...]
    l. Else,
      [...]
      ii. Let replacement be ? GetSubstitution(matched, S, position, captures, namedCaptures, replaceValue).

  Runtime Semantics: GetSubstitution ( matched, str, position, captures, namedCaptures, replacement )

  [...]
  11. Let result be the String value derived from replacement by copying code unit elements
  from replacement to result while performing replacements as specified in Table 54.
  These $ replacements are done left-to-right, and, once such a replacement is performed,
  the new replacement text is not subject to further replacements.
  12. Return result.

  Table 54: Replacement Text Symbol Substitutions

  $<

  1. If namedCaptures is undefined, the replacement text is the String "$<".
  2. Else,
    a. Assert: Type(namedCaptures) is Object.
    b. Scan until the next > U+003E (GREATER-THAN SIGN).
    c. If none is found, the replacement text is the String "$<".
    d. Else,
      i. Let groupName be the enclosed substring.
      ii. Let capture be ? Get(namedCaptures, groupName).
      iii. If capture is undefined, replace the text through > with the empty String.
      iv. Otherwise, replace the text through > with ? ToString(capture).
features: [Symbol.replace, regexp-named-groups]
---*/

var r = /./;
var coercibleValue = {
  length: 1,
  0: 'a',
  index: 0,
  groups: {
    foo: {
      toString: function() {
        return 'toString value';
      },
      valueOf: function() {
        throw new Test262Error('This method should not be invoked.');
      },
    },
    bar: null,
  },
};

r.exec = function() {
  return coercibleValue;
};

assert.sameValue(r[Symbol.replace]('ab', '[$<foo>]'), '[toString value]b');
assert.sameValue(r[Symbol.replace]('ab', '[$<bar>]'), '[null]b');

reportCompare(0, 0);