Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to all newest packages, except for eslint v9 #460

Merged
merged 10 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,599 changes: 3,347 additions & 2,252 deletions package-lock.json
ericmorand-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't land this version pull after the last release (#455)

"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
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 };
3 changes: 2 additions & 1 deletion 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',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omitting this value, implies it's not recommended to be turned on. And also, based on this value, we compile our plugin.

url: docsUrl(__filename),
},
schema: [
{ type: 'integer', minimum: 0 },
{
// internal parameter
type: 'string',
enum: ['sonar-runtime', 'metric'],
},
],
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
3 changes: 2 additions & 1 deletion 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
3 changes: 2 additions & 1 deletion 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
4 changes: 3 additions & 1 deletion 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 Down
12 changes: 7 additions & 5 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 isTypeCheckerAvailable = isParserServicesWithTypeInformation(services);
return {
BinaryExpression: (node: TSESTree.Node) => {
const expr = node as TSESTree.BinaryExpression;
Expand All @@ -56,7 +58,7 @@ 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',
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 Down
13 changes: 11 additions & 2 deletions src/rules/no-duplicate-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type Options =
type Context = TSESLint.RuleContext<string, Options>;

const rule: TSESLint.RuleModule<string, Options> = {
defaultOptions: [
{
threshold: DEFAULT_THRESHOLD,
ignoreStrings: DEFAULT_IGNORE_STRINGS,
},
],
meta: {
messages: {
defineConstant: message,
Expand All @@ -51,7 +57,7 @@ const rule: TSESLint.RuleModule<string, Options> = {
type: 'suggestion',
docs: {
description: 'String literals should not be duplicated',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
Expand All @@ -62,7 +68,10 @@ const rule: TSESLint.RuleModule<string, Options> = {
ignoreStrings: { type: 'string', default: DEFAULT_IGNORE_STRINGS },
},
},
{ enum: ['sonar-runtime'] /* internal parameter for rules having secondary locations */ },
{
type: 'string',
enum: ['sonar-runtime'] /* internal parameter for rules having secondary locations */,
},
],
},

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-duplicated-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const message =
"This {{type}}'s code block is the same as the block for the {{type}} on line {{line}}.";

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
sameConditionalBlock: message,
Expand All @@ -39,12 +40,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
docs: {
description:
'Two branches in a conditional structure should not have exactly the same implementation',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
{
// internal parameter
type: 'string',
enum: ['sonar-runtime'],
},
],
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-element-overwrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const message =
'Verify this is the index that was intended; "{{index}}" was already set on line {{line}}.';

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
verifyIntendedIndex: message,
Expand All @@ -44,12 +45,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
type: 'problem',
docs: {
description: 'Collection elements should not be replaced unconditionally',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
{
// internal parameter
type: 'string',
enum: ['sonar-runtime'],
},
],
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-empty-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const strictlyReadingMethods = new Set([
]);

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
reviewUsageOfIdentifier:
Expand All @@ -89,7 +90,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
type: 'problem',
docs: {
description: 'Empty collections should not be accessed or iterated',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-extra-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import docsUrl from '../utils/docs-url';
const message = 'This function expects {{expectedArguments}}, but {{providedArguments}} provided.';

const rule: TSESLint.RuleModule<string, string[]> = {
defaultOptions: [],
meta: {
messages: {
tooManyArguments: message,
Expand All @@ -47,12 +48,13 @@ const rule: TSESLint.RuleModule<string, string[]> = {
type: 'problem',
docs: {
description: 'Function calls should not pass extra arguments',
recommended: 'error',
recommended: 'recommended',
url: docsUrl(__filename),
},
schema: [
{
// internal parameter
type: 'string',
enum: ['sonar-runtime'],
},
],
Expand Down
Loading