Passed
Push — master ( 657ba7...501391 )
by Andrii
02:17
created

collector.ts ➔ collector   B

Complexity

Conditions 6

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
dl 0
loc 41
rs 8.2266
c 0
b 0
f 0
1
import type { CollectingArg } from "./ts-swiss.types"
2
3
export = collector
4
5
function collector({
6
  identifiers,
7
  identifierParser,
8
  identifierMatchIndex,
9
  identifierCleanupParser,
10
  identifierCleanupReplace,
11
  allowedAtRules,
12
}: {
13
  identifiers: Record<string, true>,
14
  identifierParser: RegExp,
15
  identifierMatchIndex: number,
16
  identifierCleanupParser: RegExp,
17
  identifierCleanupReplace: string,
18
  allowedAtRules: Set<string>
19
}) {
20
  return ({selectors, parent}: CollectingArg) => {
21
    if (parent?.type === "atrule") {
22
      const {name} = parent
23
      if (name && !allowedAtRules.has(name))
24
        return identifiers
25
    }
26
27
    //TODO consider postcss-selector-parser
28
    const {length} = selectors
29
30
    for (let i = length; i--; ) {
31
      const selector = selectors[i]
32
33
      let parsed: RegExpExecArray | null
34
35
      // TODO check that parser is moving
36
      while (parsed = identifierParser.exec(selector)) {
37
        const identifier = parsed[identifierMatchIndex]
38
        .replace(identifierCleanupParser, identifierCleanupReplace)
39
40
        identifiers[identifier] = true
41
      }
42
    }
43
44
    return identifiers
45
  }
46
}
47
48