javascriptlint
javascriptlint copied to clipboard
Incorrect unreferenced_argument errors
Code example:
var loadData = function (event, context) {
if (context && context.hasActionChanged()) {
return;
}};
}
Actual result: jsLint gives erroneous warning for the first argument of the function, if it's not used and it's not last. See below: {code}/details.js(127): warning: argument declared but never referenced: event (unreferenced_argument){code}
Expected result: jsLint should give a warning only for the arguments that are not used and are located after the last argument, which is used.
Thanks for the report. This is one of those cases where the lint warning is a heuristic that often catches real issues, but also has a fair number of false positives. The "unreferenced argument" warning is intended to help keep code clean by catching cases where you've defined a function with unnecessary arguments. But as you've pointed out, there's a legitimate reason for having extra, unreferenced arguments: because the function is being passed to some other interface that calls it with those arguments, even though your function doesn't need to use them.
This is reasonably common, and the workaround is to override this warning on a case-by-case basis. For example:
var loadData = function (event, context) {
/*jsl:unused event*/
if (context && context.hasActionChanged()) {
return;
}
};
If you find this is more trouble than it's worth for your codebase, you can also turn off the warning altogether using a modified jsl.conf file. (For what it's worth, I don't currently check my code with this warning enabled, although I didn't realize until now that it was relatively easy to override, so I may start doing it!)
I don't think it's correct to say that the warning shouldn't be emitted for arguments that come before other used arguments because it's very possible that such arguments are really unused. I think it's better to rely on the programmer to indicate that the unused argument is intended, using the /jsl:unused/ comment. As I understand it, the behavior of javascriptlint here exactly matches the behavior of C's lint, which has the same problem (and uses the override /ARGSUSED/).