Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prefer-readonly-parameter-types] Cannot read property 'flags' of undefined #3405

Closed
3 tasks done
AriPerkkio opened this issue May 18, 2021 · 4 comments
Closed
3 tasks done
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Milestone

Comments

@AriPerkkio
Copy link

Hello, prefer-readonly-parameter-types rule seems to crash in certain cases. This issue was spotted by automated CI run - it is not blocking my development or anything.

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

// .eslintrc.js
module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
    },
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.json"],
  },
  plugins: ["@typescript-eslint"],
  rules: {
    "@typescript-eslint/prefer-readonly-parameter-types": "error",
  },
};
// tsconfig.json
{
  "compilerOptions": {
    "target": "es6",
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["esnext", "dom"],
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist"
  },
  "include": ["./index.ts"]
}

This is as minimal as I could narrow the issue down.

type MyType<T> = {
  [K in keyof T]: "cat" | "dog" | T[K];
};

function method<A extends any[] = string[]>(value: MyType<A>) {
  return value;
}

method(["cat", "dog"]);
method<"mouse"[]>(["cat", "mouse"]);

Expected Result

ESLint should not crash.

Actual Result

TypeError: Cannot read property 'flags' of undefined
Occurred while linting /workspaces/eslint-remote-tester/node_modules/.cache-eslint-remote-tester/querycap/webappkit/@querycap/reactutils/must.tsx:7
    at Object.isUnionType (/workspaces/eslint-remote-tester/node_modules/tsutils/typeguard/2.8/type.js:69:18)
    at unionTypeParts (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:92:19)
    at isPropertyReadonlyInType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:151:21)
    at isReadonlyPropertyFromMappedType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:197:12)
    at /workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:179:21
    at someTypePart (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:100:52)
    at isReadonlyPropertyIntersection (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:172:12)
    at Object.isPropertyReadonlyInType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:161:43)
    at isTypeReadonlyObject (/workspaces/eslint-remote-tester/node_modules/@typescript-eslint/eslint-plugin/dist/util/isTypeReadonly.js:72:28)
    at isTypeReadonlyRecurser (/workspaces/eslint-remote-tester/node_modules/@typescript-eslint/eslint-plugin/dist/util/isTypeReadonly.js:127:30)

Additional Info

Crash reports from real projects

Rule: prefer-readonly-parameter-types

  • Message: Cannot read property 'flags' of undefined Occurred while linting /workspaces/eslint-remote-tester/node_modules/.cache-eslint-remote-tester/querycap/webappkit/@querycap/reactutils/must.tsx:7
  • Path: querycap/webappkit/@querycap/reactutils/must.tsx
  • Link
   5 |
   6 | export function must<TPrepare extends Readonly<Array<any>>>(usePrepare: () => TPrepare) {
>  7 |   return function <TProps>(render: (props: TProps, ...prepare: NonNullableArray<TPrepare>) => JSX.Element | null) {
   8 |     const C = ({ "data-prepare": dataPrepare, ...props }: TProps & { ["data-prepare"]: NonNullableArray<TPrepare> }) =>
   9 |       render(props as any, ...(dataPrepare as any));
  10 |
TypeError: Cannot read property 'flags' of undefined
Occurred while linting /workspaces/eslint-remote-tester/node_modules/.cache-eslint-remote-tester/querycap/webappkit/@querycap/reactutils/must.tsx:7
    at Object.isUnionType (/workspaces/eslint-remote-tester/node_modules/tsutils/typeguard/2.8/type.js:69:18)
    at unionTypeParts (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:92:19)
    at isPropertyReadonlyInType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:151:21)
    at isReadonlyPropertyFromMappedType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:197:12)
    at /workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:179:21
    at someTypePart (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:100:52)
    at isReadonlyPropertyIntersection (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:172:12)
    at Object.isPropertyReadonlyInType (/workspaces/eslint-remote-tester/node_modules/tsutils/util/type.js:161:43)
    at isTypeReadonlyObject (/workspaces/eslint-remote-tester/node_modules/@typescript-eslint/eslint-plugin/dist/util/isTypeReadonly.js:72:28)
    at isTypeReadonlyRecurser (/workspaces/eslint-remote-tester/node_modules/@typescript-eslint/eslint-plugin/dist/util/isTypeReadonly.js:127:30)

Versions

package version
@typescript-eslint/eslint-plugin 4.23.0
@typescript-eslint/parser 4.23.0
TypeScript 4.2.4
ESLint 7.26.0
node v14.15.1
@AriPerkkio AriPerkkio added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels May 18, 2021
@bradzacher bradzacher added the bug Something isn't working label May 24, 2021
@JoshuaKGoldberg
Copy link
Member

@kirjs and I had a splendid time pairing on this: turns out tsutils crashes on a mapped type when the type mapping doesn't have modifiers.

ajafff/tsutils#136

@bradzacher bradzacher added external This issue is with another package, not typescript-eslint itself and removed triage Waiting for maintainers to take a look labels May 27, 2021
@rafaelss95
Copy link
Contributor

I'm not entirely sure if it's a duplication, but it looks a lot like the one reported in #1742 (error logs are pretty similar).

@AriPerkkio
Copy link
Author

@rafaelss95 the stack trace from tsutils seems identical so this is very likely a duplicate. I'm usually very careful not to file duplicate issues - no idea how I missed that issue here.
As this issue already has a fix proposal linked I guess it makes sense to keep this one open.

@JoshuaKGoldberg
Copy link
Member

As of #6428, the v6 branch uses ts-api-tools. Which has the same bug as tsutils. Fix is in review here: JoshuaKGoldberg/ts-api-utils#18

@JoshuaKGoldberg JoshuaKGoldberg added this to the 6.0.0 milestone Feb 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants