Passed
Push — master ( f1d6e8...e7b455 )
by Andrii
02:58
created

src/collector.ts   A

Complexity

Total Complexity 7
Complexity/F 7

Size

Lines of Code 49
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 7
mnd 6
bc 6
fnc 1
bpm 6
cpm 7
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B collector.ts ➔ collector 0 40 7
1
import type { CollectingArg, InternalOptions } from "./$defs.types"
2
import type { Options } from "./options.types"
3
4
export = collector
5
6
function collector(
7
  identifiers: Record<string, true>,
8
  {
9
    identifierParser,
10
    identifierMatchIndex,
11
    identifierCleanupParser,
12
    identifierCleanupReplace,
13
    allowedAtRuleNames,
14
  }: Pick<Required<Options>, "identifierMatchIndex"|"identifierCleanupReplace">
15
  & Pick<InternalOptions, "identifierParser"|"identifierCleanupParser"|"allowedAtRuleNames">
16
) {
17
  return ({selectors, parent}: CollectingArg) => {
18
    if (parent?.type === "atrule") {
19
      const {name} = parent
20
21
      if (name && !allowedAtRuleNames.has(name))
22
        return
23
    }
24
25
    //TODO consider postcss-selector-parser
26
    const {length} = selectors
27
28
    for (let i = length; i--; ) {
29
      const selector = selectors[i]
30
31
      let parsed: RegExpExecArray | null
32
      let lastIndex: number|undefined = undefined
33
34
      while (parsed = identifierParser.exec(selector)) {
35
        const {index} = parsed
36
        if (index === lastIndex)
37
          // TODO consider throw error
38
          return
39
40
        lastIndex = index
41
        const identifier = parsed[identifierMatchIndex]
42
        .replace(identifierCleanupParser, identifierCleanupReplace)
43
44
        identifiers[identifier] = true
45
      }
46
    }
47
  }
48
}
49
50