简体   繁体   中英

How do I make JSHint complain about missing function parameters?

If I have a function:

export function createWeeklyStats(activities, offset, length) {
    ...        
}

And I call the function like:

createWeeklyStats(myListOfActivities, 0)

JSHint does not complain that I'm missing the length parameter. I could not find a matching enforcing option here:

JSHint Options Reference

Does one exist?

I'm updating an existing method to include a new required parameter, and while I'm a self-proclaimed adult, I'll throw a fit right here if my best way out is a full-text search.

The commenters are correct that there is no way to ask js[hl]int to find this.

Here are some options:

Explicitly check arg count

Check argument count:

function createWeeklyStats(activities, offset, length) {
  assert(arguments.length === 3);

Granted, that will raise the error at run-time, rather than compile-time.

Perhaps you want to write a little higher-order function which helps you out here:

function check_arg_count(f) {
  return function() {
    assert(arguments.length === f.length);
    return f.apply(this, arguments);
  };
}

Now instead of calling createWeeklyStats , you call check_arg_count(createWeeklyStats) .

I don't see anything particularly wrong with this approach. It will be a little tricky dealing with optional arguments. At some point, you might want to throw in the towel and make the move to a more strongly typed language or TypeScript.

Use a refactoring tool

In this case you are refactoring, so how about using a refactoring tool? Many editors/IDE have such features. Check out the documentation.

There are other tools to help with refactoring. For instance, take a look at grasp . Actually, the grasp tutorial describes a similar case involving refactoring a function's argument list:

$ grasp -r -e 'createWeeklyStats($a, $o)' -R 'createWeeklyStats($a, $o, 0)' })' .

This will add the third argument to all calls. Changing the new third parameter is something you will obviously have to take care of yourself. grasp provides other features to narrow the scope of the change.

Customize the linter

You can write custom rules for eslint (but apparently not jshint; see this question ). This could get you started: https://gist.github.com/jareware/7179093 . Also http://eslint.org/docs/developer-guide/working-with-rules.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM