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-148 Fix deprecations #4681

Merged
merged 9 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 3 additions & 3 deletions packages/jsts/src/rules/S109/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const rule: Rule.RuleModule = {
const { value, parent } = numericLiteral;
if (
isPower(value) ||
isJSX(context) ||
isJSX(context, node) ||
isBitwiseOperator(parent) ||
isJsonStringify(parent)
) {
Expand Down Expand Up @@ -98,8 +98,8 @@ function isPower(value: number) {
return Number.isInteger(Math.log10(value)) || Number.isInteger(Math.log2(value));
}

function isJSX(context: Rule.RuleContext) {
return context.getAncestors().some(node => node.type.startsWith('JSX'));
function isJSX(context: Rule.RuleContext, node: estree.Node) {
return context.sourceCode.getAncestors(node).some(node => node.type.startsWith('JSX'));
}

function isBitwiseOperator(node: estree.Node) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S1128/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const rule: Rule.RuleModule = {

const ruleListener = {
ImportDeclaration: (node: estree.Node) => {
const variables = context.getDeclaredVariables(node);
const variables = context.sourceCode.getDeclaredVariables(node);
for (const variable of variables) {
if (!isExcluded(variable) && !isImplicitJsx(variable) && isUnused(variable)) {
unusedImports.push({
Expand Down Expand Up @@ -247,7 +247,7 @@ function getSuggestion(
context: Rule.RuleContext,
{ id, importDecl }: { id: estree.Identifier; importDecl: estree.ImportDeclaration },
): Rule.SuggestionReportDescriptor {
const variables = context.getDeclaredVariables(importDecl);
const variables = context.sourceCode.getDeclaredVariables(importDecl);
if (variables.length === 1) {
return {
messageId: 'suggestRemoveWholeStatement',
Expand Down
6 changes: 3 additions & 3 deletions packages/jsts/src/rules/S1172/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ function reportUnusedArgument(
}

if (
context
.getScope()
context.sourceCode
.getScope(node)
.variables.some(
v => v.name === 'arguments' && v.identifiers.length === 0 && v.references.length > 0,
)
) {
return;
}

let parametersVariable = context.getDeclaredVariables(node);
let parametersVariable = context.sourceCode.getDeclaredVariables(node);

if (functionId) {
parametersVariable = parametersVariable.filter(v => v.name !== functionId.name);
Expand Down
12 changes: 6 additions & 6 deletions packages/jsts/src/rules/S1226/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export const rule: Rule.RuleModule = {
// we do not raise issue when value is reassigned inside a top-level IfStatement, as it might be a shift or
// default value reassignment
if (
isInsideIfStatement(context) ||
context.getAncestors().some(node => node.type === 'SwitchCase') // issue-2398
isInsideIfStatement(context, identifier) ||
context.sourceCode.getAncestors(identifier).some(node => node.type === 'SwitchCase') // issue-2398
) {
return;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ export const rule: Rule.RuleModule = {

return {
onCodePathStart(_codePath: Rule.CodePath, node: estree.Node) {
const currentScope = context.getScope();
const currentScope = context.sourceCode.getScope(node);
if (currentScope && currentScope.type === 'function') {
const { referencesByIdentifier, variablesToCheck, variablesToCheckInCurrentScope } =
computeNewContextInfo(variableUsageContext, context, node);
Expand Down Expand Up @@ -226,8 +226,8 @@ export const rule: Rule.RuleModule = {
},
};

function isInsideIfStatement(context: Rule.RuleContext) {
const ancestors = context.getAncestors();
function isInsideIfStatement(context: Rule.RuleContext, node: estree.Node): boolean {
const ancestors = context.sourceCode.getAncestors(node);
for (let i = ancestors.length - 1; i >= 0; i--) {
if (
ancestors[i].type === 'IfStatement' &&
Expand Down Expand Up @@ -273,7 +273,7 @@ function computeNewContextInfo(
const referencesByIdentifier = new Map<estree.Identifier, Scope.Reference>();
const variablesToCheck = new Set<string>(variableUsageContext.variablesToCheck);
const variablesToCheckInCurrentScope = new Set<string>();
context.getDeclaredVariables(node).forEach(variable => {
context.sourceCode.getDeclaredVariables(node).forEach(variable => {
variablesToCheck.add(variable.name);
variablesToCheckInCurrentScope.add(variable.name);
for (const currentRef of variable.references) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S1481/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export const rule: Rule.RuleModule = {
jsxComponentsToIgnore.push((node as any).name);
},

'Program:exit': () => {
checkScope(context.getScope(), 'nothing');
'Program:exit': (node: estree.Node) => {
checkScope(context.sourceCode.getScope(node), 'nothing');
toIgnore = [];
jsxComponentsToIgnore = [];
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1515/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const rule: Rule.RuleModule = {
loopNode &&
!isIIEF(node, context) &&
!isAllowedCallbacks(context, node) &&
context.getScope().through.some(ref => !isSafe(ref, loopNode))
context.sourceCode.getScope(node).through.some(ref => !isSafe(ref, loopNode))
) {
context.report({
message: toEncodedMessage(message, [getMainLoopToken(loopNode, context)]),
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1526/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const rule: Rule.RuleModule = {
create(context: Rule.RuleContext) {
return {
"VariableDeclaration[kind='var']": (node: estree.Node) => {
const variables = context.getDeclaredVariables(node);
const variables = context.sourceCode.getDeclaredVariables(node);
for (const variable of variables) {
const declaration = variable.identifiers[0];
const misused = variable.references
Expand Down
5 changes: 3 additions & 2 deletions packages/jsts/src/rules/S1527/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// https://sonarsource.github.io/rspec/#/rspec/S1527/javascript

import { Rule, Scope } from 'eslint';
import estree from 'estree';

const futureReservedWords = [
'implements',
Expand Down Expand Up @@ -71,8 +72,8 @@ export const rule: Rule.RuleModule = {
}

return {
'Program:exit': () => {
checkVariablesByScope(context.getScope());
'Program:exit': (node: estree.Node) => {
checkVariablesByScope(context.sourceCode.getScope(node));
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1529/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const rule: Rule.RuleModule = {
!isNumeric(expression.right)
) {
lonelyBitwiseAndOr = expression;
lonelyBitwiseAndOrAncestors = [...context.getAncestors()];
lonelyBitwiseAndOrAncestors = [...context.sourceCode.getAncestors(node)];
} else if (BITWISE_OPERATORS.includes(expression.operator)) {
fileContainsSeveralBitwiseOperations = true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1788/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function reportExempting(
return (context, reportDescriptor) => {
if ('node' in reportDescriptor) {
const node = reportDescriptor['node'] as AssignmentPattern;
const scope = context.getScope();
const scope = context.sourceCode.getScope(node);
const variable = scope.variables.find(value => isIdentifier(node.left, value.name));
const enclosingFunction = variable?.defs?.[0]?.node as BaseFunction;
if (enclosingFunction && !exemptionCondition(enclosingFunction)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1848/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const rule: Rule.RuleModule = {
};

function isTryable(node: estree.Node, context: Rule.RuleContext) {
const ancestors = context.getAncestors();
const ancestors = context.sourceCode.getAncestors(node);
let parent = undefined;
let child = node;
while ((parent = ancestors.pop()) !== undefined) {
Expand Down
26 changes: 20 additions & 6 deletions packages/jsts/src/rules/S1854/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const rule: Rule.RuleModule = {
const variableUsages = new Map<Variable, Set<string>>();
const referencesUsedInDestructuring = new Set<ReferenceLike>();
const destructuringStack: DestructuringContext[] = [];
const codePathSegments: CodePathSegment[][] = [];
let currentCodePathSegments: CodePathSegment[] = [];

return {
':matches(AssignmentExpression, VariableDeclarator[init])': (node: estree.Node) => {
Expand All @@ -50,7 +52,7 @@ export const rule: Rule.RuleModule = {
popAssignmentContext();
},
Identifier: (node: estree.Node) => {
if (isEnumConstant()) {
if (isEnumConstant(node)) {
return;
}
checkIdentifierUsage(node as estree.Identifier);
Expand Down Expand Up @@ -89,12 +91,19 @@ export const rule: Rule.RuleModule = {
// CodePath events
onCodePathSegmentStart: (segment: CodePathSegment) => {
liveVariablesMap.set(segment.id, new LiveVariables(segment));
currentCodePathSegments.push(segment);
},
onCodePathStart: codePath => {
pushContext(new CodePathContext(codePath));
codePathSegments.push(currentCodePathSegments);
currentCodePathSegments = [];
},
onCodePathSegmentEnd() {
currentCodePathSegments.pop();
},
onCodePathEnd: () => {
popContext();
currentCodePathSegments = codePathSegments.pop() || [];
},
};

Expand Down Expand Up @@ -174,8 +183,10 @@ export const rule: Rule.RuleModule = {
);
}

function isEnumConstant() {
return (context.getAncestors() as TSESTree.Node[]).some(n => n.type === 'TSEnumDeclaration');
function isEnumConstant(node: estree.Node) {
return (context.sourceCode.getAncestors(node) as TSESTree.Node[]).some(
n => n.type === 'TSEnumDeclaration',
);
}

function isDefaultParameter(ref: ReferenceLike) {
Expand Down Expand Up @@ -246,7 +257,10 @@ export const rule: Rule.RuleModule = {
if (isJSXAttributeName(node)) {
return {};
}
const jsxReference = new JSXReference(node, context.getScope());
const jsxReference = new JSXReference(
node,
context.sourceCode.getScope(node as unknown as estree.Node),
);
return { ref: jsxReference, variable: jsxReference.resolved };
}

Expand All @@ -261,7 +275,7 @@ export const rule: Rule.RuleModule = {
const assignment = peek(assignmentStack);
assignment.add(ref);
} else {
peek(codePathStack).codePath.currentSegments.forEach(segment => {
currentCodePathSegments.forEach(segment => {
lvaForSegment(segment).add(ref);
});
}
Expand Down Expand Up @@ -303,7 +317,7 @@ export const rule: Rule.RuleModule = {
}

function resolveReference(node: estree.Identifier) {
return resolveReferenceRecursively(node, context.getScope());
return resolveReferenceRecursively(node, context.sourceCode.getScope(node));
}

function resolveReferenceRecursively(
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S1994/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const rule: Rule.RuleModule = {
) {
if (forLoopStack.length > 0) {
const currentLoop = peekFor();
const parentChain = context.getAncestors();
const parentChain = context.sourceCode.getAncestors(node);
parentChain.push(node);
const forLoopChild = getChild(currentLoop.forLoop);
if (forLoopChild) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2123/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const rule: Rule.RuleModule = {
if (
!updateExpression.prefix &&
argument.type === 'Identifier' &&
isLocalIdentifier(argument, context.getScope())
isLocalIdentifier(argument, context.sourceCode.getScope(node))
) {
reportUpdateExpression(updateExpression);
}
Expand Down
11 changes: 7 additions & 4 deletions packages/jsts/src/rules/S2234/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ export const rule: Rule.RuleModule = {
argumentIndex,
functionCall,
);
if (swappedArgumentName && !areComparedArguments([argumentName, swappedArgumentName])) {
if (
swappedArgumentName &&
!areComparedArguments([argumentName, swappedArgumentName], functionCall)
) {
raiseIssue(argumentName, swappedArgumentName, functionDeclaration, functionCall);
return;
}
}
}
}

function areComparedArguments(argumentNames: string[]): boolean {
function areComparedArguments(argumentNames: string[], node: estree.Node): boolean {
function getName(node: estree.Node): string | undefined {
switch (node.type) {
case 'Identifier':
Expand All @@ -102,8 +105,8 @@ export const rule: Rule.RuleModule = {
argumentNames.length
);
}
const maybeIfStmt = context
.getAncestors()
const maybeIfStmt = context.sourceCode
.getAncestors(node)
.reverse()
.find(ancestor => ancestor.type === 'IfStatement');
if (maybeIfStmt) {
Expand Down
10 changes: 6 additions & 4 deletions packages/jsts/src/rules/S2259/rule.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.
*/
// https://sonarsource.github.io/rspec/#/rspec/S22259/javascript
// https://sonarsource.github.io/rspec/#/rspec/S2259/javascript

import { Rule, Scope } from 'eslint';
import * as estree from 'estree';
Expand Down Expand Up @@ -69,7 +69,7 @@ export const rule: Rule.RuleModule = {
'LogicalExpression MemberExpression'(node: estree.Node) {
const { object, optional } = node as estree.MemberExpression;
if (!optional) {
const ancestors = context.getAncestors();
const ancestors = context.sourceCode.getAncestors(node);
const enclosingLogicalExpression = ancestors.find(
n => n.type === 'LogicalExpression',
) as estree.LogicalExpression;
Expand Down Expand Up @@ -148,13 +148,15 @@ function checkNullDereference(
if (node.type !== 'Identifier') {
return;
}
const scope = context.getScope();
const scope = context.sourceCode.getScope(node);
const symbol = scope.references.find(v => v.identifier === node)?.resolved;
if (!symbol) {
return;
}

const enclosingFunction = context.getAncestors().find(n => functionLike.has(n.type));
const enclosingFunction = context.sourceCode
.getAncestors(node)
.find(n => functionLike.has(n.type));

if (
!alreadyRaisedSymbols.has(symbol) &&
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2392/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const rule: Rule.RuleModule = {
return idRange[0] < scopeRange[0] || idRange[1] > scopeRange[1];
}

context.getDeclaredVariables(node).forEach(variable => {
context.sourceCode.getDeclaredVariables(node).forEach(variable => {
const referencesOutside = variable.references
.map(ref => ref.identifier)
.filter(isOutsideOfScope);
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S2424/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export const rule: Rule.RuleModule = {
}

return {
Program: () => {
checkScope(context.getScope());
Program: (node: estree.Node) => {
checkScope(context.sourceCode.getScope(node));
},
'Program:exit': () => {
overriden.forEach(node => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S2486/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const rule: Rule.RuleModule = {
return {
'CatchClause[param.type="Identifier"]'(node: estree.CatchClause) {
const param = node.param as estree.Identifier;
const scope = context.getScope();
const scope = context.sourceCode.getScope(node);
const variable = getVariableFromScope(scope, param.name);
if (variable?.references.length === 0) {
context.report({
Expand Down
5 changes: 3 additions & 2 deletions packages/jsts/src/rules/S2703/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import { Rule } from 'eslint';
import { flatMap, globalsByLibraries } from '../helpers';
import estree from 'estree';

const excludedNames = new Set(flatMap(Object.values(globalsByLibraries), globals => globals));

Expand All @@ -33,8 +34,8 @@ export const rule: Rule.RuleModule = {
},
create(context: Rule.RuleContext) {
return {
'Program:exit'() {
const globalScope = context.getScope();
'Program:exit'(node: estree.Node) {
const globalScope = context.sourceCode.getScope(node);
const alreadyReported: Set<string> = new Set();
globalScope.through
.filter(ref => ref.isWrite())
Expand Down
Loading
Loading