Skip to content

Commit 04bc8f4

Browse files
zgliczclaude
andauthored
JS-1007 Fix S6582: Prefer optional chaining expressions (#6109)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d0d47eb commit 04bc8f4

42 files changed

Lines changed: 73 additions & 115 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/jsts/src/rules/S100/rule.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ export const rule: Rule.RuleModule = {
117117
if (functionLike.has(ancestors[i].type)) {
118118
const enclosingFunction = ancestors[i];
119119
if (
120-
knowledge &&
121-
knowledge.func === enclosingFunction &&
122-
node.argument &&
123-
(node.argument as any).type.startsWith('JSX')
120+
knowledge?.func === enclosingFunction &&
121+
(node.argument as any)?.type?.startsWith('JSX')
124122
) {
125123
knowledge.returnsJSX = true;
126124
}

packages/jsts/src/rules/S1172/rule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function reportUnusedArgument(
5959
context: Rule.RuleContext,
6060
) {
6161
const parent = (node as TSESTree.Node).parent;
62-
if (parent && parent.type === 'Property' && parent.kind === 'set') {
62+
if (parent?.type === 'Property' && parent.kind === 'set') {
6363
return;
6464
}
6565

@@ -137,7 +137,7 @@ function getParameterRemovalSuggestion(
137137
token => token.value === ')',
138138
);
139139
let [start, end] = param.range!;
140-
if (openingParenthesis && openingParenthesis.value === '(') {
140+
if (openingParenthesis?.value === '(') {
141141
start = openingParenthesis.range[0];
142142
end = closingParenthesis!.range[1];
143143
}

packages/jsts/src/rules/S1226/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const rule: Rule.RuleModule = {
113113
return {
114114
onCodePathStart(_codePath: Rule.CodePath, node: estree.Node) {
115115
const currentScope = context.sourceCode.getScope(node);
116-
if (currentScope && currentScope.type === 'function') {
116+
if (currentScope?.type === 'function') {
117117
const { referencesByIdentifier, variablesToCheck, variablesToCheckInCurrentScope } =
118118
computeNewContextInfo(variableUsageContext, context, node);
119119

packages/jsts/src/rules/S138/rule.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ function isFullLineComment(line: string, lineNumber: number, comment: estree.Com
171171
function isIIFE(node: estree.Node, parent: estree.Node) {
172172
return (
173173
node.type === 'FunctionExpression' &&
174-
parent &&
175-
parent.type === 'CallExpression' &&
174+
parent?.type === 'CallExpression' &&
176175
parent.callee === node
177176
);
178177
}

packages/jsts/src/rules/S1451/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function checkPlainText(expectedLines: string[], context: Rule.RuleContext) {
8686
function checkRegularExpression(searchPattern: RegExp, context: Rule.RuleContext) {
8787
const fileContent = context.sourceCode.getText();
8888
const match = searchPattern.exec(fileContent);
89-
if (!match || match.index !== 0) {
89+
if (match?.index !== 0) {
9090
addFileIssue(context);
9191
}
9292
}

packages/jsts/src/rules/S1481/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const rule: Rule.RuleModule = {
4242
}
4343
if (toCheck === 'let-const-function') {
4444
const def = v.defs[0];
45-
if (def.parent && def.parent.type === 'VariableDeclaration' && def.parent.kind === 'var') {
45+
if (def.parent?.type === 'VariableDeclaration' && def.parent.kind === 'var') {
4646
return;
4747
}
4848
}

packages/jsts/src/rules/S1488/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const rule: Rule.RuleModule = {
7171
}
7272

7373
// there must be only one "read" - in `return` or `throw`
74-
if (sameVariable && sameVariable.references.filter(ref => ref.isRead()).length === 1) {
74+
if (sameVariable?.references.filter(ref => ref.isRead()).length === 1) {
7575
context.report({
7676
messageId: 'doImmediateAction',
7777
data: {

packages/jsts/src/rules/S1515/rule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function isIIFE(node: estree.Node, context: Rule.RuleContext) {
9999

100100
function isAllowedCallbacks(context: Rule.RuleContext, node: estree.Node) {
101101
const parent = getParent(context, node);
102-
if (parent && parent.type === 'CallExpression') {
102+
if (parent?.type === 'CallExpression') {
103103
const callee = parent.callee;
104104
if (callee.type === 'MemberExpression') {
105105
return callee.property.type === 'Identifier' && allowedCallbacks.has(callee.property.name);
@@ -113,7 +113,7 @@ function isSafe(ref: Scope.Reference, loopNode: LoopLike) {
113113
if (variable) {
114114
const definition = variable.defs[0];
115115
const declaration = definition?.parent;
116-
const kind = declaration && declaration.type === 'VariableDeclaration' ? declaration.kind : '';
116+
const kind = declaration?.type === 'VariableDeclaration' ? declaration.kind : '';
117117

118118
if (kind !== 'let' && kind !== 'const') {
119119
return hasConstValue(variable, loopNode);

packages/jsts/src/rules/S1854/rule.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,12 @@ export const rule: Rule.RuleModule = {
179179

180180
function isIncrementOrDecrement(ref: ReferenceLike) {
181181
const parent = (ref.identifier as TSESTree.Identifier).parent;
182-
return parent && parent.type === 'UpdateExpression';
182+
return parent?.type === 'UpdateExpression';
183183
}
184184

185185
function isNullAssignment(ref: ReferenceLike) {
186186
const parent = (ref.identifier as TSESTree.Identifier).parent;
187-
return (
188-
parent &&
189-
parent.type === 'AssignmentExpression' &&
190-
isNullLiteral(parent.right as estree.Node)
191-
);
187+
return parent?.type === 'AssignmentExpression' && isNullLiteral(parent.right as estree.Node);
192188
}
193189

194190
function isEnumConstant(node: estree.Node) {
@@ -202,7 +198,7 @@ export const rule: Rule.RuleModule = {
202198
return false;
203199
}
204200
const parent = (ref.identifier as TSESTree.Identifier).parent;
205-
return parent && parent.type === 'AssignmentPattern';
201+
return parent?.type === 'AssignmentPattern';
206202
}
207203

208204
function isLocalVar(variable: Scope.Variable) {
@@ -434,7 +430,7 @@ function findJSXVariableInScope(
434430

435431
function isJSXAttributeName(node: TSESTree.JSXIdentifier) {
436432
const parent = node.parent;
437-
return parent && parent.type === 'JSXAttribute' && parent.name === node;
433+
return parent?.type === 'JSXAttribute' && parent.name === node;
438434
}
439435

440436
function isBasicValue(node: TSESTree.Node): boolean {

packages/jsts/src/rules/S2201/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const rule: Rule.RuleModule = {
188188
const { callee } = call;
189189
if (callee.type === 'MemberExpression') {
190190
const { parent } = node as TSESTree.MemberExpression;
191-
if (parent && parent.type === 'ExpressionStatement') {
191+
if (parent?.type === 'ExpressionStatement') {
192192
const methodName = context.sourceCode.getText(callee.property as estree.Node);
193193
const objectType = services.program
194194
.getTypeChecker()

0 commit comments

Comments
 (0)