Skip to content

Commit 0cfb55c

Browse files
authored
Fix typings here and there (#310)
1 parent 3dd29af commit 0cfb55c

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

src/context-collection.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CountableCollection } from './countable-collection.js'
33
class ContextCollection {
44
constructor() {
55
this._list = new CountableCollection()
6-
/** @type {[index; string]: CountableCollection} */
6+
/** @type {Map<string, CountableCollection>} */
77
this._contexts = new Map()
88
}
99

@@ -23,6 +23,14 @@ class ContextCollection {
2323
}
2424

2525
count() {
26+
/**
27+
* @type {Map<string, {
28+
* total: number,
29+
* totalUnique: number,
30+
* unique: {[string]: number},
31+
* uniquenessRatio: number
32+
* }>}
33+
*/
2634
const itemsPerContext = new Map()
2735

2836
for (let [context, value] of this._contexts.entries()) {

src/countable-collection.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class CountableCollection {
2-
/**
3-
* @param {string[]} initial
4-
*/
2+
3+
/** @param {string[]?} initial */
54
constructor(initial) {
6-
/** @type [index: string]: string */
5+
/** @type {Map<string, number>} */
76
this._items = new Map()
8-
/** @type number */
97
this._total = 0
108

119
if (initial) {
@@ -15,11 +13,7 @@ class CountableCollection {
1513
}
1614
}
1715

18-
/**
19-
* Push an item to the end of this collection
20-
* @param {string} item
21-
* @returns {void}
22-
*/
16+
/** @param {string} item */
2317
push(item) {
2418
this._total++
2519

@@ -31,17 +25,10 @@ class CountableCollection {
3125
this._items.set(item, 1)
3226
}
3327

34-
/**
35-
* Get the size of this collection
36-
* @returns {number} the size of this collection
37-
*/
3828
size() {
3929
return this._total
4030
}
4131

42-
/**
43-
* Get the counts of this collection, like total, uniques, etc.
44-
*/
4532
count() {
4633
return {
4734
total: this._total,

src/index.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { isCustom, isHack, isProperty } from './properties/property-utils.js'
1717
import { getEmbedType } from './stylesheet/stylesheet.js'
1818
import { isIe9Hack } from './values/browserhacks.js'
1919

20+
/** @typedef {[number, number, number]} Specificity */
21+
2022
function ratio(part, total) {
2123
if (total === 0) return 0
2224
return part / total
@@ -49,12 +51,12 @@ const analyze = (css) => {
4951
let embedSize = 0
5052
const embedTypes = {
5153
total: 0,
54+
/** @type {Map<string, {size: number, count: number}>} */
5255
unique: new Map()
5356
}
5457

5558
const startParse = Date.now()
5659

57-
/** @type import('css-tree').CssNode */
5860
const ast = parse(css, {
5961
parseCustomProperty: true, // To find font-families, colors, etc.
6062
positions: true, // So we can use stringifyNode()
@@ -70,7 +72,7 @@ const analyze = (css) => {
7072

7173
// Atrules
7274
let totalAtRules = 0
73-
/** @type {string}[]} */
75+
/** @type {{[property: string]: string}[]} */
7476
const fontfaces = []
7577
const layers = new CountableCollection()
7678
const imports = new CountableCollection()
@@ -94,16 +96,16 @@ const analyze = (css) => {
9496
const keyframeSelectors = new CountableCollection()
9597
const uniqueSelectors = new Set()
9698
const prefixedSelectors = new CountableCollection()
97-
/** @type [number,number,number] */
99+
/** @type {Specificity} */
98100
let maxSpecificity
99-
/** @type [number,number,number] */
101+
/** @type {Specificity} */
100102
let minSpecificity
101103
const specificityA = new AggregateCollection()
102104
const specificityB = new AggregateCollection()
103105
const specificityC = new AggregateCollection()
104106
const uniqueSpecificities = new CountableCollection()
105107
const selectorComplexities = new AggregateCollection()
106-
/** @type [number,number,number][] */
108+
/** @type {Specificity[]} */
107109
const specificities = []
108110
const ids = new CountableCollection()
109111
const a11y = new CountableCollection()
@@ -144,7 +146,6 @@ const analyze = (css) => {
144146
const atRuleName = node.name
145147

146148
if (atRuleName === 'font-face') {
147-
/** @type {[index: string]: string} */
148149
const descriptors = {}
149150

150151
node.block.children.forEach(descriptor => {
@@ -226,6 +227,7 @@ const analyze = (css) => {
226227
}
227228

228229
const [{ value: specificityObj }] = calculate(node)
230+
/** @type {Specificity} */
229231
const specificity = [specificityObj.a, specificityObj.b, specificityObj.c]
230232

231233
if (specificity[0] > 0) {
@@ -601,17 +603,17 @@ const analyze = (css) => {
601603
totalUnique: totalUniqueSelectors,
602604
uniquenessRatio: ratio(totalUniqueSelectors, totalSelectors),
603605
specificity: {
604-
/** @type [number, number, number] */
606+
/** @type Specificity */
605607
min: minSpecificity === undefined ? [0, 0, 0] : minSpecificity,
606-
/** @type [number, number, number] */
608+
/** @type Specificity */
607609
max: maxSpecificity === undefined ? [0, 0, 0] : maxSpecificity,
608-
/** @type [number, number, number] */
610+
/** @type Specificity */
609611
sum: [specificitiesA.sum, specificitiesB.sum, specificitiesC.sum],
610-
/** @type [number, number, number] */
612+
/** @type Specificity */
611613
mean: [specificitiesA.mean, specificitiesB.mean, specificitiesC.mean],
612-
/** @type [number, number, number] */
614+
/** @type Specificity */
613615
mode: [specificitiesA.mode, specificitiesB.mode, specificitiesC.mode],
614-
/** @type [number, number, number] */
616+
/** @type Specificity */
615617
median: [specificitiesA.median, specificitiesB.median, specificitiesC.median],
616618
items: specificities,
617619
unique: uniqueSpecificitiesCount.unique,

src/selectors/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function isPseudoFunction(name) {
5151
)
5252
}
5353

54+
/** @param {import('css-tree').Selector} selector */
5455
export function isAccessibility(selector) {
5556
let isA11y = false
5657

@@ -82,7 +83,7 @@ export function isAccessibility(selector) {
8283
/**
8384
* Get the Complexity for the AST of a Selector Node
8485
* @param {import('css-tree').Selector} ast - AST Node for a Selector
85-
* @return {number} - The numeric complexity of the Selector
86+
* @return {[number, boolean]} - The numeric complexity of the Selector and whether it's prefixed or not
8687
*/
8788
export function getComplexity(selector) {
8889
let complexity = 0

src/string-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function strEquals(base, test) {
3434
/**
3535
* Case-insensitive testing whether a string ends with a given substring
3636
* @param {string} base e.g. '-webkit-transform'
37-
* @param {string} cmp e.g. 'transform'
37+
* @param {string} test e.g. 'transform'
3838
* @returns {boolean} true if `test` ends with `base`, false otherwise
3939
*/
4040
export function endsWith(base, test) {

src/stylesheet/stylesheet.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export function getEmbedType(/** @type string */embed) {
1+
/** @param {string} embed */
2+
export function getEmbedType(embed) {
23
// data:image/gif;base64,R0lG
34
let start = 5 // `data:`.length
45
let semicolon = embed.indexOf(';')

0 commit comments

Comments
 (0)