0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 21:02:43 +00:00

Fix single-line detection rule. Linting.

This commit is contained in:
Trevor Buckner
2023-05-26 01:10:49 -04:00
parent e621f2d19b
commit f05e0db14b
3 changed files with 120 additions and 116 deletions

View File

@@ -3,65 +3,65 @@ const stylelint = require('stylelint');
const { report, ruleMessages, validateOptions } = stylelint.utils; const { report, ruleMessages, validateOptions } = stylelint.utils;
const ruleName = 'naturalcrit/align-colons'; const ruleName = 'naturalcrit/align-colons';
const messages = ruleMessages(ruleName, { const messages = ruleMessages(ruleName, {
expected: (rule) => `Expected colons aligned within rule "${rule}"`, expected : (rule)=>`Expected colons aligned within rule "${rule}"`,
}); });
module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) { module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) {
return function lint(postcssRoot, postcssResult) { return function lint(postcssRoot, postcssResult) {
const validOptions = validateOptions( const validOptions = validateOptions(
postcssResult, postcssResult,
ruleName, ruleName,
{ {
actual: primaryOption, actual : primaryOption,
possible: [ possible : [
true, true,
false false
] ]
} }
); );
if (!validOptions) { //If the options are invalid, don't lint if(!validOptions) { //If the options are invalid, don't lint
return; return;
} }
const isAutoFixing = Boolean(context.fix); const isAutoFixing = Boolean(context.fix);
postcssRoot.walkRules(rule => { //Iterate CSS rules postcssRoot.walkRules((rule)=>{ //Iterate CSS rules
let maxColonPos = 0; let maxColonPos = 0;
let misaligned = false; let misaligned = false;
rule.each(declaration => { rule.each((declaration)=>{
if(declaration.type != "decl") if(declaration.type != 'decl')
return; return;
let colonPos = declaration.prop.length + declaration.raws.between.indexOf(":"); const colonPos = declaration.prop.length + declaration.raws.between.indexOf(':');
if (maxColonPos > 0 && colonPos != maxColonPos) { if(maxColonPos > 0 && colonPos != maxColonPos) {
misaligned = true; misaligned = true;
} }
maxColonPos = Math.max(maxColonPos, colonPos); maxColonPos = Math.max(maxColonPos, colonPos);
}); });
if(misaligned) { if(misaligned) {
if (isAutoFixing) { //We are in “fix” mode if(isAutoFixing) { //We are in “fix” mode
rule.each(declaration => { rule.each((declaration)=>{
if(declaration.type != "decl") if(declaration.type != 'decl')
return; return;
declaration.raws.between = " ".repeat(maxColonPos - declaration.prop.length) + ":" + declaration.raws.between.split(":")[1]; declaration.raws.between = `${' '.repeat(maxColonPos - declaration.prop.length)}:${declaration.raws.between.split(':')[1]}`;
}) });
} else { //We are in “report only” mode } else { //We are in “report only” mode
report({ report({
ruleName, ruleName,
result: postcssResult, result : postcssResult,
message: messages.expected(rule.selector), // Build the reported message message : messages.expected(rule.selector), // Build the reported message
node: rule, // Specify the reported node node : rule, // Specify the reported node
word: rule.selector, // Which exact word caused the error? This positions the error properly word : rule.selector, // Which exact word caused the error? This positions the error properly
}); });
} }
} }
}); });
}; };
}); });
module.exports.ruleName = ruleName; module.exports.ruleName = ruleName;

View File

