UNPKG

eslint-plugin-sonarjs

Version:
151 lines (150 loc) • 6.33 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/S9332/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 PLAYWRIGHT_MODULES = ['@playwright/test']; const NETWORKIDLE = 'networkidle'; /** Methods whose options object is the first argument. */ const OPTIONS_FIRST_ARG_METHODS = new Set(['reload', 'goBack', 'goForward']); /** Methods whose options object follows a required first argument. */ const OPTIONS_SECOND_ARG_METHODS = new Set(['goto', 'setContent', 'waitForURL']); const MESSAGE = 'Replace this "networkidle" wait with a web-first assertion or a specific readiness condition.'; exports.rule = { meta: (0, generate_meta_js_1.generateMeta)(meta), create(context) { if (!(0, module_js_1.importsOrDependsOnModule)(context, PLAYWRIGHT_MODULES, PLAYWRIGHT_MODULES)) { return {}; } return { CallExpression(node) { const call = node; if (!(0, ast_js_1.isMethodCall)(call)) { return; } const methodName = call.callee.property.name; if (methodName === 'waitForLoadState') { reportWaitForLoadState(context, call); return; } if (OPTIONS_FIRST_ARG_METHODS.has(methodName)) { reportWaitUntilOption(context, call.arguments[0]); return; } if (OPTIONS_SECOND_ARG_METHODS.has(methodName)) { reportWaitUntilOption(context, call.arguments[1]); } }, }; }, }; function reportWaitForLoadState(context, call) { const stateArgument = call.arguments[0]; if (!stateArgument || stateArgument.type === 'SpreadElement') { return; } const stateLiteral = resolveExpression(context, stateArgument); if (isNetworkidleLiteral(stateLiteral)) { context.report({ node: stateLiteral, message: MESSAGE }); } } function reportWaitUntilOption(context, optionsArgument) { if (!optionsArgument || optionsArgument.type === 'SpreadElement') { return; } // Prefer resolveExpression over getValueOfExpression so TS wrappers on the // options object itself (e.g. `{ waitUntil: 'networkidle' } as const`) are // unwrapped before the ObjectExpression check. const optionsObject = resolveExpression(context, optionsArgument); if (optionsObject?.type !== 'ObjectExpression') { return; } const waitUntilProperty = (0, ast_js_1.getProperty)(optionsObject, 'waitUntil', context); if (!waitUntilProperty) { return; } const waitUntilValue = resolveExpression(context, waitUntilProperty.value); if (isNetworkidleLiteral(waitUntilValue)) { context.report({ node: waitUntilValue, message: MESSAGE }); } } /** * Resolves `expr` to its underlying value, unwrapping TypeScript wrapper * expressions (e.g. `as const`, `satisfies`) at every step of identifier * resolution. Unlike `getValueOfExpression`, which only matches when the * resolved write expression is *directly* the requested node type, this also * follows a variable whose own initializer is TS-wrapped * (e.g. `const options = { waitUntil: 'networkidle' } as const`). * Guards against reference cycles (e.g. `let a = a`) with a visited set. */ function resolveExpression(context, expr) { if (!expr) { return undefined; } const visited = new Set(); let current = (0, ast_js_1.unwrapTypeScriptExpression)(expr); while (current.type === 'Identifier' && !visited.has(current)) { visited.add(current); const usage = (0, ast_js_1.getUniqueWriteUsage)(context, current.name, current); if (!usage) { break; } current = (0, ast_js_1.unwrapTypeScriptExpression)(usage); } return current; } function isNetworkidleLiteral(node) { return node != null && (0, ast_js_1.isStringLiteral)(node) && node.value === NETWORKIDLE; }