diff --git a/test/yargs.js b/test/yargs.js index aa3e6fa32..2700318f4 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -2339,4 +2339,20 @@ describe('yargs dsl tests', () => { }) }) }) + + // see: https://github.com/babel/babel/pull/10733 + it('should not fail if command handler freezes object', () => { + const argv = yargs() + .command('cmd', 'a command', (yargs) => { + yargs.parserConfiguration({ 'populate--': true }) + }, (argv) => { + Object.freeze(argv) + argv._.should.eql(['cmd']) + argv['--'].should.eql(['foo']) + }).parse(['cmd', '--', 'foo']) + argv._.should.eql(['cmd', 'foo']) + // This should actually not be undefined, once we fix + // #1482. + argv['--'].should.eql(['foo']) + }) }) diff --git a/yargs.js b/yargs.js index f90173464..e7b412ae2 100644 --- a/yargs.js +++ b/yargs.js @@ -1190,9 +1190,15 @@ function Yargs (processArgs, cwd, parentRequire) { // we temporarily populate '--' rather than _, with arguments // after the '--' directive. After the parse, we copy these back. self._copyDoubleDash = function (argv) { - if (!argv._) return argv + if (!argv._ || !argv['--']) return argv argv._.push.apply(argv._, argv['--']) - delete argv['--'] + + // TODO(bcoe): refactor command parsing such that this delete is not + // necessary: https://github.com/yargs/yargs/issues/1482 + try { + delete argv['--'] + } catch (_err) {} + return argv }