Skip to content
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

Improve inline comment merging #4950

Merged
merged 4 commits into from Sep 24, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/assignDisabledRanges.js
Expand Up @@ -76,7 +76,7 @@ module.exports = function (root, result) {
if (
!(
isInlineComment(comment) &&
comment.text.startsWith('stylelint-') &&
isStylelintCommand(comment) &&
next &&
next.type === 'comment' &&
(comment.text.includes('--') || next.text.startsWith('--'))
Expand All @@ -93,7 +93,7 @@ module.exports = function (root, result) {
/** @type {PostcssComment} */
let current = next;

while (isInlineComment(current) && !current.text.startsWith('stylelint-')) {
while (isInlineComment(current) && !isStylelintCommand(current)) {
const currentLine = (current.source && current.source.end && current.source.end.line) || 0;

if (lastLine + 1 !== currentLine) break;
Expand Down Expand Up @@ -126,6 +126,13 @@ module.exports = function (root, result) {
return comment.inline || comment.raws.inline;
}

/**
* @param {PostcssComment} comment
*/
function isStylelintCommand(comment) {
return /^stylelint-(disable|enable)/.test(comment.text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just remembered that we have disableCommand and enableCommand in this file. I think we should use them instead of hardcoded values. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense. Should I just call startsWith with both or should I put a compiled RegExp in a global variable and use that? (since a regex literal couldn't be used in this case)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would choose startsWith because it's easier to understand.

}

/**
* @param {PostcssComment} comment
*/
Expand Down