Skip to content

Commit

Permalink
Add rule S6859 (eslint-plugin-import/no-absolute-path): Imports sho…
Browse files Browse the repository at this point in the history
…uld not use absolute paths
  • Loading branch information
ilia-kebets-sonarsource committed Nov 28, 2023
1 parent 78fe212 commit ff4900a
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@
"file-for-rules:S6851.js": [
0
],
"file-for-rules:S6859.js": [
0
],
"file-for-rules:boundOrAssignedEvalOrArguments.js": [
0
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@
],
"file-for-rules:S6438.js": [
1
],
"file-for-rules:S6859.js": [
1
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"file-for-rules:S6859.js": [
1
]
}
1 change: 1 addition & 0 deletions its/sources/jsts/custom/S6859.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const f = require('/foo/bar');
1 change: 1 addition & 0 deletions packages/jsts/src/linter/quickfixes/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const quickFixMessages = new Map<string, string>([
['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', 'Replace with a relative path'],
['no-empty-interface', 'Replace with type alias'],
['no-extra-bind', 'Remove .bind() call'],
['no-extra-boolean-cast', 'Remove extra cast'],
Expand Down
3 changes: 3 additions & 0 deletions packages/jsts/src/linter/quickfixes/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ export const quickFixRules = new Set([
'switch-without-default',
'unnecessary-character-escapes',
'unused-import',

// eslint-plugin-import
'no-absolute-path',
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from '/home/project/bar';

foo();
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NestedControlFlowDepthCheck.class,
NewCapCheck.class,
NewOperatorMisuseCheck.class,
NoAbsolutePathCheck.class,
NoAccessKeyCheck.class,
NoAccessStateInSetstateCheck.class,
NoAccessorFieldMismatchCheck.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h2>Why is this an issue?</h2>
<p>In Node.js importing modules is doable by providing an absolute path such as <code>/lib/foo/bar.js</code>. 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.</p>
<h2>How to fix it</h2>
<p>Replace the absolute path with one that is relative to your current file.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
import { foo } from '/home/project/api/bar.js';
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
import { foo } from '../api/bar.js';
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import">import</a> </li>
<li> Node.js docs - <a href="https://nodejs.org/api/esm.html">ECMAScript modules</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
"S6852",
"S6853",
"S6854",
"S6855"
"S6855",
"S6859"
]
}

0 comments on commit ff4900a

Please sign in to comment.