Skip to content

Commit f76c901

Browse files
noahbrennersindresorhus
authored andcommitted
Fix CLI's --space option parsing (#342)
1 parent 98dee9a commit f76c901

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ updateNotifier({pkg: cli.pkg}).notify();
114114

115115
const {input, flags: opts} = cli;
116116

117+
// Make data types for `opts.space` match those of the API
118+
// Check for string type because `xo --no-space` sets `opts.space` to `false`
119+
if (typeof opts.space === 'string') {
120+
if (/^\d+$/.test(opts.space)) {
121+
opts.space = parseInt(opts.space, 10);
122+
} else if (opts.space === 'true') {
123+
opts.space = true;
124+
} else if (opts.space === 'false') {
125+
opts.space = false;
126+
} else {
127+
if (opts.space !== '') {
128+
// Assume `opts.space` was set to a filename when run as `xo --space file.js`
129+
input.push(opts.space);
130+
}
131+
opts.space = true;
132+
}
133+
}
134+
117135
const log = report => {
118136
const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;
119137

test/fixtures/space/one-space.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log([
2+
1
3+
]);

test/fixtures/space/two-spaces.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log([
2+
1
3+
]);

test/main.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,38 @@ test('cli option takes precedence over config', async t => {
115115
// i.e make sure absent cli flags are not parsed as `false`
116116
await t.throws(main(['--stdin'], {input}));
117117
});
118+
119+
test('space option with number value', async t => {
120+
const cwd = path.join(__dirname, 'fixtures/space');
121+
const {stdout} = await t.throws(main(['--space=4', 'one-space.js'], {cwd}));
122+
t.true(stdout.includes('Expected indentation of 4 spaces'));
123+
});
124+
125+
test('space option as boolean', async t => {
126+
const cwd = path.join(__dirname, 'fixtures/space');
127+
const {stdout} = await t.throws(main(['--space'], {cwd}));
128+
t.true(stdout.includes('Expected indentation of 2 spaces'));
129+
});
130+
131+
test('space option as boolean with filename', async t => {
132+
const cwd = path.join(__dirname, 'fixtures/space');
133+
const {stdout} = await main(['--reporter=json', '--space', 'two-spaces.js'], {
134+
cwd,
135+
reject: false
136+
});
137+
const reports = JSON.parse(stdout);
138+
139+
// Only the specified file was checked (filename was not the value of `space`)
140+
t.is(reports.length, 1);
141+
142+
// The default space value of 2 was expected
143+
t.is(reports[0].errorCount, 0);
144+
});
145+
146+
test('space option with boolean strings', async t => {
147+
const cwd = path.join(__dirname, 'fixtures/space');
148+
const trueResult = await t.throws(main(['--space=true'], {cwd}));
149+
const falseResult = await t.throws(main(['--space=false'], {cwd}));
150+
t.true(trueResult.stdout.includes('Expected indentation of 2 spaces'));
151+
t.true(falseResult.stdout.includes('Expected indentation of 1 tab'));
152+
});

0 commit comments

Comments
 (0)