Skip to content

Commit 4147825

Browse files
authored
feat: enforce newlines after if-statements, add fixers, and adjust comment style (#24)
1 parent f9f7d36 commit 4147825

File tree

9 files changed

+60
-25
lines changed

9 files changed

+60
-25
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 18.20.3

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blitz/eslint-plugin",
3-
"version": "0.0.44",
3+
"version": "0.0.45",
44
"description": "An ESLint config to enforce a consistent code styles across StackBlitz projects",
55
"keywords": [
66
"eslint",
@@ -17,12 +17,16 @@
1717
"LICENSE"
1818
],
1919
"scripts": {
20-
"build": "tsc -b",
20+
"build": "rm -rf dist && tsc -b",
2121
"test": "jest",
22+
"lint": "eslint '{src,test}/**/*'",
2223
"preversion": "npm test",
2324
"postversion": "git push && git push --tags",
2425
"prepack": "npm run build"
2526
},
27+
"engines": {
28+
"node": "^18.0.0 || ^20.0.0"
29+
},
2630
"dependencies": {
2731
"@typescript-eslint/eslint-plugin": "5.59.7",
2832
"@typescript-eslint/parser": "^5.59.7",

src/configs/recommended.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export = {
9191
prev: '*',
9292
next: 'break',
9393
},
94+
{
95+
blankLine: 'always',
96+
prev: 'if',
97+
next: '*',
98+
},
9499
],
95100

96101
'@typescript-eslint/no-unused-vars': [

src/rules/catch-error-name.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ export default createRule<Options, MessageIds>({
6464
originalName,
6565
fixedName: expectedName,
6666
},
67+
fix: (fixer) => {
68+
if (node.param) {
69+
return fixer.replaceText(node.param, 'error');
70+
}
71+
72+
return null;
73+
},
6774
});
6875
}
6976
}

src/rules/comment-syntax.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default createRule<Options, MessageIds>({
140140
const secondChar = comment.value[1];
141141
const lastChar = comment.value[comment.value.length - 1];
142142

143-
if (firstChar !== SPACE_CHARCODE && firstChar !== SLASH_CHARCODE && (!isRegion(comment.value))) {
143+
if (firstChar !== SPACE_CHARCODE && firstChar !== SLASH_CHARCODE && !isRegion(comment.value)) {
144144
context.report({ node: comment, messageId: 'shouldStartWithSpace' });
145145

146146
// if this one fails, the others are interpreted incorrectly
@@ -155,7 +155,7 @@ export default createRule<Options, MessageIds>({
155155
context.report({ node: comment, messageId: 'lineCommentCapital' });
156156
}
157157

158-
if (lastChar === '.') {
158+
if (lastChar === '.' && !comment.value.endsWith('etc.') && !comment.value.endsWith('...')) {
159159
context.report({ node: comment, messageId: 'lineCommentEnding' });
160160
}
161161

src/rules/lines-around-comment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AST_NODE_TYPES, ESLintUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
2-
import { createRule, InferMessageIdsTypeFromRule, InferOptionsTypeFromRule } from '../util';
2+
import { InferMessageIdsTypeFromRule, InferOptionsTypeFromRule, createRule } from '../util';
33

44
export const ruleName = 'lines-around-comment';
55

@@ -44,8 +44,6 @@ export default createRule<Options, MessageIds>({
4444
docs: {
4545
description: 'Require empty lines around comments',
4646
recommended: false,
47-
extendsBaseRule: true,
48-
requiresTypeChecking: true,
4947
},
5048
schema: [
5149
{
@@ -213,7 +211,7 @@ export default createRule<Options, MessageIds>({
213211

214212
return {
215213
Program: (node) => {
216-
rules.Program(node);
214+
rules.Program?.(node);
217215
},
218216
};
219217
},

src/rules/newline-before-return.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,17 @@ export default createRule<Options, MessageIds>({
125125
context.report({
126126
node,
127127
messageId: 'default',
128-
suggest: [
129-
{
130-
messageId,
131-
fix(fixer) {
132-
const tokenBefore = sourceCode.getTokenBefore(node);
133-
134-
if (!tokenBefore) {
135-
return null;
136-
}
137-
138-
const newlines = node.loc.start.line === tokenBefore.loc.end.line ? '\n\n' : '\n';
139-
140-
return fixer.insertTextAfter(tokenBefore, newlines);
141-
},
142-
},
143-
],
128+
fix: (fixer) => {
129+
const tokenBefore = sourceCode.getTokenBefore(node);
130+
131+
if (!tokenBefore) {
132+
return null;
133+
}
134+
135+
const newlines = node.loc.start.line === tokenBefore.loc.end.line ? '\n\n' : '\n';
136+
137+
return fixer.insertTextAfter(tokenBefore, newlines);
138+
},
144139
});
145140
},
146141
};

test/rules/catch-error-name.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ ruleTester().run(ruleName, rule, {
2020
messageId: 'default',
2121
},
2222
],
23+
output: stripIndent`
24+
try {} catch (error) {}
25+
`,
2326
},
2427
{
2528
code: stripIndent`
26-
try {} catch (e) {}
29+
try {} catch (e) {}
2730
`,
2831
errors: [
2932
{
3033
messageId: 'default',
3134
},
3235
],
36+
output: stripIndent`
37+
try {} catch (error) {}
38+
`,
3339
},
3440
],
3541
});

test/rules/newline-before-return.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ ruleTester().run(ruleName, rule, {
5656
messageId,
5757
},
5858
],
59+
output: stripIndent`
60+
function foo() {
61+
const foo = 1;
62+
const bar = 2;
63+
64+
return foo && bar;
65+
}
66+
`,
5967
},
6068
{
6169
code: stripIndent`
@@ -73,6 +81,17 @@ ruleTester().run(ruleName, rule, {
7381
messageId,
7482
},
7583
],
84+
output: stripIndent`
85+
function foo() {
86+
const foo = 1;
87+
88+
if (foo) {
89+
// do some work
90+
}
91+
92+
return 1;
93+
}
94+
`,
7695
},
7796
],
7897
});

0 commit comments

Comments
 (0)