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

no-unused-vars doesn't remove unused require statements

Open laurent22 opened this issue 4 years ago • 3 comments

Tell us about your environment

  • eslint version: 6.1.0
  • eslint-plugin-autofix version: 0.0.8
  • eslint config:
module.exports = {
	'env': {
		'browser': true,
		'es6': true,
		'node': true,
	},
	'extends': ['eslint:recommended', 'prettier'],
	'globals': {
		'Atomics': 'readonly',
		'SharedArrayBuffer': 'readonly'
	},
	'parserOptions': {
		'ecmaVersion': 2018,
		"ecmaFeatures": {
			"jsx": true,
	    },
	},
	'rules': {
		"react/jsx-uses-react": "error",
		"react/jsx-uses-vars": "error",
		// Ignore all unused function arguments, because in some
		// case they are kept to indicate the function signature.
		"no-unused-vars": ["error", { "argsIgnorePattern": ".*" }],
		"no-constant-condition": 0,
		"no-prototype-builtins": 0,
		"prettier/prettier": "error",
		// Uncomment this to automatically remove unused requires:
		"autofix/no-unused-vars": "error",
	},
	"plugins": [
		"react",
		"prettier",
		"autofix",
	],
};

Expected behavior

When using the rule no-unused-vars it should remove unused require statements such as this one: const fs = require('fs-extra');

Actual behavior

It doesn't.

When using the rule no-unused-vars, it changes unused require statements like this one:

const { sprintf } = require('sprintf-js');

to this:

const {} = require('sprintf-js');

Which is good. However for unused require statements like this:

const fs = require('fs-extra');

It doesn't remove them, while it would be great if it could. My use case is to process a large codebase with various unused require statements so since eslint can detect it, it would be great if it could be autofixed too.

laurent22 avatar Jul 29 '19 18:07 laurent22

hi, thanks for the report! I think it was not fixed, as we cannot tell whether it has side effects:

var foo = bar(); // cannot be safely removed.

but probably requires can be seen an exception here. /cc @g-plane

aladdin-add avatar Jul 29 '19 20:07 aladdin-add

I wonder if that could be made optional, perhaps with a warning? When processing an entire codebase with eslint it would be useful if require or import statements were auto-removed. Then it's up to the user to check in the diff that nothing useful was lost. At least in my case nearly all the useless require statements could be removed.

laurent22 avatar Jul 29 '19 21:07 laurent22

Yes, it can be an exception.

g-plane avatar Jul 29 '19 23:07 g-plane