• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FullHuman / purgecss / 6677792066

28 Oct 2023 03:34PM UTC coverage: 92.031%. Remained the same
6677792066

push

github

web-flow
build(deps): bump typescript from 4.9.4 to 4.9.5 (#1159)

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.9.4 to 4.9.5.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/compare/v4.9.4...v4.9.5)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

317 of 372 branches covered (0.0%)

Branch coverage included in aggregate %.

503 of 519 relevant lines covered (96.92%)

10603.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/packages/purgecss/src/VariablesStructure.ts
1
import * as postcss from "postcss";
2
import { StringRegExpArray } from "./types";
3

4
/**
5
 * @public
6
 */
7
export class VariableNode {
44✔
8
  public nodes: VariableNode[] = [];
60✔
9
  public value: postcss.Declaration;
10
  public isUsed = false;
60✔
11

12
  constructor(declaration: postcss.Declaration) {
13
    this.value = declaration;
60✔
14
  }
15
}
16

17
/**
18
 * @public
19
 */
20
export class VariablesStructure {
44✔
21
  public nodes: Map<string, VariableNode[]> = new Map();
104✔
22
  public usedVariables: Set<string> = new Set();
104✔
23
  public safelist: StringRegExpArray = [];
104✔
24

25
  addVariable(declaration: postcss.Declaration): void {
26
    const { prop } = declaration;
60✔
27
    if (!this.nodes.has(prop)) {
60✔
28
      const node = new VariableNode(declaration);
38✔
29
      this.nodes.set(prop, [node]);
38✔
30
    } else {
31
      const node = new VariableNode(declaration);
22✔
32
      const variableNodes = this.nodes.get(prop) || [];
22!
33
      this.nodes.set(prop, [...variableNodes, node]);
22✔
34
    }
35
  }
36

37
  addVariableUsage(
38
    declaration: postcss.Declaration,
39
    matchedVariables: IterableIterator<RegExpMatchArray>
40
  ): void {
41
    const { prop } = declaration;
60✔
42
    const nodes = this.nodes.get(prop);
60✔
43
    for (const variableMatch of matchedVariables) {
60✔
44
      // capturing group containing the variable is in index 1
45
      const variableName = variableMatch[1];
26✔
46
      if (this.nodes.has(variableName)) {
26✔
47
        const usedVariableNodes = this.nodes.get(variableName);
20✔
48
        nodes?.forEach((node) => {
20!
49
          usedVariableNodes?.forEach((usedVariableNode) =>
28!
50
            node.nodes.push(usedVariableNode)
40✔
51
          );
52
        });
53
      }
54
    }
55
  }
56

57
  addVariableUsageInProperties(
58
    matchedVariables: IterableIterator<RegExpMatchArray>
59
  ): void {
60
    for (const variableMatch of matchedVariables) {
34✔
61
      // capturing group containing the variable is in index 1
62
      const variableName = variableMatch[1];
34✔
63
      this.usedVariables.add(variableName);
34✔
64
    }
65
  }
66

67
  setAsUsed(variableName: string): void {
68
    const nodes = this.nodes.get(variableName);
24✔
69
    if (nodes) {
24✔
70
      const queue = [...nodes];
24✔
71
      while (queue.length !== 0) {
24✔
72
        const currentNode = queue.pop();
66✔
73
        if (currentNode && !currentNode.isUsed) {
66✔
74
          currentNode.isUsed = true;
38✔
75
          queue.push(...currentNode.nodes);
38✔
76
        }
77
      }
78
    }
79
  }
80

81
  removeUnused(): void {
82
    // check unordered usage
83
    for (const used of this.usedVariables) {
4✔
84
      const usedNodes = this.nodes.get(used);
24✔
85
      if (usedNodes) {
24✔
86
        for (const usedNode of usedNodes) {
24✔
87
          const usedVariablesMatchesInDeclaration =
88
            usedNode.value.value.matchAll(/var\((.+?)[,)]/g);
38✔
89

90
          for (const usage of usedVariablesMatchesInDeclaration) {
38✔
91
            if (!this.usedVariables.has(usage[1])) {
16✔
92
              this.usedVariables.add(usage[1]);
10✔
93
            }
94
          }
95
        }
96
      }
97
    }
98

99
    for (const used of this.usedVariables) {
4✔
100
      this.setAsUsed(used);
24✔
101
    }
102

103
    for (const [name, declarations] of this.nodes) {
4✔
104
      for (const declaration of declarations) {
38✔
105
        if (!declaration.isUsed && !this.isVariablesSafelisted(name)) {
60✔
106
          declaration.value.remove();
18✔
107
        }
108
      }
109
    }
110
  }
111

112
  isVariablesSafelisted(variable: string): boolean {
113
    return this.safelist.some((safelistItem) => {
22✔
114
      return typeof safelistItem === "string"
10✔
115
        ? safelistItem === variable
116
        : safelistItem.test(variable);
117
    });
118
  }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc