source: frontend/node_modules/stylehacks/src/plugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2/**
3 * @typedef {object} Plugin
4 * @prop {Set<string>} targets
5 * @prop {Set<string>} nodeTypes
6 * @prop {(node: import('postcss').Node) => void} detectAndResolve
7 * @prop {(node: import('postcss').Node) => void} detectAndWarn
8 */
9
10/**
11 * @typedef {import('postcss').Node & {_stylehacks: {
12 message: string,
13 browsers: Set<string>,
14 identifier: string,
15 hack: string }}} NodeWithInfo
16 */
17
18module.exports = class BasePlugin {
19 /**
20 * @param {string[]} targets
21 * @param {string[]} nodeTypes
22 * @param {import('postcss').Result=} result
23 */
24 constructor(targets, nodeTypes, result) {
25 /** @type {NodeWithInfo[]} */
26 this.nodes = [];
27 this.targets = new Set(targets);
28 this.nodeTypes = new Set(nodeTypes);
29 this.result = result;
30 }
31
32 /**
33 * @param {import('postcss').Node} node
34 * @param {{identifier: string, hack: string}} metadata
35 * @return {void}
36 */
37 push(node, metadata) {
38 /** @type {NodeWithInfo} */ (node)._stylehacks = Object.assign(
39 {},
40 metadata,
41 {
42 message: `Bad ${metadata.identifier}: ${metadata.hack}`,
43 browsers: this.targets,
44 }
45 );
46
47 this.nodes.push(/** @type {NodeWithInfo} */ (node));
48 }
49
50 /**
51 * @param {import('postcss').Node} node
52 * @return {boolean}
53 */
54 any(node) {
55 if (this.nodeTypes.has(node.type)) {
56 this.detect(node);
57
58 return /** @type {NodeWithInfo} */ (node)._stylehacks !== undefined;
59 }
60
61 return false;
62 }
63
64 /**
65 * @param {import('postcss').Node} node
66 * @return {void}
67 */
68 detectAndResolve(node) {
69 this.nodes = [];
70
71 this.detect(node);
72
73 return this.resolve();
74 }
75
76 /**
77 * @param {import('postcss').Node} node
78 * @return {void}
79 */
80 detectAndWarn(node) {
81 this.nodes = [];
82
83 this.detect(node);
84
85 return this.warn();
86 }
87 /** @param {import('postcss').Node} node */
88 // eslint-disable-next-line no-unused-vars
89 detect(node) {
90 throw new Error('You need to implement this method in a subclass.');
91 }
92
93 /** @return {void} */
94 resolve() {
95 return this.nodes.forEach((node) => node.remove());
96 }
97
98 warn() {
99 return this.nodes.forEach((node) => {
100 const { message, browsers, identifier, hack } = node._stylehacks;
101
102 return node.warn(
103 /** @type {import('postcss').Result} */ (this.result),
104 message + JSON.stringify({ browsers, identifier, hack })
105 );
106 });
107 }
108};
Note: See TracBrowser for help on using the repository browser.