UNPKG

eslint-plugin-sonarjs

Version:
149 lines (148 loc) 5.98 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/S8782/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 mocha_js_1 = require("../helpers/mocha.js"); const meta = __importStar(require("./generated-meta.js")); const AFTER_HOOK_CONSTRUCTS = ['after', 'afterAll', 'afterEach']; function getDescribeBody(node) { const callback = node.arguments[1]; if (!callback || !ast_js_1.FUNCTION_NODES.includes(callback.type)) { return null; } const body = callback.body; return body.type === 'BlockStatement' ? body : null; } function findTestCaseRange(body) { const isTestOrSuiteStatement = (statement) => statement.type === 'ExpressionStatement' && statement.expression.type === 'CallExpression' && ((0, mocha_js_1.isTestCase)(statement.expression) || (0, mocha_js_1.isDescribeCase)(statement.expression)); const first = body.body.findIndex(isTestOrSuiteStatement); if (first === -1) { return null; } let last = body.body.length - 1; while (!isTestOrSuiteStatement(body.body[last])) { last--; } return { first, last }; } function extractLifecycleHookCall(statement) { if (statement.type !== 'ExpressionStatement' || statement.expression.type !== 'CallExpression' || !(0, mocha_js_1.isLifecycleHook)(statement.expression)) { return null; } return statement.expression; } function classifyHook(expr, index, range, afterHooksAtTop, afterHooksAtBottom, report) { const isAfterHook = (0, mocha_js_1.isTestConstruct)(expr, AFTER_HOOK_CONSTRUCTS); if (index < range.first) { isAfterHook && afterHooksAtTop.push(expr); return; } if (index > range.last) { isAfterHook ? afterHooksAtBottom.push(expr) : report(expr, 'moveBeforeHook'); return; } report(expr, isAfterHook ? 'moveAfterHook' : 'moveBeforeHook'); } exports.rule = { meta: (0, generate_meta_js_1.generateMeta)(meta, { messages: { moveBeforeHook: 'Move this hook above the test cases in the same scope.', moveAfterHook: 'Move this hook above or below the test cases in the same scope.', groupAfterHook: 'Group this hook with the other after-hooks in the same scope.', }, }), create(context) { const reportHook = (expr, messageId) => { const callee = expr.callee; context.report({ node: callee.type === 'MemberExpression' ? callee.object : callee, messageId, }); }; return { CallExpression(node) { if (!(0, mocha_js_1.isDescribeCase)(node)) { return; } const body = getDescribeBody(node); if (!body) { return; } const range = findTestCaseRange(body); if (!range) { return; } const afterHooksAtTop = []; const afterHooksAtBottom = []; for (const [i, statement] of body.body.entries()) { const expr = extractLifecycleHookCall(statement); if (expr) { classifyHook(expr, i, range, afterHooksAtTop, afterHooksAtBottom, reportHook); } } if (afterHooksAtTop.length === 0 || afterHooksAtBottom.length === 0) { return; } const minority = afterHooksAtTop.length < afterHooksAtBottom.length ? afterHooksAtTop : afterHooksAtBottom; for (const expr of minority) { reportHook(expr, 'groupAfterHook'); } }, }; }, };