Fixed the following SonarQube code smells:

1) Do not use the Array index in keys.
2) Import from the same module should be merged.
3) Mutable variables should not be exported.
4) Variables should not be initialized to undefined.
5) startswith or endswith method should be used.
6) Unwrap this unnecessarily grouped subpattern.

Additionally, addressed many other SonarQube rules.
This commit is contained in:
Akshay Joshi 2024-06-12 18:09:06 +05:30
parent 288fd7ed12
commit dea5335ce5
29 changed files with 51 additions and 57 deletions

View File

@ -70,7 +70,7 @@ FROM alpine:latest AS env-builder
# Install dependencies
COPY requirements.txt /
RUN apk add --no-cache \
RUN apk add --no-cache \
make \
python3 \
py3-pip && \
@ -187,7 +187,7 @@ COPY LICENSE /pgadmin4/LICENSE
COPY DEPENDENCIES /pgadmin4/DEPENDENCIES
# Install runtime dependencies and configure everything in one RUN step
RUN apk add \
RUN apk add --no-cache \
python3 \
py3-pip \
postfix \

View File

@ -14,12 +14,12 @@
{% trans copyright=copyright|e %}<li class="left" style="margin-left: 10px">&#169; Copyright {{ copyright }}.</li>{% endtrans %}
{%- endif %}
{%- endif %}
<li class="right" style="margin-right: 10px"><a href="genindex.html" title="General Index" accesskey="I">index</a></li>
<li class="right" style="margin-right: 10px"><a href="genindex.html" title="General Index">index</a></li>
{%- if next %}
<li class="right" ><a href="{{ next.link|e }}" title="{{ next.title|striptags|e }}" accesskey="N">next</a> |</li>
<li class="right" ><a href="{{ next.link|e }}" title="{{ next.title|striptags|e }}">next</a> |</li>
{%- endif %}
{%- if prev %}
<li class="right" ><a href="{{ prev.link|e }}" title="{{ prev.title|striptags|e }}" accesskey="P">previous</a> |</li>
<li class="right" ><a href="{{ prev.link|e }}" title="{{ prev.title|striptags|e }}">previous</a> |</li>
{%- endif %}
</ul>
</div>

View File

