summaryrefslogtreecommitdiff
path: root/cli/config_file.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-09-03 17:01:58 +0200
committerGitHub <noreply@github.com>2021-09-03 17:01:58 +0200
commitd93570a6195d2be5fe448bb0d33d4f64e839676e (patch)
treea44431235693f484308aad70cda66827605f4568 /cli/config_file.rs
parentc3001fe280add8c695c288fe26b8f81b5f2f0261 (diff)
feat(lint): add support for config file and CLI flags for rules (#11776)
This commit adds support for following flags in deno lint subcommand: --config - allows to load configuration file and parses "lint" object --rules-tags=<tags> - allows specifying which set of tagged rules should be run --rules-include=<rules> - allow specifying which rules should be run --rules-exclude=<rules> - allow specifying which rules should not be run
Diffstat (limited to 'cli/config_file.rs')
-rw-r--r--cli/config_file.rs69
1 files changed, 64 insertions, 5 deletions
diff --git a/cli/config_file.rs b/cli/config_file.rs
index e366be77c..f2f119a0b 100644
--- a/cli/config_file.rs
+++ b/cli/config_file.rs
@@ -234,7 +234,7 @@ impl TsConfig {
maybe_config_file: Option<&ConfigFile>,
) -> Result<Option<IgnoredCompilerOptions>, AnyError> {
if let Some(config_file) = maybe_config_file {
- let (value, maybe_ignored_options) = config_file.as_compiler_options()?;
+ let (value, maybe_ignored_options) = config_file.to_compiler_options()?;
self.merge(&value);
Ok(maybe_ignored_options)
} else {
@@ -266,10 +266,33 @@ impl Serialize for TsConfig {
}
}
+#[derive(Clone, Debug, Default, Deserialize)]
+#[serde(default, deny_unknown_fields)]
+pub struct LintRulesConfig {
+ pub tags: Option<Vec<String>>,
+ pub include: Option<Vec<String>>,
+ pub exclude: Option<Vec<String>>,
+}
+
+#[derive(Clone, Debug, Default, Deserialize)]
+#[serde(default, deny_unknown_fields)]
+pub struct LintFilesConfig {
+ pub include: Vec<String>,
+ pub exclude: Vec<String>,
+}
+
+#[derive(Clone, Debug, Default, Deserialize)]
+#[serde(default, deny_unknown_fields)]
+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)]
@@ -328,7 +351,7 @@ impl ConfigFile {
/// Parse `compilerOptions` and return a serde `Value`.
/// The result also contains any options that were ignored.
- pub fn as_compiler_options(
+ pub fn to_compiler_options(
&self,
) -> Result<(Value, Option<IgnoredCompilerOptions>), AnyError> {
if let Some(compiler_options) = self.json.compiler_options.clone() {
@@ -340,6 +363,16 @@ impl ConfigFile {
Ok((json!({}), None))
}
}
+
+ pub fn to_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)
+ }
+ }
}
#[cfg(test)]
@@ -397,12 +430,22 @@ 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");
let config_file = ConfigFile::new(config_text, &config_path).unwrap();
let (options_value, ignored) =
- config_file.as_compiler_options().expect("error parsing");
+ config_file.to_compiler_options().expect("error parsing");
assert!(options_value.is_object());
let options = options_value.as_object().unwrap();
assert!(options.contains_key("strict"));
@@ -414,6 +457,22 @@ mod tests {
maybe_path: Some(config_path),
}),
);
+
+ let lint_config = config_file
+ .to_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]
@@ -422,7 +481,7 @@ mod tests {
let config_path = PathBuf::from("/deno/tsconfig.json");
let config_file = ConfigFile::new(config_text, &config_path).unwrap();
let (options_value, _) =
- config_file.as_compiler_options().expect("error parsing");
+ config_file.to_compiler_options().expect("error parsing");
assert!(options_value.is_object());
}
@@ -432,7 +491,7 @@ mod tests {
let config_path = PathBuf::from("/deno/tsconfig.json");
let config_file = ConfigFile::new(config_text, &config_path).unwrap();
let (options_value, _) =
- config_file.as_compiler_options().expect("error parsing");
+ config_file.to_compiler_options().expect("error parsing");
assert!(options_value.is_object());
}