-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selectively disable rules with codingStandardsIgnoreStart and codingStandardsIgnoreLine #604
Comments
👍 |
What format would the ignoring of multiple disabled rules take? Comma-separation? |
Comma-separated rules is what JSCS uses: http://jscs.info/overview.html#disabling-specific-rules // jscs:disable requireCurlyBraces, requireDotNotation |
👍 |
1 similar comment
👍 |
👍 |
👍 |
👍 |
scss-lint disables lint on a given "scope". See: https://github.com/brigade/scss-lint#disabling-linters-via-source
eslint appears to work in a similar way. See: http://eslint.org/docs/user-guide/configuring.html#disabling-rules-with-inline-comments
In both cases it's useful to disable select lints for a given file, particularly as you're introducing new requirements over time (such as making code style stricter). |
Related PEAR request: http://pear.php.net/bugs/bug.php?id=18885 |
👍 |
👍 |
Only one function, and three methods, were found to be violating the preferred WPCS naming conventions, so these lines have been excluded. While it does mean that the rest of the function signature line is also excluded from other checks, this seems like less of an issue than excluded the whole check from the rest of the project, which may allow other incorrectly named functions and methods to be accidentally introduced. Once squizlabs/PHP_CodeSniffer#604 is addressed, this ignores should be able to target just the specific check of the function / method name.
Slevomat Coding standard has implemented something similar in its sniffs. |
The prep for this change has been pushed: 5325170 I've added a new shorter comment syntax and put a bit more core support in. The syntax for disabling PHPCS and ignoring lines is now The next step is to get these comments to allow a list of sniff codes after them so they can selectively disable or ignore some errors. If that's working, a possible extension would be to allow the new |
@gsherwood Will we be able to put explanation text at the end of the line? // phpcs:disable WordPress.VIP.SlowDBQuery.slow_db_query -- Because reason |
That's not something I had considered yet, but it's a good idea. It would need some sort of separator like you've put in your comment. Have you seen that syntax used elsewhere? Anyone else have any suggestions? |
|
While thinking about adding selective support for So I think the last feature to add is the ability to add your own comments. |
What if you matched one or more spaces followed by any non-word character? Something like this: https://regex101.com/r/w1hwws/1 (could be improved a bit, but you get the idea)
Not really, I just use it in my own projects/at work. |
That should be possible given the data that goes into those comments, but I'd prefer to decide on a specific syntax because it makes it easier to document and test. |
Hey folks, I just wanted to weigh in here on the syntax of the comments. I would really recommend that you follow the syntax for eslint because it would allow for some good overlaps. https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments as for the separators for "following comments", I would recommend something very simple. How about you just consider it as a list of comma separated rules which makes this valid // eslint-disable-next-line no-console, this is more of a comment
console.log('face'); and because Just an idea |
@mansona
|
The new comment syntax does use a comma separated list of rules, but PHPCS doesn't know if they are valid or not because it doesn't only have a built-in list of sniffs. Plus, it allows for single words to mean something important (disable or enable an entire standard - see comment above #604 (comment)) and it supports setting sniff config vars ( I'd like a comment syntax that supports all those formats without confusion. I could just assume they are sniff codes and throw them into the main array. But that means having to put a comma before your comment, like this:
And it makes life really hard if you want to comment a "disable all" style comment, like this:
(is I think a separator is the way to go, and I think |
I've added support for additional developer comments using the So you can now do this: // phpcs:disable -- Turn off PHPCS
// phpcs:disable Generic.Commenting.Todo, PSR1.Files.SideEffects -- Because reasons etc You can also use block comments if you want something with more explanation. In this case, you don't need to use the comment separator because the explanation is on a different line: /*
We pasted this code in from a library doc and it needs to
remain as is, so disable PHPCS for this block of code.
phpcs:disable
*/
function Foo() {}
// phpcs:enable |
@Frenzie I wouldn't expect an error in this case no, this process is about turning off errors so I don't think it would be necessary. One thing I would like to add to the wider conversation here, I use these eslint style "overrides" every day, and have been for a few years now but never have I wanted to add "extra comments" to the one line I don't see anything wrong with the following if you really need that:
I'd much rather a simple case like this ☝️ than over-complicate the syntax. |
You can do that with the new PHPCS syntax as well. The benefit of putting it all on one line (if it is a short message) is that the comment line itself is completely ignored by PHPCS and doesn't need to confirm to your standard's comment rules. That's not something I personally care about, but I think other developers do. |
I'll throw this idea into feedback for a while, but I think this feature is now done. |
Ignoring PHPCS in two lines of the code where apparently it is not possible to fix the violations. I tried using what @claudiosanches suggested in this comment #17909 (comment) but both tags didn't work. Lets revisit this when PHPCS 3.2.0 is released and we can selectively disable rules (see squizlabs/PHP_CodeSniffer#604).
I'm using sniff [
'foo' => 'very_long_string...' , // phpcs:disable Generic.Files.LineLength.TooLong
] In case of
|
@o5 The Edit: I'd also suggest ignoring the whole sniff given that the really long comment is probably going to push the line length to MaxExceeded. So I'd suggest |
@gsherwood Option |
This issue shows up a lot in the search so I just wanted to share this as a result of this discussion. The Some examples: For Multi-line disable// phpcs:disable
// phpcs:enable For Single line ignore// phpcs:ignore See this page for more information: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file Peace! ✌️ |
Gooood feature! I prefer using @ annotations which works like:
|
For disabling and enabling a specific rule
|
Only one function, and three methods, were found to be violating the preferred WPCS naming conventions, so these lines have been excluded. While it does mean that the rest of the function signature line is also excluded from other checks, this seems like less of an issue than excluded the whole check from the rest of the project, which may allow other incorrectly named functions and methods to be accidentally introduced. Once squizlabs/PHP_CodeSniffer#604 is addressed, this ignores should be able to target just the specific check of the function / method name.
PHPCS supports comment directives like:
However, these are all or nothing. What would be even more useful is if we can selectively ignore certain sniffs or errors within sniffs. For instance:
or
This draws inspiration from JSCS and its directives like:
//jscs:disable requireCamelCaseOrUpperCaseIdentifiers
We have this in the WordPress-Coding-Standards project in a sniff-specific ad hoc system of end-of-statement comments:
Which suppresses the
WordPress.XSS.EscapeOutput
sniff. But it would be very useful if this was standardized in PHPCS itself and available to all sniffsThe text was updated successfully, but these errors were encountered: