Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Migrate to all newest packages, except for eslint v9 (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz authored Apr 25, 2024
1 parent f5b7564 commit 67ca4c5
Show file tree
Hide file tree
Showing 45 changed files with 3,598 additions and 2,434 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ build_task:
<<: *CONTAINER_DEFINITION
docker_arguments:
CIRRUS_AWS_ACCOUNT: ${CIRRUS_AWS_ACCOUNT}
cpu: 3
cpu: 1
memory: 8G
env:
SONARSOURCE_QA: true
Expand Down
5,599 changes: 3,347 additions & 2,252 deletions package-lock.json

Large diffs are not rendered by default.

57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-sonarjs",
"version": "0.25.1",
"version": "0.26.0",
"description": "SonarJS rules for ESLint",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -36,36 +36,37 @@
"eslint": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
},
"devDependencies": {
"@babel/core": "7.23.2",
"@babel/eslint-parser": "7.22.5",
"@babel/plugin-proposal-export-default-from": "7.18.9",
"@babel/plugin-proposal-function-bind": "7.18.9",
"@babel/preset-env": "7.23.2",
"@babel/preset-flow": "7.22.15",
"@babel/preset-react": "7.22.15",
"@types/eslint": "8.40.0",
"@types/jest": "29.5.2",
"@types/lodash": "4.14.182",
"@types/minimist": "1.2.2",
"@types/node": "14.14.31",
"@typescript-eslint/parser": "5.30.7",
"@typescript-eslint/utils": "5.30.7",
"eslint": "8.47.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"@babel/core": "7.24.4",
"@babel/eslint-parser": "7.24.1",
"@babel/plugin-proposal-export-default-from": "7.24.1",
"@babel/plugin-proposal-function-bind": "7.24.1",
"@babel/preset-env": "7.24.4",
"@babel/preset-flow": "7.24.1",
"@babel/preset-react": "7.24.1",
"@types/eslint": "8.56.10",
"@types/jest": "29.5.12",
"@types/lodash": "4.17.0",
"@types/minimist": "1.2.5",
"@types/node": "20.12.7",
"@typescript-eslint/parser": "7.7.1",
"@typescript-eslint/rule-tester": "7.7.1",
"@typescript-eslint/utils": "7.7.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-notice": "0.9.10",
"eslint-plugin-sonarjs": "0.23.0",
"husky": "8.0.3",
"jest": "29.5.0",
"eslint-plugin-sonarjs": "0.25.1",
"husky": "9.0.11",
"jest": "29.7.0",
"jest-sonar-reporter": "2.0.0",
"lodash": "4.17.21",
"minimist": "1.2.6",
"prettier": "2.7.1",
"pretty-quick": "3.1.3",
"rimraf": "3.0.2",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "4.7.4"
"minimist": "1.2.8",
"prettier": "3.2.5",
"pretty-quick": "4.0.0",
"rimraf": "5.0.5",
"ts-jest": "29.1.2",
"ts-node": "10.9.2",
"typescript": "5.4.5"
},
"prettier": {
"printWidth": 100,
Expand Down
4 changes: 2 additions & 2 deletions ruling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import * as fs from 'fs';
import * as path from 'path';
import { ESLint } from 'eslint';
import * as lodash from 'lodash';
import * as minimist from 'minimist';
import lodash from 'lodash';
import minimist from 'minimist';

const rulesPath = path.join(__dirname, '../lib/rules');

Expand Down
2 changes: 1 addition & 1 deletion scripts/test-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -euo pipefail
# variable is set in dockerfile
if [ "${SONARCLOUD_ANALYSIS:-}" == "true" ]; then
echo 'Running tests with coverage and reporter'
npm run test -- --maxWorkers=50% --coverage --testResultsProcessor jest-sonar-reporter
npm run test -- --runInBand --coverage --testResultsProcessor jest-sonar-reporter
else
echo 'Running tests'
npm run test
Expand Down
23 changes: 20 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import type { TSESLint } from '@typescript-eslint/utils';
import type { FlatConfig } from '@typescript-eslint/utils/ts-eslint';

const sonarjsRules: string[] = [
'cognitive-complexity',
Expand Down Expand Up @@ -56,8 +57,17 @@ const sonarjsRules: string[] = [

const sonarjsRuleModules: { [key: string]: any } = {};

const configs: { recommended: TSESLint.Linter.Config & { plugins: string[] } } = {
recommended: { plugins: ['sonarjs'], rules: {} },
const plugin = {
configs: {},
rules: {},
};

const recommendedLegacyConfig: TSESLint.Linter.Config = { plugins: ['sonarjs'], rules: {} };
const recommendedConfig: FlatConfig.Config = {
plugins: {
sonarjs: plugin,
},
rules: {},
};

sonarjsRules.forEach(rule => {
Expand All @@ -67,7 +77,14 @@ sonarjsRules.forEach(rule => {
docs: { recommended },
},
} = sonarjsRuleModules[rule];
configs.recommended.rules![`sonarjs/${rule}`] = recommended === false ? 'off' : recommended;
recommendedConfig.rules![`sonarjs/${rule}`] = recommended === undefined ? 'off' : 'error';
});
recommendedLegacyConfig.rules = recommendedConfig.rules;

const configs = {
recommended: recommendedConfig,
'recommended-legacy': recommendedLegacyConfig,
};
plugin.configs = configs;

export { sonarjsRuleModules as rules, configs };
5 changes: 3 additions & 2 deletions src/rules/cognitive-complexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const message =
'Refactor this function to reduce its Cognitive Complexity from {{complexityAmount}} to the {{threshold}} allowed.';

const rule: TSESLint.RuleModule<string, (number | 'metric' | 'sonar-runtime')[]> = {
defaultOptions: [],
meta: {
messages: {
refactorFunction: message,
Expand All @@ -61,13 +62,13 @@ const rule: TSESLint.RuleModule<string, (number | 'metric' | 'sonar-runtime')[]>
type: 'suggestion',
docs: {
description: 'Cognitive Complexity of functions should not be too high',
recommended: 'error',
url: docsUrl(__filename),
},
schema: [
{ type: 'integer', minimum: 0 },
{
// internal parameter
type: 'string',
enum: ['sonar-runtime', 'metric'],
},
],
Expand Down Expand Up @@ -274,7 +275,7 @@ const rule: TSESLint.RuleModule<string, (number | 'metric' | 'sonar-runtime')[]>
return (
operators.includes(operator) &&
literals.includes(right.type) &&
context.getSourceCode().getText(parent.left) === context.getSourceCode().getText(left)
context.sourceCode.getText(parent.left) === context.sourceCode.getText(left)
);
default:
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/rules/elseif-without-else.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { TSESTree, TSESLint } from '@typescript-eslint/utils';
import docsUrl from '../utils/docs-url';

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
addMissingElseClause: 'Add the missing "else" clause.',
Expand All @@ -31,7 +32,6 @@ const rule: TSESLint.RuleModule<string, string[]> = {
type: 'suggestion',
docs: {
description: '"if ... else if" constructs should end with "else" clauses',
recommended: false,
url: docsUrl(__filename),
},
},
Expand All @@ -40,7 +40,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
IfStatement: (node: TSESTree.Node) => {
const ifstmt = node as TSESTree.IfStatement;
if (isElseIf(ifstmt) && !ifstmt.alternate) {
const sourceCode = context.getSourceCode();
const { sourceCode } = context;
const elseKeyword = sourceCode.getTokenBefore(
node,
token => token.type === 'Keyword' && token.value === 'else',
Expand Down
5 changes: 3 additions & 2 deletions src/rules/max-switch-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let maxSwitchCases = DEFAULT_MAX_SWITCH_CASES;
type Options = [number];

const rule: TSESLint.RuleModule<string, Options> = {
defaultOptions: [DEFAULT_MAX_SWITCH_CASES],
meta: {
messages: {
reduceNumberOfNonEmptySwitchCases:
Expand All @@ -36,7 +37,7 @@ const rule: TSESLint.RuleModule<string, Options> = {
type: 'suggestion',
docs: {
description: '"switch" statements should not have too many "case" clauses',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
Expand Down Expand Up @@ -65,7 +66,7 @@ function visitSwitchStatement(
switchCase => switchCase.consequent.length > 0 && !isDefaultCase(switchCase),
);
if (nonEmptyCases.length > maxSwitchCases) {
const switchKeyword = context.getSourceCode().getFirstToken(switchStatement)!;
const switchKeyword = context.sourceCode.getFirstToken(switchStatement)!;
context.report({
messageId: 'reduceNumberOfNonEmptySwitchCases',
loc: switchKeyword.loc,
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-all-duplicated-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { collectIfBranches, collectSwitchBranches } from '../utils/conditions';
import docsUrl from '../utils/docs-url';

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
removeOrEditConditionalStructure:
Expand All @@ -38,7 +39,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
docs: {
description:
'All branches in a conditional structure should not have exactly the same implementation',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
},
Expand Down Expand Up @@ -77,7 +78,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
return (
branches.length > 1 &&
branches.slice(1).every((branch, index) => {
return areEquivalent(branch, branches[index], context.getSourceCode());
return areEquivalent(branch, branches[index], context.sourceCode);
})
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-collapsible-if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import docsUrl from '../utils/docs-url';
const message = 'Merge this if statement with the nested one.';

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
mergeNestedIfStatement: message,
Expand All @@ -35,12 +36,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
type: 'suggestion',
docs: {
description: 'Collapsible "if" statements should be merged',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
{
// internal parameter
type: 'string',
enum: ['sonar-runtime'],
},
],
Expand All @@ -53,8 +55,8 @@ const rule: TSESLint.RuleModule<string, string[]> = {
consequent = consequent.body[0];
}
if (isIfStatementWithoutElse(node) && isIfStatementWithoutElse(consequent)) {
const ifKeyword = context.getSourceCode().getFirstToken(consequent);
const enclosingIfKeyword = context.getSourceCode().getFirstToken(node);
const ifKeyword = context.sourceCode.getFirstToken(consequent);
const enclosingIfKeyword = context.sourceCode.getFirstToken(node);
if (ifKeyword && enclosingIfKeyword) {
report(
context,
Expand Down
20 changes: 10 additions & 10 deletions src/rules/no-collection-size-mischeck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
// https://sonarsource.github.io/rspec/#/rspec/S3981

import type { TSESTree, TSESLint } from '@typescript-eslint/utils';
import { isRequiredParserServices, RequiredParserServices } from '../utils/parser-services';
import { ParserServicesWithTypeInformation } from '@typescript-eslint/typescript-estree';
import { isParserServicesWithTypeInformation } from '../utils/parser-services';
import docsUrl from '../utils/docs-url';

const CollectionLike = ['Array', 'Map', 'Set', 'WeakMap', 'WeakSet'];
const CollectionSizeLike = ['length', 'size'];

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
fixCollectionSizeCheck:
Expand All @@ -38,13 +40,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
hasSuggestions: true,
docs: {
description: 'Collection sizes and array length comparisons should make sense',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
},
create(context) {
const services = context.parserServices;
const isTypeCheckerAvailable = isRequiredParserServices(services);
const services = context.sourceCode.parserServices;
const isTypeCheckerAvailable = isParserServicesWithTypeInformation(services);
return {
BinaryExpression: (node: TSESTree.Node) => {
const expr = node as TSESTree.BinaryExpression;
Expand All @@ -56,13 +58,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
if (
property.type === 'Identifier' &&
CollectionSizeLike.includes(property.name) &&
(!isTypeCheckerAvailable || isCollection(object, services!))
(!isTypeCheckerAvailable || isCollection(object, services))
) {
context.report({
messageId: 'fixCollectionSizeCheck',
data: {
propertyName: property.name,
objectName: context.getSourceCode().getText(object),
objectName: context.sourceCode.getText(object),
},
node,
suggest: getSuggestion(expr, property.name, context),
Expand All @@ -79,7 +81,7 @@ function isZeroLiteral(node: TSESTree.Node) {
return node.type === 'Literal' && node.value === 0;
}

function isCollection(node: TSESTree.Node, services: RequiredParserServices) {
function isCollection(node: TSESTree.Node, services: ParserServicesWithTypeInformation) {
const checker = services.program.getTypeChecker();
const tp = checker.getTypeAtLocation(services.esTreeNodeToTSNodeMap.get(node));
return !!tp.symbol && CollectionLike.includes(tp.symbol.name);
Expand All @@ -91,9 +93,7 @@ function getSuggestion(
context: TSESLint.RuleContext<string, string[]>,
): TSESLint.ReportSuggestionArray<string> {
const { left, operator } = expr;
const operatorToken = context
.getSourceCode()
.getTokenAfter(left, token => token.value === operator)!;
const operatorToken = context.sourceCode.getTokenAfter(left, token => token.value === operator)!;
const fixedOperator = operator === '<' ? '==' : '>';
return [
{
Expand Down
Loading

0 comments on commit 67ca4c5

Please sign in to comment.