Skip to content

Commit

Permalink
Improve code quality from review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand-sonarsource committed May 3, 2024
1 parent 5843c5f commit 546faa7
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 72 deletions.
1 change: 0 additions & 1 deletion packages/jsts/src/rules/S1067/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export type Options = [
{
max: number;
},
string?,
];

export const rule: RuleModule<Options> = {
Expand Down
5 changes: 3 additions & 2 deletions packages/jsts/src/rules/S107/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const rule: RuleModule<Options> = {
const ruleDecoration: Rule.RuleModule = interceptReport(
eslintMaxParams,
function (context: Rule.RuleContext, descriptor: Rule.ReportDescriptor) {
const maxParams = (context.options as Options)[0].maximumFunctionParameters;
const [{ maximumFunctionParameters }] = context.options as Options;
if ('node' in descriptor) {
const functionLike = descriptor.node as TSESTree.FunctionLike;
if (!isException(functionLike)) {
Expand All @@ -76,7 +76,8 @@ export const rule: RuleModule<Options> = {

function isBeyondMaxParams(functionLike: TSESTree.FunctionLike) {
return (
functionLike.params.filter(p => p.type !== 'TSParameterProperty').length <= maxParams
functionLike.params.filter(p => p.type !== 'TSParameterProperty').length <=
maximumFunctionParameters
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/jsts/src/rules/S124/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const rule: RuleModule<Options> = {
type: 'string',
},
},
additionalProperties: false,
},
],
},
Expand Down
4 changes: 1 addition & 3 deletions packages/jsts/src/rules/S1541/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export type Options = [
{
threshold: number;
},
string?,
];

export const rule: RuleModule<Options> = {
Expand All @@ -58,8 +57,7 @@ export const rule: RuleModule<Options> = {
],
},
create(context: Rule.RuleContext) {
const options = context.options as Options;
const threshold = options[0].threshold;
const [{ threshold }] = context.options as Options;
let functionsWithParent: Map<estree.Node, estree.Node | undefined>;
let functionsDefiningModule: estree.Node[];
let functionsImmediatelyInvoked: estree.Node[];
Expand Down
1 change: 0 additions & 1 deletion packages/jsts/src/rules/S3524/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const rule: RuleModule<Options> = {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
Expand Down
49 changes: 20 additions & 29 deletions packages/shared/src/types/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,29 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { type Rule } from 'eslint';

type Schemas = Extract<Rule.RuleModule['schema'], Array<unknown>>;
type Schema = Schemas[number];
import { SONAR_RUNTIME } from '../../../jsts/src/linter/parameters';
import { JSONSchema } from '@typescript-eslint/utils';

type BaseRuleModule = Omit<Rule.RuleModule, 'schema'>;
type SonarRuntime = typeof SONAR_RUNTIME;
type RuleOptions = [Record<string, unknown>, SonarRuntime?] | [SonarRuntime];

type TypedJSONSchema<Options extends [Record<string, unknown>, string?]> = Omit<
Schema,
'type' | 'properties'
> &
Schema['properties'] &
(
| {
type: 'object';
properties: { [key in keyof Options[0]]: Schema };
}
| {
type: 'string';
enum: Array<string>;
}
);
type RuleModuleSchema<Options extends RuleOptions> =
| {
type: 'object';
properties: { [key in keyof Options[0]]: JSONSchema.JSONSchema4 };
}
| {
type: 'string';
enum: Array<string>;
};

type RuleMetaData<Options extends [Record<string, unknown>, string?]> = Omit<
Rule.RuleMetaData,
'schema'
> & {
schema: Array<TypedJSONSchema<Options>>;
type RuleMetaData<Options extends RuleOptions> = Omit<Rule.RuleMetaData, 'schema'> & {
schema: Array<RuleModuleSchema<Options>>;
};

export type RuleModule<Options extends [Record<string, unknown>, string?] | null = null> =
Options extends [Record<string, unknown>, string?]
? Omit<BaseRuleModule, 'meta'> & {
meta: RuleMetaData<Options>;
}
: BaseRuleModule;
export type RuleModule<Options extends RuleOptions | null = null> = Options extends RuleOptions
? Omit<BaseRuleModule, 'meta'> & {
meta: RuleMetaData<Options>;
}
: BaseRuleModule;
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,11 @@
*/
package org.sonar.javascript.checks;

import java.util.Collections;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.javascript.api.EslintBasedCheck;
import org.sonar.plugins.javascript.api.TypeScriptRule;

@TypeScriptRule
@Rule(key = "S1541")
public class CyclomaticComplexityTypeScriptCheck implements EslintBasedCheck {
public class CyclomaticComplexityTypeScriptCheck extends CyclomaticComplexityJavaScriptCheck {

private static final int DEFAULT_THRESHOLD = 10;

@RuleProperty(
key = "Threshold",
description = "The maximum authorized complexity.",
defaultValue = "" + DEFAULT_THRESHOLD
)
int threshold = DEFAULT_THRESHOLD;

@Override
public List<Object> configurations() {
return Collections.singletonList(
new Config(this.threshold)
);
}
@Override
public String eslintKey() {
return "cyclomatic-complexity";
}

private static class Config {

int threshold;

Config(int threshold) {
this.threshold = threshold;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GetterSetterCheck implements EslintBasedCheck {
@Override
public List<Object> configurations() {
return Collections.singletonList(
new Config(getWithoutSet)
new Config(this.getWithoutSet)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class HardcodedCredentialsCheck implements EslintBasedCheck {
@Override
public List<Object> configurations() {
return Collections.singletonList(
new Config(credentialWords.split("\\s*,\\s*"))
new Config(this.credentialWords.split("\\s*,\\s*"))
);
}

Expand Down

0 comments on commit 546faa7

Please sign in to comment.