Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Essentially, checking how the tests behave with just one change to bring compatibility with the next version of
SonarJS ESLing plugin.
  • Loading branch information
ericmorand-sonarsource committed Apr 30, 2024
1 parent 90f981a commit f116556
Show file tree
Hide file tree
Showing 29 changed files with 222 additions and 194 deletions.
288 changes: 155 additions & 133 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"type": "commonjs",
"devDependencies": {
"@types/bytes": "3.1.4",
"@types/eslint": "8.56.6",
"@types/eslint": "^8.56.10",
"@types/eslint-scope": "3.7.7",
"@types/estree": "1.0.5",
"@types/express": "4.17.21",
Expand Down Expand Up @@ -79,17 +79,17 @@
"@babel/preset-flow": "7.24.1",
"@babel/preset-react": "7.24.1",
"@eslint-community/regexpp": "4.10.0",
"@typescript-eslint/eslint-plugin": "7.4.0",
"@typescript-eslint/utils": "7.4.0",
"@typescript-eslint/parser": "7.4.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@typescript-eslint/utils": "^7.7.1",
"builtin-modules": "3.3.0",
"bytes": "3.1.2",
"eslint": "8.57.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jsx-a11y": "6.8.0",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-sonarjs": "0.25.0",
"eslint-plugin-sonarjs": "^0.26.0-2918",
"express": "4.19.2",
"functional-red-black-tree": "1.0.1",
"htmlparser2": "9.1.0",
Expand All @@ -105,8 +105,8 @@
"postcss-syntax": "0.36.2",
"postcss-value-parser": "4.2.0",
"run-node": "2.0.0",
"semver": "7.6.0",
"scslre": "0.3.0",
"semver": "7.6.0",
"stylelint": "15.10.0",
"tar": "6.2.1",
"tmp": "0.2.3",
Expand All @@ -115,7 +115,7 @@
"vue-eslint-parser": "9.4.2",
"yaml": "2.4.1"
},
"bundledDependencies": [
"bundleDependencies": [
"@typescript-eslint/eslint-plugin",
"@typescript-eslint/utils",
"@typescript-eslint/parser",
Expand Down
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 @@ -79,7 +79,7 @@ const loaders: { [key: string]: Function } = {
* Adds the rules from the Sonar ESLint plugin.
*/
pluginRules(linter: Linter) {
linter.defineRules(pluginRules);
linter.defineRules(pluginRules as unknown as Record<string, Rule.RuleModule>);
},
/**
* Loads internal rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
import { rules as sonarjsESLintRules } from 'eslint-plugin-sonarjs';
import { CustomRule } from './custom-rule';
import { Rule } from 'eslint';

/**
* The internal _cognitive complexity_ custom rule
Expand All @@ -27,6 +28,6 @@ import { CustomRule } from './custom-rule';
*/
export const rule: CustomRule = {
ruleId: 'internal-cognitive-complexity',
ruleModule: sonarjsESLintRules['cognitive-complexity'],
ruleModule: sonarjsESLintRules['cognitive-complexity'] as unknown as Rule.RuleModule,
ruleConfig: ['metric'],
};
3 changes: 2 additions & 1 deletion packages/jsts/src/linter/custom-rules/symbol-highlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
*/
import { CustomRule } from './custom-rule';
import { rule as symbolHighlightingRule } from '../visitors/symbol-highlighting';
import { Rule } from 'eslint';

/**
* The internal _symbol highlighting_ custom rule
*/
export const rule: CustomRule = {
ruleId: 'internal-symbol-highlighting',
ruleModule: symbolHighlightingRule,
ruleModule: symbolHighlightingRule as unknown as Rule.RuleModule,
ruleConfig: [],
};
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2310/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const rule: Rule.RuleModule = {
}

function checkCounter(counter: estree.Identifier, block: estree.Node) {
const variable = getVariableFromName(context, counter.name);
const variable = getVariableFromName(context, counter.name, block);
if (!variable) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2430/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function isNotClassOrFunction(context: Rule.RuleContext, node: estree.CallExpres
if (callee.type !== 'Identifier') {
return false;
}
const variable = getVariableFromName(context, callee.name);
const variable = getVariableFromName(context, callee.name, node);
if (variable) {
for (const def of variable.defs) {
if (!(def.type === 'ClassName' || def.type === 'FunctionName')) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S2598/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function checkCallExpression(context: Rule.RuleContext, callExpression: estree.C

function checkFormidable(context: Rule.RuleContext, callExpression: estree.CallExpression) {
if (callExpression.arguments.length === 0) {
const formVariable = getLhsVariable(context);
const formVariable = getLhsVariable(context, callExpression);
if (formVariable) {
formidableObjects.set(formVariable, {
uploadDirSet: false,
Expand Down Expand Up @@ -220,7 +220,7 @@ export function getVariablePropertyFromAssignment(

const memberExpr = assignment.left;
if (memberExpr.object.type === 'Identifier' && memberExpr.property.type === 'Identifier') {
const objectVariable = getVariableFromName(context, memberExpr.object.name);
const objectVariable = getVariableFromName(context, memberExpr.object.name, memberExpr);
if (objectVariable) {
return { objectVariable, property: memberExpr.property.name };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2612/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const rule: Rule.RuleModule = {
} else if (expr.type === 'Literal') {
return modeFromLiteral(expr);
} else if (expr.type === 'Identifier') {
const usage = getUniqueWriteUsage(context, expr.name);
const usage = getUniqueWriteUsage(context, expr.name, expr);
if (usage && !visited.has(usage)) {
visited.add(usage);
return modeFromExpression(usage, visited);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S3317/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const rule: Rule.RuleModule = {
ExportDefaultDeclaration: (node: estree.Node) => {
const declaration = (node as estree.ExportDefaultDeclaration).declaration;
if (declaration.type === 'Identifier') {
const variable = getVariableFromName(context, declaration.name);
const variable = getVariableFromName(context, declaration.name, node);
if (variable && variable.defs.length === 1) {
const def = variable.defs[0];
if (def.type === 'ClassName' || def.type === 'FunctionName' || isConst(def)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S3686/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function checkExpression(

function getVariable(node: estree.SimpleCallExpression, context: Rule.RuleContext) {
if (node.callee.type === 'Identifier') {
return getVariableFromName(context, node.callee.name);
return getVariableFromName(context, node.callee.name, node);
}
return undefined;
}
16 changes: 6 additions & 10 deletions packages/jsts/src/rules/S4275/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const noAccessorFieldMismatchRule: Rule.RuleModule = {
const accessorNode = node as TSESTree.Property;
const accessorInfo = getSingleDescriptorAccessorInfo(accessorNode);
if (accessorInfo) {
const fieldMap = getSingleVariableFieldMap(context, accessorInfo.name);
const fieldMap = getSingleVariableFieldMap(context, accessorInfo.name, node);
checkAccessorNode(context, accessorNode, fieldMap, accessorInfo);
}
},
Expand All @@ -149,7 +149,7 @@ const noAccessorFieldMismatchRule: Rule.RuleModule = {
const accessorNode = node as TSESTree.Property;
const accessorInfo = getMultiDescriptorsAccessorInfo(accessorNode);
if (accessorInfo) {
const fieldMap = getSingleVariableFieldMap(context, accessorInfo.name);
const fieldMap = getSingleVariableFieldMap(context, accessorInfo.name, node);
checkAccessorNode(context, accessorNode, fieldMap, accessorInfo);
}
},
Expand Down Expand Up @@ -373,13 +373,9 @@ function getName(key: TSESTree.Node) {
return null;
}

function getNodeFieldMap(
context: Rule.RuleContext,
node: TSESTree.Node | undefined | null,
info: AccessorInfo,
) {
function getNodeFieldMap(context: Rule.RuleContext, node: TSESTree.Node, info: AccessorInfo) {
if (info.definition === 'descriptor') {
return getSingleVariableFieldMap(context, info.name);
return getSingleVariableFieldMap(context, info.name, node as estree.Node);
} else if (node?.type === 'ObjectExpression') {
return getObjectExpressionFieldMap(node);
} else if (node?.type === 'ClassBody') {
Expand All @@ -389,10 +385,10 @@ function getNodeFieldMap(
}
}

function getSingleVariableFieldMap(context: Rule.RuleContext, name: string) {
function getSingleVariableFieldMap(context: Rule.RuleContext, name: string, node: estree.Node) {
const fieldMap = new Map<string, Field>();
for (const candidate of [name, `_${name}`, `${name}_`]) {
const variable = getVariableFromName(context, candidate);
const variable = getVariableFromName(context, candidate, node);
if (variable != null && variable.defs.length > 0) {
fieldMap.set(candidate, { name: candidate, node: variable.defs[0].node });
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S4327/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function reportExempting(
}

function isReferencedInsideGenerators(context: Rule.RuleContext, node: estree.Identifier) {
const variable = getVariableFromName(context, node.name);
const variable = getVariableFromName(context, node.name, node);
if (variable) {
for (const reference of variable.references) {
let scope: Scope.Scope | null = reference.from;
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S5122/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const rule: Rule.RuleModule = {
report(sensitiveCorsProperty);
}
if (arg?.type === 'Identifier') {
const usage = getUniqueWriteUsage(context, arg.name);
const usage = getUniqueWriteUsage(context, arg.name, arg);
sensitiveCorsProperty = getSensitiveCorsProperty(usage, context);
if (sensitiveCorsProperty) {
report(sensitiveCorsProperty, arg);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S5691/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const rule: Rule.RuleModule = {
if (getFullyQualifiedName(context, callee) === SERVE_STATIC && args.length > 1) {
let options: estree.Node | undefined = args[1];
if (options.type === 'Identifier') {
options = getUniqueWriteUsage(context, options.name);
options = getUniqueWriteUsage(context, options.name, node);
}

const dotfilesProperty = getProperty(options, 'dotfiles', context);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S5693/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function checkCallExpression(context: Rule.RuleContext, callExpression: estree.C
function checkFormidable(context: Rule.RuleContext, callExpression: estree.CallExpression) {
if (callExpression.arguments.length === 0) {
// options will be set later through member assignment
const formVariable = getLhsVariable(context);
const formVariable = getLhsVariable(context, callExpression);
if (formVariable) {
formidableObjects.set(formVariable, {
maxFileSize: FORMIDABLE_DEFAULT_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S5843/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class RegexPartFinder {
} else if (isStaticTemplateLiteral(node)) {
this.parts.push([node]);
} else if (isIdentifier(node)) {
const initializer = getUniqueWriteUsage(this.context, node.name);
const initializer = getUniqueWriteUsage(this.context, node.name, node);
if (initializer) {
this.find(initializer);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/jsts/src/rules/S5860/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class RegexIntelliSense {
const { callee, arguments: args } = callExpr;
if (isMethodCall(callExpr) && args.length > 0) {
const target = (callee as estree.MemberExpression).object;
const matcher = getLhsVariable(this.context);
const matcher = getLhsVariable(this.context, callExpr);
if (matcher) {
const method = (callee as estree.MemberExpression).property as estree.Identifier;
if (isString(target, this.services) && ['match', 'matchAll'].includes(method.name)) {
Expand Down Expand Up @@ -415,7 +415,7 @@ class RegexIntelliSense {
visited.add(node);
const variable = this.findVariable(node);
if (variable) {
const value = getUniqueWriteUsage(this.context, variable.name);
const value = getUniqueWriteUsage(this.context, variable.name, node);
if (value) {
const regex = this.findRegexRec(value, visited);
if (regex) {
Expand All @@ -437,7 +437,7 @@ class RegexIntelliSense {

private findVariable(node: estree.Node) {
if (node.type === 'Identifier') {
return getVariableFromName(this.context, node.name);
return getVariableFromName(this.context, node.name, node);
}
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jsts/src/rules/S6351/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ function extractRegexInvocation(
callExpr.callee.object.type === 'Identifier'
) {
const { object } = callExpr.callee;
const variable = getVariableFromName(context, object.name);
const variable = getVariableFromName(context, object.name, callExpr);
if (variable) {
const value = getUniqueWriteUsage(context, variable.name);
const value = getUniqueWriteUsage(context, variable.name, callExpr);
const regex = regexes.find(r => r.node === value);
if (regex?.flags.includes('g')) {
const usages = invocations.get(variable);
Expand Down Expand Up @@ -127,9 +127,9 @@ function extractResetRegex(
) {
const parent = getParent(context);
if (parent?.type === 'AssignmentExpression' && parent.left === node) {
const variable = getVariableFromName(context, node.object.name);
const variable = getVariableFromName(context, node.object.name, node);
if (variable) {
const value = getUniqueWriteUsage(context, variable.name);
const value = getUniqueWriteUsage(context, variable.name, node);
const regex = regexes.find(r => r.node === value);
if (regex) {
resets.add(variable);
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S6442/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const rule: Rule.RuleModule = {
const hookDeclarator = node as HookDeclarator;

if (isHookCall(hookDeclarator.init)) {
const variable = getVariableFromName(context, hookDeclarator.id.elements[1].name);
const variable = getVariableFromName(context, hookDeclarator.id.elements[1].name, node);
if (variable != null) {
setters.push(variable);
}
Expand All @@ -136,7 +136,7 @@ export const rule: Rule.RuleModule = {

const maybeSetterCall = node as SetterCall;

const calleeVariable = getVariableFromName(context, maybeSetterCall.callee.name);
const calleeVariable = getVariableFromName(context, maybeSetterCall.callee.name, node);
if (setters.some(variable => variable === calleeVariable)) {
context.report({
messageId: 'noHookSetterInBody',
Expand Down
12 changes: 8 additions & 4 deletions packages/jsts/src/rules/S6443/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ export const rule: Rule.RuleModule = {
const { elements } = node.id as estree.ArrayPattern;
const setter = (elements[1] as estree.Identifier).name;
referencesBySetterName[setter] = {
setter: getVariableFromName(context, setter),
value: getVariableFromName(context, (elements[0] as estree.Identifier).name),
setter: getVariableFromName(context, setter, node),
value: getVariableFromName(context, (elements[0] as estree.Identifier).name, node),
};
}
},
[callSelector](node: estree.CallExpression) {
const setter = getVariableFromName(context, (node.callee as estree.Identifier).name);
const value = getVariableFromName(context, (node.arguments[0] as estree.Identifier).name);
const setter = getVariableFromName(context, (node.callee as estree.Identifier).name, node);
const value = getVariableFromName(
context,
(node.arguments[0] as estree.Identifier).name,
node,
);
const key = setter?.name as string;
if (
setter &&
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S6594/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const rule: Rule.RuleModule = {
ident = parent.left;
}
if (ident) {
return getVariableFromName(context, ident.name);
return getVariableFromName(context, ident.name, node);
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jsts/src/rules/S6647/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Rule } from 'eslint';
import { eslintRules } from '../core';
import { decorate } from './decorator';
import { getVariableFromName } from '../helpers';
import estree from 'estree';

/**
* Check if method with accessibility is not useless
Expand Down Expand Up @@ -72,7 +73,7 @@ function checkInheritance(node: TSESTree.MethodDefinition, context: Rule.RuleCon
node.parent.parent.superClass
) {
const superClass = node.parent.parent.superClass as TSESTree.Identifier;
const variable = getVariableFromName(context, superClass.name);
const variable = getVariableFromName(context, superClass.name, node as estree.Node);
for (const def of variable?.defs ?? []) {
if (def.type === 'ImportBinding') {
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S6959/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const rule: Rule.RuleModule = {
},
},
create(context: Rule.RuleContext) {
const services = context.parserServices;
const services = context.sourceCode.parserServices;

function isArray(node: estree.Node) {
if (isRequiredParserServices(services)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S888/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function isUpdateByOne(

function isUsedInsideBody(id: estree.Node, loopBody: estree.Node, context: Rule.RuleContext) {
if (id.type === 'Identifier') {
const variable = getVariableFromName(context, id.name);
const variable = getVariableFromName(context, id.name, id);
const bodyRange = loopBody.range;
if (variable && bodyRange) {
return variable.references.some(ref => isInBody(ref.identifier, bodyRange));
Expand Down
Loading

0 comments on commit f116556

Please sign in to comment.