简体   繁体   English

如何让JSHint抱怨缺少函数参数?

[英]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. JSHint并不抱怨我缺少length参数。 I could not find a matching enforcing option here: 我在这里找不到匹配的强制执行选项:

JSHint Options Reference JSHint选项参考

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. 评论者是正确的,没有办法让js [hl] int找到这个。

Here are some options: 以下是一些选项:

Explicitly check arg count 明确检查arg计数

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) . 现在,不要调用createWeeklyStats ,而是调用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. 在某些时候,您可能想要放弃使用更强类型的语言或TypeScript。

Use a refactoring tool 使用重构工具

In this case you are refactoring, so how about using a refactoring tool? 在这种情况下,您正在重构,那么如何使用重构工具? Many editors/IDE have such features. 许多编辑器/ IDE都有这样的功能。 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 自定义linter

You can write custom rules for eslint (but apparently not jshint; see this question ). 您可以为eslint编写自定义规则(但显然不是jshint;请参阅此问题 )。 This could get you started: https://gist.github.com/jareware/7179093 . 这可以帮助您入门: https//gist.github.com/jareware/7179093 Also http://eslint.org/docs/developer-guide/working-with-rules.html . 还有http://eslint.org/docs/developer-guide/working-with-rules.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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