Skip to content

Commit

Permalink
feat: enforce newlines after if-statements, add fixers, and adjust co…
Browse files Browse the repository at this point in the history
…mment style (#24)
  • Loading branch information
d3lm authored Jun 25, 2024
1 parent f9f7d36 commit 4147825
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.20.3
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blitz/eslint-plugin",
"version": "0.0.44",
"version": "0.0.45",
"description": "An ESLint config to enforce a consistent code styles across StackBlitz projects",
"keywords": [
"eslint",
Expand All @@ -17,12 +17,16 @@
"LICENSE"
],
"scripts": {
"build": "tsc -b",
"build": "rm -rf dist && tsc -b",
"test": "jest",
"lint": "eslint '{src,test}/**/*'",
"preversion": "npm test",
"postversion": "git push && git push --tags",
"prepack": "npm run build"
},
"engines": {
"node": "^18.0.0 || ^20.0.0"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "5.59.7",
"@typescript-eslint/parser": "^5.59.7",
Expand Down
5 changes: 5 additions & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export = {
prev: '*',
next: 'break',
},
{
blankLine: 'always',
prev: 'if',
next: '*',
},
],

'@typescript-eslint/no-unused-vars': [
Expand Down
7 changes: 7 additions & 0 deletions src/rules/catch-error-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export default createRule<Options, MessageIds>({
originalName,
fixedName: expectedName,
},
fix: (fixer) => {
if (node.param) {
return fixer.replaceText(node.param, 'error');
}

return null;
},
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/comment-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default createRule<Options, MessageIds>({
const secondChar = comment.value[1];
const lastChar = comment.value[comment.value.length - 1];

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

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

if (lastChar === '.') {
if (lastChar === '.' && !comment.value.endsWith('etc.') && !comment.value.endsWith('...')) {
context.report({ node: comment, messageId: 'lineCommentEnding' });
}

Expand Down
6 changes: 2 additions & 4 deletions src/rules/lines-around-comment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AST_NODE_TYPES, ESLintUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
import { createRule, InferMessageIdsTypeFromRule, InferOptionsTypeFromRule } from '../util';
import { InferMessageIdsTypeFromRule, InferOptionsTypeFromRule, createRule } from '../util';

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

Expand Down Expand Up @@ -44,8 +44,6 @@ export default createRule<Options, MessageIds>({
docs: {
description: 'Require empty lines around comments',
recommended: false,
extendsBaseRule: true,
requiresTypeChecking: true,
},
schema: [
{
Expand Down Expand Up @@ -213,7 +211,7 @@ export default createRule<Options, MessageIds>({

return {
Program: (node) => {
rules.Program(node);
rules.Program?.(node);
},
};
},
Expand Down
27 changes: 11 additions & 16 deletions src/rules/newline-before-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,17 @@ export default createRule<Options, MessageIds>({
context.report({
node,
messageId: 'default',
suggest: [
{
messageId,
fix(fixer) {
const tokenBefore = sourceCode.getTokenBefore(node);

if (!tokenBefore) {
return null;
}

const newlines = node.loc.start.line === tokenBefore.loc.end.line ? '\n\n' : '\n';

return fixer.insertTextAfter(tokenBefore, newlines);
},
},
],
fix: (fixer) => {
const tokenBefore = sourceCode.getTokenBefore(node);

if (!tokenBefore) {
return null;
}

const newlines = node.loc.start.line === tokenBefore.loc.end.line ? '\n\n' : '\n';

return fixer.insertTextAfter(tokenBefore, newlines);
},
});
},
};
Expand Down
8 changes: 7 additions & 1 deletion test/rules/catch-error-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ ruleTester().run(ruleName, rule, {
messageId: 'default',
},
],
output: stripIndent`
try {} catch (error) {}
`,
},
{
code: stripIndent`
try {} catch (e) {}
try {} catch (e) {}
`,
errors: [
{
messageId: 'default',
},
],
output: stripIndent`
try {} catch (error) {}
`,
},
],
});
19 changes: 19 additions & 0 deletions test/rules/newline-before-return.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ ruleTester().run(ruleName, rule, {
messageId,
},
],
output: stripIndent`
function foo() {
const foo = 1;
const bar = 2;
return foo && bar;
}
`,
},
{
code: stripIndent`
Expand All @@ -73,6 +81,17 @@ ruleTester().run(ruleName, rule, {
messageId,
},
],
output: stripIndent`
function foo() {
const foo = 1;
if (foo) {
// do some work
}
return 1;
}
`,
},
],
});

0 comments on commit 4147825

Please sign in to comment.