Check ignore next warning comments in a warn() helper

This commit is contained in:
dualfroz committed 2026-10-05 23:11:46 +02:00
1 parent e47232149a
commit dc4f2415a8
7 files changed
+121 -82

No files matched your search

+32
View File
@@ -1,5 +1,10 @@
let { list } = require('postcss')
let vendor = require('./vendor')
const IGNORE_NEXT_WARNING = /(!\s*)?autoprefixer:\s*ignore\s+next\s+warning/i
const PREFIXED_VALUE = /(^|[\s,(])-\w+-/
/**
* Throw special error, to tell beniary,
* that this error is from Autoprefixer.
@@ -91,3 +96,30 @@ module.exports.isPureNumber = function (value) {
}
return false
}
/**
* Is the node right after an `ignore next warning` control comment?
* Prefixed copies inserted before the node are skipped on the way back.
*/
function hasIgnoreWarningComment(node) {
let prev = node.prev()
while (
prev &&
prev.type === 'decl' &&
(vendor.prefix(prev.prop) || PREFIXED_VALUE.test(prev.value))
) {
prev = prev.prev()
}
return (
!!prev && prev.type === 'comment' && IGNORE_NEXT_WARNING.test(prev.text)
)
}
/**
* Add a warning, unless the node is hidden
* by an `ignore next warning` control comment.
*/
module.exports.warn = function (result, text, opts = {}) {
if (opts.node && hasIgnoreWarningComment(opts.node)) return
result.warn(text, opts)
}