+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Conversation

ematipico
Copy link
Contributor

@ematipico ematipico commented Jul 25, 2022

Summary

Closes #2912

This PR enables a new feature in the configuration, which is the possibility to opt-in the recommended rules suggested by the Rome team.

Configuration changes

It's possible to opt-in rules in two different way:

  • Globally
{
	"linter": {
		"recommended": true
	}
}
  • Group level
{
	"linter": {
		"rules": {
			"js" {
				"recommended": true
			},
			"jsx" {
				"recommended": false
			}
		}
	}
}

De-serialization of rules

While we desarialize rules, we also validate that the rules we have in the configuration rules are valid for the current group. If not, we emit a ConfigurationError. We do so by saving an array of all the rules that belong to a certain group.

The serialization is out of scope, because we don't have an use case yet. But the serialization will need to be custom, because we want to keep the order of the original map.

Code generation

All the new code (configuration shape, de-serialization and methods) are code generated from the metadata of the rules.

  • all rules that belong to a group, are stored into an array
  • the recommended rules are also stored into a const inside the struct, and they represented as an array of RuleFilter. The name of the rule is taken from Self::GROUP_RULES, so we don't repeat the static strings
  • the struct Rules has now a method called as_analysis_filters which will emit two IndexSet of enabled filters and disabled. The reason why IndexSet was used is because the enabled filters needs to be computed from the difference with the disabled filters. Here's an example:
{
  "root": true,
  "linter": {
    "rules": {
        "recommended": true,
        "js": {
            "rules": {
                "noDebugger": "off"
            }
        }
    }
  }
}

In this example we need pull all the recommended rules, but then we have to remove the rules that have been turned off. IndexSet allows us to do so with a simple .difference function.

Computation of AnalysisFilter

Here's there have been some limitations, due to how RuleFilter is designed (all references), and because of that the creation of the AnalysisFilter object must be done in place, right before being used. This caused a bit of repetition of code. If you have a solution to avoid this repetition of code, it would be great

Test Plan

I updated some existing test and added new ones, some happy path and error path.

@ematipico ematipico requested review from leops and xunilrj as code owners July 25, 2022 10:44
@ematipico ematipico requested a review from a team July 25, 2022 10:44
@ematipico ematipico temporarily deployed to aws July 25, 2022 10:44 Inactive
@github-actions
Copy link

github-actions bot commented Jul 25, 2022

@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 395 395 0
Failed 5551 5551 0
Panics 0 0 0
Coverage 6.64% 6.64% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12393 12393 0
Failed 3864 3864 0
Panics 0 0 0
Coverage 76.23% 76.23% 0.00%

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 25, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e1ee146
Status:⚡️  Build in progress...

View logs

@ematipico ematipico force-pushed the feature/recommended-rulles branch from 31930e3 to 324dd8e Compare July 25, 2022 12:04
@ematipico ematipico requested a review from MichaReiser as a code owner July 25, 2022 12:04
@ematipico ematipico temporarily deployed to aws July 25, 2022 12:04 Inactive
@ematipico ematipico force-pushed the feature/recommended-rulles branch from 324dd8e to b80fd84 Compare July 25, 2022 13:06
@ematipico ematipico temporarily deployed to aws July 25, 2022 13:06 Inactive
Copy link
Contributor

@leops leops left a comment

Choose a reason for hiding this comment

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

The code building up the filter could be simplified by only creating the enabled_rules list instead of both the enabled and disabled set (the information in disabled_rules is redundant since those rules could simply not be included in the enabled set)

@ematipico
Copy link
Contributor Author

The code building up the filter could be simplified by only creating the enabled_rules list instead of both the enabled and disabled set (the information in disabled_rules is redundant since those rules could simply not be included in the enabled set)

I can change the code generation, but now I have a question: why do we have disabled_filters in the first place?

Also, don't you think that having the disabled rules would able more features? An example that come up to my mind is the possibility to emit a warning when we find a suppression for a rule that was disabled in the configuration.

@ematipico ematipico temporarily deployed to aws July 25, 2022 13:41 Inactive
@ematipico ematipico requested a review from leops July 25, 2022 13:41
@leops
Copy link
Contributor

leops commented Jul 25, 2022

I can change the code generation, but now I have a question: why do we have disabled_filters in the first place?

These two filters solve different problems, the idea was that enabled_rules can be used to enable only one or a few rules, while disabled_rules can be use to turn off a few rules while running all the others (in general the idea is to use the filter that would need a smaller number of entries since it's more efficient to check the rules against a small set)

Also, don't you think that having the disabled rules would able more features? An example that come up to my mind is the possibility to emit a warning when we find a suppression for a rule that was disabled in the configuration.

This should already be the case, rules that are filtered out are not added to the RuleRegistry used for analysis, so a warning will be emitted if any comment tries to suppress a disabled rule since the analyzer considers that the rule does not exist

@ematipico ematipico temporarily deployed to aws July 25, 2022 15:42 Inactive
@ematipico ematipico force-pushed the feature/recommended-rulles branch from d126c60 to daeba2c Compare July 25, 2022 16:13
@ematipico ematipico temporarily deployed to aws July 25, 2022 16:13 Inactive
Comment on lines 46 to 51
if self.is_recommended() {
enabled_rules.extend(Js::RECOMMENDED_RULES);
enabled_rules.extend(Jsx::RECOMMENDED_RULES);
enabled_rules.extend(Regex::RECOMMENDED_RULES);
enabled_rules.extend(Ts::RECOMMENDED_RULES);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any validation of the configuration. Or what happens if the user sets recomended: true but also has js: { recommended: false }?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, good point. I changed the code and added a test case. In your example we should enable all the recommended rules, exception for the recommended rules of the group js.

Do we have any validation of the configuration

Only the one that belongs to serde during serialization/deserialization

})
}

pub fn from_enabled_rules(enabled_rules: Option<&'analysis [RuleFilter<'analysis>]>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some documentation why it is necessary to have enabled and disabled rules? The thing I'm concerned about the two filters is, what if enabled and disabled disagree about a particular rule. Is it then disabled or enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what if enabled and disabled disagree about a particular rule. Is it then disabled or enabled?

This is more a question for @leops

For now I reverted the change and limit myself only to enabled_rules. I will optimize it later

Copy link
Contributor

Choose a reason for hiding this comment

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

The exact semantic of the AnalysisFilter is that a rule is included if it's present in the enabled_rules (or this field is set to None) and not present in disabled_rules, this means if a rule is present in both sets it will be disabled

@ematipico ematipico temporarily deployed to aws July 26, 2022 09:21 Inactive
@ematipico ematipico temporarily deployed to aws July 26, 2022 09:25 Inactive
@ematipico ematipico requested a review from MichaReiser July 26, 2022 09:27
@ematipico ematipico force-pushed the feature/recommended-rulles branch from 90b42f3 to 4e97bf9 Compare July 26, 2022 09:48
@ematipico ematipico temporarily deployed to aws July 26, 2022 09:48 Inactive
@ematipico ematipico temporarily deployed to aws July 26, 2022 15:43 Inactive
@ematipico ematipico merged commit e6f64fe into main Jul 26, 2022
@ematipico ematipico deleted the feature/recommended-rulles branch July 26, 2022 15:48
IWANABETHATGUY pushed a commit to IWANABETHATGUY/tools that referenced this pull request Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enable recommendation setting for rules

3 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载