1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00

fix(lint): use recommended tag if there is no tags in config file or flags (#12644)

This commit is contained in:
Zheyu Zhang 2021-11-04 23:12:12 +08:00 committed by GitHub
parent 318dcc33af
commit efe956b4fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 61 deletions

View file

@ -121,9 +121,9 @@ pub struct LintFlags {
pub files: Vec<PathBuf>, pub files: Vec<PathBuf>,
pub ignore: Vec<PathBuf>, pub ignore: Vec<PathBuf>,
pub rules: bool, pub rules: bool,
pub rules_tags: Vec<String>, pub maybe_rules_tags: Option<Vec<String>>,
pub rules_include: Vec<String>, pub maybe_rules_include: Option<Vec<String>>,
pub rules_exclude: Vec<String>, pub maybe_rules_exclude: Option<Vec<String>>,
pub json: bool, pub json: bool,
} }
@ -1976,25 +1976,25 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
None => vec![], None => vec![],
}; };
let rules = matches.is_present("rules"); let rules = matches.is_present("rules");
let rules_tags = match matches.values_of("rules-tags") { let maybe_rules_tags = matches
Some(f) => f.map(String::from).collect(), .values_of("rules-tags")
None => vec![], .map(|f| f.map(String::from).collect());
};
let rules_include = match matches.values_of("rules-include") { let maybe_rules_include = matches
Some(f) => f.map(String::from).collect(), .values_of("rules-include")
None => vec![], .map(|f| f.map(String::from).collect());
};
let rules_exclude = match matches.values_of("rules-exclude") { let maybe_rules_exclude = matches
Some(f) => f.map(String::from).collect(), .values_of("rules-exclude")
None => vec![], .map(|f| f.map(String::from).collect());
};
let json = matches.is_present("json"); let json = matches.is_present("json");
flags.subcommand = DenoSubcommand::Lint(LintFlags { flags.subcommand = DenoSubcommand::Lint(LintFlags {
files, files,
rules, rules,
rules_tags, maybe_rules_tags,
rules_include, maybe_rules_include,
rules_exclude, maybe_rules_exclude,
ignore, ignore,
json, json,
}); });
@ -2826,9 +2826,9 @@ mod tests {
PathBuf::from("script_2.ts") PathBuf::from("script_2.ts")
], ],
rules: false, rules: false,
rules_tags: vec![], maybe_rules_tags: None,
rules_include: vec![], maybe_rules_include: None,
rules_exclude: vec![], maybe_rules_exclude: None,
json: false, json: false,
ignore: vec![], ignore: vec![],
}), }),
@ -2844,9 +2844,9 @@ mod tests {
subcommand: DenoSubcommand::Lint(LintFlags { subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![], files: vec![],
rules: false, rules: false,
rules_tags: vec![], maybe_rules_tags: None,
rules_include: vec![], maybe_rules_include: None,
rules_exclude: vec![], maybe_rules_exclude: None,
json: false, json: false,
ignore: vec![ ignore: vec![
PathBuf::from("script_1.ts"), PathBuf::from("script_1.ts"),
@ -2864,9 +2864,9 @@ mod tests {
subcommand: DenoSubcommand::Lint(LintFlags { subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![], files: vec![],
rules: true, rules: true,
rules_tags: vec![], maybe_rules_tags: None,
rules_include: vec![], maybe_rules_include: None,
rules_exclude: vec![], maybe_rules_exclude: None,
json: false, json: false,
ignore: vec![], ignore: vec![],
}), }),
@ -2887,9 +2887,9 @@ mod tests {
subcommand: DenoSubcommand::Lint(LintFlags { subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![], files: vec![],
rules: false, rules: false,
rules_tags: svec![""], maybe_rules_tags: Some(svec![""]),
rules_include: svec!["ban-untagged-todo", "no-undef"], maybe_rules_include: Some(svec!["ban-untagged-todo", "no-undef"]),
rules_exclude: svec!["no-const-assign"], maybe_rules_exclude: Some(svec!["no-const-assign"]),
json: false, json: false,
ignore: vec![], ignore: vec![],
}), }),
@ -2904,9 +2904,9 @@ mod tests {
subcommand: DenoSubcommand::Lint(LintFlags { subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![PathBuf::from("script_1.ts")], files: vec![PathBuf::from("script_1.ts")],
rules: false, rules: false,
rules_tags: vec![], maybe_rules_tags: None,
rules_include: vec![], maybe_rules_include: None,
rules_exclude: vec![], maybe_rules_exclude: None,
json: true, json: true,
ignore: vec![], ignore: vec![],
}), }),
@ -2928,9 +2928,9 @@ mod tests {
subcommand: DenoSubcommand::Lint(LintFlags { subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![PathBuf::from("script_1.ts")], files: vec![PathBuf::from("script_1.ts")],
rules: false, rules: false,
rules_tags: vec![], maybe_rules_tags: None,
rules_include: vec![], maybe_rules_include: None,
rules_exclude: vec![], maybe_rules_exclude: None,
json: true, json: true,
ignore: vec![], ignore: vec![],
}), }),

View file

@ -132,8 +132,7 @@ pub fn get_lint_references(
parsed_source: &deno_ast::ParsedSource, parsed_source: &deno_ast::ParsedSource,
maybe_lint_config: Option<&LintConfig>, maybe_lint_config: Option<&LintConfig>,
) -> Result<Vec<Reference>, AnyError> { ) -> Result<Vec<Reference>, AnyError> {
let lint_rules = let lint_rules = get_configured_rules(maybe_lint_config, None, None, None)?;
get_configured_rules(maybe_lint_config, vec![], vec![], vec![])?;
let linter = create_linter(parsed_source.media_type(), lint_rules); let linter = create_linter(parsed_source.media_type(), lint_rules);
let lint_diagnostics = linter.lint_with_ast(parsed_source); let lint_diagnostics = linter.lint_with_ast(parsed_source);

View file

@ -95,6 +95,12 @@ itest!(lint_with_config_and_flags {
exit_code: 1, exit_code: 1,
}); });
itest!(lint_with_config_without_tags {
args: "lint --config lint/Deno.no_tags.jsonc lint/with_config/",
output: "lint/with_config_without_tags.out",
exit_code: 1,
});
itest!(lint_with_malformed_config { itest!(lint_with_malformed_config {
args: "lint --config lint/Deno.malformed.jsonc", args: "lint --config lint/Deno.malformed.jsonc",
output: "lint/with_malformed_config.out", output: "lint/with_malformed_config.out",

View file

@ -0,0 +1,17 @@
{
"lint": {
"files": {
"include": [
"lint/with_config/"
],
"exclude": [
"lint/with_config/b.ts"
]
},
"rules": {
"include": [
"ban-untagged-todo"
]
}
}
}

View file

@ -0,0 +1,18 @@
(ban-untagged-todo) TODO should be tagged with (@username) or (#issue)
// TODO: foo
^^^^^^^^^^^^
at [WILDCARD]a.ts:1:0
hint: Add a user tag or issue reference to the TODO comment, e.g. TODO(@djones), TODO(djones), TODO(#123)
help: for further information visit https://lint.deno.land/#ban-untagged-todo
(no-unused-vars) `add` is never used
function add(a: number, b: number): number {
^^^
at [WILDCARD]a.ts:2:9
hint: If this is intentional, prefix it with an underscore like `_add`
help: for further information visit https://lint.deno.land/#no-unused-vars
Found 2 problems
Checked 1 file

View file

@ -1,8 +1,13 @@
{ {
"lint": { "lint": {
"rules": { "rules": {
"exclude": ["camelcase"], "exclude": [
"include": ["ban-untagged-todo"] "camelcase"
],
"include": [
"ban-untagged-todo"
],
"tags": []
} }
} }
} }

View file

@ -3,5 +3,10 @@
"types": [ "types": [
"./a.d.ts" "./a.d.ts"
] ]
},
"lint": {
"rules": {
"tags": []
}
} }
} }

View file

@ -51,9 +51,9 @@ pub async fn lint(
watch: bool, watch: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let LintFlags { let LintFlags {
rules_tags, maybe_rules_tags,
rules_include, maybe_rules_include,
rules_exclude, maybe_rules_exclude,
files: args, files: args,
ignore, ignore,
json, json,
@ -102,9 +102,9 @@ pub async fn lint(
// and `--rules-include` CLI flag, only the flag value is taken into account. // and `--rules-include` CLI flag, only the flag value is taken into account.
let lint_rules = get_configured_rules( let lint_rules = get_configured_rules(
maybe_lint_config.as_ref(), maybe_lint_config.as_ref(),
rules_tags, maybe_rules_tags,
rules_include, maybe_rules_include,
rules_exclude, maybe_rules_exclude,
)?; )?;
let resolver = |changed: Option<Vec<PathBuf>>| { let resolver = |changed: Option<Vec<PathBuf>>| {
@ -488,20 +488,20 @@ fn sort_diagnostics(diagnostics: &mut Vec<LintDiagnostic>) {
pub(crate) fn get_configured_rules( pub(crate) fn get_configured_rules(
maybe_lint_config: Option<&LintConfig>, maybe_lint_config: Option<&LintConfig>,
rules_tags: Vec<String>, maybe_rules_tags: Option<Vec<String>>,
rules_include: Vec<String>, maybe_rules_include: Option<Vec<String>>,
rules_exclude: Vec<String>, maybe_rules_exclude: Option<Vec<String>>,
) -> Result<Vec<Arc<dyn LintRule>>, AnyError> { ) -> Result<Vec<Arc<dyn LintRule>>, AnyError> {
if maybe_lint_config.is_none() if maybe_lint_config.is_none()
&& rules_tags.is_empty() && maybe_rules_tags.is_none()
&& rules_include.is_empty() && maybe_rules_include.is_none()
&& rules_exclude.is_empty() && maybe_rules_exclude.is_none()
{ {
return Ok(rules::get_recommended_rules()); return Ok(rules::get_recommended_rules());
} }
let (config_file_tags, config_file_include, config_file_exclude) = let (config_file_tags, config_file_include, config_file_exclude) =
if let Some(lint_config) = maybe_lint_config.as_ref() { if let Some(lint_config) = maybe_lint_config {
( (
lint_config.rules.tags.clone(), lint_config.rules.tags.clone(),
lint_config.rules.include.clone(), lint_config.rules.include.clone(),
@ -511,26 +511,26 @@ pub(crate) fn get_configured_rules(
(None, None, None) (None, None, None)
}; };
let maybe_configured_include = if !rules_include.is_empty() { let maybe_configured_include = if maybe_rules_include.is_some() {
Some(rules_include) maybe_rules_include
} else { } else {
config_file_include config_file_include
}; };
let maybe_configured_exclude = if !rules_exclude.is_empty() { let maybe_configured_exclude = if maybe_rules_exclude.is_some() {
Some(rules_exclude) maybe_rules_exclude
} else { } else {
config_file_exclude config_file_exclude
}; };
let configured_tags = if !rules_tags.is_empty() { let maybe_configured_tags = if maybe_rules_tags.is_some() {
rules_tags maybe_rules_tags
} else { } else {
config_file_tags.unwrap_or_else(Vec::new) config_file_tags
}; };
let configured_rules = rules::get_filtered_rules( let configured_rules = rules::get_filtered_rules(
Some(configured_tags), maybe_configured_tags,
maybe_configured_exclude, maybe_configured_exclude,
maybe_configured_include, maybe_configured_include,
); );