Skip to content

Commit a096f00

Browse files
committed
Add deprecate warning for convertToContext
1 parent d4ecbd8 commit a096f00

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/repl.js

+29
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,35 @@ function regexpEscape(s) {
11951195
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
11961196
}
11971197

1198+
1199+
/**
1200+
* Converts commands that use var and function <name>() to use the
1201+
* local exports.context when evaled. This provides a local context
1202+
* on the REPL.
1203+
*
1204+
* @param {String} cmd The cmd to convert.
1205+
* @return {String} The converted command.
1206+
*/
1207+
REPLServer.prototype.convertToContext = util.deprecate(function(cmd) {
1208+
const scopeVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
1209+
const scopeFunc = /^\s*function\s*([_\w\$]+)/;
1210+
var matches;
1211+
1212+
// Replaces: var foo = "bar"; with: self.context.foo = bar;
1213+
matches = scopeVar.exec(cmd);
1214+
if (matches && matches.length === 3) {
1215+
return 'self.context.' + matches[1] + matches[2];
1216+
}
1217+
1218+
// Replaces: function foo() {}; with: foo = function foo() {};
1219+
matches = scopeFunc.exec(this.bufferedCommand);
1220+
if (matches && matches.length === 2) {
1221+
return matches[1] + ' = ' + this.bufferedCommand;
1222+
}
1223+
1224+
return cmd;
1225+
}, 'replServer.convertToContext will be removed in v8.0.0');
1226+
11981227
function bailOnIllegalToken(parser) {
11991228
return parser._literal === null &&
12001229
!parser.blockComment &&

0 commit comments

Comments
 (0)