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