From 3f31a3e68ff33dbb9ea43bc3e913cb195b320e8d Mon Sep 17 00:00:00 2001 From: Ilia Kebets Date: Tue, 28 Nov 2023 11:53:15 +0100 Subject: [PATCH] Add rule S6859 (`eslint-plugin-import/no-absolute-path`): Imports should not use absolute paths --- .../jsts/file-for-rules/javascript-S1451.json | 3 ++ .../jsts/file-for-rules/javascript-S3533.json | 3 ++ .../jsts/file-for-rules/javascript-S6859.json | 5 +++ its/sources/jsts/custom/S6859.js | 1 + .../jsts/src/linter/quickfixes/messages.ts | 1 + packages/jsts/src/linter/quickfixes/rules.ts | 3 ++ .../wrapper/quickfixes/no-absolute-path.ts | 3 ++ .../sonar/javascript/checks/CheckList.java | 1 + .../checks/NoAbsolutePathCheck.java | 36 +++++++++++++++++++ .../javascript/rules/javascript/S6859.html | 22 ++++++++++++ .../javascript/rules/javascript/S6859.json | 29 +++++++++++++++ .../rules/javascript/Sonar_way_profile.json | 3 +- 12 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6859.json create mode 100644 its/sources/jsts/custom/S6859.js create mode 100644 packages/jsts/tests/linter/fixtures/wrapper/quickfixes/no-absolute-path.ts create mode 100644 sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoAbsolutePathCheck.java create mode 100644 sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.html create mode 100644 sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.json diff --git a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S1451.json b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S1451.json index 059794a7438..102aa4fb554 100644 --- a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S1451.json +++ b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S1451.json @@ -203,6 +203,9 @@ "file-for-rules:S6851.js": [ 0 ], +"file-for-rules:S6859.js": [ +0 +], "file-for-rules:boundOrAssignedEvalOrArguments.js": [ 0 ] diff --git a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S3533.json b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S3533.json index e5619ec25a8..832c1c1b37a 100644 --- a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S3533.json +++ b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S3533.json @@ -55,5 +55,8 @@ ], "file-for-rules:S6438.js": [ 1 +], +"file-for-rules:S6859.js": [ +1 ] } diff --git a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6859.json b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6859.json new file mode 100644 index 00000000000..16a47329de1 --- /dev/null +++ b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6859.json @@ -0,0 +1,5 @@ +{ +"file-for-rules:S6859.js": [ +1 +] +} diff --git a/its/sources/jsts/custom/S6859.js b/its/sources/jsts/custom/S6859.js new file mode 100644 index 00000000000..9c9e3cf9a25 --- /dev/null +++ b/its/sources/jsts/custom/S6859.js @@ -0,0 +1 @@ +const f = require('/foo/bar'); diff --git a/packages/jsts/src/linter/quickfixes/messages.ts b/packages/jsts/src/linter/quickfixes/messages.ts index 6296577e41c..7bab10c7095 100644 --- a/packages/jsts/src/linter/quickfixes/messages.ts +++ b/packages/jsts/src/linter/quickfixes/messages.ts @@ -28,6 +28,7 @@ const quickFixMessages = new Map([ ['comma-dangle', 'Remove this trailing comma'], ['eol-last', 'Add a new line at the end of file'], ['jsx-no-useless-fragment', 'Remove redundant fragment'], + ['no-absolute-path', 'Do not import modules using an absolute path'], ['no-empty-interface', 'Replace with type alias'], ['no-extra-bind', 'Remove .bind() call'], ['no-extra-boolean-cast', 'Remove extra cast'], diff --git a/packages/jsts/src/linter/quickfixes/rules.ts b/packages/jsts/src/linter/quickfixes/rules.ts index 6ebb8883086..12b332fdf66 100644 --- a/packages/jsts/src/linter/quickfixes/rules.ts +++ b/packages/jsts/src/linter/quickfixes/rules.ts @@ -128,4 +128,7 @@ export const quickFixRules = new Set([ 'switch-without-default', 'unnecessary-character-escapes', 'unused-import', + + // eslint-plugin-import + 'no-absolute-path', ]); diff --git a/packages/jsts/tests/linter/fixtures/wrapper/quickfixes/no-absolute-path.ts b/packages/jsts/tests/linter/fixtures/wrapper/quickfixes/no-absolute-path.ts new file mode 100644 index 00000000000..3f8b1605d66 --- /dev/null +++ b/packages/jsts/tests/linter/fixtures/wrapper/quickfixes/no-absolute-path.ts @@ -0,0 +1,3 @@ +import { foo } from '/home/project/bar'; + +foo(); diff --git a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java index 1607e850333..67efda69119 100644 --- a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java +++ b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java @@ -242,6 +242,7 @@ public static List> getAllChecks() { NestedControlFlowDepthCheck.class, NewCapCheck.class, NewOperatorMisuseCheck.class, + NoAbsolutePathCheck.class, NoAccessKeyCheck.class, NoAccessStateInSetstateCheck.class, NoAccessorFieldMismatchCheck.class, diff --git a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoAbsolutePathCheck.java b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoAbsolutePathCheck.java new file mode 100644 index 00000000000..60544a4008a --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoAbsolutePathCheck.java @@ -0,0 +1,36 @@ +/** + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2023 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.sonar.check.Rule; +import org.sonar.plugins.javascript.api.EslintBasedCheck; +import org.sonar.plugins.javascript.api.JavaScriptRule; +import org.sonar.plugins.javascript.api.TypeScriptRule; + +@TypeScriptRule +@JavaScriptRule +@Rule(key = "S6859") +public class NoAbsolutePathCheck implements EslintBasedCheck { + + @Override + public String eslintKey() { + return "no-absolute-path"; + } +} diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.html b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.html new file mode 100644 index 00000000000..0ee0b0b9a13 --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.html @@ -0,0 +1,22 @@ +

Why is this an issue?

+

In Node.js importing modules is doable by providing an absolute path such as /lib/foo/bar.js. Doing this restricts the portability of +your code, making it specific to your computer’s file system and potentially causing issues when the code is distributed, for example, through NPM +packages.

+

How to fix it

+

Replace the absolute path with one that is relative to your current file.

+

Code examples

+

Noncompliant code example

+
+import { foo } from '/home/project/api/bar.js';
+
+

Compliant solution

+
+import { foo } from '../api/bar.js';
+
+

Resources

+

Documentation

+ + diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.json b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.json new file mode 100644 index 00000000000..3048afd405f --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6859.json @@ -0,0 +1,29 @@ +{ + "title": "Imports should not use absolute paths", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "pitfall", + "paths" + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-6859", + "sqKey": "S6859", + "scope": "All", + "quickfix": "covered", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "LOW" + }, + "attribute": "MODULAR" + }, + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json index 91b87e4e5a9..36ab55d9bd3 100644 --- a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json @@ -323,6 +323,7 @@ "S6852", "S6853", "S6854", - "S6855" + "S6855", + "S6859" ] }