|
| 1 | +/* Copyright 2025 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +// TODO: Remove the exception below once someone figures out how to fix it. |
| 17 | +// eslint-disable-next-line import/no-unresolved |
| 18 | +import { parse, registerWalkers, Root } from "postcss-values-parser"; |
| 19 | +import { isString } from "stylelint/lib/utils/validateTypes.mjs"; |
| 20 | +import stylelint from "stylelint"; |
| 21 | + |
| 22 | +const { |
| 23 | + createPlugin, |
| 24 | + utils: { report, validateOptions }, |
| 25 | +} = stylelint; |
| 26 | + |
| 27 | +registerWalkers(Root); |
| 28 | + |
| 29 | +const ruleName = "pdfjs/no-unused-custom-properties"; |
| 30 | + |
| 31 | +// It's a very basic linter: we don't take into account scopes. |
| 32 | +// But it should be enough for our use case. |
| 33 | + |
| 34 | +/** @type {import('stylelint').Plugin} */ |
| 35 | +const ruleFunction = |
| 36 | + (enabled, { ignoreList = [] } = {}, context = {}) => |
| 37 | + (root, result) => { |
| 38 | + const validOptions = validateOptions( |
| 39 | + result, |
| 40 | + ruleName, |
| 41 | + { |
| 42 | + actual: enabled, |
| 43 | + possible: [true], |
| 44 | + }, |
| 45 | + { |
| 46 | + actual: ignoreList, |
| 47 | + possible: [isString], |
| 48 | + optional: true, |
| 49 | + } |
| 50 | + ); |
| 51 | + |
| 52 | + if (!validOptions) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + ignoreList = ignoreList.map(s => (s.startsWith("--") ? s : `--${s}`)); |
| 57 | + |
| 58 | + const usedCustomProperties = new Set(ignoreList); |
| 59 | + const definedCustomProperties = new Set(); |
| 60 | + const usedBy = new Map(); |
| 61 | + root.walkDecls(decl => { |
| 62 | + let definingProperty = null; |
| 63 | + if (decl.prop.startsWith("--")) { |
| 64 | + // This is a custom property definition. |
| 65 | + definingProperty = decl.prop; |
| 66 | + definedCustomProperties.add(definingProperty); |
| 67 | + } |
| 68 | + // Parse the declaration value to find var() usages. |
| 69 | + const parsedValue = parse(decl.value); |
| 70 | + parsedValue.walkFuncs(node => { |
| 71 | + if (!node.isVar || node.nodes.length === 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + // This is a var() function; get the custom property name. |
| 75 | + const property = node.nodes[0].value; |
| 76 | + if (!definingProperty) { |
| 77 | + // This is a usage of a custom property but not in a definition. |
| 78 | + // width: var(--foo); |
| 79 | + usedCustomProperties.add(property); |
| 80 | + return; |
| 81 | + } |
| 82 | + let usages = usedBy.get(property); |
| 83 | + if (!usages) { |
| 84 | + usages = []; |
| 85 | + usedBy.set(property, usages); |
| 86 | + } |
| 87 | + // Record that this custom property is used by the defining property. |
| 88 | + // --foo: var(--bar); |
| 89 | + // bar is really used only if foo is. |
| 90 | + usages.push(definingProperty); |
| 91 | + }); |
| 92 | + }); |
| 93 | + const isUsed = p => |
| 94 | + usedCustomProperties.has(p) || (usedBy.get(p) || []).some(isUsed); |
| 95 | + for (const customProperty of definedCustomProperties) { |
| 96 | + if (isUsed(customProperty)) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + report({ |
| 100 | + message: `Custom property "${customProperty}" is defined but never used.`, |
| 101 | + node: root, |
| 102 | + result, |
| 103 | + ruleName, |
| 104 | + }); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | +ruleFunction.ruleName = ruleName; |
| 109 | + |
| 110 | +export default createPlugin(ruleName, ruleFunction); |
0 commit comments