UNPKG

eslint-plugin-sonarjs

Version:
64 lines (63 loc) 2.34 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) 2011-2025 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ // https://sonarsource.github.io/rspec/#/rspec/S1523/javascript // SQ key 'eval' Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const core_js_1 = require("../external/core.js"); const index_js_1 = require("../helpers/index.js"); const meta_js_1 = require("./meta.js"); const noScriptUrlRule = (0, core_js_1.getESLintCoreRule)('no-script-url'); exports.rule = { meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { messages: { safeCode: 'Make sure that this dynamic injection or execution of code is safe.', unexpectedScriptURL: "Make sure that 'javascript:' code is safe as it is a form of eval().", }, }), create(context) { return { CallExpression: (node) => checkCallExpression(node, context), NewExpression: (node) => checkCallExpression(node, context), ...noScriptUrlRule.create(context), }; }, }; function checkCallExpression(node, context) { if (node.callee.type === 'Identifier') { const { name } = node.callee; if ((name === 'eval' || name === 'Function') && hasAtLeastOneVariableArgument(node.arguments)) { context.report({ messageId: 'safeCode', node: node.callee, }); } } } function hasAtLeastOneVariableArgument(args) { return !!args.find(arg => !isLiteral(arg)); } function isLiteral(node) { if (node.type === 'Literal') { return true; } if (node.type === 'TemplateLiteral') { return node.expressions.length === 0; } return false; }