warning.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. /**
  5. * Represents a plugin’s warning. It can be created using {@link Node#warn}.
  6. *
  7. * @example
  8. * if (decl.important) {
  9. * decl.warn(result, 'Avoid !important', { word: '!important' })
  10. * }
  11. */
  12. var Warning = /*#__PURE__*/function () {
  13. /**
  14. * @param {string} text Warning message.
  15. * @param {Object} [opts] Warning options.
  16. * @param {Node} opts.node CSS node that caused the warning.
  17. * @param {string} opts.word Word in CSS source that caused the warning.
  18. * @param {number} opts.index Index in CSS node string that caused
  19. * the warning.
  20. * @param {string} opts.plugin Name of the plugin that created
  21. * this warning. {@link Result#warn} fills
  22. * this property automatically.
  23. */
  24. function Warning(text, opts) {
  25. if (opts === void 0) {
  26. opts = {};
  27. }
  28. /**
  29. * Type to filter warnings from {@link Result#messages}.
  30. * Always equal to `"warning"`.
  31. *
  32. * @type {string}
  33. *
  34. * @example
  35. * const nonWarning = result.messages.filter(i => i.type !== 'warning')
  36. */
  37. this.type = 'warning';
  38. /**
  39. * The warning message.
  40. *
  41. * @type {string}
  42. *
  43. * @example
  44. * warning.text //=> 'Try to avoid !important'
  45. */
  46. this.text = text;
  47. if (opts.node && opts.node.source) {
  48. var pos = opts.node.positionBy(opts);
  49. /**
  50. * Line in the input file with this warning’s source.
  51. * @type {number}
  52. *
  53. * @example
  54. * warning.line //=> 5
  55. */
  56. this.line = pos.line;
  57. /**
  58. * Column in the input file with this warning’s source.
  59. *
  60. * @type {number}
  61. *
  62. * @example
  63. * warning.column //=> 6
  64. */
  65. this.column = pos.column;
  66. }
  67. for (var opt in opts) {
  68. this[opt] = opts[opt];
  69. }
  70. }
  71. /**
  72. * Returns a warning position and message.
  73. *
  74. * @example
  75. * warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
  76. *
  77. * @return {string} Warning position and message.
  78. */
  79. var _proto = Warning.prototype;
  80. _proto.toString = function toString() {
  81. if (this.node) {
  82. return this.node.error(this.text, {
  83. plugin: this.plugin,
  84. index: this.index,
  85. word: this.word
  86. }).message;
  87. }
  88. if (this.plugin) {
  89. return this.plugin + ': ' + this.text;
  90. }
  91. return this.text;
  92. }
  93. /**
  94. * @memberof Warning#
  95. * @member {string} plugin The name of the plugin that created
  96. * it will fill this property automatically.
  97. * this warning. When you call {@link Node#warn}
  98. *
  99. * @example
  100. * warning.plugin //=> 'postcss-important'
  101. */
  102. /**
  103. * @memberof Warning#
  104. * @member {Node} node Contains the CSS node that caused the warning.
  105. *
  106. * @example
  107. * warning.node.toString() //=> 'color: white !important'
  108. */
  109. ;
  110. return Warning;
  111. }();
  112. var _default = Warning;
  113. exports.default = _default;
  114. module.exports = exports.default;
  115. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndhcm5pbmcuZXM2Il0sIm5hbWVzIjpbIldhcm5pbmciLCJ0ZXh0Iiwib3B0cyIsInR5cGUiLCJub2RlIiwic291cmNlIiwicG9zIiwicG9zaXRpb25CeSIsImxpbmUiLCJjb2x1bW4iLCJvcHQiLCJ0b1N0cmluZyIsImVycm9yIiwicGx1Z2luIiwiaW5kZXgiLCJ3b3JkIiwibWVzc2FnZSJdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQTs7Ozs7Ozs7SUFRTUEsTztBQUNKOzs7Ozs7Ozs7OztBQVdBLG1CQUFhQyxJQUFiLEVBQW1CQyxJQUFuQixFQUErQjtBQUFBLFFBQVpBLElBQVk7QUFBWkEsTUFBQUEsSUFBWSxHQUFMLEVBQUs7QUFBQTs7QUFDN0I7Ozs7Ozs7OztBQVNBLFNBQUtDLElBQUwsR0FBWSxTQUFaO0FBQ0E7Ozs7Ozs7OztBQVFBLFNBQUtGLElBQUwsR0FBWUEsSUFBWjs7QUFFQSxRQ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GZpbHRlciB3YXJuaW5ncyBmcm9tIHtAbGluayBSZXN1bHQjbWVzc2FnZXN9LlxuICAgICAqIEFsd2F5cyBlcXVhbCB0byBgXCJ3YXJuaW5nXCJgLlxuICAgICAqXG4gICAgICogQHR5cGUge3N0cmluZ31cbiAgICAgKlxuICAgICAqIEBleGFtcGxlXG4gICAgICogY29uc3Qgbm9uV2FybmluZyA9IHJlc3VsdC5tZXNzYWdlcy5maWx0ZXIoaSA9PiBpLnR5cGUgIT09ICd3YXJuaW5nJylcbiAgICAgKi9cbiAgICB0aGlzLnR5cGUgPSAnd2FybmluZydcbiAgICAvKipcbiAgICAgKiBUaGUgd2FybmluZyBtZXNzYWdlLlxuICAgICAqXG4gICAgICogQHR5cGUge3N0cmluZ31cbiAgICAgKlxuICAgICAqIEBleGFtcGxlXG4gICAgICogd2FybmluZy50ZXh0IC8vPT4gJ1RyeSB0byBhdm9pZCAhaW1wb3J0YW50J1xuICAgICAqL1xuICAgIHRoaXMudGV4dCA9IHRleHRcblxuICAgIGlmIChvcHRzLm5vZGUgJiYgb3B0cy5ub2RlLnNvdXJjZSkge1xuICAgICAgbGV0IHBvcyA9IG9wdHMubm9kZS5wb3NpdGlvbkJ5KG9wdHMpXG4gICAgICAvKipcbiAgICAgICAqIExpbmUgaW4gdGhlIGlucHV0IGZpbGUgd2l0aCB0aGlzIHdhcm5pbmfigJlzIHNvdXJjZS5cbiAgICAgICAqIEB0eXBlIHtudW1iZXJ9XG4gICAgICAgKlxuICAgICAgICogQGV4YW1wbGVcbiAgICAgICAqIHdhcm5pbmcubGluZSAvLz0+IDVcbiAgICAgICAqL1xuICAgICAgdGhpcy5saW5lID0gcG9zLmxpbmVcbiAgICAgIC8qKlxuICAgICAgICogQ29sdW1uIGluIHRoZSBpbnB1dCBmaWxlIHdpdGggdGhpcyB3YXJuaW5n4oCZcyBzb3VyY2UuXG4gICAgICAgKlxuICAgICAgICogQHR5cGUge251bWJlcn1cbiAgICAgICAqXG4gICAgICAgKiBA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