-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
feat: new enforceSuppressionCode
rule
#489
base: master
Are you sure you want to change the base?
Changes from 2 commits
58796db
d3a004e
f8260f3
72cdc4f
977a365
6542387
bbb4b80
79dbfec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### `enforce-suppression-code` | ||
|
||
This rule enforces a suppression code on flow suppression comments such as `$FlowFixMe` and `$FlowExpectedError`. | ||
|
||
<!-- assertions enforceSuppressionCode --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const schema = [ | ||
{ | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const message = (suppression = '') => { | ||
return `${suppression} is missing a suppression code`; | ||
}; | ||
|
||
const create = (context) => { | ||
const isMissingSuppressionCode = function (value) { | ||
const suppressionTypes = ['$FlowFixMe', '$FlowExpectedError']; | ||
|
||
let failedType; | ||
suppressionTypes.forEach((cur) => { | ||
if (value.startsWith(cur) && | ||
!value.startsWith(`${cur}[`) && | ||
!value.endsWith(']')) { | ||
failedType = cur; | ||
} | ||
}); | ||
|
||
return failedType; | ||
}; | ||
|
||
const handleComment = function (comment) { | ||
const value = comment.value.trim().split(' ').filter((arg) => { | ||
return arg.length > 0; | ||
})[0]; | ||
const failedType = isMissingSuppressionCode(value); | ||
|
||
if (failedType) { | ||
context.report(comment, message(failedType)); | ||
} | ||
}; | ||
|
||
return { | ||
Program () { | ||
context | ||
.getSourceCode() | ||
.getAllComments() | ||
.filter((comment) => { | ||
return comment.type === 'Block' || comment.type === 'Line'; | ||
}) | ||
.forEach(handleComment); | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
schema, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have here some examples of block comments as well, see: https://flow.org/en/docs/errors/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool found issue with block comments so fixed it |
||
invalid: [ | ||
{ | ||
code: '// $FlowFixMe I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowFixMe is missing a suppression code', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flow is warning with the following message when the suppression code is missing:
I understand that this would be complicated to do in Eslint, but maybe it would be nice to explain how to fix this error? For example:
What do you think? Just a nit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah no worries, let me look into it later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, agree it's heaps better |
||
}, | ||
], | ||
}, | ||
{ | ||
code: '// $FlowExpectedError I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowExpectedError is missing a suppression code', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'const text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowFixMe[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowExpectedError[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding there all the newly supported suppressions? See: https://flow.org/en/docs/config/options/#toc-suppress-comment-regex