Skip to content

Commit f369071

Browse files
authored
JS-1493 Fix open SonarCloud maintainability issues (#6642)
1 parent eaaf9f3 commit f369071

File tree

17 files changed

+65
-54
lines changed

17 files changed

+65
-54
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const rule: Rule.RuleModule = {
5454
...DEFAULT_OPTIONS,
5555
...(context.options as FromSchema<typeof meta.schema>)[0],
5656
};
57-
const whitelist = ignoreStrings.split(',');
57+
const whitelist = new Set(ignoreStrings.split(','));
5858
return {
5959
Literal: (node: estree.Node) => {
6060
const literal = node as TSESTree.Literal;
@@ -67,7 +67,7 @@ export const rule: Rule.RuleModule = {
6767
const stringContent = literal.value.trim();
6868

6969
if (
70-
!whitelist.includes(literal.value) &&
70+
!whitelist.has(literal.value) &&
7171
!isExcludedByUsageContext(context, literal) &&
7272
stringContent.length >= MIN_LENGTH &&
7373
!NO_SEPARATOR_REGEXP.test(stringContent)

packages/jsts/src/rules/S134/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const fields = [
2222
[
2323
{
2424
field: 'maximumNestingLevel',
25-
description: `Maximum allowed \\"if/for/while/switch/try\\" statements nesting depth`,
25+
description: 'Maximum allowed "if/for/while/switch/try" statements nesting depth',
2626
default: 3,
2727
},
2828
],

packages/jsts/src/rules/S139/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const fields = [
2323
{
2424
field: 'ignorePattern',
2525
description: 'Pattern (JavaScript syntax) for text of trailing comments that are allowed.',
26-
default: `^\\s*[^\\s]+$`,
26+
default: String.raw`^\s*[^\s]+$`,
2727
displayName: 'pattern',
2828
},
2929
],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export const rule: Rule.RuleModule = {
372372
if (
373373
current.operator !== '||' &&
374374
current.operator !== '??' &&
375-
(!previous || previous.operator !== current.operator)
375+
previous?.operator !== current.operator
376376
) {
377377
const operatorTokenLoc = getFirstTokenAfter(
378378
current.left,

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

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

3737
function isBuiltInMethod(symbol: ts.Symbol) {
3838
const parent = symbol.valueDeclaration?.parent;
39-
if (!parent || parent.kind !== ts.SyntaxKind.InterfaceDeclaration) {
39+
if (parent?.kind !== ts.SyntaxKind.InterfaceDeclaration) {
4040
return false;
4141
}
4242
const parentSymbol = tc.getSymbolAtLocation((parent as ts.InterfaceDeclaration).name);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function isForIterationPattern(ref: Scope.Reference) {
199199
n => n.type === 'ForOfStatement' || n.type === 'ForInStatement',
200200
) as TSESTree.ForOfStatement | TSESTree.ForInStatement;
201201

202-
return forInOrOfStatement && forInOrOfStatement.right === ref.identifier;
202+
return forInOrOfStatement?.right === ref.identifier;
203203
}
204204

205205
function isElementRead(ref: Scope.Reference) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const rule: Rule.RuleModule = {
3939
const node = pNode as unknown as TSESTree.JSXAttribute;
4040

4141
const value = node.value;
42-
if (!value || value.type !== 'JSXExpressionContainer') {
42+
if (value?.type !== 'JSXExpressionContainer') {
4343
// key='foo' or just simply 'key'
4444
return;
4545
}

packages/jsts/src/rules/helpers/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function isRequire(node: Node): node is estree.CallExpression {
7575
*/
7676
function getModuleNameFromRequire(node: Node): estree.Literal | undefined {
7777
if (isRequire(node)) {
78-
const moduleName = (node as estree.CallExpression).arguments[0];
78+
const moduleName = node.arguments[0];
7979
if (moduleName.type === 'Literal') {
8080
return moduleName;
8181
}

packages/jsts/tests/tools/testers/comment-based/helpers/locations.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ export const LINE_ADJUSTMENT = String.raw`(?:@(?<lineAdjustment>(?<relativeAdjus
2323
const STARTS_WITH_LOCATION = /^ *\^/;
2424
const COUNT = String.raw`(?<count>\d+)`;
2525
const DIRECTION = '(?<direction>[<>])';
26-
const MESSAGE = '(?<message>.*?)';
26+
const MESSAGE_CONTENT = String.raw`(?:(?!\}\}(?!\})).)*`;
2727
const LOCATION_PATTERN = new RegExp(
28-
' *' +
28+
String.raw`^ *` +
2929
// highlighted range, ex: ^^^^ |OR| ^^^@12 |OR| ^^^@-2
30-
'(?<range>\\^(?:\\[(?<params>[^\\]]+)\\]|\\^+)?)' +
30+
String.raw`(?<range>\^(?:\[(?<params>[^\]]+)\]|\^+)?)` +
3131
LINE_ADJUSTMENT +
3232
// count, ex: 3 |OR| direction
33-
' *(?:' +
33+
String.raw` *(?:` +
3434
COUNT +
35-
'|' +
35+
String.raw`|` +
3636
DIRECTION +
37-
')?' +
37+
String.raw`)?` +
3838
// message, ex: {{msg}}
39-
' *(?:\\{\\{' +
40-
MESSAGE +
41-
'\\}\\})? *' +
42-
'(?:\r(\n?)|\n)?',
39+
String.raw` *(?:\{\{` +
40+
String.raw`(?<message>${MESSAGE_CONTENT})` +
41+
String.raw`\}\})? *` +
42+
String.raw`(?:\r?\n)?`,
4343
);
4444

4545
export abstract class Location {

packages/jsts/tests/tools/testers/comment-based/helpers/quickfixes.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,35 @@
1616
*/
1717
import { LineIssues } from './issues.js';
1818
import { Comment } from './comments.js';
19-
import { extractEffectiveLine, LINE_ADJUSTMENT } from './locations.js';
19+
import { extractEffectiveLine } from './locations.js';
2020

2121
const STARTS_WITH_QUICKFIX = /^ *(edit|del|add|fix)@/;
22-
export const QUICKFIX_SEPARATOR = '[,\\s]+';
23-
export const QUICKFIX_ID =
24-
'\\[\\[(?<quickfixes>\\w+(=\\d+)?!?(?:' + QUICKFIX_SEPARATOR + '(?:\\w+(=\\d+)?!?))*)\\]\\]';
22+
export const QUICKFIX_SEPARATOR = String.raw`[,\s]+`;
23+
export const QUICKFIX_ID = String.raw`\[\[(?<quickfixes>\w+(=\d+)?!?(?:${QUICKFIX_SEPARATOR}(?:\w+(=\d+)?!?))*)\]\]`;
24+
const BRACED_TEXT_CONTENT = String.raw`(?:(?!\}\}(?!\})).)*`;
25+
// Legacy fixtures support line adjustments both as `@+1` and `+1`.
26+
const QUICKFIX_LINE_ADJUSTMENT = String.raw`(?:@?(?<lineAdjustment>(?<relativeAdjustment>[+-])?\d+))?`;
2527
const QUICKFIX_DESCRIPTION_PATTERN = new RegExp(
26-
' *' +
28+
String.raw`^ *` +
2729
// quickfix description, ex: fix@qf1 {{Replace with foo}}
28-
'fix@(?<quickfixId>\\w+)' +
30+
String.raw`fix@(?<quickfixId>\w+)` +
2931
// message, ex: {{msg}}
30-
' *(?:\\{\\{(?<message>.*?)\\}\\}(?!\\}))? *' +
31-
'(?:\\r(\\n?)|\\n)?',
32+
String.raw` *(?:\{\{(?<message>${BRACED_TEXT_CONTENT})\}\}(?!\}))? *` +
33+
String.raw`(?:\r?\n)?$`,
3234
);
3335

3436
const QUICKFIX_CHANGE_PATTERN = new RegExp(
35-
' *' +
37+
String.raw`^ *` +
3638
// quickfix edit, ex: edit@qf1
37-
'(?<type>edit|add|del)@(?<quickfixId>\\w+)' +
38-
LINE_ADJUSTMENT +
39+
String.raw`(?<type>edit|add|del)@(?<quickfixId>\w+)` +
40+
QUICKFIX_LINE_ADJUSTMENT +
3941
// start and end columns, ex: [[sc=1;ec=5]] both are optional
40-
' *(?:\\[\\[' +
41-
'(?<firstColumnType>sc|ec)=(?<firstColumnValue>\\d+)(?:;(?<secondColumnType>sc|ec)=(?<secondColumnValue>\\d+))?' +
42-
'\\]\\])?' +
42+
String.raw` *(?:\[\[` +
43+
String.raw`(?<firstColumnType>sc|ec)=(?<firstColumnValue>\d+)(?:;(?<secondColumnType>sc|ec)=(?<secondColumnValue>\d+))?` +
44+
String.raw`\]\])?` +
4345
// contents to be applied, ex: {{foo}}
44-
' *(?:\\{\\{(?<contents>.*?)\\}\\}(?!\\}))?' +
45-
' *(?:\\r(\\n?)|\\n)?',
46+
String.raw` *(?:\{\{(?<contents>${BRACED_TEXT_CONTENT})\}\}(?!\}))?` +
47+
String.raw` *(?:\r?\n)?$`,
4648
);
4749

4850
type ChangeType = 'add' | 'del' | 'edit';

0 commit comments

Comments
 (0)