@@ -4,50 +4,54 @@ const { isNumber } = require('stylelint/lib/utils/validateTypes');
const { report, ruleMessages, validateOptions } = stylelint.utils; const { report, ruleMessages, validateOptions } = stylelint.utils;
const ruleName = 'naturalcrit/declaration-block-multi-line-min-declarations'; const ruleName = 'naturalcrit/declaration-block-multi-line-min-declarations';
const messages = ruleMessages(ruleName, { const messages = ruleMessages(ruleName, {
expected: (decls) => `Rule with ${decls} declaration${decls == 1 ? '' : 's'} should be single line`, expected : (decls)=>`Rule with ${decls} declaration${decls == 1 ? '' : 's'} should be single line`,
}); });
module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) { module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) {
return function lint(postcssRoot, postcssResult) { return function lint(postcssRoot, postcssResult) {
const validOptions = validateOptions( const validOptions = validateOptions(
postcssResult, postcssResult,
ruleName, ruleName,
{ {
actual: primaryOption, actual : primaryOption,
possible: [isNumber], possible : [isNumber],
} }
); );
if (!validOptions) { //If the options are invalid, don't lint if(!validOptions) { //If the options are invalid, don't lint
return; return;
} }
const isAutoFixing = Boolean(context.fix); const isAutoFixing = Boolean(context.fix);
postcssRoot.walkRules(rule => { //Iterate CSS rules postcssRoot.walkRules((rule)=>{ //Iterate CSS rules
//Apply rule only if all children are decls (no nested rules) //Apply rule only if all children are decls (no further nested rules)
if (rule.nodes.length > primaryOption || !rule.nodes.every((node) => node.type === 'decl')) { if(rule.nodes.length > primaryOption || !rule.nodes.every((node)=>node.type === 'decl')) {
return; return;
} }
if (isAutoFixing) { //We are in “fix” mode //Ignore if already one line
rule.each((decl) => { if(!rule.nodes.some((node)=>node.raws.before.includes('\n')) && !rule.raws.after.includes('\n'))
decl.raws.before = " "; return;
});
rule.raws.after = ' '; if(isAutoFixing) { //We are in “fix” mode
} else { rule.each((decl)=>{
report({ decl.raws.before = ' ';
ruleName, });
result: postcssResult, rule.raws.after = ' ';
message: messages.expected(rule.nodes.length), // Build the reported message } else {
node: rule, // Specify the reported node report({
word: rule.selector, // Which exact word caused the error? This positions the error properly ruleName,
}); result : postcssResult,
} message : messages.expected(rule.nodes.length), // Build the reported message
}); node : rule, // Specify the reported node
}; word : rule.selector, // Which exact word caused the error? This positions the error properly
});
}
});
};
}); });
module.exports.ruleName = ruleName; module.exports.ruleName = ruleName;

View File

@@ -4,48 +4,48 @@ const { isNumber } = require('stylelint/lib/utils/validateTypes');
const { report, ruleMessages, validateOptions } = stylelint.utils; const { report, ruleMessages, validateOptions } = stylelint.utils;
const ruleName = 'naturalcrit/declaration-colon-min-space-before'; const ruleName = 'naturalcrit/declaration-colon-min-space-before';
const messages = ruleMessages(ruleName, { const messages = ruleMessages(ruleName, {
expected: (num) => `Expected at least ${num} space${num == 1 ? '' : 's'} before ":"` expected : (num)=>`Expected at least ${num} space${num == 1 ? '' : 's'} before ":"`
}); });
module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) { module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOption, secondaryOptionObject, context) {
return function lint(postcssRoot, postcssResult) { return function lint(postcssRoot, postcssResult) {
const validOptions = validateOptions( const validOptions = validateOptions(
postcssResult, postcssResult,
ruleName, ruleName,
{ {
actual: primaryOption, actual : primaryOption,
possible: [isNumber], possible : [isNumber],
} }
); );
if (!validOptions) { //If the options are invalid, don't lint if(!validOptions) { //If the options are invalid, don't lint
return; return;
} }
const isAutoFixing = Boolean(context.fix); const isAutoFixing = Boolean(context.fix);
postcssRoot.walkDecls(decl => { //Iterate CSS declarations postcssRoot.walkDecls((decl)=>{ //Iterate CSS declarations
let between = decl.raws.between; const between = decl.raws.between;
const colonIndex = between.indexOf(":"); const colonIndex = between.indexOf(':');
if (between.slice(0, colonIndex).length >= primaryOption) { if(between.slice(0, colonIndex).length >= primaryOption) {
return; return;
} }
if (isAutoFixing) { //We are in “fix” mode if(isAutoFixing) { //We are in “fix” mode
decl.raws.between = between.slice(0, colonIndex).replace(/\s*$/, ' '.repeat(primaryOption)) + between.slice(colonIndex) decl.raws.between = between.slice(0, colonIndex).replace(/\s*$/, ' '.repeat(primaryOption)) + between.slice(colonIndex);
} else { } else {
report({ report({
ruleName, ruleName,
result: postcssResult, result : postcssResult,
message: messages.expected(primaryOption), // Build the reported message message : messages.expected(primaryOption), // Build the reported message
node: decl, // Specify the reported node node : decl, // Specify the reported node
word: ":", // Which exact word caused the error? This positions the error properly word : ':', // Which exact word caused the error? This positions the error properly
}); });
} }
}); });
}; };
}); });
module.exports.ruleName = ruleName; module.exports.ruleName = ruleName;