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

feat(lint): add support for config file and CLI flags for rules #11776

Merged
merged 20 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions cli/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,34 @@ impl Serialize for TsConfig {
}
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default)]
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
pub struct LintRulesConfig {
pub tags: Option<Vec<String>>,
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
pub include: Option<Vec<String>>,
pub exclude: Option<Vec<String>>,
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default)]
pub struct LintFilesConfig {
pub include: Vec<String>,
pub exclude: Vec<String>,
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default)]
#[serde(deny_unknown_fields)]
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
pub struct LintConfig {
pub rules: LintRulesConfig,
pub files: LintFilesConfig,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigFileJson {
pub compiler_options: Option<Value>,
pub lint: Option<Value>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -340,6 +364,16 @@ impl ConfigFile {
Ok((json!({}), None))
}
}

pub fn as_lint_config(&self) -> Result<Option<LintConfig>, AnyError> {
if let Some(config) = self.json.lint.clone() {
let lint_config: LintConfig = serde_json::from_value(config)
.context("Failed to parse \"lint\" configuration")?;
Ok(Some(lint_config))
} else {
Ok(None)
}
}
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(test)]
Expand Down Expand Up @@ -397,6 +431,16 @@ mod tests {
"build": true,
// comments are allowed
"strict": true
},
"lint": {
"files": {
"include": ["src/"],
"exclude": ["src/testdata/"]
},
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"]
}
}
}"#;
let config_path = PathBuf::from("/deno/tsconfig.json");
Expand All @@ -414,6 +458,22 @@ mod tests {
maybe_path: Some(config_path),
}),
);

let lint_config = config_file
.as_lint_config()
.expect("error parsing lint object")
.expect("lint object should be defined");
assert_eq!(lint_config.files.include, vec!["src/"]);
assert_eq!(lint_config.files.exclude, vec!["src/testdata/"]);
assert_eq!(
lint_config.rules.include,
Some(vec!["ban-untagged-todo".to_string()])
);
assert_eq!(
lint_config.rules.tags,
Some(vec!["recommended".to_string()])
);
assert!(lint_config.rules.exclude.is_none());
}

#[test]
Expand Down
24 changes: 24 additions & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ Ignore linting a file by adding an ignore comment at the top of the file:
.long("rules")
.help("List available rules"),
)
.arg(config_arg())
.arg(
Arg::with_name("ignore")
.long("ignore")
Expand Down Expand Up @@ -1722,6 +1723,7 @@ fn lsp_parse(flags: &mut Flags, _matches: &clap::ArgMatches) {
}

fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
config_arg_parse(flags, matches);
let files = match matches.values_of("files") {
Some(f) => f.map(PathBuf::from).collect(),
None => vec![],
Expand Down Expand Up @@ -2508,6 +2510,28 @@ mod tests {
..Flags::default()
}
);

let r = flags_from_vec(svec![
"deno",
"lint",
"--config",
"Deno.jsonc",
"--json",
"script_1.ts"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Lint {
files: vec![PathBuf::from("script_1.ts")],
rules: false,
json: true,
ignore: vec![],
},
config_path: Some("Deno.jsonc".to_string()),
..Flags::default()
}
);
}

#[test]
Expand Down
12 changes: 10 additions & 2 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ async fn lsp_command() -> Result<(), AnyError> {
}

async fn lint_command(
_flags: Flags,
flags: Flags,
files: Vec<PathBuf>,
list_rules: bool,
ignore: Vec<PathBuf>,
Expand All @@ -507,7 +507,15 @@ async fn lint_command(
return Ok(());
}

tools::lint::lint_files(files, ignore, json).await
let program_state = ProgramState::build(flags.clone()).await?;
let maybe_lint_config =
if let Some(config_file) = &program_state.maybe_config_file {
config_file.as_lint_config()?
} else {
None
};

tools::lint::lint_files(maybe_lint_config, files, ignore, json).await
}

async fn cache_command(
Expand Down
38 changes: 29 additions & 9 deletions cli/tests/integration/lint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,79 @@ fn ignore_unexplicit_files() {
}

itest!(all {
args: "lint --unstable lint/file1.js lint/file2.ts lint/ignored_file.ts",
args: "lint lint/file1.js lint/file2.ts lint/ignored_file.ts",
output: "lint/expected.out",
exit_code: 1,
});

itest!(quiet {
args: "lint --unstable --quiet lint/file1.js",
args: "lint --quiet lint/file1.js",
output: "lint/expected_quiet.out",
exit_code: 1,
});

itest!(json {
args:
"lint --unstable --json lint/file1.js lint/file2.ts lint/ignored_file.ts lint/malformed.js",
"lint --json lint/file1.js lint/file2.ts lint/ignored_file.ts lint/malformed.js",
output: "lint/expected_json.out",
exit_code: 1,
});

itest!(ignore {
args: "lint --unstable --ignore=lint/file1.js,lint/malformed.js lint/",
args:
"lint --ignore=lint/file1.js,lint/malformed.js,lint/lint_with_config/ lint/",
output: "lint/expected_ignore.out",
exit_code: 1,
});

itest!(glob {
args: "lint --unstable --ignore=lint/malformed.js lint/",
args: "lint --ignore=lint/malformed.js,lint/lint_with_config/ lint/",
output: "lint/expected_glob.out",
exit_code: 1,
});

itest!(stdin {
args: "lint --unstable -",
args: "lint -",
input: Some("let _a: any;"),
output: "lint/expected_from_stdin.out",
exit_code: 1,
});

itest!(stdin_json {
args: "lint --unstable --json -",
args: "lint --json -",
input: Some("let _a: any;"),
output: "lint/expected_from_stdin_json.out",
exit_code: 1,
});

itest!(rules {
args: "lint --unstable --rules",
args: "lint --rules",
output: "lint/expected_rules.out",
exit_code: 0,
});

// Make sure that the rules are printed if quiet option is enabled.
itest!(rules_quiet {
args: "lint --unstable --rules -q",
args: "lint --rules -q",
output: "lint/expected_rules.out",
exit_code: 0,
});

itest!(lint_with_config {
args: "lint --config lint/Deno.jsonc lint/lint_with_config/",
dsherret marked this conversation as resolved.
Show resolved Hide resolved
output: "lint/lint_with_config.out",
exit_code: 1,
});

// Check if CLI flags take precedence
itest!(lint_with_config_and_flags {
args: "lint --config lint/Deno.jsonc --ignore=lint/lint_with_config/a.ts",
output: "lint/lint_with_config_and_flags.out",
exit_code: 1,
});

itest!(lint_with_malformed_config {
args: "lint --config lint/Deno.malformed.jsonc",
output: "lint/lint_with_malformed_config.out",
exit_code: 1,
});
12 changes: 12 additions & 0 deletions cli/tests/testdata/lint/Deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"lint": {
"files": {
"include": ["lint/lint_with_config/"],
"exclude": ["lint/lint_with_config/b.ts"]
},
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"]
}
}
}
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions cli/tests/testdata/lint/Deno.malformed.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"lint": {
"files": {
"include": ["lint/lint_with_config/"],
"exclude": ["lint/lint_with_config/b.ts"]
},
"dont_know_this_field": {},
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"]
}
}
}
18 changes: 18 additions & 0 deletions cli/tests/testdata/lint/lint_with_config.out
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions cli/tests/testdata/lint/lint_with_config/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: foo
function add(a: number, b: number): number {
return a + b;
}
4 changes: 4 additions & 0 deletions cli/tests/testdata/lint/lint_with_config/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: this file should be ignored
function subtract(a: number, b: number): number {
return a - b;
}
18 changes: 18 additions & 0 deletions cli/tests/testdata/lint/lint_with_config_and_flags.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ban-untagged-todo) TODO should be tagged with (@username) or (#issue)
// TODO: this file should be ignored
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at [WILDCARD]b.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) `subtract` is never used
function subtract(a: number, b: number): number {
^^^^^^^^
at [WILDCARD]b.ts:2:9

hint: If this is intentional, prefix it with an underscore like `_subtract`
help: for further information visit https://lint.deno.land/#no-unused-vars

Found 2 problems
Checked 1 file
4 changes: 4 additions & 0 deletions cli/tests/testdata/lint/lint_with_malformed_config.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: Failed to parse "lint" configuration

Caused by:
unknown field `dont_know_this_field`, expected `rules` or `files`
Loading