Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 93c0b18

Browse files
authored
Add prettier and reformat code (#221)
1 parent beacded commit 93c0b18

File tree

60 files changed

+989
-698
lines changed

Some content is hidden

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

60 files changed

+989
-698
lines changed

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
"node": ">=10"
1616
},
1717
"scripts": {
18-
"build": "rimraf lib && tsc -d -p tsconfig-src.json",
18+
"build": "rimraf lib && yarn check-format && tsc -d -p tsconfig-src.json",
1919
"test": "jest",
2020
"ruling": "ts-node ruling/index.ts",
2121
"typecheck": "tsc -p tsconfig.json",
2222
"lint": "eslint --ext js,ts src tests ruling/index.ts",
2323
"precommit": "lint-staged && yarn typecheck",
24-
"prepack": "yarn build"
24+
"prepack": "yarn build",
25+
"format": "prettier --write \"{src,tests}/**/!(*.lint).ts\"",
26+
"check-format": "prettier --list-different \"{src,tests}/**/!(*.lint).ts\""
2527
},
2628
"peerDependencies": {
2729
"eslint": "^5.0.0 || ^6.0.0 || ^7.0.0"
@@ -46,15 +48,18 @@
4648
"lint-staged": "7.3.0",
4749
"lodash": "4.17.21",
4850
"minimist": "1.2.3",
49-
"prettier": "1.11.1",
51+
"prettier": "2.3.0",
5052
"rimraf": "2.6.2",
5153
"ts-jest": "26.5.1",
5254
"ts-node": "9.1.1",
5355
"typescript": "4.1.4"
5456
},
5557
"prettier": {
56-
"printWidth": 120,
57-
"trailingComma": "all"
58+
"printWidth": 100,
59+
"trailingComma": "all",
60+
"singleQuote": true,
61+
"arrowParens": "avoid",
62+
"endOfLine": "lf"
5863
},
5964
"jest": {
6065
"roots": ["tests", "src"],

src/index.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
import { Linter } from "eslint";
20+
import { Linter } from 'eslint';
2121

2222
const sonarjsRules: [string, Linter.RuleLevel][] = [
23-
["cognitive-complexity", "error"],
24-
["max-switch-cases", "error"],
25-
["no-all-duplicated-branches", "error"],
26-
["no-collapsible-if", "error"],
27-
["no-collection-size-mischeck", "error"],
28-
["no-duplicate-string", "error"],
29-
["no-duplicated-branches", "error"],
30-
["no-element-overwrite", "error"],
31-
["no-extra-arguments", "error"],
32-
["no-identical-conditions", "error"],
33-
["no-identical-functions", "error"],
34-
["no-identical-expressions", "error"],
35-
["no-inverted-boolean-check", "error"],
36-
["no-one-iteration-loop", "error"],
37-
["no-redundant-boolean", "error"],
38-
["no-redundant-jump", "error"],
39-
["no-same-line-conditional", "error"],
40-
["no-small-switch", "error"],
41-
["no-unused-collection", "error"],
42-
["no-use-of-empty-return-value", "error"],
43-
["no-useless-catch", "error"],
44-
["prefer-immediate-return", "error"],
45-
["prefer-object-literal", "error"],
46-
["prefer-single-boolean-return", "error"],
47-
["prefer-while", "error"],
23+
['cognitive-complexity', 'error'],
24+
['max-switch-cases', 'error'],
25+
['no-all-duplicated-branches', 'error'],
26+
['no-collapsible-if', 'error'],
27+
['no-collection-size-mischeck', 'error'],
28+
['no-duplicate-string', 'error'],
29+
['no-duplicated-branches', 'error'],
30+
['no-element-overwrite', 'error'],
31+
['no-extra-arguments', 'error'],
32+
['no-identical-conditions', 'error'],
33+
['no-identical-functions', 'error'],
34+
['no-identical-expressions', 'error'],
35+
['no-inverted-boolean-check', 'error'],
36+
['no-one-iteration-loop', 'error'],
37+
['no-redundant-boolean', 'error'],
38+
['no-redundant-jump', 'error'],
39+
['no-same-line-conditional', 'error'],
40+
['no-small-switch', 'error'],
41+
['no-unused-collection', 'error'],
42+
['no-use-of-empty-return-value', 'error'],
43+
['no-useless-catch', 'error'],
44+
['prefer-immediate-return', 'error'],
45+
['prefer-object-literal', 'error'],
46+
['prefer-single-boolean-return', 'error'],
47+
['prefer-while', 'error'],
4848
];
4949

5050
const sonarjsRuleModules: any = {};
5151

5252
const configs: { recommended: Linter.Config & { plugins: string[] } } = {
53-
recommended: { plugins: ["sonarjs"], rules: {} },
53+
recommended: { plugins: ['sonarjs'], rules: {} },
5454
};
5555

5656
sonarjsRules.forEach(rule => (sonarjsRuleModules[rule[0]] = require(`./rules/${rule[0]}`)));

src/rules/cognitive-complexity.ts

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@
1919
*/
2020
// https://jira.sonarsource.com/browse/RSPEC-3776
2121

22-
import { Rule } from "eslint";
23-
import * as estree from "estree";
24-
import { getParent, isArrowFunctionExpression, isIfStatement, isLogicalExpression } from "../utils/nodes";
22+
import { Rule } from 'eslint';
23+
import * as estree from 'estree';
24+
import {
25+
getParent,
26+
isArrowFunctionExpression,
27+
isIfStatement,
28+
isLogicalExpression,
29+
} from '../utils/nodes';
2530
import {
2631
getMainFunctionTokenLocation,
2732
getFirstToken,
2833
getFirstTokenAfter,
2934
report,
3035
IssueLocation,
3136
issueLocation,
32-
} from "../utils/locations";
37+
} from '../utils/locations';
3338

3439
const DEFAULT_THRESHOLD = 15;
3540

@@ -44,18 +49,18 @@ type OptionalLocation = estree.SourceLocation | null | undefined;
4449

4550
const rule: Rule.RuleModule = {
4651
meta: {
47-
type: "suggestion",
52+
type: 'suggestion',
4853
schema: [
49-
{ type: "integer", minimum: 0 },
54+
{ type: 'integer', minimum: 0 },
5055
{
5156
// internal parameter
52-
enum: ["sonar-runtime", "metric"],
57+
enum: ['sonar-runtime', 'metric'],
5358
},
5459
],
5560
},
5661
create(context: Rule.RuleContext) {
5762
const threshold: number = getThreshold();
58-
const isFileComplexity: boolean = context.options.includes("metric");
63+
const isFileComplexity: boolean = context.options.includes('metric');
5964

6065
/** Complexity of the file */
6166
let fileComplexity = 0;
@@ -108,19 +113,19 @@ const rule: Rule.RuleModule = {
108113
}> = [];
109114

110115
return {
111-
":function": (node: estree.Node) => {
116+
':function': (node: estree.Node) => {
112117
onEnterFunction(node as estree.Function);
113118
},
114-
":function:exit"(node: estree.Node) {
119+
':function:exit'(node: estree.Node) {
115120
onLeaveFunction(node as estree.Function);
116121
},
117122

118-
"*"(node: estree.Node) {
123+
'*'(node: estree.Node) {
119124
if (nestingNodes.has(node)) {
120125
nesting++;
121126
}
122127
},
123-
"*:exit"(node: estree.Node) {
128+
'*:exit'(node: estree.Node) {
124129
if (nestingNodes.has(node)) {
125130
nesting--;
126131
nestingNodes.delete(node);
@@ -129,7 +134,7 @@ const rule: Rule.RuleModule = {
129134
Program() {
130135
fileComplexity = 0;
131136
},
132-
"Program:exit"(node: estree.Node) {
137+
'Program:exit'(node: estree.Node) {
133138
if (isFileComplexity) {
134139
// as issues are the only communication channel of a rule
135140
// we pass data as serialized json as an issue message
@@ -209,13 +214,23 @@ const rule: Rule.RuleModule = {
209214
secondLevelFunctions.forEach(secondLevelFunction => {
210215
totalComplexity = totalComplexity.concat(secondLevelFunction.complexityIfNested);
211216
});
212-
checkFunction(totalComplexity, getMainFunctionTokenLocation(node, getParent(context), context));
217+
checkFunction(
218+
totalComplexity,
219+
getMainFunctionTokenLocation(node, getParent(context), context),
220+
);
213221
} else {
214-
checkFunction(topLevelOwnComplexity, getMainFunctionTokenLocation(node, getParent(context), context));
222+
checkFunction(
223+
topLevelOwnComplexity,
224+
getMainFunctionTokenLocation(node, getParent(context), context),
225+
);
215226
secondLevelFunctions.forEach(secondLevelFunction => {
216227
checkFunction(
217228
secondLevelFunction.complexityIfThisSecondaryIsTopLevel,
218-
getMainFunctionTokenLocation(secondLevelFunction.node, secondLevelFunction.parent, context),
229+
getMainFunctionTokenLocation(
230+
secondLevelFunction.node,
231+
secondLevelFunction.parent,
232+
context,
233+
),
219234
);
220235
});
221236
}
@@ -269,7 +284,9 @@ const rule: Rule.RuleModule = {
269284
}
270285
}
271286

272-
function visitContinueOrBreakStatement(statement: estree.ContinueStatement | estree.BreakStatement) {
287+
function visitContinueOrBreakStatement(
288+
statement: estree.ContinueStatement | estree.BreakStatement,
289+
) {
273290
if (statement.label) {
274291
addComplexity(getFirstToken(statement, context).loc);
275292
}
@@ -289,7 +306,7 @@ const rule: Rule.RuleModule = {
289306

290307
function visitReturnStatement({ argument }: estree.ReturnStatement) {
291308
// top level function
292-
if (enclosingFunctions.length === 1 && argument && (argument.type as any) === "JSXElement") {
309+
if (enclosingFunctions.length === 1 && argument && (argument.type as any) === 'JSXElement') {
293310
reactFunctionalComponent.returnsJsx = true;
294311
}
295312
}
@@ -305,7 +322,7 @@ const rule: Rule.RuleModule = {
305322
}
306323

307324
const parent = getParent(context);
308-
if (parent && parent.type === "VariableDeclarator" && parent.id.type === "Identifier") {
325+
if (parent && parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
309326
return checkFirstLetter(parent.id.name);
310327
}
311328

@@ -330,7 +347,11 @@ const rule: Rule.RuleModule = {
330347
function flattenLogicalExpression(node: estree.Node): estree.LogicalExpression[] {
331348
if (isLogicalExpression(node)) {
332349
consideredLogicalExpressions.add(node);
333-
return [...flattenLogicalExpression(node.left), node, ...flattenLogicalExpression(node.right)];
350+
return [
351+
...flattenLogicalExpression(node.left),
352+
node,
353+
...flattenLogicalExpression(node.right),
354+
];
334355
}
335356
return [];
336357
}
@@ -376,7 +397,8 @@ const rule: Rule.RuleModule = {
376397
if (complexityAmount > threshold) {
377398
const secondaryLocations: IssueLocation[] = complexity.map(complexityPoint => {
378399
const { complexity, location } = complexityPoint;
379-
const message = complexity === 1 ? "+1" : `+${complexity} (incl. ${complexity - 1} for nesting)`;
400+
const message =
401+
complexity === 1 ? '+1' : `+${complexity} (incl. ${complexity - 1} for nesting)`;
380402
return issueLocation(location, undefined, message);
381403
});
382404

src/rules/max-switch-cases.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@
1919
*/
2020
// https://jira.sonarsource.com/browse/RSPEC-1479
2121

22-
import { Rule } from "eslint";
23-
import { Node, SwitchStatement, SwitchCase } from "estree";
22+
import { Rule } from 'eslint';
23+
import { Node, SwitchStatement, SwitchCase } from 'estree';
2424

25-
const MESSAGE = "Reduce the number of non-empty switch cases from {{numSwitchCases}} to at most {{maxSwitchCases}}.";
25+
const MESSAGE =
26+
'Reduce the number of non-empty switch cases from {{numSwitchCases}} to at most {{maxSwitchCases}}.';
2627

2728
const DEFAULT_MAX_SWITCH_CASES = 30;
2829
let maxSwitchCases = DEFAULT_MAX_SWITCH_CASES;
2930

3031
const rule: Rule.RuleModule = {
3132
meta: {
32-
type: "suggestion",
33+
type: 'suggestion',
3334
schema: [
3435
{
35-
type: "integer",
36+
type: 'integer',
3637
minimum: 0,
3738
},
3839
],
@@ -41,7 +42,9 @@ const rule: Rule.RuleModule = {
4142
if (context.options.length > 0) {
4243
maxSwitchCases = context.options[0];
4344
}
44-
return { SwitchStatement: (node: Node) => visitSwitchStatement(node as SwitchStatement, context) };
45+
return {
46+
SwitchStatement: (node: Node) => visitSwitchStatement(node as SwitchStatement, context),
47+
};
4548
},
4649
};
4750

@@ -54,7 +57,10 @@ function visitSwitchStatement(switchStatement: SwitchStatement, context: Rule.Ru
5457
context.report({
5558
message: MESSAGE,
5659
loc: switchKeyword.loc,
57-
data: { numSwitchCases: nonEmptyCases.length.toString(), maxSwitchCases: maxSwitchCases.toString() },
60+
data: {
61+
numSwitchCases: nonEmptyCases.length.toString(),
62+
maxSwitchCases: maxSwitchCases.toString(),
63+
},
5864
});
5965
}
6066
}

src/rules/no-all-duplicated-branches.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
*/
2020
// https://jira.sonarsource.com/browse/RSPEC-3923
2121

22-
import { Rule } from "eslint";
23-
import * as estree from "estree";
24-
import { getParent, isIfStatement } from "../utils/nodes";
25-
import { areEquivalent } from "../utils/equivalence";
26-
import { collectIfBranches, collectSwitchBranches } from "../utils/conditions";
22+
import { Rule } from 'eslint';
23+
import * as estree from 'estree';
24+
import { getParent, isIfStatement } from '../utils/nodes';
25+
import { areEquivalent } from '../utils/equivalence';
26+
import { collectIfBranches, collectSwitchBranches } from '../utils/conditions';
2727

28-
const MESSAGE = "Remove this conditional structure or edit its code blocks so that they're not all the same.";
28+
const MESSAGE =
29+
"Remove this conditional structure or edit its code blocks so that they're not all the same.";
2930
const MESSAGE_CONDITIONAL_EXPRESSION =
3031
'This conditional operation returns the same value whether the condition is "true" or "false".';
3132

3233
const rule: Rule.RuleModule = {
3334
meta: {
34-
type: "problem",
35+
type: 'problem',
3536
},
3637
create(context: Rule.RuleContext) {
3738
return {

src/rules/no-collapsible-if.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
*/
2020
// https://jira.sonarsource.com/browse/RSPEC-1066
2121

22-
import { Rule } from "eslint";
23-
import * as estree from "estree";
24-
import { isIfStatement, isBlockStatement } from "../utils/nodes";
25-
import { report, issueLocation } from "../utils/locations";
22+
import { Rule } from 'eslint';
23+
import * as estree from 'estree';
24+
import { isIfStatement, isBlockStatement } from '../utils/nodes';
25+
import { report, issueLocation } from '../utils/locations';
2626

2727
const rule: Rule.RuleModule = {
2828
meta: {
29-
type: "suggestion",
29+
type: 'suggestion',
3030
schema: [
3131
{
3232
// internal parameter
33-
enum: ["sonar-runtime"],
33+
enum: ['sonar-runtime'],
3434
},
3535
],
3636
},

0 commit comments

Comments
 (0)