import parser from '@typescript-eslint/parser'; import { RuleTester } from '@typescript-eslint/rule-tester'; import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import dedent from 'dedent'; import { noPrematureFpTsEffects } from './no-premature-fp-ts-effects.mjs'; const ruleTester = new RuleTester({ languageOptions: { parserOptions: { sourceType: 'module', project: './tsconfig.tests.json', }, parser, }, }); ruleTester.run('no-premature-fp-ts-effects', noPrematureFpTsEffects, { valid: [ { filename: 'file.ts', code: dedent` const myFunc = () => "hello"; const result: string = myFunc(); `, }, { filename: 'file.ts', code: dedent` const myFunc = (s: string) => "hello " + s; const result: string = myFunc("asdf"); `, }, { filename: 'file.ts', code: dedent` export interface Lazy { (): A } const lazy: Lazy = () => "hello"; const lazyResult: string = lazy(); `, }, // exercise the try/catch around calleeType.symbol.name (this test prompts it to throw) { filename: 'file.ts', code: dedent` const foo: { a: () => string } | undefined = Date.now() > 0 ? undefined : { a: () => "" }; foo?.a(); `, }, ], invalid: [ { filename: 'file.ts', code: dedent` export interface IO { (): A } const io: IO = () => "hello"; const ioResult: string = io(); `, errors: [ { messageId: 'errorStringGeneric', type: AST_NODE_TYPES.CallExpression, }, ], }, { filename: 'file.ts', code: dedent` export interface Task { (): Promise } const task: Task = () => Promise.resolve("hello"); const taskResult: Promise = task(); `, errors: [ { messageId: 'errorStringGeneric', type: AST_NODE_TYPES.CallExpression, }, ], }, { filename: 'file.ts', code: dedent` export interface IO { (): A } const logErrors = (): IO => () => undefined; const result = logErrors()(); `, errors: [ { messageId: 'errorStringGeneric', type: AST_NODE_TYPES.CallExpression, }, ], }, ], } as const);