From f05e0db14b646a2e8e13df9605c1e351fa07045f Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 26 May 2023 01:10:49 -0400 Subject: [PATCH] Fix single-line detection rule. Linting. --- stylelint_plugins/align-colons.js | 96 +++++++++---------- ...ation-block-multi-line-min-declarations.js | 74 +++++++------- .../declaration-colon-min-space-before.js | 66 ++++++------- 3 files changed, 120 insertions(+), 116 deletions(-) diff --git a/stylelint_plugins/align-colons.js b/stylelint_plugins/align-colons.js index a7740c266..149246e5a 100644 --- a/stylelint_plugins/align-colons.js +++ b/stylelint_plugins/align-colons.js @@ -3,65 +3,65 @@ const stylelint = require('stylelint'); const { report, ruleMessages, validateOptions } = stylelint.utils; const ruleName = 'naturalcrit/align-colons'; 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) { - return function lint(postcssRoot, postcssResult) { + return function lint(postcssRoot, postcssResult) { - const validOptions = validateOptions( - postcssResult, - ruleName, - { - actual: primaryOption, - possible: [ - true, - false - ] - } - ); + const validOptions = validateOptions( + postcssResult, + ruleName, + { + actual : primaryOption, + possible : [ + true, + false + ] + } + ); - if (!validOptions) { //If the options are invalid, don't lint - return; - } - const isAutoFixing = Boolean(context.fix); - postcssRoot.walkRules(rule => { //Iterate CSS rules + if(!validOptions) { //If the options are invalid, don't lint + return; + } + const isAutoFixing = Boolean(context.fix); + postcssRoot.walkRules((rule)=>{ //Iterate CSS rules - let maxColonPos = 0; - let misaligned = false; - rule.each(declaration => { + let maxColonPos = 0; + let misaligned = false; + rule.each((declaration)=>{ - if(declaration.type != "decl") - return; + if(declaration.type != 'decl') + return; - let colonPos = declaration.prop.length + declaration.raws.between.indexOf(":"); - if (maxColonPos > 0 && colonPos != maxColonPos) { - misaligned = true; - } - maxColonPos = Math.max(maxColonPos, colonPos); - }); + const colonPos = declaration.prop.length + declaration.raws.between.indexOf(':'); + if(maxColonPos > 0 && colonPos != maxColonPos) { + misaligned = true; + } + maxColonPos = Math.max(maxColonPos, colonPos); + }); - if(misaligned) { - if (isAutoFixing) { //We are in “fix” mode - rule.each(declaration => { - if(declaration.type != "decl") - return; + if(misaligned) { + if(isAutoFixing) { //We are in “fix” mode + rule.each((declaration)=>{ + if(declaration.type != 'decl') + return; - declaration.raws.between = " ".repeat(maxColonPos - declaration.prop.length) + ":" + declaration.raws.between.split(":")[1]; - }) - } else { //We are in “report only” mode - report({ - ruleName, - result: postcssResult, - message: messages.expected(rule.selector), // Build the reported message - node: rule, // Specify the reported node - word: rule.selector, // Which exact word caused the error? This positions the error properly - }); - } - } - }); - }; + declaration.raws.between = `${' '.repeat(maxColonPos - declaration.prop.length)}:${declaration.raws.between.split(':')[1]}`; + }); + } else { //We are in “report only” mode + report({ + ruleName, + result : postcssResult, + message : messages.expected(rule.selector), // 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; diff --git a/stylelint_plugins/declaration-block-multi-line-min-declarations.js b/stylelint_plugins/declaration-block-multi-line-min-declarations.js index 57b308cdf..7144a923b 100644 --- a/stylelint_plugins/declaration-block-multi-line-min-declarations.js +++ b/stylelint_plugins/declaration-block-multi-line-min-declarations.js @@ -4,50 +4,54 @@ const { isNumber } = require('stylelint/lib/utils/validateTypes'); const { report, ruleMessages, validateOptions } = stylelint.utils; const ruleName = 'naturalcrit/declaration-block-multi-line-min-declarations'; 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) { - return function lint(postcssRoot, postcssResult) { + return function lint(postcssRoot, postcssResult) { - const validOptions = validateOptions( - postcssResult, - ruleName, - { - actual: primaryOption, - possible: [isNumber], - } - ); + const validOptions = validateOptions( + postcssResult, + ruleName, + { + actual : primaryOption, + possible : [isNumber], + } + ); - if (!validOptions) { //If the options are invalid, don't lint - return; - } - const isAutoFixing = Boolean(context.fix); + if(!validOptions) { //If the options are invalid, don't lint + return; + } + 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) - if (rule.nodes.length > primaryOption || !rule.nodes.every((node) => node.type === 'decl')) { - return; - } + //Apply rule only if all children are decls (no further nested rules) + if(rule.nodes.length > primaryOption || !rule.nodes.every((node)=>node.type === 'decl')) { + return; + } - if (isAutoFixing) { //We are in “fix” mode - rule.each((decl) => { - decl.raws.before = " "; - }); - rule.raws.after = ' '; - } else { - report({ - 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 - }); - } - }); - }; + //Ignore if already one line + if(!rule.nodes.some((node)=>node.raws.before.includes('\n')) && !rule.raws.after.includes('\n')) + return; + + if(isAutoFixing) { //We are in “fix” mode + rule.each((decl)=>{ + decl.raws.before = ' '; + }); + rule.raws.after = ' '; + } else { + report({ + 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; diff --git a/stylelint_plugins/declaration-colon-min-space-before.js b/stylelint_plugins/declaration-colon-min-space-before.js index a758d5f26..962084aa1 100644 --- a/stylelint_plugins/declaration-colon-min-space-before.js +++ b/stylelint_plugins/declaration-colon-min-space-before.js @@ -4,48 +4,48 @@ const { isNumber } = require('stylelint/lib/utils/validateTypes'); const { report, ruleMessages, validateOptions } = stylelint.utils; const ruleName = 'naturalcrit/declaration-colon-min-space-before'; 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) { - return function lint(postcssRoot, postcssResult) { + return function lint(postcssRoot, postcssResult) { - const validOptions = validateOptions( - postcssResult, - ruleName, - { - actual: primaryOption, - possible: [isNumber], - } - ); + const validOptions = validateOptions( + postcssResult, + ruleName, + { + actual : primaryOption, + possible : [isNumber], + } + ); - if (!validOptions) { //If the options are invalid, don't lint - return; - } - const isAutoFixing = Boolean(context.fix); + if(!validOptions) { //If the options are invalid, don't lint + return; + } + const isAutoFixing = Boolean(context.fix); - postcssRoot.walkDecls(decl => { //Iterate CSS declarations + postcssRoot.walkDecls((decl)=>{ //Iterate CSS declarations - let between = decl.raws.between; - const colonIndex = between.indexOf(":"); + const between = decl.raws.between; + const colonIndex = between.indexOf(':'); - if (between.slice(0, colonIndex).length >= primaryOption) { - return; - } - if (isAutoFixing) { //We are in “fix” mode - decl.raws.between = between.slice(0, colonIndex).replace(/\s*$/, ' '.repeat(primaryOption)) + between.slice(colonIndex) - } else { - report({ - ruleName, - result: postcssResult, - message: messages.expected(primaryOption), // Build the reported message - node: decl, // Specify the reported node - word: ":", // Which exact word caused the error? This positions the error properly - }); - } - }); - }; + if(between.slice(0, colonIndex).length >= primaryOption) { + return; + } + if(isAutoFixing) { //We are in “fix” mode + decl.raws.between = between.slice(0, colonIndex).replace(/\s*$/, ' '.repeat(primaryOption)) + between.slice(colonIndex); + } else { + report({ + ruleName, + result : postcssResult, + message : messages.expected(primaryOption), // Build the reported message + node : decl, // Specify the reported node + word : ':', // Which exact word caused the error? This positions the error properly + }); + } + }); + }; }); module.exports.ruleName = ruleName;