Skip to content

Commit

Permalink
fix: temporary fix for libraries that call Object.freeze() (yargs#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Nov 19, 2019
1 parent aa09faf commit 99c2dc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 16 additions & 0 deletions test/yargs.js
Expand Up @@ -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'])
})
})
10 changes: 8 additions & 2 deletions yargs.js
Expand Up @@ -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
}

Expand Down

0 comments on commit 99c2dc8

Please sign in to comment.