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-case/index.js
Patrick Marsceill b7b0d0d7bf
Initial commit
2017-03-09 13:16:08 -05:00

62 lines
1.9 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 report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const mediaParser = require("postcss-media-query-parser").default
const ruleName = "media-feature-name-case"
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
})
const rule = function (expectation) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"lower",
"upper",
],
})
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
}
const expectedFeatureName = expectation === "lower" ? value.toLowerCase() : value.toUpperCase()
if (value === expectedFeatureName) {
return
}
report({
index: atRuleParamIndex(atRule) + sourceIndex,
message: messages.expected(value, expectedFeatureName),
node: atRule,
ruleName,
result,
})
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule