Skip to content

Commit

Permalink
Support 'true' and 'false' values for --space
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbrenner committed Jul 26, 2018
1 parent 462f95e commit e5cb28d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 10 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,19 @@ updateNotifier({pkg: cli.pkg}).notify();
const {input, flags: opts} = cli;

// Make data types for `opts.space` match those of the API
// Check for string type because `xo --no-space` sets `opts.space` to `false`
if (typeof opts.space === 'string') {
if (/^\d+$/.test(opts.space)) {
opts.space = parseInt(opts.space, 10);
} else if (opts.space) {
// Assume `opts.space` was set to a filename when run as `xo --space file.js`
input.push(opts.space);
}

if (typeof opts.space !== 'number') {
} else if (opts.space === 'true') {
opts.space = true;
} else if (opts.space === 'false') {
opts.space = false;
} else {
if (opts.space !== '') {
// Assume `opts.space` was set to a filename when run as `xo --space file.js`
input.push(opts.space);
}
opts.space = true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ test('space option as boolean with filename', async t => {
// The default space value of 2 was expected
t.is(reports[0].errorCount, 0);
});

test('space option with boolean strings', async t => {
const cwd = path.join(__dirname, 'fixtures/space');
const trueResult = await t.throws(main(['--space=true'], {cwd}));
const falseResult = await t.throws(main(['--space=false'], {cwd}));
t.true(trueResult.stdout.includes('Expected indentation of 2 spaces'));
t.true(falseResult.stdout.includes('Expected indentation of 1 tab'));
});

0 comments on commit e5cb28d

Please sign in to comment.