1
0
mirror of https://github.com/thangisme/notes.git synced 2024-11-01 08:17:34 -04:00
notes/node_modules/stylelint/lib/rules/media-feature-name-whitelist/index.js
Patrick Marsceill b7b0d0d7bf
Initial commit
2017-03-09 13:16:08 -05:00

59 lines
1.8 KiB
JavaScript

"use strict"
const atRuleParamIndex = require("../../utils/atRuleParamIndex")
const isCustomMediaQuery = require("../../utils/isCustomMediaQuery")
const isRangeContextMediaFeature = require("../../utils/isRangeContextMediaFeature")
const isStandardSyntaxMediaFeatureName = require("../../utils/isStandardSyntaxMediaFeatureName")
const matchesStringOrRegExp = require("../../utils/matchesStringOrRegExp")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const _ = require("lodash")
const mediaParser = require("postcss-media-query-parser").default
const ruleName = "media-feature-name-whitelist"
const messages = ruleMessages(ruleName, {
rejected: name => `Unexpected media feature name "${name}"`,
})
const rule = function (whitelist) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: whitelist,
possible: [_.isString],
})
if (!validOptions) {
return
}
root.walkAtRules(/^media$/i, atRule => {
mediaParser(atRule.params).walk(/^media-feature$/i, mediaFeatureNode => {
const parent = mediaFeatureNode.parent,
sourceIndex = mediaFeatureNode.sourceIndex,
value = mediaFeatureNode.value
if (isRangeContextMediaFeature(parent.value) || !isStandardSyntaxMediaFeatureName(value) || isCustomMediaQuery(value)) {
return
}
if (matchesStringOrRegExp(value.toLowerCase(), whitelist)) {
return
}
report({
index: atRuleParamIndex(atRule) + sourceIndex,
message: messages.rejected(value),
node: atRule,
ruleName,
result,
})
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule