Skip to content
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-597 Remove sonar-context #5167

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports.rules = [
meta: {},
create(context) {
console.log('Rule context options: ', context.options);
console.log('Work dir received: ', context.settings.workDir);
return {
CallExpression(node) {
console.log('detected call expression');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ void test() {
)
)
).isEmpty();
assertThat(
buildResult.getLogsLines(l -> l.matches(".*INFO: Work dir received: .*/.scannerwork$"))
).hasSize(2);
assertThat(buildResult.getLogsLines(l -> l.contains("Rule context options:"))).hasSize(2);
List<Issue> issues = findIssues("eslint-custom-rules:sqKey", orchestrator);
assertThat(issues).hasSize(2);
Expand Down
17 changes: 2 additions & 15 deletions packages/jsts/src/linter/config/rule-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import { hasSonarContextOption } from '../parameters/sonar-context.js';
import { FileType } from '../../../../shared/src/helpers/files.js';
import { JsTsLanguage } from '../../../../shared/src/helpers/language.js';
import { AnalysisMode } from '../../analysis/analysis.js';
Expand Down Expand Up @@ -48,25 +47,13 @@ export interface RuleConfig {
*
* A rule configuration might be extended depending on the rule definition.
* Primarily, this includes adding the default options stored in `schema.json` files in the rule directory
* Also, it allows the extension is to activate additional features during linting, e.g., secondary locations.
*
* _A rule extension only applies to rules whose implementation is available._
*
* @param sonarMeta the internal meta information of the rule
* @param inputRule the rule configuration
* @param workDir the working directory used by rules using the 'sonar-context' flag
* @returns the extended rule configuration
*/
export function extendRuleConfig(
sonarMeta: SonarMeta | undefined,
inputRule: RuleConfig,
workDir?: string,
) {
const options = Object.values(
merge(defaultOptions(sonarMeta?.fields), [...inputRule.configurations]),
);
if (hasSonarContextOption(sonarMeta?.schema)) {
options.push({ workDir });
}
return options;
export function extendRuleConfig(sonarMeta: SonarMeta | undefined, inputRule: RuleConfig) {
return Object.values(merge(defaultOptions(sonarMeta?.fields), [...inputRule.configurations]));
}
18 changes: 7 additions & 11 deletions packages/jsts/src/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ export class Linter {
},
rules: this.rulesConfig.get(key) || Linter.createInternalRulesRecord(sonarlint),
/* using "max" version to prevent `eslint-plugin-react` from printing a warning */
settings: { react: { version: '999.999.999' }, fileType, sonarRuntime: true },
settings: {
react: { version: '999.999.999' },
fileType,
sonarRuntime: true,
workDir: Linter.rulesWorkdir,
},
files: [`**/*${path.posix.extname(toUnixPath(filePath))}`],
};

Expand Down Expand Up @@ -274,16 +279,7 @@ export class Linter {
// in the case of bundles, rule.key will not be present in the ruleMetas
const ruleMeta =
rule.key in ruleMetas ? ruleMetas[rule.key as keyof typeof ruleMetas] : undefined;
rules[`sonarjs/${rule.key}`] = [
'error',
/**
* the rule configuration can be decorated with special markers
* to activate internal features: a rule that reports secondary
* locations would be `["error", "sonar-runtime"]`, where the "sonar-runtime"`
* is a marker for a post-linting processing to decode such locations.
*/
...extendRuleConfig(ruleMeta, rule, Linter.rulesWorkdir),
];
rules[`sonarjs/${rule.key}`] = ['error', ...extendRuleConfig(ruleMeta, rule)];
return rules;
}, {} as ESLintLinter.RulesRecord),
...Linter.createInternalRulesRecord(sonarlint),
Expand Down
55 changes: 0 additions & 55 deletions packages/jsts/src/linter/parameters/sonar-context.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/jsts/src/rules/S3776/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type { Rule } from 'eslint';
import estree from 'estree';
import * as meta from './meta.js';
import { FromSchema } from 'json-schema-to-ts';
import { isSonarRuntime } from '../helpers/sonar-runtime.js';

const DEFAULT_THRESHOLD = 15;

Expand Down Expand Up @@ -63,7 +64,7 @@ export const rule: Rule.RuleModule = {
const threshold = typeof thresholdOption === 'number' ? thresholdOption : DEFAULT_THRESHOLD;

/** Indicator if the file complexity should be reported */
const isFileComplexity = context.options.includes('metric');
const isFileComplexity = isSonarRuntime(context);

/** Complexity of the file */
let fileComplexity = 0;
Expand Down
5 changes: 2 additions & 3 deletions packages/jsts/src/rules/S3776/schema.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"type": "array",
"minItems": 0,
"maxItems": 2,
"maxItems": 1,
"items": [
{ "oneOf": [{ "type": "integer", "minimum": 0 },{ "type": "string"}] },
{ "type": "string" }
{ "type": "integer", "minimum": 0 }
]
}
26 changes: 0 additions & 26 deletions packages/jsts/src/rules/helpers/generate-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,5 @@ export function generateMeta(
}

metadata.messages.sonarRuntime = '{{sonarRuntimeData}}';
if (sonarMeta.hasSecondaries) {
const sonarOptions = {
type: 'string',
enum: ['metric'], // 'metric' only used by S3776
};

if (metadata.schema) {
if (Array.isArray(metadata.schema)) {
metadata.schema = [...metadata.schema, sonarOptions];
} else if (metadata.schema.type === 'array') {
if (Array.isArray(metadata.schema.items)) {
metadata.schema = {
...metadata.schema,
items: [...metadata.schema.items, sonarOptions],
};
} else {
metadata.schema = {
...metadata.schema,
items: [metadata.schema.items, sonarOptions],
};
}
}
} else {
metadata.schema = [sonarOptions];
}
}
return metadata;
}
25 changes: 1 addition & 24 deletions packages/jsts/tests/linter/config/rule-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,9 @@
import { describe, it } from 'node:test';
import { expect } from 'expect';
import { extendRuleConfig, RuleConfig } from '../../../src/linter/config/rule-config.js';
import { SONAR_CONTEXT } from '../../../src/linter/parameters/sonar-context.js';
import { ESLintConfiguration } from '../../../src/rules/helpers/configs.js';

describe('extendRuleConfig', () => {
it('should include the context', () => {
const inputRule: RuleConfig = {
key: 'some-rule',
configurations: [42],
fileTypeTargets: ['MAIN'],
language: 'js',
analysisModes: ['DEFAULT'],
};

const config = extendRuleConfig(
// @ts-ignore
{ sonarKey: 'some-rule', meta: {}, schema: [{ title: SONAR_CONTEXT }] },
inputRule,
'/tmp/dir',
);
expect(config).toEqual([42, { workDir: '/tmp/dir' }]);
});

it('should merge with a simple default configuration with sonar runtime present', () => {
const inputRule: RuleConfig = {
key: 'some-rule',
Expand All @@ -61,14 +42,11 @@ describe('extendRuleConfig', () => {
{
sonarKey: 'some-rule',
meta: {},
// @ts-ignore
schema: [{ title: SONAR_CONTEXT }],
fields: defaultConfiguration,
},
inputRule,
'/tmp/dir',
);
expect(config).toEqual([42, { format: 'allow' }, { workDir: '/tmp/dir' }]);
expect(config).toEqual([42, { format: 'allow' }]);
});

it('should use default configuration when empty configuration provided', () => {
Expand All @@ -92,7 +70,6 @@ describe('extendRuleConfig', () => {
const config = extendRuleConfig(
{ meta: {}, sonarKey: 'some-rule', fields: defaultConfiguration },
inputRule,
'/tmp/dir',
);
expect(config).toEqual([100, { format: 'allow' }]);
});
Expand Down
36 changes: 0 additions & 36 deletions packages/jsts/tests/linter/parameters/sonar-context.test.ts

This file was deleted.