Skip to content
Permalink
b63eeba8fa
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
109 lines (101 sloc) 2.75 KB
/**
* Represents a plugin’s warning. It can be created using {@link Node#warn}.
*
* @example
* if (decl.important) {
* decl.warn(result, 'Avoid !important', { word: '!important' })
* }
*/
class Warning {
/**
* @param {string} text Warning message.
* @param {Object} [opts] Warning options.
* @param {Node} opts.node CSS node that caused the warning.
* @param {string} opts.word Word in CSS source that caused the warning.
* @param {number} opts.index Index in CSS node string that caused
* the warning.
* @param {string} opts.plugin Name of the plugin that created
* this warning. {@link Result#warn} fills
* this property automatically.
*/
constructor (text, opts = { }) {
/**
* Type to filter warnings from {@link Result#messages}.
* Always equal to `"warning"`.
*
* @type {string}
*
* @example
* const nonWarning = result.messages.filter(i => i.type !== 'warning')
*/
this.type = 'warning'
/**
* The warning message.
*
* @type {string}
*
* @example
* warning.text //=> 'Try to avoid !important'
*/
this.text = text
if (opts.node && opts.node.source) {
let pos = opts.node.positionBy(opts)
/**
* Line in the input file with this warning’s source.
* @type {number}
*
* @example
* warning.line //=> 5
*/
this.line = pos.line
/**
* Column in the input file with this warning’s source.
*
* @type {number}
*
* @example
* warning.column //=> 6
*/
this.column = pos.column
}
for (let opt in opts) this[opt] = opts[opt]
}
/**
* Returns a warning position and message.
*
* @example
* warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
*
* @return {string} Warning position and message.
*/
toString () {
if (this.node) {
return this.node.error(this.text, {
plugin: this.plugin,
index: this.index,
word: this.word
}).message
}
if (this.plugin) {
return this.plugin + ': ' + this.text
}
return this.text
}
/**
* @memberof Warning#
* @member {string} plugin The name of the plugin that created
* it will fill this property automatically.
* this warning. When you call {@link Node#warn}
*
* @example
* warning.plugin //=> 'postcss-important'
*/
/**
* @memberof Warning#
* @member {Node} node Contains the CSS node that caused the warning.
*
* @example
* warning.node.toString() //=> 'color: white !important'
*/
}
module.exports = Warning