Skip to content

Commit

Permalink
Upgrade eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-waarneming-nl committed Oct 6, 2024
1 parent 9bb06c6 commit 39a0293
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 355 deletions.
11 changes: 0 additions & 11 deletions .eslintrc.js

This file was deleted.

5 changes: 4 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions dist/rules/no-function-without-logging.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'

export default [
{
files: ['**/*.{js,mjs,cjs,ts}'],
},
pluginJs.configs.recommended,
{
rules: {
'no-console': 'warn',
curly: ['error', 'multi-line'],
'prefer-destructuring': ['error'],
},
},
...tseslint.configs.recommended,
]
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
"lint": "tsc && eslint 'src/**'"
},
"dependencies": {
"@types/eslint": "^8.21.1",
"@types/node": "^18.14.0",
"@typescript-eslint/parser": "^5.53.0",
"@typescript-eslint/utils": "^5.53.0",
"eslint": "^8.34.0",
"@typescript-eslint/parser": "^8.7.0",
"@typescript-eslint/utils": "^8.7.0",
"jest": "^29.4.3",
"ts-jest": "^29.0.5",
"typescript": "5.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@eslint/js": "^9.11.1",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/rule-tester": "^8.7.0",
"eslint": "^9.11.1",
"eslint-plugin-import": "^2.27.5",
"prettier": "^2.8.4"
"prettier": "^2.8.4",
"typescript-eslint": "^8.7.0"
},
"packageManager": "[email protected]"
}
15 changes: 6 additions & 9 deletions src/rules/__tests__/no-function-without-logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ESLintUtils } from '@typescript-eslint/utils'

import noFunctionWithoutLogging from '../no-function-without-logging'
import { RuleTester } from '@typescript-eslint/rule-tester'

const ruleTester = new ESLintUtils.RuleTester({
parser: '@typescript-eslint/parser',
})
const ruleTester = new RuleTester()

