Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
with:
node-version: 16
- run: npm ci
- run: npm run build
- run: npm test

publish-npm:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run build
- run: npm test
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@
"maintainability"
],
"files": [
"dist"
"dist",
"src"
],
"type": "module",
"source": "src/index.js",
"main": "./dist/css-code-quality.cjs",
"module": "./dist/css-code-quality.module.js",
"unpkg": "./dist/css-code-quality.umd.js",
"exports": {
"require": "./dist/css-code-quality.cjs",
"default": "./dist/css-code-quality.modern.js"
".": {
"import": "./dist/css-code-quality.modern.js",
"require": "./dist/css-code-quality.cjs"
},
"./core": {
"import": "./src/core.js"
}
},
"types": "./dist/index.d.ts",
"scripts": {
Expand All @@ -40,4 +46,4 @@
"microbundle": "^0.15.1",
"uvu": "^0.5.6"
}
}
}
6 changes: 1 addition & 5 deletions src/complexity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compareSpecificity } from '@projectwallace/css-analyzer'

const guards = [
export const guards = [

// Complexity per Selector should not differ too much from the most common Complexity
result => {
Expand Down Expand Up @@ -124,7 +124,3 @@ const guards = [
return outcome
},
]

export {
guards
}
47 changes: 47 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { guards as performanceGuards } from './performance.js'
import { guards as maintainabilityGuards } from './maintainability.js'
import { guards as complexityGuards } from './complexity.js'

function calculateScore({ result, guards }) {
let score = 100
let violations = []
let passes = []

for (const guard of guards) {
/** @type {{score: number, actual: number, id: string}} */
const outcome = guard(result)

if (outcome.score > 0) {
score -= outcome.score
violations.push(outcome)
} else {
passes.push(outcome)
}
}

return {
score: Math.max(score, 0),
violations,
passes,
}
}

export function calculate(analysis) {
const performance = calculateScore({ result: analysis, guards: performanceGuards })
const maintainability = calculateScore({ result: analysis, guards: maintainabilityGuards })
const complexity = calculateScore({ result: analysis, guards: complexityGuards })

return {
/** @deprecated */
score: 0,
violations: performance.violations
.concat(maintainability.violations)
.concat(complexity.violations),
passes: performance.passes
.concat(maintainability.passes)
.concat(complexity.passes),
performance,
maintainability,
complexity,
}
}
11 changes: 11 additions & 0 deletions src/core.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as assert from 'uvu/assert'
import { suite } from 'uvu'
import { calculate } from './core.js'

const Core = suite('Core')

Core('exports calculate', () => {
assert.is(typeof calculate, 'function')
})

Core.run()
54 changes: 4 additions & 50 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
import { analyze } from '@projectwallace/css-analyzer'
import { guards as performanceGuards } from './performance.js'
import { guards as maintainabilityGuards } from './maintainability.js'
import { guards as complexityGuards } from './complexity.js'
import { calculate as calculateFromAnalysis } from './core.js'

function calculateScore({ result, guards }) {
let score = 100
let violations = []
let passes = []

for (const guard of guards) {
/** @type {{score: number, actual: number, id: string}} */
const outcome = guard(result)

if (outcome.score > 0) {
score -= outcome.score
violations.push(outcome)
} else {
passes.push(outcome)
}
}

return {
score: Math.max(score, 0),
violations,
passes,
}
}

function calculate(css) {
const result = analyze(css)

const performance = calculateScore({ result, guards: performanceGuards })
const maintainability = calculateScore({ result, guards: maintainabilityGuards })
const complexity = calculateScore({ result, guards: complexityGuards })

return {
score: 0,
violations: performance.violations
.concat(maintainability.violations)
.concat(complexity.violations),
passes: performance.passes
.concat(maintainability.passes)
.concat(complexity.passes),
performance,
maintainability,
complexity,
}
export function calculate(css) {
const analysis = analyze(css)
return calculateFromAnalysis(analysis)
}

export {
calculate
}
20 changes: 20 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as assert from 'uvu/assert'
import { suite } from 'uvu'
import { calculate } from './index.js'
import { calculate as pkgCalculate } from '../dist/css-code-quality.modern.js'

const Index = suite('Index')

Index('exposes a calculcate function', () => {
assert.is(typeof calculate, 'function')
})

Index.run()

const Package = suite('NPM Package')

Package('exposes a calculate function', () => {
assert.is(typeof pkgCalculate, 'function')
})

Package.run()
4 changes: 1 addition & 3 deletions src/maintainability.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const guards = [
export const guards = [

// Source Lines of Code should be low'
result => {
Expand Down Expand Up @@ -148,5 +148,3 @@ const guards = [
return outcome
},
]

export { guards }
4 changes: 1 addition & 3 deletions src/performance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const guards = [
export const guards = [

// Should not contain @import rules
result => ({
Expand Down Expand Up @@ -75,5 +75,3 @@ const guards = [
}
},
]

export { guards }