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
Fix block-no-empty
false positives for reportNeedlessDisables
#6381
Merged
ybiquitous
merged 1 commit into
main
from
fix-false-positives-for-reportNeedlessDisables
Oct 2, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"stylelint": patch | ||
--- | ||
|
||
Fixed: `block-no-empty` false positives for `reportNeedlessDisables` |
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
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,36 @@ | ||
'use strict'; | ||
|
||
const postcss = require('postcss'); | ||
|
||
const { extractStylelintCommand, isStylelintCommand } = require('../stylelintCommand'); | ||
|
||
test('extractStylelintCommand', () => { | ||
expect(extractStylelintCommand(comment('stylelint-disable'))).toBe('stylelint-disable'); | ||
expect(extractStylelintCommand(comment('stylelint-disable '))).toBe('stylelint-disable'); | ||
expect(extractStylelintCommand(comment('stylelint-disable\t'))).toBe('stylelint-disable'); | ||
expect(extractStylelintCommand(comment('stylelint-disable --'))).toBe('stylelint-disable'); | ||
|
||
expect(extractStylelintCommand(comment(''))).toBe(''); | ||
expect(extractStylelintCommand(comment(' '))).toBe(''); | ||
expect(extractStylelintCommand(comment('\t'))).toBe(''); | ||
}); | ||
|
||
test('isStylelintCommand', () => { | ||
expect(isStylelintCommand(comment('stylelint-disable'))).toBe(true); | ||
expect(isStylelintCommand(comment('stylelint-disable-line'))).toBe(true); | ||
expect(isStylelintCommand(comment('stylelint-disable-next-line'))).toBe(true); | ||
expect(isStylelintCommand(comment('stylelint-enable'))).toBe(true); | ||
|
||
expect(isStylelintCommand(comment('stylelint-'))).toBe(false); | ||
expect(isStylelintCommand(comment('stylelint-disable-'))).toBe(false); | ||
expect(isStylelintCommand(comment('stylelint-disable-lineee'))).toBe(false); | ||
expect(isStylelintCommand(comment('stylelint-disable-next'))).toBe(false); | ||
expect(isStylelintCommand(comment('stylelint-enable-'))).toBe(false); | ||
expect(isStylelintCommand(comment(''))).toBe(false); | ||
expect(isStylelintCommand(comment(' '))).toBe(false); | ||
expect(isStylelintCommand(comment('\t'))).toBe(false); | ||
}); | ||
|
||
function comment(text) { | ||
return postcss.comment({ text }); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
'use strict'; | ||
|
||
const hasBlock = require('./hasBlock'); | ||
|
||
/** | ||
* Check if a statement has an empty block. | ||
* | ||
* @param {import('postcss').Rule | import('postcss').AtRule} statement - postcss rule or at-rule node | ||
* @return {boolean} True if the statement has a block and it is empty | ||
*/ | ||
module.exports = function hasEmptyBlock(statement) { | ||
return ( | ||
statement.nodes !== undefined && statement.nodes.length === 0 // has block | ||
); // and is empty | ||
return hasBlock(statement) && statement.nodes.length === 0; | ||
}; |
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,53 @@ | ||
'use strict'; | ||
|
||
const { assertString } = require('./validateTypes'); | ||
|
||
const DISABLE_COMMAND = 'stylelint-disable'; | ||
const DISABLE_LINE_COMMAND = 'stylelint-disable-line'; | ||
const DISABLE_NEXT_LINE_COMMAND = 'stylelint-disable-next-line'; | ||
const ENABLE_COMMAND = 'stylelint-enable'; | ||
|
||
const ALL_COMMANDS = new Set([ | ||
DISABLE_COMMAND, | ||
DISABLE_LINE_COMMAND, | ||
DISABLE_NEXT_LINE_COMMAND, | ||
ENABLE_COMMAND, | ||
]); | ||
|
||
/** @typedef {import('postcss').Comment} Comment */ | ||
|
||
/** | ||
* Extract a command from a given comment. | ||
* | ||
* @param {Comment} comment | ||
* @returns {string} | ||
*/ | ||
function extractStylelintCommand(comment) { | ||
const [command] = comment.text.split(/\s/, 1); | ||
|
||
assertString(command); | ||
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. [note] Since |
||
|
||
return command; | ||
} | ||
|
||
/** | ||
* Tests if the given comment is a Stylelint command. | ||
* | ||
* @param {Comment} comment | ||
* @returns {boolean} | ||
*/ | ||
function isStylelintCommand(comment) { | ||
const command = extractStylelintCommand(comment); | ||
|
||
return command !== undefined && ALL_COMMANDS.has(command); | ||
} | ||
|
||
module.exports = { | ||
DISABLE_COMMAND, | ||
DISABLE_LINE_COMMAND, | ||
DISABLE_NEXT_LINE_COMMAND, | ||
ENABLE_COMMAND, | ||
|
||
extractStylelintCommand, | ||
isStylelintCommand, | ||
}; |
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.
[note] It should be more readable and less duplicated thanks to
hasBlock
:stylelint/lib/utils/hasBlock.js
Lines 9 to 11 in 736bc29