-
Notifications
You must be signed in to change notification settings - Fork 510
Provide filtering hook for errors so they can be suppressed by plugins #335
Comments
You can have a simple pre-commit hook to run JSCS against changed files. This would solve your problem if I got you right. |
As I mentioned above, we have a pre-commit hook that runs JSCS against staged files only. That part works just fine. The problem is this: suppose I have a file The commit will fail when the pre-commit hook runs, because JSCS will report four style errors. What we want to do is ignore errors on lines that are not added or modified in the staged commit. In the example, it would be easy to fix the other errors and try the commit again. Unfortunately, in some files there are literally hundreds of existing style errors. We are working to clean them up, but in the interim we don't want to spam developers with a ton of style errors unrelated to their changes. I've implemented a reporter that accomplishes the above by splicing the unrelated errors out of the module.exports = function (errorsCollection) {
errorsCollection.forEach(function (errors) {
if (!errors.isEmpty()) {
var errorList = errors.getErrorList();
var i = 0;
while (i < errorList.length) {
// is this line modified in the currently staged changes?
if (!isModified(errors.getFilename(), errorList[i].line)) {
errorList.splice(i, 1);
} else {
i++;
}
}
}
});
// delegate to the console reporter
require('jscs/lib/reporters/console')(errorsCollection);
}; This works, but it seems hacky to me, since it depends on the implementation detail that reporters receive the same So, my question is: do you plan to keep this implementation detail in future versions of JSCS, and, if not, would you accept a pull request that adds a filter hook to |
@lawnsea thanks for contributing. IMO, when an engineer cleans up a file, they should clean up the whole thing. This resolves the problem much quicker and gets to a nicely styled codebase quickly. However, I understand your situation. I will be adding a hook to filter errors shortly, and you'll be able to guarantee that it will happen right before errors are passed to reporters and you'll be able to hook into it. |
@mikesherov To clarify the potential implementation, are you talking about adding another cli option that allows a user to supply a path to a filters module (that exports a function that takes in the error list) that we require in and call with the errors before passing it to the reporter? |
@mrjoelkemp yes, exactly. The idea is to split filtering out from reporting. The only built in filter we'd have is the maxError option, which is also slated for 1.7. |
@mikesherov So if there's a custom filter hook, then use it and supply the return value of the hook to the reporter. Separately, if maxError is set to X (specifics of how to set it addressed in #238 (comment)), then If the above specification is the expected flow for filters, the implementation seems pretty straightforward. I can tackle both this and maxErr #238 (pending the specifics on setting the value) and have a PR ready for review very soon. |
@mrjoelkemp, we need to have maxError as a separate filter from any user supplied filter. Perhaps an array of filters seems the best. |
Are we in agreement of what a filter should do, has access to, and when it's invoked? |
However, I think either method is fine. On Tue, Sep 30, 2014 at 11:37 PM, Joel Kemp [email protected]
|
Thanks, Mike. I wanted to avoid the case of post-filtering since maxerr would cap the It would be strange to see 2 errors reported post-filtering when there are
|
Makes sense. |
Still, a 2 part filter makes sense:
|
@mikesherov @markelog I plan to implement the following: // sample config
{
filenameFilter: 'path/to/my/filter.js',
errorFilter: 'path/to/my/filter.js'
} Similarly, there will be cli options:
In regards to the config, I know that there are various possible forms of a jscsrc. Should I support a function object passed as the value for I'll open a PR this week. |
I don't think we need this kind of functionality, this could be solved in number of ways, not to mentioned that this is pretty much an edge case, plus i hope autoformating would fix it all. |
Happy you objected, Oleg. The The Agreed that autoformatting would fix the root of this problem. @lawnsea What are your thoughts on waiting until we have an autoformatter to take care of fixing all errors within a file? |
I'm not sure that an autoformatter would exactly fix our use case, as we would want to do any autoformatting in a separate PR. That said, we have the solution I originally outlined in place now, so we're in no rush. I have, however, received several inquiries about implementing my hack in other codebases, so there may be more need for this feature than is apparent from this thread. |
I agree with @lawnsea. There only needs to be one "filter", which should be a path to a filter module, which receives the errors, and can manually inspect them and discard them however it sees fit. This would allow precommit hooks, CI tools, etc. inspect certain files in a way that 'excludeFiles' can not. Even with autoformat, and with custom reporters, it seems like a good idea to have filtering as a separate, user definable step. |
@lawnsea, what does your solution look like? I'll love to have that help inform the direction we go in with this. |
I recently implemented a JSCS pre-commit hook on a large codebase. Unfortunately, there are thousands of existing style violations. We have filtered the hook to staged files, but this can still result in a situation where an engineer makes a stylistically-correct change to a file and has their commit aborted because of pre-existing style violations in the file they modified.
The solution to this problem that I have come up with is to filter JSCS's results against lines modified in the staged file. I have a couple of questions:
I have a strawman that seems like a bad/workable idea: reporters are passed the actual collection of style errors, so I could write a reporter that filters that against the currently staged changeset.
Imma do this, but I wonder if it would be better to teach JSCS a filtering hook instead.
What do y'all think?
The text was updated successfully, but these errors were encountered: