ray-template/scripts/eslint-rules/no-undefined.mjs
XiaoDaiGua-Ray 911c403434
feat: refine admin workflows for v6.0.3
Improve theme consistency, keyboard navigation, and reusable frontend components.
Harden fullstack initialization and expose scoped lint and Vitest commands.
2026-12-01 11:04:13 +08:00

140 lines
3.9 KiB
JavaScript

const statementLists = new Set([
'Program',
'BlockStatement',
'StaticBlock',
'SwitchCase',
'TSModuleBlock',
])
function needsParentheses(node) {
const { parent } = node
switch (parent.type) {
case 'CallExpression':
case 'NewExpression':
return parent.callee === node
case 'MemberExpression':
return parent.object === node
case 'TaggedTemplateExpression':
return parent.tag === node
case 'BinaryExpression':
return parent.operator === '**' && parent.left === node
case 'ClassDeclaration':
case 'ClassExpression':
return parent.superClass === node
case 'Decorator':
case 'TSInstantiationExpression':
case 'TSNonNullExpression':
return true
default:
return false
}
}
function replacementText(sourceCode, node) {
if (node.parent.type === 'Property' && node.parent.shorthand)
return `${sourceCode.getText(node)}: void 0`
if (!needsParentheses(node)) return 'void 0'
const before = sourceCode.getTokenBefore(node)
if (before?.value === '(' && sourceCode.getTokenAfter(node)?.value === ')')
return 'void 0'
// 补上的左括号可能被解析为上一条表达式的函数调用。
const startsStatement = sourceCode
.getAncestors(node)
.some(
(ancestor) =>
ancestor.type === 'ExpressionStatement' &&
ancestor.range[0] === node.range[0] &&
statementLists.has(ancestor.parent.type),
)
return startsStatement && before && ![';', '{', ':'].includes(before.value)
? ';(void 0)'
: '(void 0)'
}
export default {
meta: {
type: 'suggestion',
fixable: 'code',
schema: [],
messages: {
useVoidZero: 'Use void 0 for the runtime undefined value.',
unexpectedUndefined: 'Unexpected use of undefined as an identifier.',
},
},
create(context) {
const { sourceCode } = context
const evalScopes = new Set()
const canFix = (reference) => {
const { identifier } = reference
const { parent } = identifier
if (
identifier.type !== 'Identifier' ||
!reference.isReadOnly() ||
parent.type === 'ExportSpecifier' ||
(parent.type === 'UnaryExpression' && parent.operator === 'delete')
)
return false
for (let scope = reference.from; scope; scope = scope.upper) {
if (scope.type === 'with' || evalScopes.has(scope)) return false
}
return true
}
return {
'CallExpression[callee.type="Identifier"][callee.name="eval"]'(node) {
// 保守处理直接 eval 可能在运行时引入的局部绑定。
if (!node.optional)
evalScopes.add(sourceCode.getScope(node).variableScope)
},
'Program:exit'(program) {
const globalScope = sourceCode.getScope(program)
for (const scope of sourceCode.scopeManager.scopes) {
const variable = scope.set.get('undefined')
if (!variable) continue
for (const { name } of variable.defs)
context.report({ node: name, messageId: 'unexpectedUndefined' })
for (const reference of variable.references) {
const node = reference.identifier
if (
reference.init ||
reference.isValueReference === false ||
node.parent.type === 'TSTypeQuery' ||
node.parent.type === 'TSQualifiedName'
)
continue
if (
scope === globalScope &&
!variable.defs.length &&
canFix(reference)
) {
context.report({
node,
messageId: 'useVoidZero',
fix: (fixer) =>
fixer.replaceText(node, replacementText(sourceCode, node)),
})
} else {
context.report({ node, messageId: 'unexpectedUndefined' })
}
}
}
},
}
},
}