Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: no RegExp side effects for static properties #18937

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ function REPLServer(prompt,
// Just for backwards compat, see github.com/joyent/node/pull/7127
self.rli = this;

const savedRegExMatches = ['', '', '', '', '', '', '', '', '', ''];
const sep = '\u0000\u0000\u0000';
const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
`${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
`${sep}(.*)$`);
const savedRegExMatches = ['', '', '', '', '', '', '', '', ''];
const savedRegEx = {
input: '', // '$_'
lastMatch: '', //'$&'
};

eval_ = eval_ || defaultEval;

Expand Down Expand Up @@ -262,7 +262,10 @@ function REPLServer(prompt,

// This will set the values from `savedRegExMatches` to corresponding
// predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9`
regExMatcher.test(savedRegExMatches.join(sep));
const matcher = savedRegExMatches[0].length ?
regexBuildGroup(savedRegExMatches, savedRegEx.lastMatch) :
new RegExp(regexpEscape(savedRegEx.lastMatch));
matcher.test(savedRegEx.input);

let finished = false;
function finishExecution(err, result) {
Expand All @@ -271,9 +274,11 @@ function REPLServer(prompt,

// After executing the current expression, store the values of RegExp
// predefined properties back in `savedRegExMatches`
for (var idx = 1; idx < savedRegExMatches.length; idx += 1) {
savedRegExMatches[idx] = RegExp[`$${idx}`];
for (var idx = 0; idx < savedRegExMatches.length; idx += 1) {
savedRegExMatches[idx] = RegExp[`$${idx + 1}`];
}
savedRegEx.input = '' + RegExp.input;
savedRegEx.lastMatch = '' + RegExp.lastMatch;

cb(err, result);
}
Expand Down Expand Up @@ -1449,6 +1454,35 @@ function regexpEscape(s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

function regexBuildGroup(matches, input) {
const groups = [];
const lowercase = input.toLocaleLowerCase();
let offset = 0;
let ignoreCase = false;
for (const m of matches) {
if (!m.length) {
if (input.length - offset > 0) {
groups.push(regexpEscape(input.substring(offset, input.length)));
}
break;
}

let idx = input.indexOf(m, offset);
if (idx === -1) {
ignoreCase = true;
idx = lowercase.indexOf(m.toLocaleLowerCase(), offset);
}

if (idx - offset > 0) {
groups.push(regexpEscape(input.substring(offset, idx)));
}

groups.push('(.{' + m.length + '})');
offset = idx + m.length;
}
return new RegExp(groups.join(''), ignoreCase ? 'i' : '');
}

// If the error is that we've unexpectedly ended the input,
// then let the user try to recover by adding more input.
function isRecoverableError(e, code) {
Expand Down
62 changes: 61 additions & 1 deletion test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ const errorTests = [
},
// Regression test for https://github.com/nodejs/node/issues/597
{
send: '/(.)(.)(.)(.)(.)(.)(.)(.)(.)/.test(\'123456789\')\n',
send: '/(.)(.)(.)(.)(.)(.)(.)(.)(.)/.test(\'123456789abc\')\n',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a new block instead of changing this one?

expect: 'true'
},
// the following test's result depends on the RegExp's match from the above
Expand All @@ -450,6 +450,66 @@ const errorTests = [
expect: ['\'1\'', '\'2\'', '\'3\'', '\'4\'', '\'5\'', '\'6\'',
'\'7\'', '\'8\'', '\'9\'']
},
// input against which a regular expression is matched
{
send: 'RegExp.$_',
expect: '\'123456789abc\''
},
// substring of input against which a regular expression is matched
{
send: 'RegExp.lastMatch',
expect: '\'123456789\''
},
{
send: 'RegExp.lastParen',
expect: '\'9\''
},
{
send: 'RegExp.leftContext',
expect: '\'\''
},
{
send: 'RegExp.rightContext',
expect: '\'abc\''
},
{
send: 'new RegExp(\'XXX(.*)x(Jkl.*)XxX\', \'i\')' +
'.test(\'ABCDXXXEFGHIXJKLMNXXXOPQ\')\n',
expect: 'true'
},
// the following test's result depends on the RegExp's match from the above
{
send: 'RegExp.$1\nRegExp.$2\nRegExp.$3\nRegExp.$4\nRegExp.$5\n' +
'RegExp.$6\nRegExp.$7\nRegExp.$8\nRegExp.$9\n',
expect: ['\'EFGHI\'', '\'JKLMN\'', '\'\'', '\'\'', '\'\'', '\'\'',
'\'\'', '\'\'', '\'\'']
},
// input against which a regular expression is matched
{
send: 'RegExp.$_',
expect: '\'ABCDXXXEFGHIXJKLMNXXXOPQ\''
},
// substring of input against which a regular expression is matched
{
send: 'RegExp.lastMatch',
expect: '\'XXXEFGHIXJKLMNXXX\''
},
{
send: 'RegExp.lastParen',
expect: '\'JKLMN\''
},
{
send: 'RegExp.leftContext',
expect: '\'ABCD\''
},
{
send: 'RegExp.rightContext',
expect: '\'OPQ\''
},
{
send: '/XXX(.*)X(.*)XXX/.test(\'ABCDXXXEFGHIXJKLMNXXXOPQ\')\n',
expect: 'true'
},
// regression tests for https://github.com/nodejs/node/issues/2749
{
send: 'function x() {\nreturn \'\\n\';\n }',
Expand Down