gajus / eslint-plugin-jsdoc

JSDoc specific linting rules for ESLint.
Other
1.1k stars 160 forks source link

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

Open hecticme opened 4 days ago

hecticme commented 4 days ago

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