mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat(lint): automatically opt-in packages to jsr
lint tag (#23072)
This automatically opts packages (deno.json's with a name, version, and exports field) into the "jsr" lint tag.
This commit is contained in:
parent
fb1aa4e6d2
commit
0346e597bf
10 changed files with 104 additions and 44 deletions
|
@ -878,41 +878,46 @@ pub fn get_configured_rules(
|
|||
let implicit_no_slow_types = maybe_config_file
|
||||
.map(|c| c.is_package() || !c.json.workspaces.is_empty())
|
||||
.unwrap_or(false);
|
||||
if rules.tags.is_none() && rules.include.is_none() && rules.exclude.is_none()
|
||||
{
|
||||
ConfiguredRules {
|
||||
rules: rules::get_recommended_rules(),
|
||||
no_slow_types: implicit_no_slow_types,
|
||||
}
|
||||
} else {
|
||||
let no_slow_types = implicit_no_slow_types
|
||||
&& !rules
|
||||
.exclude
|
||||
.as_ref()
|
||||
.map(|exclude| exclude.iter().any(|i| i == NO_SLOW_TYPES_NAME))
|
||||
.unwrap_or(false);
|
||||
let rules = rules::get_filtered_rules(
|
||||
rules.tags.or_else(|| Some(vec!["recommended".to_string()])),
|
||||
rules.exclude.map(|exclude| {
|
||||
exclude
|
||||
.into_iter()
|
||||
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
||||
.collect()
|
||||
}),
|
||||
rules.include.map(|include| {
|
||||
include
|
||||
.into_iter()
|
||||
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
||||
.collect()
|
||||
}),
|
||||
);
|
||||
ConfiguredRules {
|
||||
rules,
|
||||
no_slow_types,
|
||||
}
|
||||
let no_slow_types = implicit_no_slow_types
|
||||
&& !rules
|
||||
.exclude
|
||||
.as_ref()
|
||||
.map(|exclude| exclude.iter().any(|i| i == NO_SLOW_TYPES_NAME))
|
||||
.unwrap_or(false);
|
||||
let rules = rules::get_filtered_rules(
|
||||
rules
|
||||
.tags
|
||||
.or_else(|| Some(get_default_tags(maybe_config_file))),
|
||||
rules.exclude.map(|exclude| {
|
||||
exclude
|
||||
.into_iter()
|
||||
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
||||
.collect()
|
||||
}),
|
||||
rules.include.map(|include| {
|
||||
include
|
||||
.into_iter()
|
||||
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
||||
.collect()
|
||||
}),
|
||||
);
|
||||
ConfiguredRules {
|
||||
rules,
|
||||
no_slow_types,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_default_tags(
|
||||
maybe_config_file: Option<&deno_config::ConfigFile>,
|
||||
) -> Vec<String> {
|
||||
let mut tags = Vec::with_capacity(2);
|
||||
tags.push("recommended".to_string());
|
||||
if maybe_config_file.map(|c| c.is_package()).unwrap_or(false) {
|
||||
tags.push("jsr".to_string());
|
||||
}
|
||||
tags
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use deno_lint::rules::get_recommended_rules;
|
||||
|
|
14
tests/specs/lint/jsr_tag/__test__.jsonc
Normal file
14
tests/specs/lint/jsr_tag/__test__.jsonc
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
// packages will be automatically entered into the "jsr" tag
|
||||
"steps": [{
|
||||
"args": "lint",
|
||||
"cwd": "./package",
|
||||
"output": "package.out",
|
||||
"exitCode": 1
|
||||
}, {
|
||||
"args": "lint",
|
||||
"cwd": "./non_package",
|
||||
"output": "non_package.out",
|
||||
"exitCode": 0
|
||||
}]
|
||||
}
|
1
tests/specs/lint/jsr_tag/non_package.out
Normal file
1
tests/specs/lint/jsr_tag/non_package.out
Normal file
|
@ -0,0 +1 @@
|
|||
Checked 2 files
|
3
tests/specs/lint/jsr_tag/non_package/mod.ts
Normal file
3
tests/specs/lint/jsr_tag/non_package/mod.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { MyType } from "./type.ts";
|
||||
|
||||
export const myVar: MyType = "hello";
|
1
tests/specs/lint/jsr_tag/non_package/type.ts
Normal file
1
tests/specs/lint/jsr_tag/non_package/type.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export type MyType = string;
|
12
tests/specs/lint/jsr_tag/package.out
Normal file
12
tests/specs/lint/jsr_tag/package.out
Normal file
|
@ -0,0 +1,12 @@
|
|||
error[verbatim-module-syntax]: All import identifiers are used in types
|
||||
--> [WILDCARD]mod.ts:1:1
|
||||
|
|
||||
1 | import { MyType } from "./type.ts";
|
||||
| ^^^^^^
|
||||
= hint: Change `import` to `import type` and optionally add an explicit side effect import
|
||||
|
||||
docs: https://lint.deno.land/rules/verbatim-module-syntax
|
||||
|
||||
|
||||
Found 1 problem (1 fixable via --fix)
|
||||
Checked 2 files
|
5
tests/specs/lint/jsr_tag/package/deno.json
Normal file
5
tests/specs/lint/jsr_tag/package/deno.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "@scope/package",
|
||||
"version": "1.0.0",
|
||||
"exports": "./mod.ts"
|
||||
}
|
3
tests/specs/lint/jsr_tag/package/mod.ts
Normal file
3
tests/specs/lint/jsr_tag/package/mod.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { MyType } from "./type.ts";
|
||||
|
||||
export const myVar: MyType = "hello";
|
1
tests/specs/lint/jsr_tag/package/type.ts
Normal file
1
tests/specs/lint/jsr_tag/package/type.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export type MyType = string;
|
|
@ -128,19 +128,22 @@ fn run_test(test: &Test, diagnostic_logger: Rc<RefCell<Vec<u8>>>) {
|
|||
context.deno_dir().path().remove_dir_all();
|
||||
}
|
||||
|
||||
let expected_output = if step.output.ends_with(".out") {
|
||||
let test_output_path = cwd.join(&step.output);
|
||||
test_output_path.read_to_string()
|
||||
} else {
|
||||
step.output.clone()
|
||||
};
|
||||
let command = context.new_command().envs(&step.envs);
|
||||
let command = match &step.args {
|
||||
VecOrString::Vec(args) => command.args_vec(args),
|
||||
VecOrString::String(text) => command.args(text),
|
||||
};
|
||||
let command = match &step.cwd {
|
||||
Some(cwd) => command.current_dir(cwd),
|
||||
None => command,
|
||||
};
|
||||
let output = command.run();
|
||||
output.assert_matches_text(expected_output);
|
||||
if step.output.ends_with(".out") {
|
||||
let test_output_path = cwd.join(&step.output);
|
||||
output.assert_matches_file(test_output_path);
|
||||
} else {
|
||||
output.assert_matches_text(&step.output);
|
||||
}
|
||||
output.assert_exit_code(step.exit_code);
|
||||
}
|
||||
}
|
||||
|
@ -194,6 +197,7 @@ struct StepMetaData {
|
|||
#[serde(default)]
|
||||
pub clean_deno_dir: bool,
|
||||
pub args: VecOrString,
|
||||
pub cwd: Option<String>,
|
||||
#[serde(default)]
|
||||
pub envs: HashMap<String, String>,
|
||||
pub output: String,
|
||||
|
@ -299,12 +303,23 @@ fn collect_tests() -> Vec<TestCategory> {
|
|||
}
|
||||
.with_context(|| format!("Failed to parse {}", metadata_path))
|
||||
.unwrap();
|
||||
|
||||
let test_name =
|
||||
format!("{}::{}", category.name, entry.file_name().to_string_lossy());
|
||||
|
||||
// only support characters that work with filtering with `cargo test`
|
||||
if !test_name
|
||||
.chars()
|
||||
.all(|c| c.is_alphanumeric() || matches!(c, '_' | ':'))
|
||||
{
|
||||
panic!(
|
||||
"Invalid test name (only supports alphanumeric and underscore): {}",
|
||||
test_name
|
||||
);
|
||||
}
|
||||
|
||||
category.tests.push(Test {
|
||||
name: format!(
|
||||
"{}::{}",
|
||||
category.name,
|
||||
entry.file_name().to_string_lossy()
|
||||
),
|
||||
name: test_name,
|
||||
cwd: test_dir,
|
||||
metadata,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue