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): automatically opt-in packages to jsr lint tag #23072

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 37 additions & 32 deletions cli/tools/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,39 +878,44 @@ 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)]
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/lint/jsr_tag/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions tests/specs/lint/jsr_tag/non_package.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checked 2 files
3 changes: 3 additions & 0 deletions tests/specs/lint/jsr_tag/non_package/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MyType } from "./type.ts";

export const myVar: MyType = "hello";
1 change: 1 addition & 0 deletions tests/specs/lint/jsr_tag/non_package/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MyType = string;
12 changes: 12 additions & 0 deletions tests/specs/lint/jsr_tag/package.out
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions tests/specs/lint/jsr_tag/package/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@scope/package",
"version": "1.0.0",
"exports": "./mod.ts"
}
3 changes: 3 additions & 0 deletions tests/specs/lint/jsr_tag/package/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MyType } from "./type.ts";

export const myVar: MyType = "hello";
1 change: 1 addition & 0 deletions tests/specs/lint/jsr_tag/package/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MyType = string;
39 changes: 27 additions & 12 deletions tests/specs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});
Expand Down