buildo / eslint-plugin-fp-ts

ESLint rules for fp-ts
112 stars 8 forks source link

Linter crash #45

Closed jleider closed 3 years ago

jleider commented 3 years ago

While attempting to adopt this plugin I ran into a crash. I have provided the code and

Oops! Something went wrong! :(

ESLint: 7.18.0 typescript-eslint/eslint-plugin: 4.14.1 typescript-eslint/parser: 4.14.1 Node: v12.18.4

TypeError: this[node.body.type] is not a function
Occurred while linting /scripts/react/components/error/FormattedRespErrors.tsx:14
    at Object.ArrowFunctionExpression (/node_modules/astring/dist/astring.js:683:27)
    at formatSequence (/node_modules/astring/dist/astring.js:87:29)
    at Object.CallExpression (/node_modules/astring/dist/astring.js:978:5)
    at Object.generate (/node_modules/astring/dist/astring.js:1158:29)
    at Object.prettyPrint (/node_modules/eslint-plugin-fp-ts/lib/utils.js:109:22)
    at Object.fix (/node_modules/eslint-plugin-fp-ts/lib/rules/prefer-bimap.js:78:237)
    at normalizeFixes (/node_modules/eslint/lib/linter/report-translator.js:178:28)
    at /node_modules/eslint/lib/linter/report-translator.js:206:22
    at Array.map (<anonymous>)
    at mapSuggestions (/node_modules/eslint/lib/linter/report-translator.js:200:10)
import React, { Fragment, ReactElement } from "react";
import { fold, fromNullable, getOrElse } from "fp-ts/lib/Option";
import { pipe } from "fp-ts/lib/pipeable";
import { map as mapT, mapLeft as mapLeftT } from "fp-ts/lib/These";
import { ContextEntry, Errors, ValidationError } from "io-ts";

import { Empty } from "@scripts/react/components/Empty";
import { JsonSyntaxHighlight } from "@scripts/ui/json";

export type RespOrErrors = These<Option<Response>, t.Errors>;

export const FormattedRespErrors = (re: RespOrErrors): ReactElement => isProd ? (<Empty/>) : (
  <Fragment>
    {pipe( // <== line 14 as reported in the stack trace
      re,
      mapLeftT(
        fold(
          () => (<h3>No response object</h3>),
          (r: Response) => (<h3>Response Error: {r.status} -- {r.statusText}</h3>)
        )
      ),
      mapT(
        (errs: Errors) => (
          <div>
            <h3>IO-TS Errors</h3>
            {errs.map((v: ValidationError, vIdx: number) => (
              // eslint-disable-next-line react/no-array-index-key
              <section key={vIdx} style={{
                background: "#fafafa",
                border: "1px dashed #333",
                padding: "1rem",
              }}>
                <dl>
                  <dt>Message</dt>
                  <dd>
                    {pipe(
                      fromNullable(v.message),
                      getOrElse(() => "No message")
                    )}
                  </dd>
                  <dt>Value</dt>
                  <dd><JsonSyntaxHighlight json={v.value} /></dd>
                  <dt>Context</dt>
                  <dd>
                    {[...v.context].reverse().map((c: ContextEntry, cIdx: number) => (
                      // eslint-disable-next-line react/no-array-index-key
                      <dl key={cIdx} style={{
                        borderTop: "0.125rem solid #999",
                        marginTop: "3rem",
                        paddingTop: "2rem",
                      }}>
                        <dt>Key</dt>
                        <dd>
                          <JsonSyntaxHighlight json={c.key} />
                        </dd>
                        <dt>Expected Type</dt>
                        <dd>
                          <pre style={{whiteSpace: "normal"}}>{c.type.name}</pre>
                        </dd>
                        <dt>Actual Value</dt>
                        <dd>
                          <JsonSyntaxHighlight json={c.actual} />
                        </dd>
                      </dl>
                    ))}
                  </dd>
                </dl>
              </section>
            ))}
          </div>
        )
      )
    )}
  </Fragment>
);
gabro commented 3 years ago

Hey @jleider, thanks for reporting! I'll take a look as soon as I have a minute

gabro commented 3 years ago

Ok, it turns out the pretty printer I was using didn't support JSX 😓. Fixed by #46 and released in v0.1.13 (on its way to npm)

gabro commented 3 years ago

@jleider let me know if the latest release fixes it for you!

jleider commented 3 years ago

Yeah, its working now, thanks for the quick turnaround!