| Total Complexity | 7 |
| Complexity/F | 7 |
| Lines of Code | 49 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 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 |