0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-20 22:32:45 +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

@@ -26,16 +26,16 @@ module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOpti
return;
}
const isAutoFixing = Boolean(context.fix);
postcssRoot.walkRules(rule => { //Iterate CSS rules
postcssRoot.walkRules((rule)=>{ //Iterate CSS rules
let maxColonPos = 0;
let misaligned = false;
rule.each(declaration => {
rule.each((declaration)=>{
if(declaration.type != "decl")
if(declaration.type != 'decl')
return;
let colonPos = declaration.prop.length + declaration.raws.between.indexOf(":");
const colonPos = declaration.prop.length + declaration.raws.between.indexOf(':');
if(maxColonPos > 0 && colonPos != maxColonPos) {
misaligned = true;
}
@@ -44,12 +44,12 @@ module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOpti
if(misaligned) {
if(isAutoFixing) { //We are in “fix” mode
rule.each(declaration => {
if(declaration.type != "decl")
rule.each((declaration)=>{
if(declaration.type != 'decl')
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
report({
ruleName,

View File

@@ -25,16 +25,20 @@ module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOpti
}
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')) {
return;
}
//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 = " ";
decl.raws.before = ' ';
});
rule.raws.after = ' ';
} else {

View File

@@ -25,23 +25,23 @@ module.exports = stylelint.createPlugin(ruleName, function getPlugin(primaryOpti
}
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)
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
word : ':', // Which exact word caused the error? This positions the error properly
});
}
});