-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
267 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] {{}} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoSelfImportCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S7060.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
26 changes: 26 additions & 0 deletions
26
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S7060.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -338,6 +338,7 @@ | |
"S6957", | ||
"S6958", | ||
"S6959", | ||
"S7059" | ||
"S7059", | ||
"S7060" | ||
] | ||
} |