plugin.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = plugin;
  6. function plugin(targets, nodeTypes, detect) {
  7. class Plugin {
  8. constructor(result) {
  9. this.nodes = [];
  10. this.result = result;
  11. this.targets = targets;
  12. this.nodeTypes = nodeTypes;
  13. }
  14. push(node, metadata) {
  15. node._stylehacks = Object.assign({}, metadata, {
  16. message: `Bad ${metadata.identifier}: ${metadata.hack}`,
  17. browsers: this.targets
  18. });
  19. this.nodes.push(node);
  20. }
  21. any(node) {
  22. if (~this.nodeTypes.indexOf(node.type)) {
  23. detect.apply(this, arguments);
  24. return !!node._stylehacks;
  25. }
  26. return false;
  27. }
  28. detectAndResolve(...args) {
  29. this.nodes = [];
  30. detect.apply(this, args);
  31. return this.resolve();
  32. }
  33. detectAndWarn(...args) {
  34. this.nodes = [];
  35. detect.apply(this, args);
  36. return this.warn();
  37. }
  38. resolve() {
  39. return this.nodes.forEach(node => node.remove());
  40. }
  41. warn() {
  42. return this.nodes.forEach(node => {
  43. const { message, browsers, identifier, hack } = node._stylehacks;
  44. return node.warn(this.result, message, { browsers, identifier, hack });
  45. });
  46. }
  47. }
  48. return Plugin;
  49. }
  50. module.exports = exports["default"];