| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d3f0a6a commit 72fc4eb
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -471,19 +471,7 @@ function REPLServer(prompt, | |||
| 471 | 471 | } | |
| 472 | 472 | ||
| 473 | 473 | var evalCmd = self.bufferedCommand + cmd; | |
| 474 | - if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) { | ||
| 475 | - // It's confusing for `{ a : 1 }` to be interpreted as a block | ||
| 476 | - // statement rather than an object literal. So, we first try | ||
| 477 | - // to wrap it in parentheses, so that it will be interpreted as | ||
| 478 | - // an expression. | ||
| 479 | - evalCmd = '(' + evalCmd + ')\n'; | ||
| 480 | - self.wrappedCmd = true; | ||
| 481 | - } else { | ||
| 482 | - // otherwise we just append a \n so that it will be either | ||
| 483 | - // terminated, or continued onto the next expression if it's an | ||
| 484 | - // unexpected end of input. | ||
| 485 | - evalCmd = evalCmd + '\n'; | ||
| 486 | - } | ||
| 474 | + evalCmd = preprocess(evalCmd); | ||
| 487 | 475 | ||
| 488 | 476 | debug('eval %j', evalCmd); | |
| 489 | 477 | self.eval(evalCmd, self.context, 'repl', finish); | |
@@ -538,6 +526,26 @@ function REPLServer(prompt, | |||
| 538 | 526 | // Display prompt again | |
| 539 | 527 | self.displayPrompt(); | |
| 540 | 528 | } | |
| 529 | + | ||
| 530 | + function preprocess(code) { | ||
| 531 | + let cmd = code; | ||
| 532 | + if (/^\s*\{/.test(cmd) && /\}\s*$/.test(cmd)) { | ||
| 533 | + // It's confusing for `{ a : 1 }` to be interpreted as a block | ||
| 534 | + // statement rather than an object literal. So, we first try | ||
| 535 | + // to wrap it in parentheses, so that it will be interpreted as | ||
| 536 | + // an expression. | ||
| 537 | + cmd = `(${cmd})`; | ||
| 538 | + self.wrappedCmd = true; | ||
| 539 | + } else { | ||
| 540 | + // Mitigate https://github.com/nodejs/node/issues/548 | ||
| 541 | + cmd = cmd.replace(/^\s*function\s+([^(]+)/, | ||
| 542 | + (_, name) => `var ${name} = function ${name}`); | ||
| 543 | + } | ||
| 544 | + // Append a \n so that it will be either | ||
| 545 | + // terminated, or continued onto the next expression if it's an | ||
| 546 | + // unexpected end of input. | ||
| 547 | + return `${cmd}\n`; | ||
| 548 | + } | ||
| 541 | 549 | }); | |
| 542 | 550 | ||
| 543 | 551 | self.on('SIGCONT', function() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + // Reference: https://github.com/nodejs/node/pull/7624 | ||
| 2 | + 'use strict'; | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const repl = require('repl'); | ||
| 6 | + const stream = require('stream'); | ||
| 7 | + | ||
| 8 | + common.globalCheck = false; | ||
| 9 | + | ||
| 10 | + const r = initRepl(); | ||
| 11 | + | ||
| 12 | + r.input.emit('data', 'function a() { return 42; } (1)\n'); | ||
| 13 | + r.input.emit('data', 'a\n'); | ||
| 14 | + r.input.emit('data', '.exit'); | ||
| 15 | + | ||
| 16 | + const expected = '1\n[Function a]\n'; | ||
| 17 | + const got = r.output.accumulator.join(''); | ||
| 18 | + assert.strictEqual(got, expected); | ||
| 19 | + | ||
| 20 | + function initRepl() { | ||
| 21 | + const input = new stream(); | ||
| 22 | + input.write = input.pause = input.resume = () => {}; | ||
| 23 | + input.readable = true; | ||
| 24 | + | ||
| 25 | + const output = new stream(); | ||
| 26 | + output.writable = true; | ||
| 27 | + output.accumulator = []; | ||
| 28 | + | ||
| 29 | + output.write = (data) => output.accumulator.push(data); | ||
| 30 | + | ||
| 31 | + return repl.start({ | ||
| 32 | + input, | ||
| 33 | + output, | ||
| 34 | + useColors: false, | ||
| 35 | + terminal: false, | ||
| 36 | + prompt: '' | ||
| 37 | + }); | ||
| 38 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -328,6 +328,11 @@ function error_test() { | |||
| 328 | 328 | // or block comment. https://github.com/nodejs/node/issues/3611 | |
| 329 | 329 | { client: client_unix, send: 'a = 3.5e', | |
| 330 | 330 | expect: /^SyntaxError: Unexpected token ILLEGAL/ }, | |
| 331 | + // Mitigate https://github.com/nodejs/node/issues/548 | ||
| 332 | + { client: client_unix, send: 'function name(){ return "node"; };name()', | ||
| 333 | + expect: "'node'\n" + prompt_unix }, | ||
| 334 | + { client: client_unix, send: 'function name(){ return "nodejs"; };name()', | ||
| 335 | + expect: "'nodejs'\n" + prompt_unix }, | ||
| 331 | 336 | ]); | |
| 332 | 337 | } | |
| 333 | 338 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments