From ab2b72b4089c56dcdc123e4301be0c5c1a36f0d3 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Fri, 18 Feb 2022 13:21:42 +0100 Subject: [PATCH 1/4] Upgrade eslint-plugin-sonarjs to 0.12.0 --- eslint-bridge/package-lock.json | 14 +++++++------- eslint-bridge/package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/eslint-bridge/package-lock.json b/eslint-bridge/package-lock.json index e94c5e98895..f2f014cdbfb 100644 --- a/eslint-bridge/package-lock.json +++ b/eslint-bridge/package-lock.json @@ -44,7 +44,7 @@ "bytes": "3.1.0", "eslint": "7.32.0", "eslint-plugin-chai-friendly": "0.7.2", - "eslint-plugin-sonarjs": "0.12.0-504", + "eslint-plugin-sonarjs": "0.12.0", "express": "4.17.1", "functional-red-black-tree": "1.0.1", "regexpp": "3.2.0", @@ -4234,9 +4234,9 @@ } }, "node_modules/eslint-plugin-sonarjs": { - "version": "0.12.0-504", - "resolved": "https://repox.jfrog.io/repox/api/npm/npm/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0-504.tgz", - "integrity": "sha1-tS8zNWY3sTkaLwqhBEGuX6OriD4=", + "version": "0.12.0", + "resolved": "https://repox.jfrog.io/repox/api/npm/npm/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0.tgz", + "integrity": "sha1-A6FgpY8Xn94IQTW67Ahla1l4a4w=", "inBundle": true, "license": "LGPL-3.0", "engines": { @@ -13166,9 +13166,9 @@ "requires": {} }, "eslint-plugin-sonarjs": { - "version": "0.12.0-504", - "resolved": "https://repox.jfrog.io/repox/api/npm/npm/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0-504.tgz", - "integrity": "sha1-tS8zNWY3sTkaLwqhBEGuX6OriD4=", + "version": "0.12.0", + "resolved": "https://repox.jfrog.io/repox/api/npm/npm/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0.tgz", + "integrity": "sha1-A6FgpY8Xn94IQTW67Ahla1l4a4w=", "requires": {} }, "eslint-scope": { diff --git a/eslint-bridge/package.json b/eslint-bridge/package.json index cfa22acab58..f99484e85ca 100644 --- a/eslint-bridge/package.json +++ b/eslint-bridge/package.json @@ -55,7 +55,7 @@ "bytes": "3.1.0", "eslint": "7.32.0", "eslint-plugin-chai-friendly": "0.7.2", - "eslint-plugin-sonarjs": "0.12.0-504", + "eslint-plugin-sonarjs": "0.12.0", "express": "4.17.1", "functional-red-black-tree": "1.0.1", "regexpp": "3.2.0", From e2a2de47945779bbcce682ce0912b2bab97f851f Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Fri, 18 Feb 2022 13:22:50 +0100 Subject: [PATCH 2/4] Update S4144 ('no-identical-functions'): Support configurable minimum function size --- .../checks/IdenticalFunctionsCheck.java | 17 +++++++++ .../checks/IdenticalFunctionsCheckTest.java | 36 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java diff --git a/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java b/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java index 47c4293256f..24951b8193f 100644 --- a/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java +++ b/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java @@ -19,7 +19,11 @@ */ package org.sonar.javascript.checks; +import java.util.Collections; +import java.util.List; + import org.sonar.check.Rule; +import org.sonar.check.RuleProperty; import org.sonar.plugins.javascript.api.EslintBasedCheck; import org.sonar.plugins.javascript.api.JavaScriptRule; import org.sonar.plugins.javascript.api.TypeScriptRule; @@ -29,6 +33,19 @@ @Rule(key = "S4144") public class IdenticalFunctionsCheck implements EslintBasedCheck { + private static final int DEFAULT_THRESHOLD = 3; + + @RuleProperty( + key = "Threshold", + description = "The minimum number of lines to trigger an issue.", + defaultValue = "" + DEFAULT_THRESHOLD) + int threshold = DEFAULT_THRESHOLD; + + @Override + public List configurations() { + return Collections.singletonList(threshold); + } + @Override public String eslintKey() { return "no-identical-functions"; diff --git a/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java b/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java new file mode 100644 index 00000000000..d365d9f50d1 --- /dev/null +++ b/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java @@ -0,0 +1,36 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2022 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 GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * 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 GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.javascript.checks; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class IdenticalFunctionsCheckTest { + + @Test + void configurations() { + IdenticalFunctionsCheck identicalFunctionsCheck = new IdenticalFunctionsCheck(); + // default configuration + assertThat(identicalFunctionsCheck.configurations()).containsExactly(3); + identicalFunctionsCheck.threshold = 10; + assertThat(identicalFunctionsCheck.configurations()).containsExactly(10); + } +} From 48d08c6e5bdadeca6b50e58f054bdf70c0dfcd04 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Fri, 18 Feb 2022 13:51:59 +0100 Subject: [PATCH 3/4] Fix after review --- .../sonar/javascript/checks/IdenticalFunctionsCheck.java | 9 +-------- .../javascript/checks/IdenticalFunctionsCheckTest.java | 3 --- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java b/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java index 24951b8193f..bc3ceb96caf 100644 --- a/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java +++ b/javascript-checks/src/main/java/org/sonar/javascript/checks/IdenticalFunctionsCheck.java @@ -23,7 +23,6 @@ import java.util.List; import org.sonar.check.Rule; -import org.sonar.check.RuleProperty; import org.sonar.plugins.javascript.api.EslintBasedCheck; import org.sonar.plugins.javascript.api.JavaScriptRule; import org.sonar.plugins.javascript.api.TypeScriptRule; @@ -35,15 +34,9 @@ public class IdenticalFunctionsCheck implements EslintBasedCheck { private static final int DEFAULT_THRESHOLD = 3; - @RuleProperty( - key = "Threshold", - description = "The minimum number of lines to trigger an issue.", - defaultValue = "" + DEFAULT_THRESHOLD) - int threshold = DEFAULT_THRESHOLD; - @Override public List configurations() { - return Collections.singletonList(threshold); + return Collections.singletonList(DEFAULT_THRESHOLD); } @Override diff --git a/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java b/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java index d365d9f50d1..843a6e04ad5 100644 --- a/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java +++ b/javascript-checks/src/test/java/org/sonar/javascript/checks/IdenticalFunctionsCheckTest.java @@ -28,9 +28,6 @@ class IdenticalFunctionsCheckTest { @Test void configurations() { IdenticalFunctionsCheck identicalFunctionsCheck = new IdenticalFunctionsCheck(); - // default configuration assertThat(identicalFunctionsCheck.configurations()).containsExactly(3); - identicalFunctionsCheck.threshold = 10; - assertThat(identicalFunctionsCheck.configurations()).containsExactly(10); } } From 1e967d5c0c85c5e31c66b3c77ee9d5117e8f57fc Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Fri, 18 Feb 2022 14:20:44 +0100 Subject: [PATCH 4/4] Update ruling expectations --- .../js/angular.js/javascript-S1126.json | 9 ++++++ .../javascript-S1126.json | 15 ++++++++++ .../expected/js/jshint/javascript-S1126.json | 9 ++++++ .../expected/js/p5.js/javascript-S1126.json | 1 + .../expected/js/p5.js/javascript-S4624.json | 6 ---- .../js/prototype/javascript-S1126.json | 6 ++++ .../expected/js/qunit/javascript-S1126.json | 5 ++++ .../expected/ts/console/typescript-S4624.json | 4 --- .../ts/postgraphql/typescript-S4624.json | 28 ------------------- 9 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 its/ruling/src/test/expected/js/angular.js/javascript-S1126.json create mode 100644 its/ruling/src/test/expected/js/jshint/javascript-S1126.json delete mode 100644 its/ruling/src/test/expected/js/p5.js/javascript-S4624.json create mode 100644 its/ruling/src/test/expected/js/prototype/javascript-S1126.json create mode 100644 its/ruling/src/test/expected/js/qunit/javascript-S1126.json diff --git a/its/ruling/src/test/expected/js/angular.js/javascript-S1126.json b/its/ruling/src/test/expected/js/angular.js/javascript-S1126.json new file mode 100644 index 00000000000..e5ff0f27368 --- /dev/null +++ b/its/ruling/src/test/expected/js/angular.js/javascript-S1126.json @@ -0,0 +1,9 @@ +{ +'angular.js:src/ngMock/angular-mocks.js':[ +2115, +], +'angular.js:src/ngParseExt/ucd.js':[ +561, +1215, +], +} diff --git a/its/ruling/src/test/expected/js/javascript-test-sources/javascript-S1126.json b/its/ruling/src/test/expected/js/javascript-test-sources/javascript-S1126.json index 9b8c30d1d0d..36103738205 100644 --- a/its/ruling/src/test/expected/js/javascript-test-sources/javascript-S1126.json +++ b/its/ruling/src/test/expected/js/javascript-test-sources/javascript-S1126.json @@ -1,5 +1,20 @@ { +'javascript-test-sources:src/ecmascript6/Ghost/core/client/app/components/gh-tags-management-container.js':[ +41, +], +'javascript-test-sources:src/ecmascript6/Ghost/core/client/app/mixins/ed-editor-scroll.js':[ +19, +], +'javascript-test-sources:src/ecmascript6/Ghost/core/client/app/mixins/editor-base-controller.js':[ +162, +], +'javascript-test-sources:src/ecmascript6/Ghost/core/server/api/utils.js':[ +299, +], 'javascript-test-sources:src/ecmascript6/Ghost/core/server/middleware/auth.js':[ 54, ], +'javascript-test-sources:src/ecmascript6/Ghost/core/server/middleware/check-ssl.js':[ +7, +], } diff --git a/its/ruling/src/test/expected/js/jshint/javascript-S1126.json b/its/ruling/src/test/expected/js/jshint/javascript-S1126.json new file mode 100644 index 00000000000..e02a035784b --- /dev/null +++ b/its/ruling/src/test/expected/js/jshint/javascript-S1126.json @@ -0,0 +1,9 @@ +{ +'jshint:src/jshint.js':[ +127, +178, +], +'jshint:src/state.js':[ +40, +], +} diff --git a/its/ruling/src/test/expected/js/p5.js/javascript-S1126.json b/its/ruling/src/test/expected/js/p5.js/javascript-S1126.json index 937dd5f0d07..339cb9f6345 100644 --- a/its/ruling/src/test/expected/js/p5.js/javascript-S1126.json +++ b/its/ruling/src/test/expected/js/p5.js/javascript-S1126.json @@ -1,5 +1,6 @@ { 'p5.js:lib/addons/p5.sound.js':[ 1926, +2169, ], } diff --git a/its/ruling/src/test/expected/js/p5.js/javascript-S4624.json b/its/ruling/src/test/expected/js/p5.js/javascript-S4624.json deleted file mode 100644 index fb20018369d..00000000000 --- a/its/ruling/src/test/expected/js/p5.js/javascript-S4624.json +++ /dev/null @@ -1,6 +0,0 @@ -{ -'p5.js:src/webgl/light.js':[ -788, -788, -], -} diff --git a/its/ruling/src/test/expected/js/prototype/javascript-S1126.json b/its/ruling/src/test/expected/js/prototype/javascript-S1126.json new file mode 100644 index 00000000000..63b0f679356 --- /dev/null +++ b/its/ruling/src/test/expected/js/prototype/javascript-S1126.json @@ -0,0 +1,6 @@ +{ +'prototype:src/prototype/dom/dom.js':[ +99, +774, +], +} diff --git a/its/ruling/src/test/expected/js/qunit/javascript-S1126.json b/its/ruling/src/test/expected/js/qunit/javascript-S1126.json new file mode 100644 index 00000000000..2048a7bc72a --- /dev/null +++ b/its/ruling/src/test/expected/js/qunit/javascript-S1126.json @@ -0,0 +1,5 @@ +{ +'qunit:src/equiv.js':[ +54, +], +} diff --git a/its/ruling/src/test/expected/ts/console/typescript-S4624.json b/its/ruling/src/test/expected/ts/console/typescript-S4624.json index 127d13462a2..066260bb11d 100644 --- a/its/ruling/src/test/expected/ts/console/typescript-S4624.json +++ b/its/ruling/src/test/expected/ts/console/typescript-S4624.json @@ -1,8 +1,4 @@ { -'console:src/utils/valueparser.ts':[ -30, -34, -], 'console:src/views/models/DatabrowserView/Cell/SelectNodesCell/SelectNodesCell.tsx':[ 324, 334, diff --git a/its/ruling/src/test/expected/ts/postgraphql/typescript-S4624.json b/its/ruling/src/test/expected/ts/postgraphql/typescript-S4624.json index b2c515c5870..1e9fb573dd5 100644 --- a/its/ruling/src/test/expected/ts/postgraphql/typescript-S4624.json +++ b/its/ruling/src/test/expected/ts/postgraphql/typescript-S4624.json @@ -5,40 +5,12 @@ 138, 141, ], -'postgraphql:src/postgraphql/withPostGraphQLContext.ts':[ -176, -], 'postgraphql:src/postgres/inventory/__tests__/conditionToSql-test.ts':[ 14, ], 'postgraphql:src/postgres/inventory/addPgCatalogToInventory.ts':[ 65, ], -'postgraphql:src/postgres/inventory/collection/PgCollection.ts':[ -164, -166, -], -'postgraphql:src/postgres/inventory/collection/PgCollectionKey.ts':[ -172, -176, -179, -233, -237, -], -'postgraphql:src/postgres/inventory/paginator/PgPaginatorOrderingAttributes.ts':[ -83, -92, -99, -99, -], -'postgraphql:src/postgres/inventory/paginator/PgPaginatorOrderingOffset.ts':[ -132, -132, -134, -], -'postgraphql:src/postgres/inventory/type/custom/pgIntervalType.ts':[ -91, -], 'postgraphql:src/postgres/utils/__tests__/sql-test.ts':[ 154, ],