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

Skip ignore comment checks for files with no ignore comments #730

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import valuesParser from 'postcss-value-parser';
import { isBlockIgnored } from './is-ignored';
import { isBlockIgnored, hasIgnoreComment } from './is-ignored';

// return custom selectors from the css root, conditionally removing them
export default function getCustomPropertiesFromRoot(root, opts): Map<string, valuesParser.ParsedValue> {
Expand All @@ -8,6 +8,9 @@ export default function getCustomPropertiesFromRoot(root, opts): Map<string, val
const customPropertiesFromRootPseudo: Map<string, valuesParser.ParsedValue> = new Map();
const out: Map<string, valuesParser.ParsedValue> = new Map();


const isIgnoreCommentInFile = root.source && hasIgnoreComment(root.source.input.css);

// for each html or :root rule
root.nodes.slice().forEach(rule => {
const customPropertiesObject = isHtmlRule(rule)
Expand All @@ -19,7 +22,7 @@ export default function getCustomPropertiesFromRoot(root, opts): Map<string, val
// for each custom property
if (customPropertiesObject) {
rule.nodes.slice().forEach(decl => {
if (decl.variable && !isBlockIgnored(decl)) {
if (decl.variable && (!isIgnoreCommentInFile || !isBlockIgnored(decl))) {
const { prop } = decl;

// write the parsed value to the custom property
Expand All @@ -33,7 +36,7 @@ export default function getCustomPropertiesFromRoot(root, opts): Map<string, val
});

// conditionally remove the empty html or :root rule
if (!opts.preserve && isEmptyParent(rule) && !isBlockIgnored(rule)) {
if (!opts.preserve && isEmptyParent(rule) && (!isIgnoreCommentInFile || !isBlockIgnored(rule))) {
rule.remove();
}
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/postcss-custom-properties/src/lib/is-ignored.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function hasIgnoreComment(input: string) {
return /(!\s*)?postcss-custom-properties:\s*(?:off|ignore\s+next)\b/.test(input);
}

function isBlockIgnored(ruleOrDeclaration) {
const rule = ruleOrDeclaration.selector ?
ruleOrDeclaration : ruleOrDeclaration.parent;
Expand All @@ -15,6 +19,7 @@ function isRuleIgnored(rule) {
}

export {
hasIgnoreComment,
isBlockIgnored,
isRuleIgnored,
};