-
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.
Fix S6661 (
prefer-object-spread
): Disable for projects that don't s…
…upport the object spread syntax and improve rule description (#4552) Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
- Loading branch information
1 parent
ad82539
commit c49fcbb
Showing
22 changed files
with
213 additions
and
41 deletions.
There are no files selected for viewing
5 changes: 0 additions & 5 deletions
5
its/ruling/src/test/expected/jsts/paper.js/javascript-S6661.json
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,73 @@ | ||
/* | ||
* 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. | ||
*/ | ||
export * from './package-json'; | ||
|
||
import { getNearestPackageJsons } from '.'; | ||
import * as semver from 'semver'; | ||
|
||
/** | ||
* Minimum version per reference | ||
*/ | ||
type MinimumVersions = { | ||
node?: string; | ||
}; | ||
|
||
/** | ||
* Checks if context where the filename is located supports the provided | ||
* minimum versions. | ||
*/ | ||
export function isSupported(filename: string, minVersions: MinimumVersions): boolean { | ||
validateVersions(minVersions); | ||
return isSupportedNodeVersion(filename, minVersions.node); | ||
} | ||
|
||
/** | ||
* Check if the versions are valid semver | ||
*/ | ||
function validateVersions(versions: MinimumVersions) { | ||
for (const [ref, version] of Object.entries(versions)) { | ||
if (semver.valid(version) === null) { | ||
throw new Error(`Invalid semver version: "${version}" for "${ref}"`); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if the feature is supported by the minimum Node.js version of the project. | ||
* | ||
* @param filename | ||
* @param requiredVersion | ||
* @returns | ||
*/ | ||
function isSupportedNodeVersion(filename: string, requiredVersion?: string): boolean { | ||
if (!requiredVersion) { | ||
return true; | ||
} | ||
const packageJsons = getNearestPackageJsons(filename); | ||
const versionRange = packageJsons.find(pj => pj.contents.engines?.node)?.contents.engines?.node; | ||
if (!versionRange) { | ||
return true; | ||
} | ||
const projectMinVersion = semver.minVersion(versionRange); | ||
if (!projectMinVersion) { | ||
return true; | ||
} | ||
return semver.gte(projectMinVersion, requiredVersion); | ||
} |
File renamed without changes.
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
5 changes: 5 additions & 0 deletions
5
packages/jsts/src/rules/S6661/fixtures/unsupported-node/package.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,5 @@ | ||
{ | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
packages/jsts/tests/project-metadata/fixtures/is-supported-node/no-node/package.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,5 @@ | ||
{ | ||
"name": "project", | ||
"version": "1.0.0", | ||
"author": "Your Name <email@example.com>" | ||
} |
8 changes: 8 additions & 0 deletions
8
.../jsts/tests/project-metadata/fixtures/is-supported-node/with-node-no-minimum/package.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,8 @@ | ||
{ | ||
"name": "project", | ||
"version": "1.0.0", | ||
"author": "Your Name <email@example.com>", | ||
"engines": { | ||
"node": ">4 <3" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...sts/tests/project-metadata/fixtures/is-supported-node/with-node-with-minimum/package.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,8 @@ | ||
{ | ||
"name": "project", | ||
"version": "1.0.0", | ||
"author": "Your Name <email@example.com>", | ||
"engines": { | ||
"node": ">=5.0.0" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
8 changes: 6 additions & 2 deletions
8
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6661.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