Skip to content

Commit c370222

Browse files
committed
Use type-check to throw error instead of a broad try/catch
Signed-off-by: Dinika Saxena <[email protected]>
1 parent 28c801c commit c370222

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Diff for: lib/cli/options.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,18 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
110110
// 4. we can then reapply the values after yargs-parser is done.
111111
const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce(
112112
(acc, arg) => {
113-
try {
114-
const pair = arg.split('=');
115-
let flag = pair[0];
116-
if (isNodeFlag(flag, false)) {
117-
flag = flag.replace(/^--?/, '');
118-
return arg.includes('=')
119-
? acc.concat([[flag, pair[1]]])
120-
: acc.concat([[flag, true]]);
121-
}
122-
return acc;
123-
} catch (err) {
113+
if (typeof arg !== 'string') {
124114
throw new Error(`Invalid option ${arg} passed to mocha cli`);
125115
}
116+
const pair = arg.split('=');
117+
let flag = pair[0];
118+
if (isNodeFlag(flag, false)) {
119+
flag = flag.replace(/^--?/, '');
120+
return arg.includes('=')
121+
? acc.concat([[flag, pair[1]]])
122+
: acc.concat([[flag, true]]);
123+
}
124+
return acc;
126125
},
127126
[]
128127
);
@@ -196,7 +195,8 @@ const loadPkgRc = (args = {}) => {
196195
filepath
197196
);
198197
} else {
199-
debug('failed to read default package.json at %s; ignoring', filepath);
198+
debug('failed to read default package.json at %s; ignoring',
199+
filepath);
200200
return result;
201201
}
202202
}

Diff for: test/node-unit/cli/options.spec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ describe('options', function () {
204204
const filepath = '/some/package.json';
205205
readFileSync = sinon.stub();
206206
// package.json
207-
readFileSync.onFirstCall().returns('{definitely-invalid');
207+
readFileSync
208+
.onFirstCall()
209+
.returns('{definitely-invalid');
208210
findConfig = sinon.stub().returns('/some/.mocharc.json');
209211
loadConfig = sinon.stub().returns({});
210212
findupSync = sinon.stub().returns(filepath);
@@ -222,7 +224,7 @@ describe('options', function () {
222224
loadOptions();
223225
},
224226
'to throw',
225-
/SyntaxError/
227+
/SyntaxError/,
226228
);
227229
});
228230
});

0 commit comments

Comments
 (0)