Skip to content

Commit

Permalink
Modify S4328: Use new package.json shared methods (#4377)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez authored Nov 14, 2023
1 parent 3367e4d commit 2be9541
Show file tree
Hide file tree
Showing 63 changed files with 108 additions and 103 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@
"@sonar/css": "lib/css/src",
"@sonar/html": "lib/html/src",
"@sonar/jsts": "lib/jsts/src",
"@sonar/jsts/embedded": "lib/jsts/src/embedded",
"@sonar/shared": "lib/shared/src",
"@sonar/yaml": "lib/yaml/src"
}
Expand Down
3 changes: 1 addition & 2 deletions packages/bridge/src/errors/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import express from 'express';
import { ErrorCode } from '@sonar/shared/errors';
import { ErrorCode, error } from '@sonar/shared';
import { JsTsAnalysisOutput } from '@sonar/jsts';
import { error } from '@sonar/shared/helpers';

/**
* Express.js middleware for handling error while serving requests.
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as v8 from 'v8';
import * as os from 'os';
import fs from 'fs';
import { constants, NodeGCPerformanceDetail, PerformanceObserver } from 'perf_hooks';
import { debug, error, getContext, info, warn } from '@sonar/shared/helpers';
import { debug, error, getContext, info, warn } from '@sonar/shared';

const MB = 1024 * 1024;

Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import http from 'http';
import path from 'path';
import router from './router';
import { errorMiddleware } from './errors';
import { debug, getContext } from '@sonar/shared/helpers';
import { debug, getContext } from '@sonar/shared';
import { timeoutMiddleware } from './timeout';
import { AddressInfo } from 'net';
import { Worker, SHARE_ENV } from 'worker_threads';
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/tests/errors/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { ErrorCode, APIError } from '@sonar/shared/errors';
import { ErrorCode, APIError } from '@sonar/shared';
import * as express from 'express';
import { EMPTY_JSTS_ANALYSIS_OUTPUT, errorMiddleware } from '../../src/errors';

Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/tests/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { setContext, toUnixPath } from '@sonar/shared/helpers';
import { setContext, toUnixPath } from '@sonar/shared';
import http from 'http';
import { createAndSaveProgram, RuleConfig } from '@sonar/jsts';
import path from 'path';
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { start } from '../src/server';
import { promisify } from 'util';
import path from 'path';
import { setContext } from '@sonar/shared/helpers';
import { setContext } from '@sonar/shared';
import { AddressInfo } from 'net';
import { request } from './tools';
import http from 'http';
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/tests/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import path from 'path';
import { Worker } from 'worker_threads';
import { ErrorCode } from '@sonar/shared/errors';
import { ErrorCode } from '@sonar/shared';

describe('worker', () => {
let worker: Worker;
Expand Down
2 changes: 1 addition & 1 deletion packages/css/src/analysis/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { Issue, RuleConfig } from '../linter';
import { AnalysisInput, AnalysisOutput } from '@sonar/shared/types';
import { AnalysisInput, AnalysisOutput } from '@sonar/shared';

/**
* A CSS analysis input
Expand Down
2 changes: 1 addition & 1 deletion packages/css/src/linter/issues/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as stylelint from 'stylelint';
import { debug } from '@sonar/shared/helpers';
import { debug } from '@sonar/shared';
import { Issue } from './issue';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/css/tests/analysis/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { analyzeCSS, CssAnalysisInput } from '../../src/analysis';
import { RuleConfig } from '../../src/linter';
import path from 'path';
import { readFile } from '@sonar/shared/helpers';
import { readFile } from '@sonar/shared';

const rules = [{ key: 'block-no-empty', configurations: [] }];

Expand Down
2 changes: 1 addition & 1 deletion packages/css/tests/linter/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import path from 'path';
import * as stylelint from 'stylelint';
import { readFile } from '@sonar/shared/helpers';
import { readFile } from '@sonar/shared';
import { createStylelintConfig, LinterWrapper, RuleConfig } from '../../src/linter';

describe('LinterWrapper', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
analyzeEmbedded,
EmbeddedAnalysisInput as HtmlAnalysisInput,
EmbeddedAnalysisOutput as HtmlAnalysisOutput,
} from '@sonar/jsts/embedded';
} from '@sonar/jsts';

import { parseHTML } from './parser';

Expand Down
2 changes: 1 addition & 1 deletion packages/html/src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as htmlparser from 'htmlparser2';
import { EmbeddedJS } from '@sonar/jsts/embedded';
import { EmbeddedJS } from '@sonar/jsts';

/**
* References:
Expand Down
5 changes: 2 additions & 3 deletions packages/html/tests/analysis/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import { join } from 'path';
import { embeddedInput } from '../../../jsts/tests/tools';
import { parseHTML } from '../../src/parser';
import { setContext } from '@sonar/shared/helpers';
import { analyzeEmbedded } from '@sonar/jsts/embedded';
import { initializeLinter } from '@sonar/jsts';
import { setContext } from '@sonar/shared';
import { analyzeEmbedded, initializeLinter } from '@sonar/jsts';

describe('analyzeHTML', () => {
const fixturesPath = join(__dirname, 'fixtures');
Expand Down
2 changes: 1 addition & 1 deletion packages/html/tests/builder/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { join } from 'path';
import { parseHTML } from '../../src/parser';
import { embeddedInput } from '../../../jsts/tests/tools';
import { buildSourceCodes } from '@sonar/jsts/embedded';
import { buildSourceCodes } from '@sonar/jsts';

describe('buildSourceCodes()', () => {
const fixturesPath = join(__dirname, 'fixtures');
Expand Down
2 changes: 1 addition & 1 deletion packages/html/tests/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import path from 'path';
import { parseHTML } from '../../src/parser';
import { readFile } from '@sonar/shared/helpers';
import { readFile } from '@sonar/shared';

describe('parseHtml', () => {
it('should return embedded JavaScript', async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/jsts/src/analysis/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { FileType, JsTsLanguage } from '@sonar/shared/helpers';
import { FileType, JsTsLanguage, AnalysisInput, AnalysisOutput } from '@sonar/shared';
import { CpdToken, Issue, Metrics, SymbolHighlight, SyntaxHighlight } from '../linter';
import { AnalysisInput, AnalysisOutput } from '@sonar/shared/types';
import { Perf } from '../monitoring';

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/jsts/src/analysis/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { APIError } from '@sonar/shared/errors';
import { APIError, debug, getContext, JsTsLanguage } from '@sonar/shared';
import { SourceCode } from 'eslint';
import { debug, getContext, JsTsLanguage } from '@sonar/shared/helpers';
import {
computeMetrics,
findNoSonarLines,
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/builders/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { debug, getContext, JsTsLanguage } from '@sonar/shared/helpers';
import { debug, getContext, JsTsLanguage } from '@sonar/shared';
import { JsTsAnalysisInput } from '../analysis';
import { buildParserOptions, parseForESLint, parsers } from '../parsers';
import { getProgramById } from '../program';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import fs from 'fs';
import path from 'path';
import { toUnixPath, debug, error, readFileSync } from '@sonar/shared/helpers';
import { toUnixPath, debug, error, readFileSync } from '@sonar/shared';
import { PackageJson as PJ } from 'type-fest';
import { Minimatch } from 'minimatch';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/embedded/analysis/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import { Perf } from 'jsts/src/monitoring';
import { Issue } from '../../linter';
import { AnalysisInput, AnalysisOutput } from '@sonar/shared/types';
import { AnalysisInput, AnalysisOutput } from '@sonar/shared';

/**
* An analysis input of embedded code
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/embedded/analysis/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Position } from 'estree';
import { getLinter, Issue, LinterWrapper } from '../../linter';
import { buildSourceCodes, ExtendedSourceCode, LanguageParser } from '../builder';
import { EmbeddedAnalysisInput, EmbeddedAnalysisOutput } from './analysis';
import { debug } from '@sonar/shared/helpers';
import { debug } from '@sonar/shared';
import { measureDuration } from '../../monitoring';
import { findNcloc } from '../../linter/visitors/metrics/ncloc';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/embedded/builder/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { AST, SourceCode } from 'eslint';
import { Comment, Node } from 'estree';
import { visit } from '../../linter';
import { EmbeddedJS } from '../analysis';
import { APIError } from '@sonar/shared/errors';
import { APIError } from '@sonar/shared';

/**
* Patches the ESLint SourceCode instance parsed with an ESLint-based parser
Expand Down
1 change: 1 addition & 0 deletions packages/jsts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './linter';
export * from './parsers';
export * from './program';
export * from './rules';
export * from './embedded';
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/bundle-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { rules as reactESLintRules } from 'eslint-plugin-react';
import { rules as reactA11yRules } from 'eslint-plugin-jsx-a11y';
import { rules as internalRules } from '../rules';
import { customRules as internalCustomRules, CustomRule } from './custom-rules';
import { debug, getContext } from '@sonar/shared/helpers';
import { debug, getContext } from '@sonar/shared';

export function loadCustomRules(linter: Linter, rules: CustomRule[] = []) {
for (const rule of rules) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/config/linter-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { Linter, Rule } from 'eslint';
import { getContext } from '@sonar/shared/helpers';
import { getContext } from '@sonar/shared';
import { customRules as internalCustomRules } from '../custom-rules';
import { extendRuleConfig, RuleConfig } from './rule-config';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/config/rule-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { Rule } from 'eslint';
import { FileType, getContext, JsTsLanguage } from '@sonar/shared/helpers';
import { FileType, getContext, JsTsLanguage } from '@sonar/shared';
import { hasSonarContextOption, hasSonarRuntimeOption, SONAR_RUNTIME } from '../parameters';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/issues/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { Linter, SourceCode } from 'eslint';
import { transformFixes } from '../quickfixes';
import { Issue } from './issue';
import { error } from '@sonar/shared/helpers';
import { error } from '@sonar/shared';

/**
* Converts an ESLint message into a SonarQube issue
Expand Down
3 changes: 1 addition & 2 deletions packages/jsts/src/linter/linters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { APIError } from '@sonar/shared/errors';
import { debug } from '@sonar/shared/helpers';
import { APIError, debug } from '@sonar/shared';
import { LinterWrapper } from './wrapper';
import { RuleConfig } from './config';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/parameters/helpers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { Rule } from 'eslint';
import { debug } from '@sonar/shared/helpers';
import { debug } from '@sonar/shared';

/**
* Extracts the schema of a rule
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/linter/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { Linter, SourceCode } from 'eslint';
import { loadBundles, loadCustomRules } from './bundle-loader';
import { createLinterConfig, RuleConfig } from './config';
import { debug, FileType, JsTsLanguage } from '@sonar/shared/helpers';
import { debug, FileType, JsTsLanguage } from '@sonar/shared';
import { LintingResult, transformMessages } from './issues';
import { CustomRule } from './custom-rules';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/parsers/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { APIError } from '@sonar/shared/errors';
import { APIError } from '@sonar/shared';
import { SourceCode } from 'eslint';
import { ParseFunction } from './eslint';

Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/program/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
readFileSync,
toUnixPath,
warn,
} from '@sonar/shared/helpers';
} from '@sonar/shared';
import tmp from 'tmp';
import { promisify } from 'util';
import fs from 'fs/promises';
Expand Down
29 changes: 8 additions & 21 deletions packages/jsts/src/rules/S4328/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import * as path from 'path';
import * as fs from 'fs';
import * as ts from 'typescript';
import { RequiredParserServices } from '../helpers';
import { stripBOM } from '@sonar/shared/helpers';
import { toUnixPath } from '@sonar/shared';
import { getNearestPackageJsons } from '@sonar/jsts';
import { PackageJson } from 'type-fest';

const DefinitelyTyped = '@types/';

Expand Down Expand Up @@ -156,7 +158,7 @@ function getPackageName(name: string) {
}

function getDependencies(fileName: string) {
let dirname = path.dirname(fileName);
let dirname = path.posix.dirname(toUnixPath(fileName));
const cached = cache.get(dirname);
if (cached) {
return cached;
Expand All @@ -165,34 +167,24 @@ function getDependencies(fileName: string) {
const result = new Set<string>();
cache.set(dirname, result);

while (true) {
for (const packageJson of getNearestPackageJsons(fileName)) {
dirname = path.posix.dirname(packageJson.filename);
const dirCached = dirCache.get(dirname);
if (dirCached) {
dirCached.forEach(d => result.add(d));
} else {
const packageJsonPath = path.join(path.resolve(dirname), 'package.json');
const dep = fs.existsSync(packageJsonPath)
? getDependenciesFromPackageJson(packageJsonPath)
: new Set<string>();
const dep = getDependenciesFromPackageJson(packageJson.contents);
dep.forEach(d => result.add(d));
dirCache.set(dirname, dep);
}

const upperDir = path.dirname(dirname);
if (upperDir === dirname) {
break;
} else {
dirname = upperDir;
}
}

return result;
}

function getDependenciesFromPackageJson(packageJsonPath: string) {
function getDependenciesFromPackageJson(content: PackageJson) {
const result = new Set<string>();
try {
const content = JSON.parse(readFile(packageJsonPath));
if (content.dependencies !== undefined) {
addDependencies(result, content.dependencies);
}
Expand Down Expand Up @@ -268,8 +260,3 @@ function extractPathMappingPatterns(
function getBaseUrl(parserServices: RequiredParserServices): string | undefined {
return parserServices.program?.getCompilerOptions().baseUrl;
}

function readFile(filePath: string) {
const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
return stripBOM(fileContent);
}
6 changes: 5 additions & 1 deletion packages/jsts/src/rules/S4328/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import { RuleTester } from 'eslint';
import { rule } from './';
import path from 'path';
import { getAllPackageJsons, searchPackageJsonFiles } from '@sonar/jsts';

const fixtures = path.join(__dirname, 'fixtures');
//reset and search package.json files in rule dir
getAllPackageJsons().clear();
searchPackageJsonFiles(__dirname, []);

const fixtures = path.join(__dirname, 'fixtures');
const filename = path.join(fixtures, 'package-json-project/file.js');
const options = [];
const tsParserPath = require.resolve('@typescript-eslint/parser');
Expand Down
Loading

0 comments on commit 2be9541

Please sign in to comment.