warning.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict'
  2. let Container = require('./container')
  3. let { my } = require('./symbols')
  4. class Warning {
  5. constructor(text, opts = {}) {
  6. this.type = 'warning'
  7. this.text = text
  8. if (opts.node && opts.node.source) {
  9. if (!opts.node[my]) {
  10. // The node comes from another PostCSS copy in node_modules, so it does
  11. // not have this copy’s methods. Container#normalize() rebuilds such
  12. // nodes on insert, but a node passed straight to Result#warn() never
  13. // goes through it.
  14. Container.rebuild(opts.node)
  15. }
  16. let range = opts.node.rangeBy(opts)
  17. this.line = range.start.line
  18. this.column = range.start.column
  19. this.endLine = range.end.line
  20. this.endColumn = range.end.column
  21. }
  22. for (let opt in opts) this[opt] = opts[opt]
  23. }
  24. toString() {
  25. if (this.node) {
  26. return this.node.error(this.text, {
  27. index: this.index,
  28. plugin: this.plugin,
  29. word: this.word
  30. }).message
  31. }
  32. if (this.plugin) {
  33. return this.plugin + ': ' + this.text
  34. }
  35. return this.text
  36. }
  37. }
  38. module.exports = Warning
  39. Warning.default = Warning