-
Notifications
You must be signed in to change notification settings - Fork 183
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
JS-54 Add rule S7060 Modules should not import themselves #4810
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
import f from './cb.fixture.js'; // Noncompliant [[qf1]] | ||
// fix@qf1 {{Remove this import}} | ||
// edit@qf1 [[sc=0;ec=32]] {{}} | ||
|
||
import f from './cb.fixture'; // Noncompliant | ||
|
||
const f = require('./cb.fixture.js'); // Noncompliant [[qf2]] | ||
// fix@qf2 {{Remove this require}} | ||
// edit@qf2 [[sc=0;ec=37]] {{}} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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. | ||
*/ | ||
import { check } from '../../../tests/tools'; | ||
import { rule } from './'; | ||
import path from 'path'; | ||
|
||
const sonarId = path.basename(__dirname); | ||
|
||
describe('Rule S7060', () => { | ||
check(sonarId, rule, __dirname); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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. | ||
*/ | ||
// https://sonarsource.github.io/rspec/#/rspec/S7060/javascript | ||
|
||
import { Rule } from 'eslint'; | ||
import { generateMeta, interceptReport } from '../helpers'; | ||
import { meta } from './meta'; | ||
import * as estree from 'estree'; | ||
|
||
export function decorate(rule: Rule.RuleModule): Rule.RuleModule { | ||
return interceptReport( | ||
{ | ||
...rule, | ||
meta: generateMeta(meta as Rule.RuleMetaData, { | ||
...rule.meta!, | ||
hasSuggestions: true, | ||
}), | ||
}, | ||
reportWithQuickFix, | ||
); | ||
} | ||
|
||
function reportWithQuickFix(context: Rule.RuleContext, reportDescriptor: Rule.ReportDescriptor) { | ||
if (!('node' in reportDescriptor)) { | ||
return; | ||
} | ||
const { node } = reportDescriptor; | ||
const suggest: Rule.SuggestionReportDescriptor[] = []; | ||
if (node.type === 'ImportDeclaration') { | ||
suggest.push({ | ||
desc: 'Remove this import', | ||
fix: fixer => fixer.remove(node), | ||
}); | ||
} else if (node.type === 'CallExpression') { | ||
const variableDecl = findRequireVariableDeclaration(node); | ||
if (variableDecl) { | ||
suggest.push({ | ||
desc: 'Remove this require', | ||
fix: fixer => fixer.remove(variableDecl), | ||
}); | ||
} | ||
} | ||
context.report({ | ||
...reportDescriptor, | ||
suggest, | ||
}); | ||
} | ||
|
||
function findRequireVariableDeclaration(node: estree.Node) { | ||
const parent = getParent(getParent(node)); | ||
if (parent.type === 'VariableDeclaration') { | ||
return parent; | ||
} | ||
return undefined; | ||
} | ||
|
||
function getParent(node: estree.Node): estree.Node { | ||
// @ts-ignore | ||
return node.parent as estree.Node; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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. | ||
*/ | ||
|
||
import { rules as importRules } from 'eslint-plugin-import'; | ||
import { decorate } from './decorator'; | ||
|
||
export const rule = decorate(importRules['no-self-import']); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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. | ||
*/ | ||
|
||
// DO NOT EDIT! This file is autogenerated by "npm run generate-meta" | ||
|
||
export const meta = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @victor-diez-sonarsource because I am not sure.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks 👍 |
||
type: 'suggestion', | ||
docs: { | ||
description: 'Module should not import itself', | ||
recommended: true, | ||
url: 'https://sonarsource.github.io/rspec/#/rspec/S7060/javascript', | ||
requiresTypeChecking: false, | ||
}, | ||
fixable: 'code', | ||
}; | ||
|
||
export const sonarKey = 'S7060'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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.Check; | ||
import org.sonar.plugins.javascript.api.JavaScriptRule; | ||
import org.sonar.plugins.javascript.api.TypeScriptRule; | ||
|
||
@TypeScriptRule | ||
@JavaScriptRule | ||
@Rule(key = "S7060") | ||
public class NoSelfImportCheck extends Check { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>When a module imports itself it has no effect. This means that the import statement does nothing useful and serves no purpose. This can happen | ||
during refactoring or when a developer mistakenly imports the module itself.</p> | ||
<p>To fix the problem remove the self-import statement.</p> | ||
<pre> | ||
// file: foo.js | ||
import foo from './foo'; // Noncompliant | ||
|
||
const foo = require('./foo'); // Noncompliant | ||
</pre> | ||
<pre> | ||
// file: index.js | ||
import index from '.'; // Noncompliant | ||
|
||
const index = require('.'); // Noncompliant | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">Modules</a> </li> | ||
<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/modules.html#requireid">Node.js require</a> </li> | ||
</ul> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"title": "Module should not import itself", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7060", | ||
"sqKey": "S7060", | ||
"scope": "All", | ||
"quickfix": "covered", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "MEDIUM", | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "MODULAR" | ||
}, | ||
"compatibleLanguages": [ | ||
"JAVASCRIPT", | ||
"TYPESCRIPT" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -338,6 +338,7 @@ | |
"S6957", | ||
"S6958", | ||
"S6959", | ||
"S7059" | ||
"S7059", | ||
"S7060" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually not - this rule doesn't provide the
fix
, it's a bug in metadata generation