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

no-undefined-types treat normal variables as undefined in function scope #1342

Closed
hecticme opened this issue Nov 26, 2024 · 6 comments
Closed

Comments

@hecticme
Copy link

Reproduction: https://stackblitz.com/edit/vitejs-vite-d1gqkb

Expected behavior

I expected that this code snippet does not cause any lint error:

function retrieveHexCode(colorName) {
  const hashmap = {
    white: '#fff',
  };

  // 👇️ Lint error: The type 'hashmap' is undefined.
  /** @typedef {keyof typeof hashmap} KeyOfHashmap */
  // It seems like normal variables are not recognized.
  /** @typedef {typeof hashmap} TypeOfHashmap */

  const colorHexCode = hashmap[/** @type {KeyOfHashmap} */ (colorName)];

  return colorHexCode ?? '#000';
}

const hashmapTwo = {
  white: '#fff',
};

// 👇️ However, the error does not occur outside of function scope.
/** @typedef {typeof hashmapTwo} TypeOfHashmapTwo */

Actual behavior

In the above snippet, hashmap is reported as undefined by jsdoc/no-undefined-types. This only happens inside the function scope, please take a look at the reproduction playground for more details. Intellisense was still working properly.

ESLint Config

import jsdoc from 'eslint-plugin-jsdoc';

export default [
  {
    files: ['**/*.js'],
    plugins: {
      jsdoc,
    },
    rules: {
      'jsdoc/no-undefined-types': [
        'error',
        {
          definedTypes: [],
          disableReporting: false,
          markVariablesAsUsed: true,
        },
      ],
    },
  },
];

ESLint sample

See → https://stackblitz.com/edit/vitejs-vite-d1gqkb or check the code in "Expected behavior".

Environment

  • Node version: v18.20.3
  • ESLint version: v9.15.0
  • eslint-plugin-jsdoc version: 50.5.0
@RobertAKARobin
Copy link

I'm experiencing similar unexpected behavior with typeof.

@sebastian-fredriksson-bernholtz
Copy link
Contributor

sebastian-fredriksson-bernholtz commented Mar 14, 2025

I think this is described in #178. That was closed as completed because typescript supposedly supports this now so you don't need this rule1,2.

I haven't been able to figure out how to get typescript to flag invalid references in jsdoc though. If anyone has figured it out, would love to know how.

@hecticme
Copy link
Author

hecticme commented Mar 15, 2025

Thanks Bernholtz :). I didn't see #178 when I first searched for related issue. My mistake.

Going to close this issue as "not planned" in a few days if no one else is interested.

@hecticme hecticme closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2025
@sebastian-fredriksson-bernholtz
Copy link
Contributor

@hecticme This was probably obvious to you since your code is js, but mine is ts, so I only now figured out that what was being referred to was that it's recommended to use typescript to typecheck your js files to catch your issue.

I think it might still be worth leaving your issue open however, because

  1. The rule should probably be marked deprecated and include an explanation that it's recommended to use typescript to typecheck your js files, instead of using this rule.
    Unless there is another open issue tracking that - or we create one, or reopen #178 - that you can close this in preference of, I think this isn't completely resolved. Although the issue would be one of lacking documentation instead.
  2. I wouldn't actually consider the rule deprecated, because it still provides functionality that typescript does not (see #178 (comment)), so the underlying issue of not detecting local variables is still an issue, and unless that gets tracked in another issue, might as well keep this issue open.

@hecticme
Copy link
Author

Sure thing. I will let the maintainer decide whether to close it or not.

@hecticme hecticme reopened this Mar 15, 2025
sebastian-fredriksson-bernholtz added a commit to sebastian-fredriksson-bernholtz/eslint-plugin-jsdoc that referenced this issue Mar 16, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
sebastian-fredriksson-bernholtz added a commit to sebastian-fredriksson-bernholtz/eslint-plugin-jsdoc that referenced this issue Mar 16, 2025
@brettz9
Copy link
Collaborator

brettz9 commented Mar 17, 2025

Fixed by #1363

@brettz9 brettz9 closed this as completed Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants