diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:05:55 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:05:55 +0800 |
| commit | c06f7ee0a40255214c477e13d9b956a7e07c2900 (patch) | |
| tree | f5982a568ef8fab865d0142e71c7494fea3e4c1b /node_modules/stylehacks/src/plugin.js | |
| parent | 3fd9400d4dccc25aa29b5702216732e0b7542073 (diff) | |
postcss,(plugins): nanocss, postcss-preset-env
Diffstat (limited to 'node_modules/stylehacks/src/plugin.js')
| -rw-r--r-- | node_modules/stylehacks/src/plugin.js | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/node_modules/stylehacks/src/plugin.js b/node_modules/stylehacks/src/plugin.js new file mode 100644 index 00000000..15c6b5a9 --- /dev/null +++ b/node_modules/stylehacks/src/plugin.js @@ -0,0 +1,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 }) + ); + }); + } +}; |
