Files
SnowLuma/.pnpm-store/v11/files/38/010a4489b46476d084597ab3b78eac6c0600897ad002d6584bbd4507a01b663fe6ab04b76982e67783628ffe7dbe2e7fb709e0e67ab482b2090fa9f6e587b7
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

81 lines
1.9 KiB
Plaintext

/**
* @fileoverview Rule to flag variables that are never assigned
* @author Jacob Bandes-Storch <https://github.com/jtbandes>
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../types').Rule.RuleModule} */
module.exports = {
meta: {
type: "problem",
dialects: ["typescript", "javascript"],
language: "javascript",
docs: {
description:
"Disallow `let` or `var` variables that are read but never assigned",
recommended: true,
url: "https://eslint.org/docs/latest/rules/no-unassigned-vars",
},
schema: [],
messages: {
unassigned:
"'{{name}}' is always 'undefined' because it's never assigned.",
},
},
create(context) {
const sourceCode = context.sourceCode;
let insideDeclareModule = false;
return {
"TSModuleDeclaration[declare=true]"() {
insideDeclareModule = true;
},
"TSModuleDeclaration[declare=true]:exit"() {
insideDeclareModule = false;
},
VariableDeclarator(node) {
/** @type {import('estree').VariableDeclaration} */
const declaration = node.parent;
const shouldSkip =
node.init ||
node.id.type !== "Identifier" ||
declaration.kind === "const" ||
declaration.declare ||
insideDeclareModule;
if (shouldSkip) {
return;
}
const [variable] = sourceCode.getDeclaredVariables(node);
if (!variable) {
return;
}
let hasRead = false;
for (const reference of variable.references) {
if (reference.isWrite()) {
return;
}
if (reference.isRead()) {
hasRead = true;
}
}
if (!hasRead) {
// Variables that are never read should be flagged by no-unused-vars instead
return;
}
context.report({
node,
messageId: "unassigned",
data: { name: node.id.name },
});
},
};
},
};