diff --git a/main.js b/main.js index d3b5e244..cf8657ed 100755 --- a/main.js +++ b/main.js @@ -114,6 +114,24 @@ 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 === '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; + } +} + const log = report => { const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty; diff --git a/test/fixtures/space/one-space.js b/test/fixtures/space/one-space.js new file mode 100644 index 00000000..0b1b5e7d --- /dev/null +++ b/test/fixtures/space/one-space.js @@ -0,0 +1,3 @@ +console.log([ + 1 +]); diff --git a/test/fixtures/space/two-spaces.js b/test/fixtures/space/two-spaces.js new file mode 100644 index 00000000..baa2c240 --- /dev/null +++ b/test/fixtures/space/two-spaces.js @@ -0,0 +1,3 @@ +console.log([ + 1 +]); diff --git a/test/main.js b/test/main.js index 07b6a5a1..b55ab082 100644 --- a/test/main.js +++ b/test/main.js @@ -115,3 +115,38 @@ test('cli option takes precedence over config', async t => { // i.e make sure absent cli flags are not parsed as `false` await t.throws(main(['--stdin'], {input})); }); + +test('space option with number value', async t => { + const cwd = path.join(__dirname, 'fixtures/space'); + const {stdout} = await t.throws(main(['--space=4', 'one-space.js'], {cwd})); + t.true(stdout.includes('Expected indentation of 4 spaces')); +}); + +test('space option as boolean', async t => { + const cwd = path.join(__dirname, 'fixtures/space'); + const {stdout} = await t.throws(main(['--space'], {cwd})); + t.true(stdout.includes('Expected indentation of 2 spaces')); +}); + +test('space option as boolean with filename', async t => { + const cwd = path.join(__dirname, 'fixtures/space'); + const {stdout} = await main(['--reporter=json', '--space', 'two-spaces.js'], { + cwd, + reject: false + }); + const reports = JSON.parse(stdout); + + // Only the specified file was checked (filename was not the value of `space`) + t.is(reports.length, 1); + + // 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')); +});