Skip to content

Improve detection of static arguments of context.report() in several rules #129

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

Merged
merged 1 commit into from
Jun 16, 2021

Conversation

bmish
Copy link
Member

@bmish bmish commented Jun 15, 2021

Several of our rules check the arguments of context.report(). This PR makes some improvements:

  1. When inferring the arguments passed to context.report() in utils.getReportInfo(), take into account the static value of the second argument when available.
  2. Update the rules using utils.getReportInfo() to also consider the static value or variable declaration of the message argument when necessary.

I noticed these issues because in a number of my plugins, we have stored rule error messages in variables like const MESSAGE = 'do A instead of B';, and this prevented many of the eslint-plugin-eslint-plugin rules from operating correctly.

Example of how this improvement enables the no-deprecated-report-api rule to correctly autofix this code (previously no autofix):

const MESSAGE = 'do A instead of B';
module.exports = {
    create(context) {
		// Before autofix
    	context.report(theNode, MESSAGE);

		// After autofix
		context.report({node: theNode, message: MESSAGE});
	}
};

…eral rules

Several of our rules check the arguments of `context.report()`. This PR makes some improvements:

1. When inferring the arguments passed to `context.report()` in `utils.getReportInfo()`, take into account the static value of the second argument when available.
2. Update the rules using `utils.getReportInfo()` to also consider the static value or variable declaration of the message argument when necessary.

I noticed these issues because in a number of my plugins, we have stored rule error messages in variables like `const MESSAGE = 'do A instead of B';`, and this prevented many of our rules from operating correctly.

Example of how this improvement enables the `no-deprecated-report-api` rule to correctly autofix this code:

```js
const MESSAGE = 'do A instead of B';
module.exports = {
    create(context) {
		// Before autofix
    	context.report(theNode, MESSAGE);

		// After autofix
		context.report({node: theNode, message: MESSAGE});
	}
};
```
Copy link
Contributor

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@aladdin-add aladdin-add merged commit 6d5be9f into eslint-community:master Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants