|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Enforce pinned deps in package.json: |
| 5 | + * - allow protocols (workspace:, file:, link:, etc.) for internal packages |
| 6 | + * - require exact x.y.z for everything else |
| 7 | + * - no autofix |
| 8 | + */ |
| 9 | + |
| 10 | +const DEFAULT_DEP_FIELDS = [ |
| 11 | + "dependencies", |
| 12 | + "devDependencies", |
| 13 | + "peerDependencies", |
| 14 | + "optionalDependencies", |
| 15 | +]; |
| 16 | + |
| 17 | +function isObjectExpression(node) { |
| 18 | + return node && (node.type === "ObjectExpression" || node.type === "JSONObjectExpression"); |
| 19 | +} |
| 20 | + |
| 21 | +function getPropKeyString(prop) { |
| 22 | + const k = prop.key; |
| 23 | + if (!k) return null; |
| 24 | + // JS parser: Literal |
| 25 | + if (k.type === "Literal" && typeof k.value === "string") return k.value; |
| 26 | + // jsonc-eslint-parser: JSONLiteral |
| 27 | + if (k.type === "JSONLiteral" && typeof k.value === "string") return k.value; |
| 28 | + return null; |
| 29 | +} |
| 30 | + |
| 31 | +function getLiteralString(node) { |
| 32 | + if (!node) return null; |
| 33 | + if (node.type === "Literal" && typeof node.value === "string") return node.value; |
| 34 | + if (node.type === "JSONLiteral" && typeof node.value === "string") return node.value; |
| 35 | + return null; |
| 36 | +} |
| 37 | + |
| 38 | +function getObjectPropertyValue(objExpr, keyName) { |
| 39 | + if (!isObjectExpression(objExpr)) return null; |
| 40 | + for (const p of objExpr.properties || []) { |
| 41 | + if (!p || (p.type !== "Property" && p.type !== "JSONProperty")) continue; |
| 42 | + const k = getPropKeyString(p); |
| 43 | + if (k === keyName) return p.value; |
| 44 | + } |
| 45 | + return null; |
| 46 | +} |
| 47 | + |
| 48 | +module.exports = { |
| 49 | + meta: { |
| 50 | + type: "problem", |
| 51 | + docs: { description: "Require pinned dependency versions in package.json" }, |
| 52 | + schema: [ |
| 53 | + { |
| 54 | + type: "object", |
| 55 | + additionalProperties: false, |
| 56 | + properties: { |
| 57 | + depFields: { type: "array", items: { type: "string" } }, |
| 58 | + excludeList: { type: "array", items: { type: "string" } }, |
| 59 | + internalScopes: { type: "array", items: { type: "string" } }, |
| 60 | + allowProtocols: { type: "array", items: { type: "string" } }, |
| 61 | + allowProtocolsOnlyForInternal: { type: "boolean" }, |
| 62 | + allowExactPrerelease: { type: "boolean" }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + |
| 68 | + create(context) { |
| 69 | + const opt = context.options[0] || {}; |
| 70 | + const depFields = opt.depFields || DEFAULT_DEP_FIELDS; |
| 71 | + const excludeList = opt.excludeList || []; |
| 72 | + const internalScopes = opt.internalScopes || []; |
| 73 | + const allowProtocols = opt.allowProtocols || ["workspace:", "file:", "link:"]; |
| 74 | + const allowProtocolsOnlyForInternal = opt.allowProtocolsOnlyForInternal !== false; // default true |
| 75 | + const allowExactPrerelease = !!opt.allowExactPrerelease; |
| 76 | + |
| 77 | + const exact = allowExactPrerelease |
| 78 | + ? /^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z-.]+)?(?:\+[0-9A-Za-z-.]+)?$/ |
| 79 | + : /^[0-9]+\.[0-9]+\.[0-9]+$/; |
| 80 | + |
| 81 | + function shouldExclude(name) { |
| 82 | + return excludeList.some((p) => name.startsWith(p)); |
| 83 | + } |
| 84 | + |
| 85 | + function isInternal(name) { |
| 86 | + return internalScopes.some((p) => name.startsWith(p)); |
| 87 | + } |
| 88 | + |
| 89 | + function isAllowedProtocol(version) { |
| 90 | + return allowProtocols.some((p) => version.startsWith(p)); |
| 91 | + } |
| 92 | + |
| 93 | + function report(node, depName, field, version) { |
| 94 | + context.report({ |
| 95 | + node, |
| 96 | + message: |
| 97 | + `Dependency "${depName}" in "${field}" must be pinned to an exact version (x.y.z). ` + |
| 98 | + `Got "${version}".` + |
| 99 | + (internalScopes.length ? ` Internal scopes: ${internalScopes.join(", ")}.` : "") + |
| 100 | + (allowProtocols.length |
| 101 | + ? ` Allowed protocols: ${allowProtocols.join(", ")}.` + |
| 102 | + (allowProtocolsOnlyForInternal ? " (internal only)" : "") |
| 103 | + : ""), |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + "Program:exit"(program) { |
| 109 | + const expr = program.body?.[0]?.expression; |
| 110 | + if (!isObjectExpression(expr)) return; |
| 111 | + |
| 112 | + for (const field of depFields) { |
| 113 | + const depObj = getObjectPropertyValue(expr, field); |
| 114 | + if (!isObjectExpression(depObj)) continue; |
| 115 | + |
| 116 | + for (const p of depObj.properties || []) { |
| 117 | + if (!p || (p.type !== "Property" && p.type !== "JSONProperty")) continue; |
| 118 | + |
| 119 | + const depName = getPropKeyString(p); |
| 120 | + const version = getLiteralString(p.value); |
| 121 | + if (!depName || version == null) continue; |
| 122 | + |
| 123 | + if (shouldExclude(depName)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + // allow protocols |
| 128 | + if (isAllowedProtocol(version)) { |
| 129 | + if (allowProtocolsOnlyForInternal && !isInternal(depName)) { |
| 130 | + report(p.value, depName, field, version); |
| 131 | + } |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + // require exact semver |
| 136 | + if (!exact.test(version)) { |
| 137 | + report(p.value, depName, field, version); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
| 142 | + }; |
| 143 | + }, |
| 144 | +}; |
0 commit comments