1 | ;
|
2 |
|
3 | /**
|
4 | * Create a collection of Maps that serve to contextualize a given node.
|
5 | * This is useful to ensure that you only compare nodes that share a certain
|
6 | * context.
|
7 | *
|
8 | * All nodes are initially contextualized by their input source.
|
9 | * From there, you can contextualize them however you want.
|
10 | *
|
11 | * For a usage example, see `selector-no-descending-specificity`.
|
12 | */
|
13 | module.exports = function () {
|
14 | const contextMap = new Map();
|
15 |
|
16 | return {
|
17 | /**
|
18 | * @param {import('postcss').Node} node
|
19 | */
|
20 | getContext(node, /** @type {any[]} */ ...subContexts) {
|
21 | // TODO TYPES node.source possible undefined
|
22 | const nodeSource = /** @type {import('postcss').NodeSource} */ (node.source).input.from;
|
23 | const baseContext = creativeGetMap(contextMap, nodeSource);
|
24 |
|
25 | return subContexts.reduce((result, context) => {
|
26 | return creativeGetMap(result, context);
|
27 | }, baseContext);
|
28 | },
|
29 | };
|
30 | };
|
31 |
|
32 | /**
|
33 | * TODO TYPES
|
34 | * @param {Map<any, any>} someMap
|
35 | * @param {any} someThing
|
36 | */
|
37 | function creativeGetMap(someMap, someThing) {
|
38 | if (!someMap.has(someThing)) {
|
39 | someMap.set(someThing, new Map());
|
40 | }
|
41 |
|
42 | return someMap.get(someThing);
|
43 | }
|