Skip to content

Commit

Permalink
[Fix] ES2022+: GetSubstitution: match updated semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 29, 2024
1 parent f3fe0b7 commit 48b2d9f
Show file tree
Hide file tree
Showing 6 changed files with 625 additions and 196 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@
}],
},
},
{
"files": "./*/GetSubstitution.js",
"rules": {
"max-depth": "off",
},
},
],
}
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,6 @@
/2022/GetOwnPropertyKeys.js spackled linguist-generated=true
/2022/GetPromiseResolve.js spackled linguist-generated=true
/2022/GetPrototypeFromConstructor.js spackled linguist-generated=true
/2022/GetSubstitution.js spackled linguist-generated=true
/2022/GetV.js spackled linguist-generated=true
/2022/GetValueFromBuffer.js spackled linguist-generated=true
/2022/HasOwnProperty.js spackled linguist-generated=true
Expand Down Expand Up @@ -1211,7 +1210,6 @@
/2023/GetPromiseResolve.js spackled linguist-generated=true
/2023/GetPrototypeFromConstructor.js spackled linguist-generated=true
/2023/GetStringIndex.js spackled linguist-generated=true
/2023/GetSubstitution.js spackled linguist-generated=true
/2023/GetV.js spackled linguist-generated=true
/2023/GetValueFromBuffer.js spackled linguist-generated=true
/2023/HasOwnProperty.js spackled linguist-generated=true
Expand Down
177 changes: 97 additions & 80 deletions 2022/GetSubstitution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,138 @@ var GetIntrinsic = require('get-intrinsic');

var $TypeError = GetIntrinsic('%TypeError%');

var callBound = require('call-bind/callBound');
var regexTester = require('safe-regex-test');
var every = require('../helpers/every');

var $charAt = callBound('String.prototype.charAt');
var $strSlice = callBound('String.prototype.slice');
var $indexOf = callBound('String.prototype.indexOf');
var $parseInt = parseInt;

var isDigit = regexTester(/^[0-9]$/);

var inspect = require('object-inspect');

var Get = require('./Get');
var IsArray = require('./IsArray');
var ToObject = require('./ToObject');
var min = require('./min');
var StringIndexOf = require('./StringIndexOf');
var StringToNumber = require('./StringToNumber');
var substring = require('./substring');
var ToString = require('./ToString');
var Type = require('./Type');

var isInteger = require('../helpers/isInteger');
var isStringOrUndefined = require('../helpers/isStringOrUndefined');
var isPrefixOf = require('../helpers/isPrefixOf');

// http://www.ecma-international.org/ecma-262/12.0/#sec-getsubstitution
var startsWithDollarDigit = regexTester(/^\$[0-9]/);

// http://www.ecma-international.org/ecma-262/13.0/#sec-getsubstitution

// eslint-disable-next-line max-statements, max-params, max-lines-per-function
module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacement) {
if (Type(matched) !== 'String') {
module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
if (typeof matched !== 'string') {
throw new $TypeError('Assertion failed: `matched` must be a String');
}
var matchLength = matched.length;

if (Type(str) !== 'String') {
if (typeof str !== 'string') {
throw new $TypeError('Assertion failed: `str` must be a String');
}
var stringLength = str.length;

if (!isInteger(position) || position < 0 || position > stringLength) {
throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
if (!isInteger(position) || position < 0) {
throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
}

if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
}

if (Type(replacement) !== 'String') {
throw new $TypeError('Assertion failed: `replacement` must be a String');
if (typeof namedCaptures !== 'undefined' && Type(namedCaptures) !== 'Object') {
throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
}

var tailPos = position + matchLength;
var m = captures.length;
if (Type(namedCaptures) !== 'Undefined') {
namedCaptures = ToObject(namedCaptures); // eslint-disable-line no-param-reassign
if (typeof replacementTemplate !== 'string') {
throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
}

var result = '';
for (var i = 0; i < replacement.length; i += 1) {
// if this is a $, and it's not the end of the replacement
var current = $charAt(replacement, i);
var isLast = (i + 1) >= replacement.length;
var nextIsLast = (i + 2) >= replacement.length;
if (current === '$' && !isLast) {
var next = $charAt(replacement, i + 1);
if (next === '$') {
result += '$';
i += 1;
} else if (next === '&') {
result += matched;
i += 1;
} else if (next === '`') {
result += position === 0 ? '' : $strSlice(str, 0, position - 1);
i += 1;
} else if (next === "'") {
result += tailPos >= stringLength ? '' : $strSlice(str, tailPos);
i += 1;
} else {
var nextNext = nextIsLast ? null : $charAt(replacement, i + 2);
if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
// $1 through $9, and not followed by a digit
var n = $parseInt(next, 10);
// if (n > m, impl-defined)
result += n <= m && Type(captures[n - 1]) === 'Undefined' ? '' : captures[n - 1];
i += 1;
} else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
// $00 through $99
var nn = next + nextNext;
var nnI = $parseInt(nn, 10) - 1;
// if nn === '00' or nn > m, impl-defined
result += nn <= m && Type(captures[nnI]) === 'Undefined' ? '' : captures[nnI];
i += 2;
} else if (next === '<') {
// eslint-disable-next-line max-depth
if (Type(namedCaptures) === 'Undefined') {
result += '$<';
i += 2;
} else {
var endIndex = $indexOf(replacement, '>', i);
// eslint-disable-next-line max-depth
if (endIndex > -1) {
var groupName = $strSlice(replacement, i + '$<'.length, endIndex);
var capture = Get(namedCaptures, groupName);
// eslint-disable-next-line max-depth
if (Type(capture) !== 'Undefined') {
result += ToString(capture);
}
i += ('<' + groupName + '>').length;
var stringLength = str.length; // step 1

if (position > stringLength) {
throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
}

var templateRemainder = replacementTemplate; // step 3

var result = ''; // step 4

while (templateRemainder !== '') { // step 5
// 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.

var ref, refReplacement, found, capture;
if (isPrefixOf('$$', templateRemainder)) { // step 5.b
ref = '$$'; // step 5.b.i
refReplacement = '$'; // step 5.b.ii
} else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
ref = '$`'; // step 5.c.i
refReplacement = substring(str, 0, position); // step 5.c.ii
} else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
ref = '$&'; // step 5.d.i
refReplacement = matched; // step 5.d.ii
} else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
ref = '$\''; // step 5.e.i
var matchLength = matched.length; // step 5.e.ii
var tailPos = position + matchLength; // step 5.e.iii
refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
// 5.e.v NOTE: tailPos can exceed stringLength only if this abstract operation was invoked by a call to the intrinsic @@replace method of %RegExp.prototype% on an object whose "exec" property is not the intrinsic %RegExp.prototype.exec%.
} else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
found = false; // step 5.f.i
for (var d = 2; d > 0; d -= 1) { // step 5.f.ii
// If found is false and templateRemainder starts with "$" followed by d or more decimal digits, then
if (!found) { // step 5.f.ii.1
found = true; // step 5.f.ii.1.a
ref = substring(templateRemainder, 0, 1 + d); // step 5.f.ii.1.b
var digits = substring(templateRemainder, 1, 1 + d); // step 5.f.ii.1.c
var index = StringToNumber(digits); // step 5.f.ii.1.d
if (index < 0 || index > 99) {
throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.ii.1.e
}
if (index === 0) { // step 5.f.ii.1.f
refReplacement = ref;
} else if (index <= captures.length) { // step 5.f.ii.1.g
capture = captures[index - 1]; // step 5.f.ii.1.g.i
if (typeof capture === 'undefined') { // step 5.f.ii.1.g.ii
refReplacement = ''; // step 5.f.ii.1.g.ii.i
} else { // step 5.f.ii.1.g.iii
refReplacement = capture; // step 5.f.ii.1.g.iii.i
}
} else { // step 5.f.ii.1.h
refReplacement = ref; // step 5.f.ii.1.h.i
}
} else {
result += '$';
}
}
} else {
// the final $, or else not a $
result += $charAt(replacement, i);
} else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
if (gtPos === -1 || typeof namedCaptures === 'undefined') { // step 5.g.ii
ref = '$<'; // step 5.g.ii.1
refReplacement = ref; // step 5.g.ii.2
} else { // step 5.g.iii
ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
if (Type(namedCaptures) !== 'Object') {
throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
}
capture = Get(namedCaptures, groupName); // step 5.g.iii.4
if (typeof capture === 'undefined') { // step 5.g.iii.5
refReplacement = ''; // step 5.g.iii.5.a
} else { // step 5.g.iii.6
refReplacement = ToString(capture); // step 5.g.iii.6.a
}
}
} else { // step 5.h
ref = substring(templateRemainder, 0, 1); // step 5.h.i
refReplacement = ref; // step 5.h.ii
}

var refLength = ref.length; // step 5.i

templateRemainder = substring(templateRemainder, refLength); // step 5.j

result += refReplacement; // step 5.k
}
return result;

return result; // step 6
};
Loading

0 comments on commit 48b2d9f

Please sign in to comment.