eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
76 lines (75 loc) • 3.04 kB
JavaScript
/*
* 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/S5256/javascript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const jsx_ast_utils_1 = __importDefault(require("jsx-ast-utils"));
const { getLiteralPropValue, getProp } = jsx_ast_utils_1.default;
const table_js_1 = require("../helpers/table.js");
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta),
create(context) {
const checkValidTable = (tree) => {
const grid = (0, table_js_1.computeGrid)(context, tree);
if (grid === null) {
// Unknown table structure, dont raise issue
return true;
}
if (grid.length === 0) {
return false;
}
for (const row of grid) {
if (row.every(({ isHeader }) => isHeader)) {
return true;
}
}
for (let col = 0; col < grid[0].length; col++) {
if (grid.every(row => col >= row.length || row[col].isHeader)) {
return true;
}
}
return false;
};
return {
JSXElement(node) {
const tree = node;
const elementType = (0, index_js_1.getElementType)(context)(tree.openingElement);
if (elementType === 'table') {
if ((0, index_js_1.isPresentationTable)(context, tree.openingElement)) {
return;
}
const ariaHidden = getProp(tree.openingElement.attributes, 'aria-hidden');
if (ariaHidden && getLiteralPropValue(ariaHidden) === true) {
return;
}
if (!checkValidTable(tree)) {
context.report({
node,
message: 'Add a valid header row or column to this "<table>".',
});
}
}
},
};
},
};
;