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-unused-vars doesn't remove unused require statements #55

Open
laurent22 opened this issue Jul 29, 2019 · 3 comments
Open

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

laurent22 opened this issue Jul 29, 2019 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@laurent22
Copy link

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 laurent22 added the bug Something isn't working label Jul 29, 2019
@aladdin-add
Copy link
Owner

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

@laurent22
Copy link
Author

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.

@g-plane
Copy link
Collaborator

g-plane commented Jul 29, 2019

Yes, it can be an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants