Files
NapCatQQ/.pnpm-store/v3/files/97/6421c611eacf9115de0ae36a67f373935a0e604623bea7630b9ec94274db76d5c1233c5045b55f1b0c9ca78da66c7649d2a313a37dabc43acff507bd825db4
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

72 lines
2.2 KiB
Plaintext

import { createRule } from '../utils/index.js';
function isRequire(node) {
return (node.callee?.type === 'Identifier' &&
node.callee.name === 'require' &&
node.arguments.length > 0);
}
function isDynamicImport(node) {
return (node?.callee &&
node.callee.type === 'Import');
}
function isStaticValue(node) {
return (node.type === 'Literal' ||
(node.type === 'TemplateLiteral' && node.expressions.length === 0));
}
export default createRule({
name: 'no-dynamic-require',
meta: {
type: 'suggestion',
docs: {
category: 'Static analysis',
description: 'Forbid `require()` calls with expressions.',
},
schema: [
{
type: 'object',
properties: {
esmodule: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
import: 'Calls to import() should use string literals',
require: 'Calls to require() should use string literals',
},
},
defaultOptions: [],
create(context) {
const options = context.options[0] || {};
return {
CallExpression(node) {
if (!node.arguments[0] || isStaticValue(node.arguments[0])) {
return;
}
if (isRequire(node)) {
return context.report({
node,
messageId: 'require',
});
}
if (options.esmodule && isDynamicImport(node)) {
return context.report({
node,
messageId: 'import',
});
}
},
ImportExpression(node) {
if (!options.esmodule || isStaticValue(node.source)) {
return;
}
return context.report({
node,
messageId: 'import',
});
},
};
},
});
//# sourceMappingURL=no-dynamic-require.js.map