-
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.
Load
package.json
on init-linter (#4363)
Co-authored-by: Yassin Kammoun <[email protected]>
- Loading branch information
1 parent
7b4c424
commit 4057f72
Showing
37 changed files
with
906 additions
and
116 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
export * from './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,37 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { PackageJsons } from './project-package-json'; | ||
|
||
const PackageJsonsByBaseDir = new PackageJsons(); | ||
|
||
async function searchPackageJsonFiles(baseDir: string, exclusions: string[]) { | ||
await PackageJsonsByBaseDir.searchPackageJsonFiles(baseDir, exclusions); | ||
} | ||
|
||
function getNearestPackageJson(file: string) { | ||
return PackageJsonsByBaseDir.getPackageJsonForFile(file); | ||
} | ||
|
||
function getAllPackageJsons() { | ||
return PackageJsonsByBaseDir.db; | ||
} | ||
|
||
export { searchPackageJsonFiles, getNearestPackageJson, getAllPackageJsons, PackageJsonsByBaseDir }; |
96 changes: 96 additions & 0 deletions
96
packages/jsts/src/dependencies/package-json/project-package-json.ts
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,96 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import { toUnixPath, debug, error, readFile } from '@sonar/shared/helpers'; | ||
import { PackageJson as PJ } from 'type-fest'; | ||
import { Minimatch } from 'minimatch'; | ||
|
||
const PACKAGE_JSON = 'package.json'; | ||
|
||
// Patterns enforced to be ignored no matter what the user configures on sonar.properties | ||
const IGNORED_PATTERNS = ['**/.scannerwork/**']; | ||
|
||
export interface PackageJson { | ||
filename: string; | ||
contents: PJ; | ||
} | ||
|
||
export class PackageJsons { | ||
readonly db: Map<string, PackageJson> = new Map(); | ||
|
||
/** | ||
* Look for package.json files in a given path and its child paths. | ||
* node_modules is ignored | ||
* | ||
* @param dir parent folder where the search starts | ||
* @param exclusions glob patterns to ignore while walking the tree | ||
*/ | ||
async searchPackageJsonFiles(dir: string, exclusions: string[]) { | ||
try { | ||
const patterns = exclusions | ||
.concat(IGNORED_PATTERNS) | ||
.map(exclusion => new Minimatch(exclusion)); | ||
await this.walkDirectory(path.posix.normalize(toUnixPath(dir)), patterns); | ||
} catch (e) { | ||
error(`Error while searching for package.json files: ${e}`); | ||
} | ||
} | ||
|
||
async walkDirectory(dir: string, ignoredPatterns: Minimatch[]) { | ||
const files = await fs.readdir(dir, { withFileTypes: true }); | ||
for (const file of files) { | ||
const filename = path.posix.join(dir, file.name); | ||
if (ignoredPatterns.some(pattern => pattern.match(filename))) { | ||
continue; // is ignored pattern | ||
} | ||
if (file.isDirectory()) { | ||
await this.walkDirectory(filename, ignoredPatterns); | ||
} else if (file.name.toLowerCase() === PACKAGE_JSON && !file.isDirectory()) { | ||
try { | ||
debug(`Found package.json: ${filename}`); | ||
const contents = JSON.parse(await readFile(filename)); | ||
this.db.set(dir, { filename, contents }); | ||
} catch (e) { | ||
debug(`Error reading file ${filename}: ${e}`); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Given a filename, find the nearest package.json | ||
* | ||
* @param file source file for which we need a package.json | ||
*/ | ||
getPackageJsonForFile(file: string) { | ||
if (this.db.size === 0) { | ||
return null; | ||
} | ||
let currentDir = path.posix.dirname(path.posix.normalize(toUnixPath(file))); | ||
do { | ||
const packageJson = this.db.get(currentDir); | ||
if (packageJson) { | ||
return packageJson; | ||
} | ||
currentDir = path.posix.dirname(currentDir); | ||
} while (currentDir !== path.posix.dirname(currentDir)); | ||
return null; | ||
} | ||
} |
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
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,3 @@ | ||
<script> | ||
foo() | ||
</script> |
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 @@ | ||
foo() |
5 changes: 5 additions & 0 deletions
5
packages/jsts/tests/analysis/fixtures/package-json/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": "test-module", | ||
"version": "1.0.0", | ||
"author": "Your Name <[email protected]>" | ||
} |
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,5 @@ | ||
{ | ||
"name": "test-build-module", | ||
"version": "1.0.0", | ||
"author": "Your Name <[email protected]>" | ||
} |
Oops, something went wrong.