eslint-plugin-node icon indicating copy to clipboard operation
eslint-plugin-node copied to clipboard

Incorrect regex check no-extraneous-require

Open tafelnl opened this issue 5 years ago • 2 comments

I want to use this package in combination with https://github.com/Rush/link-module-alias to be able to use aliases in requires/imports instead of a long ../../../../../path/to/file.

But the ESLinter (rightly) gives me the node/no-extraneous-require error, when I write: const Something = require('~/path/to/something'); Because the ~ package does not exist in the package.json. But the thing is that the link-module-alias package takes care of that by using symlinks. (https://github.com/Rush/link-module-alias#how-does-it-work).

So I can ignore that by doing the following:

    'node': {
      'allowModules': ['~']
    }

Therefore my eslintrc.js looks like this:

module.exports = {
  'env': {
    'es6': true,
    'node': true
  },
  'parser': 'babel-eslint',
  'parserOptions': {
    'sourceType': 'module',
    'ecmaVersion': 2020
  },
  'extends': [
    'eslint:recommended',
    'plugin:node/recommended'
  ],
  'settings': {
    'node': {
      'allowModules': ['~']
    }
  },
};

This works. BUT the problem with this is that the linter does not check for missing imports/requires anymore

Therefore it would be better to do just:

  'rules': {
    'node/no-extraneous-require': ['error', {
      'allowModules': ['~']
    }],
  }

Making the estlintrc.js look like the following:

module.exports = {
  'env': {
    'es6': true,
    'node': true
  },
  'parser': 'babel-eslint',
  'parserOptions': {
    'sourceType': 'module',
    'ecmaVersion': 2020
  },
  'extends': [
    'eslint:recommended',
    'plugin:node/recommended'
  ],
  'rules': {
    'node/no-extraneous-require': ['error', {
      'allowModules': ['~']
    }],
  },
};

I think this would have worked. But I get the following error:

Configuration for rule "node/no-extraneous-require" is invalid:
Value "~" should match pattern "^(?:@[a-zA-Z0-9_\-.]+/)?[a-zA-Z0-9_\-.]+$".

I think that the use-case above is a valid one and should therefore be allowed for in the regex. Do you agree?

tafelnl avatar May 04 '20 09:05 tafelnl

I agree, that would be super useful and is directly linked to https://github.com/mysticatea/eslint-plugin-node/issues/233

thomasklein-winemaker avatar Aug 30 '20 11:08 thomasklein-winemaker

I think this would still leave out the case where you had a typo in an aliased import right? Since the typo would still pass the rule, built ultimate fail?

jhoffmcd avatar Nov 29 '20 04:11 jhoffmcd