|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Any modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +import stylelint from 'stylelint'; |
| 13 | +import { NAMESPACE } from '../..'; |
| 14 | +import { |
| 15 | + ComplianceEngine, |
| 16 | + getTrackedMessage, |
| 17 | + getUntrackedMessage, |
| 18 | + getNotCompliantMessage, |
| 19 | + getRulesFromConfig, |
| 20 | + isColorProperty, |
| 21 | + isValidOptions, |
| 22 | +} from '../../utils'; |
| 23 | + |
| 24 | +const isOuiAuditEnabled = Boolean(process.env.OUI_AUDIT_ENABLED); |
| 25 | + |
| 26 | +const { ruleMessages, report } = stylelint.utils; |
| 27 | +const engine = ComplianceEngine.default; |
| 28 | + |
| 29 | +const ruleName = 'no_custom_colors'; |
| 30 | +const messages = ruleMessages(ruleName, { |
| 31 | + expected: (message) => `${message}`, |
| 32 | +}); |
| 33 | + |
| 34 | +const ruleFunction = ( |
| 35 | + primaryOption: Record<string, any>, |
| 36 | + secondaryOptionObject: Record<string, any>, |
| 37 | + context |
| 38 | +) => { |
| 39 | + return (postcssRoot: any, postcssResult: any) => { |
| 40 | + const validOptions = isValidOptions(postcssResult, ruleName, primaryOption); |
| 41 | + if (!validOptions) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const rules = getRulesFromConfig(primaryOption.config); |
| 46 | + |
| 47 | + const isAutoFixing = Boolean(context.fix); |
| 48 | + |
| 49 | + postcssRoot.walkDecls((decl: any) => { |
| 50 | + if (!isColorProperty(decl.prop)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + let shouldReport = false; |
| 55 | + |
| 56 | + const nodeInfo = { |
| 57 | + selector: decl.parent.selector, |
| 58 | + prop: decl.prop, |
| 59 | + value: decl.value, |
| 60 | + }; |
| 61 | + |
| 62 | + const reportInfo = { |
| 63 | + ruleName: `${NAMESPACE}/${ruleName}`, |
| 64 | + result: postcssResult, |
| 65 | + node: decl, |
| 66 | + message: '', |
| 67 | + }; |
| 68 | + |
| 69 | + if (isOuiAuditEnabled && !engine.isTracked(rules, nodeInfo)) { |
| 70 | + reportInfo.message = messages.expected(getUntrackedMessage(nodeInfo)); |
| 71 | + report(reportInfo); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const ruleObject = engine.getComplianceRule(rules, nodeInfo); |
| 76 | + |
| 77 | + if (!ruleObject) { |
| 78 | + if (isOuiAuditEnabled) { |
| 79 | + reportInfo.message = messages.expected(getTrackedMessage(nodeInfo)); |
| 80 | + report(reportInfo); |
| 81 | + } |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + shouldReport = !ruleObject.isComplaint; |
| 86 | + |
| 87 | + if (shouldReport && isAutoFixing) { |
| 88 | + decl.value = ruleObject.expected; |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (!shouldReport) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + reportInfo.message = messages.expected(getNotCompliantMessage(ruleObject.message)); |
| 97 | + report(reportInfo); |
| 98 | + }); |
| 99 | + }; |
| 100 | +}; |
| 101 | + |
| 102 | +ruleFunction.ruleName = ruleName; |
| 103 | +ruleFunction.messages = messages; |
| 104 | + |
| 105 | +// eslint-disable-next-line import/no-default-export |
| 106 | +export default ruleFunction; |
0 commit comments