UNPKG

eslint-plugin-sonarjs

Version:
185 lines (184 loc) • 7.59 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) SonarSource Sàrl * mailto:info AT sonarsource DOT com * * You can redistribute and/or modify this program under the terms of * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. * * 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/S9144/javascript var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const ast_js_1 = require("../helpers/ast.js"); const generate_meta_js_1 = require("../helpers/generate-meta.js"); const module_js_1 = require("../helpers/module.js"); const meta = __importStar(require("./generated-meta.js")); const JQUERY_IDENTIFIERS = new Set(['jQuery', '$']); const JQUERY_MODULES = new Set([ 'jquery', 'jquery.slim', 'jquery.dist.jquery.slim', 'jquery.dist.jquery.slim.js', ]); const JQUERY_NAMED_IMPORTS = new Set([ 'jquery.$', 'jquery.jQuery', 'jquery.slim.$', 'jquery.slim.jQuery', 'jquery.dist.jquery.slim.$', 'jquery.dist.jquery.slim.jQuery', 'jquery.dist.jquery.slim.js.$', 'jquery.dist.jquery.slim.js.jQuery', ]); const JQUERY_METHODS = new Map([ ['isArray', { alternative: 'Array.isArray()', messageId: 'deprecated' }], ['parseJSON', { alternative: 'JSON.parse()', messageId: 'deprecated' }], ['now', { alternative: 'Date.now()', messageId: 'deprecated' }], ['trim', { alternative: 'String.prototype.trim()', messageId: 'deprecated' }], [ 'inArray', { alternative: 'Array.prototype.indexOf()', messageId: 'dependencyReduction', }, ], ]); exports.rule = { meta: (0, generate_meta_js_1.generateMeta)(meta, { messages: { deprecated: 'Use {{alternative}} instead of deprecated jQuery.{{method}}().', dependencyReduction: 'Use {{alternative}} instead of jQuery.{{method}}() to reduce dependence on jQuery.', }, }), create(context) { const jqueryReceivers = new Map(); return { CallExpression(node) { const call = node; const jqueryMethod = getJQueryMethod(call, context, jqueryReceivers); if (jqueryMethod === undefined) { return; } context.report({ node: jqueryMethod.property, messageId: jqueryMethod.method.messageId, data: { alternative: jqueryMethod.method.alternative, method: jqueryMethod.property.name, }, }); }, }; }, }; function getJQueryMethod(call, context, jqueryReceivers) { if (call.type !== 'CallExpression' || call.optional || !(0, ast_js_1.isMethodCall)(call) || call.callee.optional || !(0, ast_js_1.isIdentifier)(call.callee.object) || !isJQueryReceiver(call.callee.object, context, jqueryReceivers)) { return undefined; } const method = JQUERY_METHODS.get(call.callee.property.name); return method === undefined ? undefined : { method, property: call.callee.property }; } function isJQueryReceiver(identifier, context, jqueryReceivers) { const variable = (0, ast_js_1.getVariableFromName)(context, identifier.name, identifier); if (variable === undefined || variable.defs.length === 0) { return JQUERY_IDENTIFIERS.has(identifier.name); } if (jqueryReceivers.has(variable)) { return jqueryReceivers.get(variable) ?? false; } const isReceiver = isDirectJQueryModuleBinding(variable, context) && isJQueryModuleReference(context, identifier); jqueryReceivers.set(variable, isReceiver); return isReceiver; } function isJQueryModuleReference(context, identifier) { const fullyQualifiedName = (0, module_js_1.getFullyQualifiedName)(context, identifier) ?? ''; return JQUERY_MODULES.has(fullyQualifiedName) || JQUERY_NAMED_IMPORTS.has(fullyQualifiedName); } function isDirectJQueryModuleBinding(variable, context) { if (variable.defs.length !== 1) { return false; } const [definition] = variable.defs; return (isDirectJQueryImportBinding(definition) || isDirectJQueryRequireBinding(variable, definition, context)); } function isDirectJQueryImportBinding(definition) { if (definition.type !== 'ImportBinding') { return false; } const { node: specifier, parent: declaration } = definition; if (declaration.type === 'TSImportEqualsDeclaration') { return (declaration.moduleReference.type === 'TSExternalModuleReference'); } return (declaration.type === 'ImportDeclaration' && (specifier.type === 'ImportDefaultSpecifier' || specifier.type === 'ImportNamespaceSpecifier' || (specifier.type === 'ImportSpecifier' && isJQueryImportSpecifier(specifier)))); } function isJQueryImportSpecifier(specifier) { if (isDefaultImportSpecifier(specifier)) { return true; } return (specifier.imported.type === 'Identifier' && (specifier.imported.name === '$' || specifier.imported.name === 'jQuery')); } function isDefaultImportSpecifier(specifier) { return ((specifier.imported.type === 'Identifier' && specifier.imported.name === 'default') || (specifier.imported.type === 'Literal' && specifier.imported.value === 'default')); } function isDirectJQueryRequireBinding(variable, definition, context) { const value = (0, ast_js_1.getUniqueWriteReference)(variable); return (definition.type === 'Variable' && definition.node.id.type === 'Identifier' && value !== undefined && (0, module_js_1.isRequire)(value) && !(0, module_js_1.isGlobalShadowed)(context.sourceCode, value, 'require')); }