@ -212,7 +212,7 @@ function launchPgAdminWindow() {
// https://github.com/nwjs/nw.js/issues/7973
pgadminWindow.on('close', function () {
// Resize Window
resizeHeightBy = pgadminWindow.window.outerHeight - pgadminWindow.window.innerHeight;
let resizeHeightBy = pgadminWindow.window.outerHeight - pgadminWindow.window.innerHeight;
pgadminWindow.resizeBy(0, -resizeHeightBy);
// Remove 'close' event handler, and then close window
pgadminWindow.removeAllListeners('close');
@ -554,7 +554,7 @@ function getSubMenu(menuItem) {
if (sub.submenu?.items?.length) {
sub.submenu.items.forEach((m) => {
if (m.type == 'checkbox') {
m.label == item.label ? m.checked = true : m.checked = false;
m.checked = m.label == item.label;
}
});
}

View File

@ -10,7 +10,7 @@
import * as Node from 'pgbrowser/node';
import * as SchemaTreeNode from './schema_child_tree_node';
let SchemaChildNode = Node.extend({
const SchemaChildNode = Node.extend({
parent_type: ['schema', 'catalog'],
canDrop: SchemaTreeNode.isTreeItemOfChildOfSchema,
canDropCascade: SchemaTreeNode.isTreeItemOfChildOfSchema,

View File

@ -587,7 +587,7 @@ export default class ColumnSchema extends BaseUISchema {
}
validate(state, setError) {
let msg = undefined;
let msg;
if (!_.isUndefined(state.cltype) && !isEmptyString(state.attlen)) {
// Validation for Length field

View File

@ -527,7 +527,7 @@ def parse_rule_definition(res):
condition_part = condition_part_match.group(1)
condition_match = re.search(
r"(?:WHERE)\s+(\([\s\S]*\))\s+(?:DO)", condition_part)
r"(?:WHERE)?\s+(\([\s\S]*\))\s+(?:DO)?", condition_part)
if condition_match is not None:
condition = condition_match.group(1)
@ -537,7 +537,7 @@ def parse_rule_definition(res):
# Parse data for statements
statement_match = re.search(
r"(?:DO\s+)(?:INSTEAD\s+)?([\s\S]*)(?:;)", data_def)
r"(?:DO\s+)?(?:INSTEAD\s+)?([\s\S]*)(?:;)?", data_def)
statement = ''
if statement_match is not None:

View File

@ -106,14 +106,14 @@ const DiskStatsTable = (props) => {
<Table classNameRoot='Storage-tableWhiteSpace'>
<thead>
<tr>
{tableHeader.map((item, index) => (
<th key={index}>{item.header}</th>
{tableHeader.map((item) => (
<th key={item.header}>{item.header}</th>
))}
</tr>
</thead>
<tbody>
{data.map((item, index) => (
<tr key={index}>
{data.map((item) => (
<tr key={item.file_system_type + item.mount_point}>
{tableHeader.map((header, id) => (
<td key={header.accessorKey+'-'+id}>{item[header.accessorKey]}</td>
))}

View File

@ -54,8 +54,8 @@ const SummaryTable = (props) => {
</tr>
</thead>
<tbody>
{data.map((item, index) => (
<tr key={index}>
{data.map((item) => (
<tr key={item.name}>
<td>{item.name}</td>
<td>{item.value}</td>
</tr>

View File

@ -248,8 +248,7 @@ export function getAWSSummary(cloud, cloudInstanceDetails, cloudDBDetails) {
}
const getStorageType = (cloudInstanceDetails) => {
let _storage_type = 'General Purpose SSD (gp2)',
_io1 = undefined;
let _storage_type = 'General Purpose SSD (gp2)', _io1;
if(cloudInstanceDetails.storage_type == 'gp2'){ _storage_type = 'General Purpose SSD (gp2)';}
else if (cloudInstanceDetails.storage_type == 'gp3'){ _storage_type = 'General Purpose SSD (gp3)';}

View File

@ -273,7 +273,7 @@ export function checkClusternameAvailbility(clusterName){
resolve(res.data);
}
}).catch((error) => {
reject(gettext(`Error while checking server name availability with Microsoft Azure: ${error.response.data.errormsg}`));
reject(new Error(gettext(`Error while checking server name availability with Microsoft Azure: ${error.response.data.errormsg}`)));
});
});
}

View File

@ -717,7 +717,7 @@ class AzureClusterSchema extends BaseUISchema {
}
validate(data, setErr) {
if ( !isEmptyString(data.name) && (!/^[a-z0-9\-]*$/.test(data.name) || data.name.length < 3)) {
if ( !isEmptyString(data.name) && (!/^[a-z0-9-]*$/.test(data.name) || data.name.length < 3)) {
setErr('name',gettext('Name must be more than 2 characters and must only contain lowercase letters, numbers, and hyphens'));
return true;
}

View File

@ -8,14 +8,13 @@
//////////////////////////////////////////////////////////////
import React from 'react';
import { ToggleButton, ToggleButtonGroup } from '@mui/material';
import { ToggleButton, ToggleButtonGroup, TableBody, TableCell, TableHead, TableRow } from '@mui/material';
import CheckRoundedIcon from '@mui/icons-material/CheckRounded';
import { DefaultButton, PrimaryButton } from '../../../../static/js/components/Buttons';
import PropTypes from 'prop-types';
import { getAWSSummary } from './aws';
import {getAzureSummary} from './azure';
import { getBigAnimalSummary } from './biganimal';
import { TableBody, TableCell, TableHead, TableRow } from '@mui/material';
import gettext from 'sources/gettext';
import { getGoogleSummary } from './google';
import { CLOUD_PROVIDERS_LABELS } from './cloud_constants';

View File

@ -158,9 +158,9 @@ function Multitext({currentXpos, currentYpos, label, maxWidth}) {
return res;
};
for (let i = 0; i < words.length; i++) {
for (let word in words) {
let tmpArr = splitTextInMultiLine(
currLine, widthSoFar, words[i]
currLine, widthSoFar, word
);
if (currLine) {

View File

@ -601,7 +601,7 @@ export default function DataGridView({
<PgReactTableBody>
{rows.map((row, i) => {
return <PgReactTableRow key={row.index}>
<DataTableRow index={i} key={i} row={row} totalRows={rows.length} isResizing={isResizing}
<DataTableRow index={i} row={row} totalRows={rows.length} isResizing={isResizing}
schema={schemaRef.current} schemaRef={schemaRef} accessPath={accessPath.concat([row.index])}
moveRow={moveRow} isHovered={i == hoverIndex} setHoverIndex={setHoverIndex} viewHelperProps={viewHelperProps}
/>

View File

@ -103,14 +103,11 @@ function isValueEqual(val1, val2) {
/* If the orig value was null and new one is empty string, then its a "no change" */
/* If the orig value and new value are of different datatype but of same value(numeric) "no change" */
/* If the orig value is undefined or null and new value is boolean false "no change" */
if (_.isEqual(val1, val2)
return (_.isEqual(val1, val2)
|| ((val1 === null || _.isUndefined(val1)) && val2 === '')
|| ((val1 === null || _.isUndefined(val1)) && typeof(val2) === 'boolean' && !val2)
|| (attrDefined ? (!_.isObject(val1) && _.isEqual(val1.toString(), val2.toString())) : false)
) {
return true;
}
return false;
);
}
/* Compare two objects */
@ -144,7 +141,7 @@ function getChangedData(topSchema, viewHelperProps, sessData, stringify=false, i
if(_.isNull(change) && parseChanges.depth === 0) {
change = '';
}
return levelChanges[id] = change;
levelChanges[id] = change;
}
};

View File

@ -171,7 +171,7 @@ export const PgReactTableCell = forwardRef(({row, cell, children, className}, re
flex: `var(--col-${cell.column.id.replace(/\W/g, '_')}-size) 0 auto`,
width: `calc(var(--col-${cell.column.id.replace(/\W/g, '_')}-size)*1px)`,
...(cell.column.columnDef.maxSize ? { maxWidth: `${cell.column.columnDef.maxSize}px` } : {})
}} role='cell'
}}
className={classNames.join(' ')}
title={String(cell.getValue() ?? '')}>
<div className='pgrd-row-cell-content'>{children}</div>
@ -189,7 +189,7 @@ PgReactTableCell.propTypes = {
export const PgReactTableRow = forwardRef(({ children, className, ...props }, ref)=>{
return (
<div className={['pgrt-row', className].join(' ')} ref={ref} role="row" {...props}>
<div className={['pgrt-row', className].join(' ')} ref={ref} {...props}>
{children}
</div>
);
@ -232,8 +232,8 @@ PgReactTableRowExpandContent.propTypes = {
export function PgReactTableHeader({table}) {
return (
<div className='pgrt-header'>
{table.getHeaderGroups().map((headerGroup, idx) => (
<div key={idx} className='pgrt-header-row' style={{ }}>
{table.getHeaderGroups().map((headerGroup) => (
<div key={headerGroup.id} className='pgrt-header-row' style={{ }}>
{headerGroup.headers.map((header) => (
<div
key={header.id}

View File

@ -11,7 +11,7 @@ let getWindowOpener = (opener) => {
return opener.opener?.pgAdmin ? getWindowOpener(opener.opener) : opener;
};
let pgWindow = function() {
const pgWindow = function() {
let localPgWindow = null;
try {
if(window.opener?.pgAdmin) {

View File

@ -103,7 +103,7 @@ class BackupMessage(IProcessDesc):
return ''
for arg in _args:
if arg and len(arg) >= 2 and arg[:2] == '--':
if arg and len(arg) >= 2 and arg.startswith('--'):
self.cmd += ' ' + arg
else:
self.cmd += cmd_arg(arg)

View File

@ -269,7 +269,7 @@ export default class DebuggerModule {
}
getUrl(_d, newTreeInfo, trans_id) {
let baseUrl = undefined;
let baseUrl;
if (_d._type == 'function' || _d._type == 'edbfunc') {
baseUrl = url_for(
'debugger.initialize_target_for_function', {

View File

@ -24,7 +24,7 @@ class EmptySchema extends BaseUISchema {
}
export function getTableDialogSchema(attributes, isNew, tableNodesDict, colTypes, schemas) {
let treeNodeInfo = undefined;
let treeNodeInfo;
let columnSchema = new ColumnSchema(
()=>{/*This is intentional (SonarQube)*/},

View File

@ -80,7 +80,7 @@ class IEMessage(IProcessDesc):
replace_next = False
for arg in _args:
if arg and len(arg) >= 2 and arg[:2] == '--':
if arg and len(arg) >= 2 and arg.startswith('--'):
if arg == '--command':
replace_next = True
self._cmd += ' ' + arg

View File

@ -72,7 +72,7 @@ class RestoreMessage(IProcessDesc):
return ''
for arg in _args:
if arg and len(arg) >= 2 and arg[:2] == '--':
if arg and len(arg) >= 2 and arg.startswith('--'):
self.cmd += ' ' + arg
else:
self.cmd += cmd_arg(arg)

View File

@ -758,7 +758,7 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
);
});
if(existIdx > -1) {
reject(gettext('Connection with this configuration already present.'));
reject(new Error(gettext('Connection with this configuration already present.')));
return;
}
updateQueryToolConnection(connectionData, true)

View File

@ -189,7 +189,7 @@ export default function MacrosDialog({onClose, onSave}) {
formType={'dialog'}
getInitData={()=>{
if(macrosErr) {
return Promise.reject(macrosErr);
return Promise.reject(new Error(macrosErr));
}
return Promise.resolve({macro: userMacrosData.filter((m)=>Boolean(m.name))});
}}

View File

@ -14,7 +14,7 @@ import gettext from 'sources/gettext';
import PropTypes from 'prop-types';
import url_for from 'sources/url_for';
import Loader from 'sources/components/Loader';
import { Box } from '@mui/material';
import { Box, useTheme } from '@mui/material';
import ShowChartRoundedIcon from '@mui/icons-material/ShowChartRounded';
import ZoomOutMapIcon from '@mui/icons-material/ZoomOutMap';
import SaveAltIcon from '@mui/icons-material/SaveAlt';
@ -26,7 +26,6 @@ import { LineChart, BarChart, PieChart, DATA_POINT_STYLE, DATA_POINT_SIZE,
LightenDarkenColor} from 'sources/chartjs';
import { QueryToolEventsContext, QueryToolContext } from '../QueryToolComponent';
import { QUERY_TOOL_EVENTS, PANELS } from '../QueryToolConstants';
import { useTheme } from '@mui/material';
import { getChartColor } from '../../../../../../static/js/utils';
const StyledBox = styled(Box)(({theme}) => ({

View File

@ -166,10 +166,10 @@ class UserManagementCollection extends BaseUISchema {
if (state.auth_source != AUTH_METHODS['INTERNAL']) {
if (obj.isNew(state) && obj.top?._sessData?.userManagement) {
for (let i=0; i < obj.top._sessData.userManagement.length; i++) {
if (obj.top._sessData.userManagement[i]?.id &&
obj.top._sessData.userManagement[i].username.toLowerCase() == state.username.toLowerCase() &&
obj.top._sessData.userManagement[i].auth_source == state.auth_source) {
for (let user in obj.top._sessData.userManagement) {
if (user?.id &&
user.username.toLowerCase() == state.username.toLowerCase() &&
user.auth_source == state.auth_source) {
msg = gettext('User name \'%s\' already exists', state.username);
setError('username', msg);
return true;
@ -179,7 +179,7 @@ class UserManagementCollection extends BaseUISchema {
}
if (state.auth_source == AUTH_METHODS['INTERNAL']) {
let email_filter = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
let email_filter = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (isEmptyString(state.email)) {
msg = gettext('Email cannot be empty');
setError('email', msg);
@ -193,9 +193,9 @@ class UserManagementCollection extends BaseUISchema {
}
if (obj.isNew(state) && obj.top?._sessData?.userManagement) {
for (let i=0; i < obj.top._sessData.userManagement.length; i++) {
if (obj.top._sessData.userManagement[i]?.id &&
obj.top._sessData.userManagement[i].email?.toLowerCase() == state.email?.toLowerCase()) {
for (let user in obj.top._sessData.userManagement) {
if (user?.id &&
user.email?.toLowerCase() == state.email?.toLowerCase()) {
msg = gettext('Email address \'%s\' already exists', state.email);
setError('email', msg);
return true;

View File

@ -336,7 +336,7 @@ class Driver(BaseDriver):
return True
# certain types should not be quoted even though it contains a space.
# Evilness.
elif for_types and value[-2:] == "[]":
elif for_types and value.endswith('[]'):
val_noarray = value[:-2]
if for_types and val_noarray.lower() in [

View File

@ -7,6 +7,6 @@
//
//////////////////////////////////////////////////////////////////////////
let treeMenu = null;
const treeMenu = null;
export {treeMenu};

View File

@ -95,6 +95,6 @@ export const addNewDatagridRow = async (user, ctrl)=>{
await user.click(ctrl.container.querySelector('[data-test="add-row"] button'));
};
export let genericBeforeEach = ()=> {
export const genericBeforeEach = ()=> {
pgWindow.pgAdmin = pgAdmin;
};