-
-
Notifications
You must be signed in to change notification settings - Fork 153
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
Open
Brianzchen
wants to merge
8
commits into
gajus:master
Choose a base branch
from
Brianzchen:suppression-error-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58796db
add suppression code rule
Brianzchen d3a004e
make more concise logic
Brianzchen f8260f3
add more suppression types and more test cases
Brianzchen 72cdc4f
Merge branch 'master' into suppression-error-code
Brianzchen 977a365
Merge branch 'master' of github.com:gajus/eslint-plugin-flowtype into…
Brianzchen 6542387
update error messaging
Brianzchen bbb4b80
Merge branch 'suppression-error-code' of github.com:Brianzchen/eslint…
Brianzchen 79dbfec
Merge branch 'master' into suppression-error-code
Brianzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const schema = [ | ||
{ | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const message = (suppression = '') => { | ||
return `${suppression} is missing a suppression error code. Please update this suppression to use an error code: ${suppression}[…]`; | ||
}; | ||
|
||
const create = (context) => { | ||
const isMissingSuppressionCode = function (value) { | ||
const suppressionTypes = ['$FlowFixMe', '$FlowExpectedError', '$FlowIssue', '$FlowIgnore']; | ||
|
||
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.type === 'Block' ? | ||
comment.value.replace(/\*/g, '') : | ||
comment.value; | ||
const suppression = value.trim().split(' ').filter((arg) => { | ||
return arg.length > 0; | ||
})[0]; | ||
const failedType = isMissingSuppressionCode(suppression); | ||
|
||
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: '// $FlowFixMe I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowFixMe is missing a suppression error code. Please update this suppression to use an error code: $FlowFixMe[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// $FlowExpectedError I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowExpectedError is missing a suppression error code. Please update this suppression to use an error code: $FlowExpectedError[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// $FlowIssue I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowIssue is missing a suppression error code. Please update this suppression to use an error code: $FlowIssue[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// $FlowIgnore I am doing something evil here\nconst text = \'HELLO\';', | ||
errors: [ | ||
{ | ||
message: '$FlowIgnore is missing a suppression error code. Please update this suppression to use an error code: $FlowIgnore[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '/* $FlowIgnore I am doing something evil here */', | ||
errors: [ | ||
{ | ||
message: '$FlowIgnore is missing a suppression error code. Please update this suppression to use an error code: $FlowIgnore[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '{ /* $FlowIgnore I am doing something evil here */ }', | ||
errors: [ | ||
{ | ||
message: '$FlowIgnore is missing a suppression error code. Please update this suppression to use an error code: $FlowIgnore[…]', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `/** | ||
* $FlowIgnore I am doing something evil here | ||
*/`, | ||
errors: [ | ||
{ | ||
message: '$FlowIgnore is missing a suppression error code. Please update this suppression to use an error code: $FlowIgnore[…]', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'const text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowFixMe[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowExpectedError[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowIssue[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '// $FlowIgnore[incompatible-call] TODO 48\nconst text = \'HELLO\';', | ||
}, | ||
{ | ||
code: '/* $FlowIgnore[incompatible-call] TODO 48 */', | ||
}, | ||
{ | ||
code: `/** | ||
* $FlowIgnore[incompatible-call] TODO 48 | ||
*/`, | ||
}, | ||
{ | ||
code: '/* $FlowIgnore[incompatible-call] TODO 48 */', | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Cool found issue with block comments so fixed it