import rule from "./no-premature-fp-ts-effects"; import { AST_NODE_TYPES } from "@typescript-eslint/utils"; import { ESLintUtils } from "@typescript-eslint/utils"; // eslint-disable-next-line functional/prefer-immutable-types const ruleTester = new ESLintUtils.RuleTester({ parserOptions: { sourceType: "module", project: "./tsconfig.tests.json", }, parser: "@typescript-eslint/parser", }); // eslint-disable-next-line functional/no-expression-statements ruleTester.run("no-premature-fp-ts-effects", rule, { valid: [ { filename: "file.ts", code: ` const myFunc = () => "hello"; const result: string = myFunc(); `, }, { filename: "file.ts", code: ` const myFunc = (s: string) => "hello " + s; const result: string = myFunc("asdf"); `, }, { filename: "file.ts", code: ` 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: ` const foo: { a: () => string } | undefined = Date.now() > 0 ? undefined : { a: () => "" }; foo?.a(); `, }, ], invalid: [ { filename: "file.ts", code: ` export interface IO { (): A } const io: IO = () => "hello"; const ioResult: string = io(); `, errors: [ { messageId: "errorStringGeneric", type: AST_NODE_TYPES.CallExpression, }, ], }, { filename: "file.ts", code: ` 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: ` export interface IO { (): A } const logErrors = (): IO => () => undefined; const result = logErrors()(); `, errors: [ { messageId: "errorStringGeneric", type: AST_NODE_TYPES.CallExpression, }, ], }, ], } as const);