Skip to content

Commit

Permalink
implement type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Veneman committed Nov 2, 2023
1 parent 1cf7361 commit 74e3308
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 39 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

jobs:
test:
name: Unit tests
name: Tests
runs-on: ubuntu-latest

strategy:
Expand All @@ -20,13 +20,20 @@ jobs:
- 14.13.0
- 16
- 18
- 20

steps:
- uses: actions/checkout@v3
- name: Checkout code
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm install --ignore-scripts --no-audit
- run: npm test
- name: Install dependencies
run: npm install --ignore-scripts --no-audit
- name: Run unit tests
run: npm test
- name: Run type checks
run: npm run check
if: ${{ matrix.node-version }} == 20
34 changes: 27 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"scripts": {
"test": "uvu",
"build": "microbundle"
"build": "microbundle",
"check": "tsc --noEmit"
},
"keywords": [
"projectwallace",
Expand All @@ -47,6 +48,7 @@
},
"devDependencies": {
"microbundle": "^0.15.1",
"typescript": "^5.2.2",
"uvu": "^0.5.6"
},
"mangle": {
Expand Down
6 changes: 3 additions & 3 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ export class Collection {
* @property {number} column
* @property {number} offset
* @property {number} length
*
* @returns {{ total: number; totalUnique: number; uniquenessRatio: number; unique: Record<string, number>; __unstable__uniqueWithLocations: Record<string, CssLocation[]>}}
*/
count() {
let uniqueWithLocations = new Map()
/** @type Record<string, number> */
let unique = {}
this._items.forEach((list, key) => {
let nodes = list.map(index => ({
Expand All @@ -73,6 +72,7 @@ export class Collection {
totalUnique: this._items.size,
unique,
uniquenessRatio: this._total === 0 ? 0 : this._items.size / this._total,
/** @type {Record<string, CssLocation[]>} */
__unstable__uniqueWithLocations: Object.fromEntries(uniqueWithLocations),
}
}
Expand All @@ -81,7 +81,7 @@ export class Collection {
total: this._total,
totalUnique: this._items.size,
unique,
uniquenessRatio: this._total === 0 ? 0 : this._items.size / this._total,
uniquenessRatio: this._total === 0 ? 0 : this._items.size / this._total
}
}
}
2 changes: 1 addition & 1 deletion src/countable-collection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CountableCollection {

/** @param {string[]?} initial */
constructor(initial) {
constructor(initial = undefined) {
/** @type {Map<string, number>} */
this._items = new Map()
this._total = 0
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let defaults = {
* @param {string} css
* @param {Options} options
*/
export function analyze(css, options = {}) {
export function analyze(css, options = { useUnstableLocations: false }) {
let settings = Object.assign({}, defaults, options)
let useLocations = settings.useUnstableLocations === true
let start = Date.now()
Expand Down Expand Up @@ -84,7 +84,7 @@ export function analyze(css, options = {}) {

// Atrules
let totalAtRules = 0
/** @type {Record<string: string>}[]} */
/** @type {Record<string, string>[]}} */
let fontfaces = []
let layers = new Collection({ useLocations })
let imports = new Collection({ useLocations })
Expand Down Expand Up @@ -165,6 +165,7 @@ export function analyze(css, options = {}) {
let atRuleName = node.name

if (atRuleName === 'font-face') {
/** @type Record<string, string> */
let descriptors = {}

node.block.children.forEach(descriptor => {
Expand Down Expand Up @@ -269,7 +270,7 @@ export function analyze(css, options = {}) {

uniqueSelectors.add(selector)
selectorComplexities.push(complexity)
uniqueSelectorComplexities.push(complexity, node.loc)
uniqueSelectorComplexities.push(String(complexity), node.loc)

// #region specificity
let [{ value: specificityObj }] = calculate(node)
Expand Down
1 change: 0 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ Api('handles empty input gracefully', () => {
"total": 0,
"totalUnique": 0,
"unique": {},
"total": 0,
"uniquenessRatio": 0,
"items": []
},
Expand Down
1 change: 0 additions & 1 deletion src/rules/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Rules('should handle CSS without rules', () => {
range: 0,
sum: 0,
items: [],
items: [],
unique: {},
total: 0,
totalUnique: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/selectors/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { hasVendorPrefix } from '../vendor-prefix.js'
/**
*
* @param {import('css-tree').SelectorList} selectorListAst
* @returns {Selector[]} Analyzed selectors in the selectorList
* @returns {import('css-tree').Selector[]} Analyzed selectors in the selectorList
*/
function analyzeList(selectorListAst, cb) {
let childSelectors = []
Expand Down Expand Up @@ -64,7 +64,7 @@ export function isAccessibility(selector) {

/**
* Get the Complexity for the AST of a Selector Node
* @param {import('css-tree').Selector} ast - AST Node for a Selector
* @param {import('css-tree').Selector} selector - AST Node for a Selector
* @return {[number, boolean]} - The numeric complexity of the Selector and whether it's prefixed or not
*/
export function getComplexity(selector) {
Expand Down
4 changes: 2 additions & 2 deletions src/string-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Case-insensitive compare two character codes
* @param {string} referenceCode
* @param {string} testCode
* @param {number} referenceCode
* @param {number} testCode
* @see https://github.com/csstree/csstree/blob/41f276e8862d8223eeaa01a3d113ab70bb13d2d9/lib/tokenizer/utils.js#L22
*/
function compareChar(referenceCode, testCode) {
Expand Down
14 changes: 0 additions & 14 deletions src/values/destructure-font-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,6 @@ export function destructure(value, stringifyNode) {
return
}

// If, after taking care of font-size and line-height, we still have a remaining dimension, it must be the oblique angle
if (
node.type === 'Dimension' &&
item.prev &&
item.prev.data.type === TYPE_IDENTIFIER &&
item.prev.data.name === 'oblique'
) {
// put in the correct amount of whitespace between `oblique` and `<angle>`
font_style +=
''.padStart(node.loc.start.offset - item.prev.data.loc.end.offset) +
stringifyNode(node)
return
}

// any node that's a number and not previously caught by line-height or font-size is the font-weight
// (oblique <angle> will not be caught here, because that's a Dimension, not a Number)
if (node.type === 'Number') {
Expand Down
33 changes: 33 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"include": ["src/**/*"],

"compilerOptions": {
"rootDir": "src",

// target node v8+ (https://node.green/)
// the only missing feature is Array.prototype.values
"lib": ["es2019", "DOM", "DOM.Iterable"],
"target": "es2019",

"module": "esnext",
"moduleResolution": "node",

// silences wrong TS error, we don't compile, we only typecheck
"outDir": "./irrelevant/unused",

"declaration": true,
"declarationDir": "types",

"noEmitOnError": true,
"noErrorTruncation": true,

"allowJs": true,
"checkJs": true,

// TODO: error all the things
//"strict": true,
// "noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
}
}

0 comments on commit 74e3308

Please sign in to comment.