@projectwallace/css-code-quality 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ var e=require("@projectwallace/css-analyzer");function r(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,o=new Array(r);t<r;t++)o[t]=e[t];return o}var t=[function(e){return{id:"Imports",score:10*e.atrules.import.total,value:e.atrules.import.total}},function(e){return{id:"EmptyRules",score:e.rules.empty.total,value:e.rules.empty.total}},function(e){var r={id:"SelectorDuplications",score:0,value:1-e.selectors.uniquenessRatio};return e.selectors.uniquenessRatio<.66&&(r.score=Math.floor(10*(1-e.selectors.uniquenessRatio))),r},function(e){var r={id:"DeclarationDuplications",score:0,value:1-e.declarations.unique.ratio};return e.declarations.unique.ratio<.66&&(r.score=Math.floor(10*(1-e.declarations.unique.ratio))),r},function(e){return{id:"CssSize",score:e.stylesheet.size>2e5?5:0,value:e.stylesheet.size}},function(e){var r=e.stylesheet.comments;return{id:"TooMuchComments",score:Math.min(10,Math.floor(r.size/250)),value:r.size}},function(e){var r=e.stylesheet.embeddedContent.size;return{id:"TooMuchEmbeddedContent",score:Math.min(20,Math.floor(r.total/250)),value:r.total}}],o=[function(e){var r={id:"SourceLinesOfCode",score:0,value:e.stylesheet.sourceLinesOfCode};if(e.stylesheet.sourceLinesOfCode>1e4){var t=Math.floor((e.stylesheet.sourceLinesOfCode-1e4)/1e3);r.score=Math.min(15,t)}return r},function(e){var r=e.rules.selectors.mean,t={id:"AverageSelectorsPerRule",score:0,value:r};if(r>2){var o=Math.floor(5*(r-2));t.score=Math.min(15,o)}return t},function(e){var r={id:"AverageDeclarationsPerRule",score:0,value:e.rules.declarations.mean};return e.rules.declarations.mean>5&&(r.score=Math.min(15,0)),r},function(e){var r={id:"MaxSelectorsPerRule",score:0,value:e.rules.selectors.max};if(e.rules.selectors.max>10){var t=Math.ceil(.5*(e.rules.selectors.max-10));r.score=Math.min(t,15)}return r},function(e){var r={id:"MaxDeclarationsPerRule",score:0,value:e.rules.declarations.max};if(e.rules.declarations.max>10){var t=Math.ceil(.5*(e.rules.declarations.max-10));r.score=Math.min(15,t)}return r},function(e){var r=e.rules.selectors.mode,t=e.rules.selectors.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonSelectorsPerRule",score:0,value:e.rules.selectors.mode};if(t>.1*e.rules.total){var a=Math.floor(.01*t);o.score=Math.min(15,a)}return o},function(e){var r=e.rules.selectors.mode,t=e.rules.declarations.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonDeclarationsPerRule",score:0,value:e.rules.declarations.mode};if(t>.1*e.rules.total){var a=Math.floor(.01*t);o.score=Math.min(15,a)}return o}],a=[function(e){var r=e.selectors.complexity.mode,t=e.selectors.complexity.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonSelectorComplexity",score:0,value:0===e.selectors.total?0:t/e.selectors.total};if(t>.1*e.selectors.total){var a=Math.floor(.01*t);o.score=Math.min(10,a)}return o},function(r){var t=r.selectors.specificity.mode,o=r.selectors.specificity.items.filter(function(r){return e.compareSpecificity(r,t)<0}).length,a={id:"MoreThanMostCommonSelectorSpecificity",score:0,value:0===r.selectors.total?0:o/r.selectors.total};if(o>.1*r.selectors.total){var s=Math.floor(.01*o);a.score=Math.min(10,s)}return a},function(e){var r=e.selectors.complexity.max,t={id:"MaxSelectorComplexity",score:0,value:e.selectors.complexity.max};if(r>5){var o=Math.ceil(.5*(r-5));t.score=Math.min(5,o)}return t},function(e){var r=e.selectors.complexity.mean,t={id:"AverageSelectorComplexity",score:0,value:r};if(r>2){var o=Math.ceil(2*(r-2));t.score=Math.min(10,o)}return t},function(e){var r=e.selectors.id.ratio,t={id:"IdSelectorRatio",score:0,value:r};if(r>.01){var o=Math.floor(10*(r-.01));t.score=Math.min(o,5)}return t},function(e){var r=e.declarations.importants.ratio,t={id:"ImportantRatio",score:0,value:r};if(r>.01){var o=Math.floor(10*(r-.01));t.score=Math.min(o,5)}return t}];function s(e){for(var t,o=e.result,a=100,s=[],n=[],i=function(e,t){var o="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(o)return(o=o.call(e)).next.bind(o);if(Array.isArray(e)||(o=function(e,t){if(e){if("string"==typeof e)return r(e,t);var o=Object.prototype.toString.call(e).slice(8,-1);return"Object"===o&&e.constructor&&(o=e.constructor.name),"Map"===o||"Set"===o?Array.from(e):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?r(e,t):void 0}}(e))){o&&(e=o);var a=0;return function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e.guards);!(t=i()).done;){var l=(0,t.value)(o);l.score>0?(a-=l.score,s.push(l)):n.push(l)}return{score:Math.max(a,0),violations:s,passes:n}}exports.calculate=function(r){var n=e.analyze(r),i=s({result:n,guards:t}),l=s({result:n,guards:o}),c=s({result:n,guards:a});return{score:0,violations:i.violations.concat(l.violations).concat(c.violations),passes:i.passes.concat(l.passes).concat(c.passes),performance:i,maintainability:l,complexity:c}};
2
+ //# sourceMappingURL=css-code-quality.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"css-code-quality.cjs","sources":["../src/performance.js","../src/maintainability.js","../src/complexity.js","../src/index.js"],"sourcesContent":["const guards = [\n\n // Should not contain @import rules\n result => ({\n id: 'Imports',\n score: result.atrules.import.total * 10,\n value: result.atrules.import.total,\n }),\n\n // Should not contain empty rules\n result => ({\n id: 'EmptyRules',\n score: result.rules.empty.total,\n value: result.rules.empty.total,\n }),\n\n // Too many selectors appear multiple times\n result => {\n const outcome = {\n id: 'SelectorDuplications',\n score: 0,\n value: 1 - result.selectors.uniquenessRatio,\n }\n\n if (result.selectors.uniquenessRatio < 0.66) {\n outcome.score = Math.floor((1 - result.selectors.uniquenessRatio) * 10)\n }\n\n return outcome\n },\n\n // Too many declarations appear multiple times\n result => {\n const outcome = {\n id: 'DeclarationDuplications',\n score: 0,\n value: 1 - result.declarations.unique.ratio,\n }\n\n if (result.declarations.unique.ratio < 0.66) {\n outcome.score = Math.floor((1 - result.declarations.unique.ratio) * 10)\n }\n\n return outcome\n },\n\n // The total amount of CSS should not be too high\n result => ({\n id: 'CssSize',\n score: result.stylesheet.size > 200_000 ? 5 : 0,\n value: result.stylesheet.size,\n }),\n\n // Should not contain (too much) comments\n // Deduct 1 point for every 250 bytes\n result => {\n const { comments } = result.stylesheet\n return {\n id: 'TooMuchComments',\n score: Math.min(10, Math.floor(comments.size / 250)),\n value: comments.size,\n }\n },\n\n // Should not contain too much embedded content\n // Deduct 1 point for every 250 bytes\n result => {\n const { size } = result.stylesheet.embeddedContent\n return {\n id: 'TooMuchEmbeddedContent',\n score: Math.min(20, Math.floor(size.total / 250)),\n value: size.total,\n }\n },\n]\n\nexport { guards }","const guards = [\n\n // Source Lines of Code should be low'\n result => {\n const outcome = {\n id: 'SourceLinesOfCode',\n score: 0,\n value: result.stylesheet.sourceLinesOfCode,\n }\n\n if (result.stylesheet.sourceLinesOfCode > 10000) {\n // deduct 1 point per 1000 lines of code over 10,000\n const score = Math.floor((result.stylesheet.sourceLinesOfCode - 10000) / 1000)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Selectors per RuleSet should be low\n result => {\n const ALLOWED_SELECTORS_PER_RULESET = 2\n const actual = result.rules.selectors.mean\n\n const outcome = {\n id: 'AverageSelectorsPerRule',\n score: 0,\n value: actual,\n }\n\n // Deduct 5 points per selector over 2\n if (actual > ALLOWED_SELECTORS_PER_RULESET) {\n const score = Math.floor((actual - ALLOWED_SELECTORS_PER_RULESET) * 5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Declarations per RuleSet should be low\n result => {\n const ALLOWED_DECLARATIONS_PER_RULESET = 5\n\n const outcome = {\n id: 'AverageDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mean,\n }\n\n // Deduct 5 points per declaration over 5\n if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {\n const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)\n outcome.score = Math.min(15, 0)\n }\n\n return outcome\n },\n\n // Max number of Selectors per Rule should be low\n result => {\n const MAX_SELECTORS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.max,\n }\n\n // Deduct 0.5 points per selectors over 10\n if (result.rules.selectors.max > MAX_SELECTORS_PER_RULESET) {\n const score = Math.ceil((result.rules.selectors.max - MAX_SELECTORS_PER_RULESET) * 0.5)\n outcome.score = Math.min(score, 15)\n }\n\n return outcome\n },\n\n // Max number of Declarations per Rule should be low\n result => {\n const MAX_DECLARATIONS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.max,\n }\n\n // Deduct 0.5 points per declarations over 10\n if (result.rules.declarations.max > MAX_DECLARATIONS_PER_RULESET) {\n const score = Math.ceil((result.rules.declarations.max - MAX_DECLARATIONS_PER_RULESET) * 0.5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Selectors per RuleSet should not differ too much from the most common amount of\n // Selectors per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.selectors.items\n .filter(item => item > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.mode,\n }\n\n // if more than 10% of RuleSets has more Selectors than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Declarations per RuleSet should not differ too much from the most common amount of\n // Declarations per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length\n\n const outcome = {\n id: 'MoreThanMostCommonDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mode,\n }\n\n // if more than 10% of RuleSets has more Declarations than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n]\n\nexport { guards }","import { compareSpecificity } from '@projectwallace/css-analyzer'\n\nconst guards = [\n\n // Complexity per Selector should not differ too much from the most common Complexity\n result => {\n const mode = result.selectors.complexity.mode\n const selectorsAboveMode = result.selectors.complexity.items\n .filter(c => c > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorComplexity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Specificity per Selector should not differ too much from the most common Specificity\n result => {\n const mode = result.selectors.specificity.mode\n const selectorsAboveMode = result.selectors.specificity.items\n .filter(c => compareSpecificity(c, mode) < 0)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorSpecificity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Maximum Selector Complexity should be low\n result => {\n const MAX_SELECTOR_COMPLEXITY = 5\n const actual = result.selectors.complexity.max\n\n const outcome = {\n id: 'MaxSelectorComplexity',\n score: 0,\n value: result.selectors.complexity.max,\n }\n\n // Deduct 0.5 points per complexity over 5, up to 5 points\n if (actual > MAX_SELECTOR_COMPLEXITY) {\n const score = Math.ceil((actual - MAX_SELECTOR_COMPLEXITY) * 0.5)\n outcome.score = Math.min(5, score)\n }\n\n return outcome\n },\n\n // Average Selector Complexity should be low\n result => {\n const ALLOWED_COMPLEXITY = 2\n const actual = result.selectors.complexity.mean\n\n const outcome = {\n id: 'AverageSelectorComplexity',\n score: 0,\n value: actual,\n }\n\n // Deduct 2 points per selector over 2\n if (actual > ALLOWED_COMPLEXITY) {\n const score = Math.ceil((actual - ALLOWED_COMPLEXITY) * 2)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.selectors.id.ratio\n const outcome = {\n id: 'IdSelectorRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.declarations.importants.ratio\n const outcome = {\n id: 'ImportantRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n]\n\nexport {\n guards\n}","import { analyze } from '@projectwallace/css-analyzer'\nimport { guards as performanceGuards } from './performance.js'\nimport { guards as maintainabilityGuards } from './maintainability.js'\nimport { guards as complexityGuards } from './complexity.js'\n\nfunction calculateScore({ result, guards }) {\n let score = 100\n let violations = []\n let passes = []\n\n for (const guard of guards) {\n /** @type {{score: number, actual: number, id: string}} */\n const outcome = guard(result)\n\n if (outcome.score > 0) {\n score -= outcome.score\n violations.push(outcome)\n } else {\n passes.push(outcome)\n }\n }\n\n return {\n score: Math.max(score, 0),\n violations,\n passes,\n }\n}\n\nfunction calculate(css) {\n const result = analyze(css)\n\n const performance = calculateScore({ result, guards: performanceGuards })\n const maintainability = calculateScore({ result, guards: maintainabilityGuards })\n const complexity = calculateScore({ result, guards: complexityGuards })\n\n return {\n score: 0,\n violations: performance.violations\n .concat(maintainability.violations)\n .concat(complexity.violations),\n passes: performance.passes\n .concat(maintainability.passes)\n .concat(complexity.passes),\n performance,\n maintainability,\n complexity,\n }\n}\n\nexport {\n calculate\n}"],"names":["guards","result","id","score","atrules","total","value","rules","empty","outcome","selectors","uniquenessRatio","Math","floor","declarations","unique","ratio","stylesheet","size","comments","min","embeddedContent","sourceLinesOfCode","actual","mean","max","ceil","mode","rulesHavingMoreThanMode","items","filter","item","length","complexity","selectorsAboveMode","c","specificity","compareSpecificity","importants","calculateScore","violations","passes","guard","push","css","analyze","performance","performanceGuards","maintainability","maintainabilityGuards","complexityGuards","concat"],"mappings":"wJAAA,IAAMA,EAAS,CAGb,SAAAC,SAAW,CACTC,GAAI,UACJC,MAAqC,GAA9BF,EAAOG,eAAeC,MAC7BC,MAAOL,EAAOG,eAAeC,QAI/B,SAAAJ,SAAW,CACTC,GAAI,aACJC,MAAOF,EAAOM,MAAMC,MAAMH,MAC1BC,MAAOL,EAAOM,MAAMC,MAAMH,QAI5B,SAAAJ,GACE,IAAMQ,EAAU,CACdP,GAAI,uBACJC,MAAO,EACPG,MAAO,EAAIL,EAAOS,UAAUC,iBAO9B,OAJIV,EAAOS,UAAUC,gBAAkB,MACrCF,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOS,UAAUC,mBAG5CF,GAIT,SAAAR,GACE,IAAMQ,EAAU,CACdP,GAAI,0BACJC,MAAO,EACPG,MAAO,EAAIL,EAAOa,aAAaC,OAAOC,OAOxC,OAJIf,EAAOa,aAAaC,OAAOC,MAAQ,MACrCP,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOa,aAAaC,OAAOC,SAGtDP,GAIT,SAAAR,SAAW,CACTC,GAAI,UACJC,MAAOF,EAAOgB,WAAWC,KAAO,IAAU,EAAI,EAC9CZ,MAAOL,EAAOgB,WAAWC,OAK3B,SAAAjB,GACE,IAAQkB,EAAalB,EAAOgB,WAApBE,SACR,MAAO,C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c,MAC7BP,EAAU,CACdP,GAAI,kBACJC,MAAO,EACPG,MAAOiB,GAGT,GAAIA,EARY,IAQM,CACpB,IAAMpB,EAAQS,KAAKC,MAA2B,IAApBU,EATZ,MAUdd,EAAQN,MAAQS,KAAKQ,IAAIjB,EAAO,GAGlC,OAAOM,GAGT,SAAAR,GACE,IACMsB,EAAStB,EAAOa,aAAawB,WAAWtB,MACxCP,EAAU,CACdP,GAAI,iBACJC,MAAO,EACPG,MAAOiB,GAGT,GAAIA,EARY,IAQM,CACpB,IAAMpB,EAAQS,KAAKC,MAA2B,IAApBU,EATZ,MAUdd,EAAQN,MAAQS,KAAKQ,IAAIjB,EAAO,GAGlC,OAAOM,IChHX,SAAS8B,KAKP,UALwBtC,IAAAA,OACpBE,EAAQ,IACRqC,EAAa,GACbC,EAAS,irBAHmBzC,uBAKJ,KAEpBS,GAAUiC,WAAMzC,GAElBQ,EAAQN,MAAQ,GAClBA,GAASM,EAAQN,MACjBqC,EAAWG,KAAKlC,IAEhBgC,EAAOE,KAAKlC,GAIhB,MAAO,CACLN,MAAOS,KAAKa,IAAItB,EAAO,GACvBqC,WAAAA,EACAC,OAAAA,qBAIJ,SAAmBG,GACjB,IAAM3C,EAAS4C,UAAQD,GAEjBE,EAAcP,EAAe,CAAEtC,OAAAA,EAAQD,OAAQ+C,IAC/CC,EAAkBT,EAAe,CAAEtC,OAAAA,EAAQD,OAAQiD,IACnDhB,EAAaM,EAAe,CAAEtC,OAAAA,EAAQD,OAAQkD,IAEpD,MAAO,CACL/C,MAAO,EACPqC,WAAYM,EAAYN,WACrBW,OAAOH,EAAgBR,YACvBW,OAAOlB,EAAWO,YACrBC,OAAQK,EAAYL,OACjBU,OAAOH,EAAgBP,QACvBU,OAAOlB,EAAWQ,QACrBK,YAAAA,EACAE,gBAAAA,EACAf,WAAAA"}
@@ -0,0 +1,2 @@
1
+ import{compareSpecificity as e,analyze as t}from"@projectwallace/css-analyzer";const o=[e=>({id:"Imports",score:10*e.atrules.import.total,value:e.atrules.import.total}),e=>({id:"EmptyRules",score:e.rules.empty.total,value:e.rules.empty.total}),e=>{const t={id:"SelectorDuplications",score:0,value:1-e.selectors.uniquenessRatio};return e.selectors.uniquenessRatio<.66&&(t.score=Math.floor(10*(1-e.selectors.uniquenessRatio))),t},e=>{const t={id:"DeclarationDuplications",score:0,value:1-e.declarations.unique.ratio};return e.declarations.unique.ratio<.66&&(t.score=Math.floor(10*(1-e.declarations.unique.ratio))),t},e=>({id:"CssSize",score:e.stylesheet.size>2e5?5:0,value:e.stylesheet.size}),e=>{const{comments:t}=e.stylesheet;return{id:"TooMuchComments",score:Math.min(10,Math.floor(t.size/250)),value:t.size}},e=>{const{size:t}=e.stylesheet.embeddedContent;return{id:"TooMuchEmbeddedContent",score:Math.min(20,Math.floor(t.total/250)),value:t.total}}],s=[e=>{const t={id:"SourceLinesOfCode",score:0,value:e.stylesheet.sourceLinesOfCode};if(e.stylesheet.sourceLinesOfCode>1e4){const o=Math.floor((e.stylesheet.sourceLinesOfCode-1e4)/1e3);t.score=Math.min(15,o)}return t},e=>{const t=e.rules.selectors.mean,o={id:"AverageSelectorsPerRule",score:0,value:t};if(t>2){const e=Math.floor(5*(t-2));o.score=Math.min(15,e)}return o},e=>{const t={id:"AverageDeclarationsPerRule",score:0,value:e.rules.declarations.mean};return e.rules.declarations.mean>5&&(t.score=Math.min(15,0)),t},e=>{const t={id:"MaxSelectorsPerRule",score:0,value:e.rules.selectors.max};if(e.rules.selectors.max>10){const o=Math.ceil(.5*(e.rules.selectors.max-10));t.score=Math.min(o,15)}return t},e=>{const t={id:"MaxDeclarationsPerRule",score:0,value:e.rules.declarations.max};if(e.rules.declarations.max>10){const o=Math.ceil(.5*(e.rules.declarations.max-10));t.score=Math.min(15,o)}return t},e=>{const t=e.rules.selectors.mode,o=e.rules.selectors.items.filter(e=>e>t).length,s={id:"MoreThanMostCommonSelectorsPerRule",score:0,value:e.rules.selectors.mode};if(o>.1*e.rules.total){const e=Math.floor(.01*o);s.score=Math.min(15,e)}return s},e=>{const t=e.rules.selectors.mode,o=e.rules.declarations.items.filter(e=>e>t).length,s={id:"MoreThanMostCommonDeclarationsPerRule",score:0,value:e.rules.declarations.mode};if(o>.1*e.rules.total){const e=Math.floor(.01*o);s.score=Math.min(15,e)}return s}],r=[e=>{const t=e.selectors.complexity.mode,o=e.selectors.complexity.items.filter(e=>e>t).length,s={id:"MoreThanMostCommonSelectorComplexity",score:0,value:0===e.selectors.total?0:o/e.selectors.total};if(o>.1*e.selectors.total){const e=Math.floor(.01*o);s.score=Math.min(10,e)}return s},t=>{const o=t.selectors.specificity.mode,s=t.selectors.specificity.items.filter(t=>e(t,o)<0).length,r={id:"MoreThanMostCommonSelectorSpecificity",score:0,value:0===t.selectors.total?0:s/t.selectors.total};if(s>.1*t.selectors.total){const e=Math.floor(.01*s);r.score=Math.min(10,e)}return r},e=>{const t=e.selectors.complexity.max,o={id:"MaxSelectorComplexity",score:0,value:e.selectors.complexity.max};if(t>5){const e=Math.ceil(.5*(t-5));o.score=Math.min(5,e)}return o},e=>{const t=e.selectors.complexity.mean,o={id:"AverageSelectorComplexity",score:0,value:t};if(t>2){const e=Math.ceil(2*(t-2));o.score=Math.min(10,e)}return o},e=>{const t=e.selectors.id.ratio,o={id:"IdSelectorRatio",score:0,value:t};if(t>.01){const e=Math.floor(10*(t-.01));o.score=Math.min(e,5)}return o},e=>{const t=e.declarations.importants.ratio,o={id:"ImportantRatio",score:0,value:t};if(t>.01){const e=Math.floor(10*(t-.01));o.score=Math.min(e,5)}return o}];function l({result:e,guards:t}){let o=100,s=[],r=[];for(const l of t){const t=l(e);t.score>0?(o-=t.score,s.push(t)):r.push(t)}return{score:Math.max(o,0),violations:s,passes:r}}function a(e){const a=t(e),c=l({result:a,guards:o}),i=l({result:a,guards:s}),n=l({result:a,guards:r});return{score:0,violations:c.violations.concat(i.violations).concat(n.violations),passes:c.passes.concat(i.passes).concat(n.passes),performance:c,maintainability:i,complexity:n}}export{a as calculate};
2
+ //# sourceMappingURL=css-code-quality.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"css-code-quality.modern.js","sources":["../src/performance.js","../src/maintainability.js","../src/complexity.js","../src/index.js"],"sourcesContent":["const guards = [\n\n // Should not contain @import rules\n result => ({\n id: 'Imports',\n score: result.atrules.import.total * 10,\n value: result.atrules.import.total,\n }),\n\n // Should not contain empty rules\n result => ({\n id: 'EmptyRules',\n score: result.rules.empty.total,\n value: result.rules.empty.total,\n }),\n\n // Too many selectors appear multiple times\n result => {\n const outcome = {\n id: 'SelectorDuplications',\n score: 0,\n value: 1 - result.selectors.uniquenessRatio,\n }\n\n if (result.selectors.uniquenessRatio < 0.66) {\n outcome.score = Math.floor((1 - result.selectors.uniquenessRatio) * 10)\n }\n\n return outcome\n },\n\n // Too many declarations appear multiple times\n result => {\n const outcome = {\n id: 'DeclarationDuplications',\n score: 0,\n value: 1 - result.declarations.unique.ratio,\n }\n\n if (result.declarations.unique.ratio < 0.66) {\n outcome.score = Math.floor((1 - result.declarations.unique.ratio) * 10)\n }\n\n return outcome\n },\n\n // The total amount of CSS should not be too high\n result => ({\n id: 'CssSize',\n score: result.stylesheet.size > 200_000 ? 5 : 0,\n value: result.stylesheet.size,\n }),\n\n // Should not contain (too much) comments\n // Deduct 1 point for every 250 bytes\n result => {\n const { comments } = result.stylesheet\n return {\n id: 'TooMuchComments',\n score: Math.min(10, Math.floor(comments.size / 250)),\n value: comments.size,\n }\n },\n\n // Should not contain too much embedded content\n // Deduct 1 point for every 250 bytes\n result => {\n const { size } = result.stylesheet.embeddedContent\n return {\n id: 'TooMuchEmbeddedContent',\n score: Math.min(20, Math.floor(size.total / 250)),\n value: size.total,\n }\n },\n]\n\nexport { guards }","const guards = [\n\n // Source Lines of Code should be low'\n result => {\n const outcome = {\n id: 'SourceLinesOfCode',\n score: 0,\n value: result.stylesheet.sourceLinesOfCode,\n }\n\n if (result.stylesheet.sourceLinesOfCode > 10000) {\n // deduct 1 point per 1000 lines of code over 10,000\n const score = Math.floor((result.stylesheet.sourceLinesOfCode - 10000) / 1000)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Selectors per RuleSet should be low\n result => {\n const ALLOWED_SELECTORS_PER_RULESET = 2\n const actual = result.rules.selectors.mean\n\n const outcome = {\n id: 'AverageSelectorsPerRule',\n score: 0,\n value: actual,\n }\n\n // Deduct 5 points per selector over 2\n if (actual > ALLOWED_SELECTORS_PER_RULESET) {\n const score = Math.floor((actual - ALLOWED_SELECTORS_PER_RULESET) * 5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Declarations per RuleSet should be low\n result => {\n const ALLOWED_DECLARATIONS_PER_RULESET = 5\n\n const outcome = {\n id: 'AverageDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mean,\n }\n\n // Deduct 5 points per declaration over 5\n if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {\n const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)\n outcome.score = Math.min(15, 0)\n }\n\n return outcome\n },\n\n // Max number of Selectors per Rule should be low\n result => {\n const MAX_SELECTORS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.max,\n }\n\n // Deduct 0.5 points per selectors over 10\n if (result.rules.selectors.max > MAX_SELECTORS_PER_RULESET) {\n const score = Math.ceil((result.rules.selectors.max - MAX_SELECTORS_PER_RULESET) * 0.5)\n outcome.score = Math.min(score, 15)\n }\n\n return outcome\n },\n\n // Max number of Declarations per Rule should be low\n result => {\n const MAX_DECLARATIONS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.max,\n }\n\n // Deduct 0.5 points per declarations over 10\n if (result.rules.declarations.max > MAX_DECLARATIONS_PER_RULESET) {\n const score = Math.ceil((result.rules.declarations.max - MAX_DECLARATIONS_PER_RULESET) * 0.5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Selectors per RuleSet should not differ too much from the most common amount of\n // Selectors per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.selectors.items\n .filter(item => item > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.mode,\n }\n\n // if more than 10% of RuleSets has more Selectors than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Declarations per RuleSet should not differ too much from the most common amount of\n // Declarations per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length\n\n const outcome = {\n id: 'MoreThanMostCommonDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mode,\n }\n\n // if more than 10% of RuleSets has more Declarations than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n]\n\nexport { guards }","import { compareSpecificity } from '@projectwallace/css-analyzer'\n\nconst guards = [\n\n // Complexity per Selector should not differ too much from the most common Complexity\n result => {\n const mode = result.selectors.complexity.mode\n const selectorsAboveMode = result.selectors.complexity.items\n .filter(c => c > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorComplexity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Specificity per Selector should not differ too much from the most common Specificity\n result => {\n const mode = result.selectors.specificity.mode\n const selectorsAboveMode = result.selectors.specificity.items\n .filter(c => compareSpecificity(c, mode) < 0)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorSpecificity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Maximum Selector Complexity should be low\n result => {\n const MAX_SELECTOR_COMPLEXITY = 5\n const actual = result.selectors.complexity.max\n\n const outcome = {\n id: 'MaxSelectorComplexity',\n score: 0,\n value: result.selectors.complexity.max,\n }\n\n // Deduct 0.5 points per complexity over 5, up to 5 points\n if (actual > MAX_SELECTOR_COMPLEXITY) {\n const score = Math.ceil((actual - MAX_SELECTOR_COMPLEXITY) * 0.5)\n outcome.score = Math.min(5, score)\n }\n\n return outcome\n },\n\n // Average Selector Complexity should be low\n result => {\n const ALLOWED_COMPLEXITY = 2\n const actual = result.selectors.complexity.mean\n\n const outcome = {\n id: 'AverageSelectorComplexity',\n score: 0,\n value: actual,\n }\n\n // Deduct 2 points per selector over 2\n if (actual > ALLOWED_COMPLEXITY) {\n const score = Math.ceil((actual - ALLOWED_COMPLEXITY) * 2)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.selectors.id.ratio\n const outcome = {\n id: 'IdSelectorRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.declarations.importants.ratio\n const outcome = {\n id: 'ImportantRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n]\n\nexport {\n guards\n}","import { analyze } from '@projectwallace/css-analyzer'\nimport { guards as performanceGuards } from './performance.js'\nimport { guards as maintainabilityGuards } from './maintainability.js'\nimport { guards as complexityGuards } from './complexity.js'\n\nfunction calculateScore({ result, guards }) {\n let score = 100\n let violations = []\n let passes = []\n\n for (const guard of guards) {\n /** @type {{score: number, actual: number, id: string}} */\n const outcome = guard(result)\n\n if (outcome.score > 0) {\n score -= outcome.score\n violations.push(outcome)\n } else {\n passes.push(outcome)\n }\n }\n\n return {\n score: Math.max(score, 0),\n violations,\n passes,\n }\n}\n\nfunction calculate(css) {\n const result = analyze(css)\n\n const performance = calculateScore({ result, guards: performanceGuards })\n const maintainability = calculateScore({ result, guards: maintainabilityGuards })\n const complexity = calculateScore({ result, guards: complexityGuards })\n\n return {\n score: 0,\n violations: performance.violations\n .concat(maintainability.violations)\n .concat(complexity.violations),\n passes: performance.passes\n .concat(maintainability.passes)\n .concat(complexity.passes),\n performance,\n maintainability,\n complexity,\n }\n}\n\nexport {\n calculate\n}"],"names":["guards","result","id","score","atrules","import","total","value","rules","empty","outcome","selectors","uniquenessRatio","Math","floor","declarations","unique","ratio","stylesheet","size","comments","min","embeddedContent","sourceLinesOfCode","actual","mean","max","ceil","mode","rulesHavingMoreThanMode","items","filter","item","length","complexity","selectorsAboveMode","c","specificity","compareSpecificity","importants","calculateScore","violations","passes","guard","push","calculate","css","analyze","performance","performanceGuards","maintainability","maintainabilityGuards","complexityGuards","concat"],"mappings":"+EAAA,MAAMA,EAAS,CAGbC,KACEC,GAAI,UACJC,MAAqC,GAA9BF,EAAOG,QAAQC,OAAOC,MAC7BC,MAAON,EAAOG,QAAQC,OAAOC,QAI/BL,KACEC,GAAI,aACJC,MAAOF,EAAOO,MAAMC,MAAMH,MAC1BC,MAAON,EAAOO,MAAMC,MAAMH,QAI5BL,IACE,MAAMS,EAAU,CACdR,GAAI,uBACJC,MAAO,EACPI,MAAO,EAAIN,EAAOU,UAAUC,iBAO9B,OAJIX,EAAOU,UAAUC,gBA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DjB,EAAaM,EAAe,CAAEvC,OAAAA,EAAQD,OAAQoD,IAEpD,MAAO,CACLjD,MAAO,EACPsC,WAAYO,EAAYP,WACrBY,OAAOH,EAAgBT,YACvBY,OAAOnB,EAAWO,YACrBC,OAAQM,EAAYN,OACjBW,OAAOH,EAAgBR,QACvBW,OAAOnB,EAAWQ,QACrBM,YAAAA,EACAE,gBAAAA,EACAhB,WAAAA"}
@@ -0,0 +1,2 @@
1
+ import{compareSpecificity as e,analyze as r}from"@projectwallace/css-analyzer";function t(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,o=new Array(r);t<r;t++)o[t]=e[t];return o}var o=[function(e){return{id:"Imports",score:10*e.atrules.import.total,value:e.atrules.import.total}},function(e){return{id:"EmptyRules",score:e.rules.empty.total,value:e.rules.empty.total}},function(e){var r={id:"SelectorDuplications",score:0,value:1-e.selectors.uniquenessRatio};return e.selectors.uniquenessRatio<.66&&(r.score=Math.floor(10*(1-e.selectors.uniquenessRatio))),r},function(e){var r={id:"DeclarationDuplications",score:0,value:1-e.declarations.unique.ratio};return e.declarations.unique.ratio<.66&&(r.score=Math.floor(10*(1-e.declarations.unique.ratio))),r},function(e){return{id:"CssSize",score:e.stylesheet.size>2e5?5:0,value:e.stylesheet.size}},function(e){var r=e.stylesheet.comments;return{id:"TooMuchComments",score:Math.min(10,Math.floor(r.size/250)),value:r.size}},function(e){var r=e.stylesheet.embeddedContent.size;return{id:"TooMuchEmbeddedContent",score:Math.min(20,Math.floor(r.total/250)),value:r.total}}],a=[function(e){var r={id:"SourceLinesOfCode",score:0,value:e.stylesheet.sourceLinesOfCode};if(e.stylesheet.sourceLinesOfCode>1e4){var t=Math.floor((e.stylesheet.sourceLinesOfCode-1e4)/1e3);r.score=Math.min(15,t)}return r},function(e){var r=e.rules.selectors.mean,t={id:"AverageSelectorsPerRule",score:0,value:r};if(r>2){var o=Math.floor(5*(r-2));t.score=Math.min(15,o)}return t},function(e){var r={id:"AverageDeclarationsPerRule",score:0,value:e.rules.declarations.mean};return e.rules.declarations.mean>5&&(r.score=Math.min(15,0)),r},function(e){var r={id:"MaxSelectorsPerRule",score:0,value:e.rules.selectors.max};if(e.rules.selectors.max>10){var t=Math.ceil(.5*(e.rules.selectors.max-10));r.score=Math.min(t,15)}return r},function(e){var r={id:"MaxDeclarationsPerRule",score:0,value:e.rules.declarations.max};if(e.rules.declarations.max>10){var t=Math.ceil(.5*(e.rules.declarations.max-10));r.score=Math.min(15,t)}return r},function(e){var r=e.rules.selectors.mode,t=e.rules.selectors.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonSelectorsPerRule",score:0,value:e.rules.selectors.mode};if(t>.1*e.rules.total){var a=Math.floor(.01*t);o.score=Math.min(15,a)}return o},function(e){var r=e.rules.selectors.mode,t=e.rules.declarations.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonDeclarationsPerRule",score:0,value:e.rules.declarations.mode};if(t>.1*e.rules.total){var a=Math.floor(.01*t);o.score=Math.min(15,a)}return o}],s=[function(e){var r=e.selectors.complexity.mode,t=e.selectors.complexity.items.filter(function(e){return e>r}).length,o={id:"MoreThanMostCommonSelectorComplexity",score:0,value:0===e.selectors.total?0:t/e.selectors.total};if(t>.1*e.selectors.total){var a=Math.floor(.01*t);o.score=Math.min(10,a)}return o},function(r){var t=r.selectors.specificity.mode,o=r.selectors.specificity.items.filter(function(r){return e(r,t)<0}).length,a={id:"MoreThanMostCommonSelectorSpecificity",score:0,value:0===r.selectors.total?0:o/r.selectors.total};if(o>.1*r.selectors.total){var s=Math.floor(.01*o);a.score=Math.min(10,s)}return a},function(e){var r=e.selectors.complexity.max,t={id:"MaxSelectorComplexity",score:0,value:e.selectors.complexity.max};if(r>5){var o=Math.ceil(.5*(r-5));t.score=Math.min(5,o)}return t},function(e){var r=e.selectors.complexity.mean,t={id:"AverageSelectorComplexity",score:0,value:r};if(r>2){var o=Math.ceil(2*(r-2));t.score=Math.min(10,o)}return t},function(e){var r=e.selectors.id.ratio,t={id:"IdSelectorRatio",score:0,value:r};if(r>.01){var o=Math.floor(10*(r-.01));t.score=Math.min(o,5)}return t},function(e){var r=e.declarations.importants.ratio,t={id:"ImportantRatio",score:0,value:r};if(r>.01){var o=Math.floor(10*(r-.01));t.score=Math.min(o,5)}return t}];function n(e){for(var r,o=e.result,a=100,s=[],n=[],i=function(e,r){var o="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(o)return(o=o.call(e)).next.bind(o);if(Array.isArray(e)||(o=function(e,r){if(e){if("string"==typeof e)return t(e,r);var o=Object.prototype.toString.call(e).slice(8,-1);return"Object"===o&&e.constructor&&(o=e.constructor.name),"Map"===o||"Set"===o?Array.from(e):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?t(e,r):void 0}}(e))){o&&(e=o);var a=0;return function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e.guards);!(r=i()).done;){var l=(0,r.value)(o);l.score>0?(a-=l.score,s.push(l)):n.push(l)}return{score:Math.max(a,0),violations:s,passes:n}}function i(e){var t=r(e),i=n({result:t,guards:o}),l=n({result:t,guards:a}),c=n({result:t,guards:s});return{score:0,violations:i.violations.concat(l.violations).concat(c.violations),passes:i.passes.concat(l.passes).concat(c.passes),performance:i,maintainability:l,complexity:c}}export{i as calculate};
2
+ //# sourceMappingURL=css-code-quality.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"css-code-quality.module.js","sources":["../src/performance.js","../src/maintainability.js","../src/complexity.js","../src/index.js"],"sourcesContent":["const guards = [\n\n // Should not contain @import rules\n result => ({\n id: 'Imports',\n score: result.atrules.import.total * 10,\n value: result.atrules.import.total,\n }),\n\n // Should not contain empty rules\n result => ({\n id: 'EmptyRules',\n score: result.rules.empty.total,\n value: result.rules.empty.total,\n }),\n\n // Too many selectors appear multiple times\n result => {\n const outcome = {\n id: 'SelectorDuplications',\n score: 0,\n value: 1 - result.selectors.uniquenessRatio,\n }\n\n if (result.selectors.uniquenessRatio < 0.66) {\n outcome.score = Math.floor((1 - result.selectors.uniquenessRatio) * 10)\n }\n\n return outcome\n },\n\n // Too many declarations appear multiple times\n result => {\n const outcome = {\n id: 'DeclarationDuplications',\n score: 0,\n value: 1 - result.declarations.unique.ratio,\n }\n\n if (result.declarations.unique.ratio < 0.66) {\n outcome.score = Math.floor((1 - result.declarations.unique.ratio) * 10)\n }\n\n return outcome\n },\n\n // The total amount of CSS should not be too high\n result => ({\n id: 'CssSize',\n score: result.stylesheet.size > 200_000 ? 5 : 0,\n value: result.stylesheet.size,\n }),\n\n // Should not contain (too much) comments\n // Deduct 1 point for every 250 bytes\n result => {\n const { comments } = result.stylesheet\n return {\n id: 'TooMuchComments',\n score: Math.min(10, Math.floor(comments.size / 250)),\n value: comments.size,\n }\n },\n\n // Should not contain too much embedded content\n // Deduct 1 point for every 250 bytes\n result => {\n const { size } = result.stylesheet.embeddedContent\n return {\n id: 'TooMuchEmbeddedContent',\n score: Math.min(20, Math.floor(size.total / 250)),\n value: size.total,\n }\n },\n]\n\nexport { guards }","const guards = [\n\n // Source Lines of Code should be low'\n result => {\n const outcome = {\n id: 'SourceLinesOfCode',\n score: 0,\n value: result.stylesheet.sourceLinesOfCode,\n }\n\n if (result.stylesheet.sourceLinesOfCode > 10000) {\n // deduct 1 point per 1000 lines of code over 10,000\n const score = Math.floor((result.stylesheet.sourceLinesOfCode - 10000) / 1000)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Selectors per RuleSet should be low\n result => {\n const ALLOWED_SELECTORS_PER_RULESET = 2\n const actual = result.rules.selectors.mean\n\n const outcome = {\n id: 'AverageSelectorsPerRule',\n score: 0,\n value: actual,\n }\n\n // Deduct 5 points per selector over 2\n if (actual > ALLOWED_SELECTORS_PER_RULESET) {\n const score = Math.floor((actual - ALLOWED_SELECTORS_PER_RULESET) * 5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Declarations per RuleSet should be low\n result => {\n const ALLOWED_DECLARATIONS_PER_RULESET = 5\n\n const outcome = {\n id: 'AverageDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mean,\n }\n\n // Deduct 5 points per declaration over 5\n if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {\n const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)\n outcome.score = Math.min(15, 0)\n }\n\n return outcome\n },\n\n // Max number of Selectors per Rule should be low\n result => {\n const MAX_SELECTORS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.max,\n }\n\n // Deduct 0.5 points per selectors over 10\n if (result.rules.selectors.max > MAX_SELECTORS_PER_RULESET) {\n const score = Math.ceil((result.rules.selectors.max - MAX_SELECTORS_PER_RULESET) * 0.5)\n outcome.score = Math.min(score, 15)\n }\n\n return outcome\n },\n\n // Max number of Declarations per Rule should be low\n result => {\n const MAX_DECLARATIONS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.max,\n }\n\n // Deduct 0.5 points per declarations over 10\n if (result.rules.declarations.max > MAX_DECLARATIONS_PER_RULESET) {\n const score = Math.ceil((result.rules.declarations.max - MAX_DECLARATIONS_PER_RULESET) * 0.5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Selectors per RuleSet should not differ too much from the most common amount of\n // Selectors per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.selectors.items\n .filter(item => item > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.mode,\n }\n\n // if more than 10% of RuleSets has more Selectors than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Declarations per RuleSet should not differ too much from the most common amount of\n // Declarations per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length\n\n const outcome = {\n id: 'MoreThanMostCommonDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mode,\n }\n\n // if more than 10% of RuleSets has more Declarations than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n]\n\nexport { guards }","import { compareSpecificity } from '@projectwallace/css-analyzer'\n\nconst guards = [\n\n // Complexity per Selector should not differ too much from the most common Complexity\n result => {\n const mode = result.selectors.complexity.mode\n const selectorsAboveMode = result.selectors.complexity.items\n .filter(c => c > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorComplexity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Specificity per Selector should not differ too much from the most common Specificity\n result => {\n const mode = result.selectors.specificity.mode\n const selectorsAboveMode = result.selectors.specificity.items\n .filter(c => compareSpecificity(c, mode) < 0)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorSpecificity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Maximum Selector Complexity should be low\n result => {\n const MAX_SELECTOR_COMPLEXITY = 5\n const actual = result.selectors.complexity.max\n\n const outcome = {\n id: 'MaxSelectorComplexity',\n score: 0,\n value: result.selectors.complexity.max,\n }\n\n // Deduct 0.5 points per complexity over 5, up to 5 points\n if (actual > MAX_SELECTOR_COMPLEXITY) {\n const score = Math.ceil((actual - MAX_SELECTOR_COMPLEXITY) * 0.5)\n outcome.score = Math.min(5, score)\n }\n\n return outcome\n },\n\n // Average Selector Complexity should be low\n result => {\n const ALLOWED_COMPLEXITY = 2\n const actual = result.selectors.complexity.mean\n\n const outcome = {\n id: 'AverageSelectorComplexity',\n score: 0,\n value: actual,\n }\n\n // Deduct 2 points per selector over 2\n if (actual > ALLOWED_COMPLEXITY) {\n const score = Math.ceil((actual - ALLOWED_COMPLEXITY) * 2)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.selectors.id.ratio\n const outcome = {\n id: 'IdSelectorRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.declarations.importants.ratio\n const outcome = {\n id: 'ImportantRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n]\n\nexport {\n guards\n}","import { analyze } from '@projectwallace/css-analyzer'\nimport { guards as performanceGuards } from './performance.js'\nimport { guards as maintainabilityGuards } from './maintainability.js'\nimport { guards as complexityGuards } from './complexity.js'\n\nfunction calculateScore({ result, guards }) {\n let score = 100\n let violations = []\n let passes = []\n\n for (const guard of guards) {\n /** @type {{score: number, actual: number, id: string}} */\n const outcome = guard(result)\n\n if (outcome.score > 0) {\n score -= outcome.score\n violations.push(outcome)\n } else {\n passes.push(outcome)\n }\n }\n\n return {\n score: Math.max(score, 0),\n violations,\n passes,\n }\n}\n\nfunction calculate(css) {\n const result = analyze(css)\n\n const performance = calculateScore({ result, guards: performanceGuards })\n const maintainability = calculateScore({ result, guards: maintainabilityGuards })\n const complexity = calculateScore({ result, guards: complexityGuards })\n\n return {\n score: 0,\n violations: performance.violations\n .concat(maintainability.violations)\n .concat(complexity.violations),\n passes: performance.passes\n .concat(maintainability.passes)\n .concat(complexity.passes),\n performance,\n maintainability,\n complexity,\n }\n}\n\nexport {\n calculate\n}"],"names":["guards","result","id","score","atrules","total","value","rules","empty","outcome","selectors","uniquenessRatio","Math","floor","declarations","unique","ratio","stylesheet","size","comments","min","embeddedContent","sourceLinesOfCode","actual","mean","max","ceil","mode","rulesHavingMoreThanMode","items","filter","item","length","complexity","selectorsAboveMode","c","specificity","compareSpecificity","importants","calculateScore","violations","passes","guard","push","calculate","css","analyze","performance","performanceGuards","maintainability","maintainabilityGuards","complexityGuards","concat"],"mappings":"yLAAA,IAAMA,EAAS,CAGb,SAAAC,SAAW,CACTC,GAAI,UACJC,MAAqC,GAA9BF,EAAOG,eAAeC,MAC7BC,MAAOL,EAAOG,eAAeC,QAI/B,SAAAJ,SAAW,CACTC,GAAI,aACJC,MAAOF,EAAOM,MAAMC,MAAMH,MAC1BC,MAAOL,EAAOM,MAAMC,MAAMH,QAI5B,SAAAJ,GACE,IAAMQ,EAAU,CACdP,GAAI,uBACJC,MAAO,EACPG,MAAO,EAAIL,EAAOS,UAAUC,iBAO9B,OAJIV,EAAOS,UAAUC,gBAAkB,MACrCF,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOS,UAAUC,mBAG5CF,GAIT,SAAAR,GACE,IAAMQ,EAAU,CACdP,GAAI,0BACJC,MAAO,EACPG,MAAO,EAAIL,EAAOa,aAAaC,OAAOC,OAOxC,OAJIf,EAAOa,aAAaC,OAAOC,MAAQ,MACrCP,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOa,aAAaC,OAAOC,SAGtDP,GAIT,SAAAR,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W,OAAOnB,EAAWQ,QACrBM,YAAAA,EACAE,gBAAAA,EACAhB,WAAAA"}
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@projectwallace/css-analyzer")):"function"==typeof define&&define.amd?define(["exports","@projectwallace/css-analyzer"],t):t((e||self).cssCodeQuality={},e.cssAnalyzer)}(this,function(e,t){function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,o=new Array(t);r<t;r++)o[r]=e[r];return o}var o=[function(e){return{id:"Imports",score:10*e.atrules.import.total,value:e.atrules.import.total}},function(e){return{id:"EmptyRules",score:e.rules.empty.total,value:e.rules.empty.total}},function(e){var t={id:"SelectorDuplications",score:0,value:1-e.selectors.uniquenessRatio};return e.selectors.uniquenessRatio<.66&&(t.score=Math.floor(10*(1-e.selectors.uniquenessRatio))),t},function(e){var t={id:"DeclarationDuplications",score:0,value:1-e.declarations.unique.ratio};return e.declarations.unique.ratio<.66&&(t.score=Math.floor(10*(1-e.declarations.unique.ratio))),t},function(e){return{id:"CssSize",score:e.stylesheet.size>2e5?5:0,value:e.stylesheet.size}},function(e){var t=e.stylesheet.comments;return{id:"TooMuchComments",score:Math.min(10,Math.floor(t.size/250)),value:t.size}},function(e){var t=e.stylesheet.embeddedContent.size;return{id:"TooMuchEmbeddedContent",score:Math.min(20,Math.floor(t.total/250)),value:t.total}}],a=[function(e){var t={id:"SourceLinesOfCode",score:0,value:e.stylesheet.sourceLinesOfCode};if(e.stylesheet.sourceLinesOfCode>1e4){var r=Math.floor((e.stylesheet.sourceLinesOfCode-1e4)/1e3);t.score=Math.min(15,r)}return t},function(e){var t=e.rules.selectors.mean,r={id:"AverageSelectorsPerRule",score:0,value:t};if(t>2){var o=Math.floor(5*(t-2));r.score=Math.min(15,o)}return r},function(e){var t={id:"AverageDeclarationsPerRule",score:0,value:e.rules.declarations.mean};return e.rules.declarations.mean>5&&(t.score=Math.min(15,0)),t},function(e){var t={id:"MaxSelectorsPerRule",score:0,value:e.rules.selectors.max};if(e.rules.selectors.max>10){var r=Math.ceil(.5*(e.rules.selectors.max-10));t.score=Math.min(r,15)}return t},function(e){var t={id:"MaxDeclarationsPerRule",score:0,value:e.rules.declarations.max};if(e.rules.declarations.max>10){var r=Math.ceil(.5*(e.rules.declarations.max-10));t.score=Math.min(15,r)}return t},function(e){var t=e.rules.selectors.mode,r=e.rules.selectors.items.filter(function(e){return e>t}).length,o={id:"MoreThanMostCommonSelectorsPerRule",score:0,value:e.rules.selectors.mode};if(r>.1*e.rules.total){var a=Math.floor(.01*r);o.score=Math.min(15,a)}return o},function(e){var t=e.rules.selectors.mode,r=e.rules.declarations.items.filter(function(e){return e>t}).length,o={id:"MoreThanMostCommonDeclarationsPerRule",score:0,value:e.rules.declarations.mode};if(r>.1*e.rules.total){var a=Math.floor(.01*r);o.score=Math.min(15,a)}return o}],s=[function(e){var t=e.selectors.complexity.mode,r=e.selectors.complexity.items.filter(function(e){return e>t}).length,o={id:"MoreThanMostCommonSelectorComplexity",score:0,value:0===e.selectors.total?0:r/e.selectors.total};if(r>.1*e.selectors.total){var a=Math.floor(.01*r);o.score=Math.min(10,a)}return o},function(e){var r=e.selectors.specificity.mode,o=e.selectors.specificity.items.filter(function(e){return t.compareSpecificity(e,r)<0}).length,a={id:"MoreThanMostCommonSelectorSpecificity",score:0,value:0===e.selectors.total?0:o/e.selectors.total};if(o>.1*e.selectors.total){var s=Math.floor(.01*o);a.score=Math.min(10,s)}return a},function(e){var t=e.selectors.complexity.max,r={id:"MaxSelectorComplexity",score:0,value:e.selectors.complexity.max};if(t>5){var o=Math.ceil(.5*(t-5));r.score=Math.min(5,o)}return r},function(e){var t=e.selectors.complexity.mean,r={id:"AverageSelectorComplexity",score:0,value:t};if(t>2){var o=Math.ceil(2*(t-2));r.score=Math.min(10,o)}return r},function(e){var t=e.selectors.id.ratio,r={id:"IdSelectorRatio",score:0,value:t};if(t>.01){var o=Math.floor(10*(t-.01));r.score=Math.min(o,5)}return r},function(e){var t=e.declarations.importants.ratio,r={id:"ImportantRatio",score:0,value:t};if(t>.01){var o=Math.floor(10*(t-.01));r.score=Math.min(o,5)}return r}];function n(e){for(var t,o=e.result,a=100,s=[],n=[],i=function(e,t){var o="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(o)return(o=o.call(e)).next.bind(o);if(Array.isArray(e)||(o=function(e,t){if(e){if("string"==typeof e)return r(e,t);var o=Object.prototype.toString.call(e).slice(8,-1);return"Object"===o&&e.constructor&&(o=e.constructor.name),"Map"===o||"Set"===o?Array.from(e):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?r(e,t):void 0}}(e))){o&&(e=o);var a=0;return function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e.guards);!(t=i()).done;){var l=(0,t.value)(o);l.score>0?(a-=l.score,s.push(l)):n.push(l)}return{score:Math.max(a,0),violations:s,passes:n}}e.calculate=function(e){var r=t.analyze(e),i=n({result:r,guards:o}),l=n({result:r,guards:a}),c=n({result:r,guards:s});return{score:0,violations:i.violations.concat(l.violations).concat(c.violations),passes:i.passes.concat(l.passes).concat(c.passes),performance:i,maintainability:l,complexity:c}}});
2
+ //# sourceMappingURL=css-code-quality.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"css-code-quality.umd.js","sources":["../src/performance.js","../src/maintainability.js","../src/complexity.js","../src/index.js"],"sourcesContent":["const guards = [\n\n // Should not contain @import rules\n result => ({\n id: 'Imports',\n score: result.atrules.import.total * 10,\n value: result.atrules.import.total,\n }),\n\n // Should not contain empty rules\n result => ({\n id: 'EmptyRules',\n score: result.rules.empty.total,\n value: result.rules.empty.total,\n }),\n\n // Too many selectors appear multiple times\n result => {\n const outcome = {\n id: 'SelectorDuplications',\n score: 0,\n value: 1 - result.selectors.uniquenessRatio,\n }\n\n if (result.selectors.uniquenessRatio < 0.66) {\n outcome.score = Math.floor((1 - result.selectors.uniquenessRatio) * 10)\n }\n\n return outcome\n },\n\n // Too many declarations appear multiple times\n result => {\n const outcome = {\n id: 'DeclarationDuplications',\n score: 0,\n value: 1 - result.declarations.unique.ratio,\n }\n\n if (result.declarations.unique.ratio < 0.66) {\n outcome.score = Math.floor((1 - result.declarations.unique.ratio) * 10)\n }\n\n return outcome\n },\n\n // The total amount of CSS should not be too high\n result => ({\n id: 'CssSize',\n score: result.stylesheet.size > 200_000 ? 5 : 0,\n value: result.stylesheet.size,\n }),\n\n // Should not contain (too much) comments\n // Deduct 1 point for every 250 bytes\n result => {\n const { comments } = result.stylesheet\n return {\n id: 'TooMuchComments',\n score: Math.min(10, Math.floor(comments.size / 250)),\n value: comments.size,\n }\n },\n\n // Should not contain too much embedded content\n // Deduct 1 point for every 250 bytes\n result => {\n const { size } = result.stylesheet.embeddedContent\n return {\n id: 'TooMuchEmbeddedContent',\n score: Math.min(20, Math.floor(size.total / 250)),\n value: size.total,\n }\n },\n]\n\nexport { guards }","const guards = [\n\n // Source Lines of Code should be low'\n result => {\n const outcome = {\n id: 'SourceLinesOfCode',\n score: 0,\n value: result.stylesheet.sourceLinesOfCode,\n }\n\n if (result.stylesheet.sourceLinesOfCode > 10000) {\n // deduct 1 point per 1000 lines of code over 10,000\n const score = Math.floor((result.stylesheet.sourceLinesOfCode - 10000) / 1000)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Selectors per RuleSet should be low\n result => {\n const ALLOWED_SELECTORS_PER_RULESET = 2\n const actual = result.rules.selectors.mean\n\n const outcome = {\n id: 'AverageSelectorsPerRule',\n score: 0,\n value: actual,\n }\n\n // Deduct 5 points per selector over 2\n if (actual > ALLOWED_SELECTORS_PER_RULESET) {\n const score = Math.floor((actual - ALLOWED_SELECTORS_PER_RULESET) * 5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Average count of Declarations per RuleSet should be low\n result => {\n const ALLOWED_DECLARATIONS_PER_RULESET = 5\n\n const outcome = {\n id: 'AverageDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mean,\n }\n\n // Deduct 5 points per declaration over 5\n if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {\n const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)\n outcome.score = Math.min(15, 0)\n }\n\n return outcome\n },\n\n // Max number of Selectors per Rule should be low\n result => {\n const MAX_SELECTORS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.max,\n }\n\n // Deduct 0.5 points per selectors over 10\n if (result.rules.selectors.max > MAX_SELECTORS_PER_RULESET) {\n const score = Math.ceil((result.rules.selectors.max - MAX_SELECTORS_PER_RULESET) * 0.5)\n outcome.score = Math.min(score, 15)\n }\n\n return outcome\n },\n\n // Max number of Declarations per Rule should be low\n result => {\n const MAX_DECLARATIONS_PER_RULESET = 10\n\n const outcome = {\n id: 'MaxDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.max,\n }\n\n // Deduct 0.5 points per declarations over 10\n if (result.rules.declarations.max > MAX_DECLARATIONS_PER_RULESET) {\n const score = Math.ceil((result.rules.declarations.max - MAX_DECLARATIONS_PER_RULESET) * 0.5)\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Selectors per RuleSet should not differ too much from the most common amount of\n // Selectors per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.selectors.items\n .filter(item => item > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorsPerRule',\n score: 0,\n value: result.rules.selectors.mode,\n }\n\n // if more than 10% of RuleSets has more Selectors than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n\n // Number of Declarations per RuleSet should not differ too much from the most common amount of\n // Declarations per RuleSet\n result => {\n const mode = result.rules.selectors.mode\n const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length\n\n const outcome = {\n id: 'MoreThanMostCommonDeclarationsPerRule',\n score: 0,\n value: result.rules.declarations.mode,\n }\n\n // if more than 10% of RuleSets has more Declarations than most common:\n if (rulesHavingMoreThanMode > result.rules.total * 0.1) {\n // then deduct 0.01 for ever applicable RuleSet\n const score = Math.floor(rulesHavingMoreThanMode * 0.01)\n // with a maximum of 10 points\n outcome.score = Math.min(15, score)\n }\n\n return outcome\n },\n]\n\nexport { guards }","import { compareSpecificity } from '@projectwallace/css-analyzer'\n\nconst guards = [\n\n // Complexity per Selector should not differ too much from the most common Complexity\n result => {\n const mode = result.selectors.complexity.mode\n const selectorsAboveMode = result.selectors.complexity.items\n .filter(c => c > mode)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorComplexity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Specificity per Selector should not differ too much from the most common Specificity\n result => {\n const mode = result.selectors.specificity.mode\n const selectorsAboveMode = result.selectors.specificity.items\n .filter(c => compareSpecificity(c, mode) < 0)\n .length\n\n const outcome = {\n id: 'MoreThanMostCommonSelectorSpecificity',\n score: 0,\n value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,\n }\n\n if (selectorsAboveMode > result.selectors.total * 0.1) {\n const score = Math.floor(selectorsAboveMode * 0.01)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n // Maximum Selector Complexity should be low\n result => {\n const MAX_SELECTOR_COMPLEXITY = 5\n const actual = result.selectors.complexity.max\n\n const outcome = {\n id: 'MaxSelectorComplexity',\n score: 0,\n value: result.selectors.complexity.max,\n }\n\n // Deduct 0.5 points per complexity over 5, up to 5 points\n if (actual > MAX_SELECTOR_COMPLEXITY) {\n const score = Math.ceil((actual - MAX_SELECTOR_COMPLEXITY) * 0.5)\n outcome.score = Math.min(5, score)\n }\n\n return outcome\n },\n\n // Average Selector Complexity should be low\n result => {\n const ALLOWED_COMPLEXITY = 2\n const actual = result.selectors.complexity.mean\n\n const outcome = {\n id: 'AverageSelectorComplexity',\n score: 0,\n value: actual,\n }\n\n // Deduct 2 points per selector over 2\n if (actual > ALLOWED_COMPLEXITY) {\n const score = Math.ceil((actual - ALLOWED_COMPLEXITY) * 2)\n outcome.score = Math.min(10, score)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.selectors.id.ratio\n const outcome = {\n id: 'IdSelectorRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n\n result => {\n const ALLOWED = 0.01\n const actual = result.declarations.importants.ratio\n const outcome = {\n id: 'ImportantRatio',\n score: 0,\n value: actual,\n }\n\n if (actual > ALLOWED) {\n const score = Math.floor((actual - ALLOWED) * 10)\n outcome.score = Math.min(score, 5)\n }\n\n return outcome\n },\n]\n\nexport {\n guards\n}","import { analyze } from '@projectwallace/css-analyzer'\nimport { guards as performanceGuards } from './performance.js'\nimport { guards as maintainabilityGuards } from './maintainability.js'\nimport { guards as complexityGuards } from './complexity.js'\n\nfunction calculateScore({ result, guards }) {\n let score = 100\n let violations = []\n let passes = []\n\n for (const guard of guards) {\n /** @type {{score: number, actual: number, id: string}} */\n const outcome = guard(result)\n\n if (outcome.score > 0) {\n score -= outcome.score\n violations.push(outcome)\n } else {\n passes.push(outcome)\n }\n }\n\n return {\n score: Math.max(score, 0),\n violations,\n passes,\n }\n}\n\nfunction calculate(css) {\n const result = analyze(css)\n\n const performance = calculateScore({ result, guards: performanceGuards })\n const maintainability = calculateScore({ result, guards: maintainabilityGuards })\n const complexity = calculateScore({ result, guards: complexityGuards })\n\n return {\n score: 0,\n violations: performance.violations\n .concat(maintainability.violations)\n .concat(complexity.violations),\n passes: performance.passes\n .concat(maintainability.passes)\n .concat(complexity.passes),\n performance,\n maintainability,\n complexity,\n }\n}\n\nexport {\n calculate\n}"],"names":["guards","result","id","score","atrules","total","value","rules","empty","outcome","selectors","uniquenessRatio","Math","floor","declarations","unique","ratio","stylesheet","size","comments","min","embeddedContent","sourceLinesOfCode","actual","mean","max","ceil","mode","rulesHavingMoreThanMode","items","filter","item","length","complexity","selectorsAboveMode","c","specificity","compareSpecificity","importants","calculateScore","violations","passes","guard","push","css","analyze","performance","performanceGuards","maintainability","maintainabilityGuards","complexityGuards","concat"],"mappings":"yaAAA,IAAMA,EAAS,CAGb,SAAAC,SAAW,CACTC,GAAI,UACJC,MAAqC,GAA9BF,EAAOG,eAAeC,MAC7BC,MAAOL,EAAOG,eAAeC,QAI/B,SAAAJ,SAAW,CACTC,GAAI,aACJC,MAAOF,EAAOM,MAAMC,MAAMH,MAC1BC,MAAOL,EAAOM,MAAMC,MAAMH,QAI5B,SAAAJ,GACE,IAAMQ,EAAU,CACdP,GAAI,uBACJC,MAAO,EACPG,MAAO,EAAIL,EAAOS,UAAUC,iBAO9B,OAJIV,EAAOS,UAAUC,gBAAkB,MACrCF,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOS,UAAUC,mBAG5CF,GAIT,SAAAR,GACE,IAAMQ,EAAU,CACdP,GAAI,0BACJC,MAAO,EACPG,MAAO,EAAIL,EAAOa,aAAaC,OAAOC,OAOxC,OAJIf,EAAOa,aAAaC,OAAOC,MAAQ,MACrCP,EAAQN,MAAQS,KAAKC,MAA+C,IAAxC,EAAIZ,EAAOa,aAAaC,OAAOC,SAGtDP,GAIT,SAAAR,SAAW,CACTC,GAAI,UACJC,MAAOF,EAAOgB,WAAWC,KAAO,IAAU,EAAI,EAC9C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+1,52 @@
1
+ export function calculate(css: any): {
2
+ score: number;
3
+ violations: {
4
+ score: number;
5
+ actual: number;
6
+ id: string;
7
+ }[];
8
+ passes: {
9
+ score: number;
10
+ actual: number;
11
+ id: string;
12
+ }[];
13
+ performance: {
14
+ score: number;
15
+ violations: {
16
+ score: number;
17
+ actual: number;
18
+ id: string;
19
+ }[];
20
+ passes: {
21
+ score: number;
22
+ actual: number;
23
+ id: string;
24
+ }[];
25
+ };
26
+ maintainability: {
27
+ score: number;
28
+ violations: {
29
+ score: number;
30
+ actual: number;
31
+ id: string;
32
+ }[];
33
+ passes: {
34
+ score: number;
35
+ actual: number;
36
+ id: string;
37
+ }[];
38
+ };
39
+ complexity: {
40
+ score: number;
41
+ violations: {
42
+ score: number;
43
+ actual: number;
44
+ id: string;
45
+ }[];
46
+ passes: {
47
+ score: number;
48
+ actual: number;
49
+ id: string;
50
+ }[];
51
+ };
52
+ };
package/license ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Bart Veneman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@projectwallace/css-code-quality",
3
+ "description": "Calculate the Code Quality score of your CSS based on a range of different quality guards",
4
+ "version": "0.2.0",
5
+ "repository": "git@github.com:projectwallace/css-code-quality.git",
6
+ "author": {
7
+ "email": "bart@projectwallace.com",
8
+ "name": "Bart Veneman"
9
+ },
10
+ "keywords": [
11
+ "css",
12
+ "codequality",
13
+ "codesniffer",
14
+ "quality",
15
+ "complexity",
16
+ "performance",
17
+ "maintainability"
18
+ ],
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "type": "module",
23
+ "source": "src/index.js",
24
+ "main": "./dist/css-code-quality.cjs",
25
+ "module": "./dist/css-code-quality.module.js",
26
+ "unpkg": "./dist/css-code-quality.umd.js",
27
+ "exports": {
28
+ "require": "./dist/css-code-quality.cjs",
29
+ "default": "./dist/css-code-quality.modern.js"
30
+ },
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "test": "uvu",
34
+ "build": "microbundle"
35
+ },
36
+ "dependencies": {
37
+ "@projectwallace/css-analyzer": "^5.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "microbundle": "^0.14.2",
41
+ "uvu": "^0.5.3"
42
+ }
43
+ }
package/readme.md ADDED
@@ -0,0 +1,29 @@
1
+ # CSS Code Quality calculator
2
+
3
+ <div align="center">
4
+ <img src="logo.png" height="160" width="160" alt="Analyzer logo">
5
+ </div>
6
+
7
+ <div align="center">
8
+ <a href="https://npmjs.org/package/@projectwallace/css-code-quality">
9
+ <img src="https://badgen.net/npm/v/@projectwallace/css-code-quality" alt="version" />
10
+ </a>
11
+ <a href="https://npmjs.org/package/@projectwallace/css-code-quality">
12
+ <img src="https://badgen.now.sh/npm/dm/@projectwallace/css-code-quality" alt="downloads" />
13
+ </a>
14
+ <a href="https://packagephobia.com/result?p=%40projectwallace%2Fcss-code-quality">
15
+ <img src="https://packagephobia.com/badge?p=%40projectwallace%2Fcss-code-quality" alt="install size" />
16
+ </a>
17
+ </div>
18
+
19
+ <div align="center">
20
+ Calculate the Code Quality score of your CSS based on a range of different quality guards.
21
+ </div>
22
+
23
+ ---
24
+
25
+ This package analyzes your CSS on a high level and comes up with a score, divided in three areas:
26
+
27
+ - **Maintainability**: how difficult is it for someone looking at the CSS from a high level to find the exact spot to fix a bug?
28
+ - **Complexity**: how difficult is it for someone to make a change and them being confident that they can make that change without side-effects?
29
+ - **Performance**: How likely is the CSS to have a negative impact on performance, based on high-level metrics? (Not including using hardware accelerated transforms and the like, because other tools are more suite for that.)