ruleTester.run('no-function-without-logging', noFunctionWithoutLogging, {
valid: [
Expand All @@ -18,7 +15,7 @@ ruleTester.run('no-function-without-logging', noFunctionWithoutLogging, {
},
{
name: 'Static function declaration',
code: "static function functionName(){ Log.debug('file:functionName') }",
code: "class ClassName { static functionName(){ Log.debug('file:functionName') } }",
},
{
name: 'Function declaration in class',
Expand Down Expand Up @@ -114,7 +111,7 @@ ruleTester.run('no-function-without-logging', noFunctionWithoutLogging, {
},
{
name: 'Missing logging in static function declaration',
code: 'static function functionName(){ }',
code: 'class ClassName { static functionName(){ } }',
errors: [
{
messageId: 'missingLogging',
Expand All @@ -123,12 +120,12 @@ ruleTester.run('no-function-without-logging', noFunctionWithoutLogging, {
{
messageId: 'addLoggingSuggestion',
data: { suggestedCode: "Log.trace('file:functionName');" },
output: "static function functionName(){Log.trace('file:functionName'); }",
output: "class ClassName { static functionName(){Log.trace('file:functionName'); } }",
},
{
messageId: 'addLoggingSuggestion',
data: { suggestedCode: "Log.debug('file:functionName');" },
output: "static function functionName(){Log.debug('file:functionName'); }",
output: "class ClassName { static functionName(){Log.debug('file:functionName'); } }",
},
],
},
Expand Down
26 changes: 17 additions & 9 deletions src/rules/no-function-without-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path'

import { TSESTree } from '@typescript-eslint/utils'
import { ESLintUtils } from '@typescript-eslint/utils'
import { ReportSuggestionArray, RuleContext, RuleFixer } from '@typescript-eslint/utils/dist/ts-eslint'
import { ReportSuggestionArray, RuleContext, RuleFixer } from '@typescript-eslint/utils/ts-eslint'

import {
isArrowFunctionExpression,
Expand Down Expand Up @@ -48,7 +48,7 @@ const createSuggestions = (
}

const addMissingLogStatementSuggestions = (
context: Readonly<RuleContext<messageIds, any[]>>,
context: Readonly<RuleContext<messageIds, unknown[]>>,
node: TSESTree.Node,
blockStatement: TSESTree.BlockStatement,
correctLogging: string,
Expand Down Expand Up @@ -107,7 +107,7 @@ const isLogStatement = (expression: TSESTree.CallExpression) => {
}

const containsLoggingStatement = (blockStatement: TSESTree.BlockStatement): boolean => {
for (var statement of blockStatement.body) {
for (const statement of blockStatement.body) {
if (isExpressionStatement(statement)) {
const { expression } = statement

Expand All @@ -121,7 +121,7 @@ const containsLoggingStatement = (blockStatement: TSESTree.BlockStatement): bool
}

const checkFunctionDeclaration = (
context: Readonly<RuleContext<messageIds, any[]>>,
context: Readonly<RuleContext<messageIds, unknown[]>>,
node: TSESTree.FunctionDeclaration,
) => {
const functionName = node.id ? node.id.name : ''
Expand All @@ -133,7 +133,7 @@ const checkFunctionDeclaration = (
}
}

const checkCallExpression = (context: Readonly<RuleContext<messageIds, any[]>>, node: TSESTree.CallExpression) => {
const checkCallExpression = (context: Readonly<RuleContext<messageIds, unknown[]>>, node: TSESTree.CallExpression) => {
if (isLogStatement(node)) {
const filename = path.parse(context.getFilename()).name
const functionName = getFunctionName(node)
Expand All @@ -151,6 +151,9 @@ const checkCallExpression = (context: Readonly<RuleContext<messageIds, any[]>>,
suggest: [
{
messageId: 'incorrectLogging',
data: {
expectedLogging,
},
fix: (fixer) => {
return fixer.insertTextAfterRange(newRange, `'${expectedLogging}'`)
},
Expand All @@ -171,6 +174,9 @@ const checkCallExpression = (context: Readonly<RuleContext<messageIds, any[]>>,
suggest: [
{
messageId: 'incorrectLogging',
data: {
expectedLogging,
},
fix: (fixer) => {
return fixer.replaceTextRange(argument.range, `'${expectedLogging}'`)
},
Expand All @@ -183,7 +189,7 @@ const checkCallExpression = (context: Readonly<RuleContext<messageIds, any[]>>,
}

const checkVariableDeclaration = (
context: Readonly<RuleContext<messageIds, any[]>>,
context: Readonly<RuleContext<messageIds, unknown[]>>,
node: TSESTree.VariableDeclaration,
) => {
if (node.declarations.length !== 1) {
Expand Down Expand Up @@ -215,7 +221,7 @@ const checkVariableDeclaration = (
}

const checkPropertyDefinition = (
context: Readonly<RuleContext<messageIds, any[]>>,
context: Readonly<RuleContext<messageIds, unknown[]>>,
node: TSESTree.PropertyDefinition,
) => {
if (
Expand Down Expand Up @@ -245,7 +251,10 @@ const isSetterLikeMethodDefinition = (node: TSESTree.MethodDefinition, functionN
return hasSetterLikeFunctionName && returnsVoid
}

const checkMethodDefinition = (context: Readonly<RuleContext<messageIds, any[]>>, node: TSESTree.MethodDefinition) => {
const checkMethodDefinition = (
context: Readonly<RuleContext<messageIds, unknown[]>>,
node: TSESTree.MethodDefinition,
) => {
if (node.kind === 'constructor') {
return
}
Expand Down Expand Up @@ -286,7 +295,6 @@ const noFunctionWithoutLogging = createRule({
meta: {
docs: {
description: 'All functions should include a logging statement',
recommended: 'error',
},
messages: {
incorrectLogging: "Logging should include the filename and function name: Log.debug('{{ expectedLogging }}')",
Expand Down
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"compilerOptions": {
"module": "CommonJS",
"module": "NodeNext",
"outDir": "dist",
"lib": ["es2019"],
"strict": true,
"target": "esnext"
"target": "esnext",
"moduleResolution": "NodeNext",
"skipLibCheck": true
},
"files": ["src/index.ts"],
"exclude": ["node_modules"]
"files": ["src/index.ts"]
}
Loading

0 comments on commit 39a0293

Please sign in to comment.