-
-
Notifications
You must be signed in to change notification settings - Fork 861
fix(editor): re-add tester and prevent empty inverted diagnostics #9991
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| console.log("Hello, world!"); |
48 changes: 48 additions & 0 deletions
48
crates/oxc_language_server/fixtures/linter/issue_9958/.oxlintrc.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| { | ||
| "env": { | ||
| "browser": true, | ||
| "node": true, | ||
| "es2022": true | ||
| }, | ||
| "plugins": [ | ||
| "node", | ||
| "eslint", | ||
| "oxc", | ||
| "unicorn", | ||
| // "import", | ||
| "typescript", | ||
| "react", | ||
| "react-perf", | ||
| "jsx-a11y", | ||
| "vitest", | ||
| "nextjs" | ||
| ], | ||
|
|
||
| "rules": { | ||
| "eqeqeq": "error", | ||
| "react/rules-of-hooks": "error", | ||
| "react/exhaustive-deps": "error", | ||
| "eslint/no-unused-vars": [ | ||
| "error", | ||
| { | ||
| "varsIgnorePattern": "^_", | ||
| "argsIgnorePattern": "^_", | ||
| "caughtErrorsIgnorePattern": "^_", | ||
| "ignoreRestSiblings": true | ||
| } | ||
| ] | ||
| }, | ||
| "categories": { | ||
| "correctness": "error" | ||
| }, | ||
| "overrides": [ | ||
| { | ||
| "files": ["**/*.spec.{ts,tsx,js,jsx}"], | ||
| "rules": { | ||
| "typescript/no-non-null-assertion": "off", | ||
| "typescript/no-non-null-asserted-optional-chain": "off" | ||
| } | ||
| } | ||
| ], | ||
| "ignorePatterns": ["e2e/**/*", "codemods/**/*", "scripts/**/*"] | ||
| } |
13 changes: 13 additions & 0 deletions
13
crates/oxc_language_server/fixtures/linter/issue_9958/issue.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Test case from https://github.com/oxc-project/oxc/issues/9958 | ||
| export function test() { | ||
| let x = true; | ||
| console.log(!!x ? 10 : 20); | ||
|
|
||
| let a: { | ||
| b?: { | ||
| c: number; | ||
| }; | ||
| } = {}; | ||
|
|
||
| console.log(a?.b?.c!); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
..._language_server/src/linter/snapshots/fixtures_linter_hello_world.js@deny_no_console.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| source: crates/oxc_language_server/src/linter/tester.rs | ||
| --- | ||
| code: "eslint(no-console)" | ||
| code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console" | ||
| message: "eslint(no-console): Unexpected console statement.\nhelp: Delete this console statement." | ||
| range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 11 } } | ||
| related_information[0].message: "" | ||
| related_information[0].location.uri: "file://<variable>/fixtures/linter/hello_world.js" | ||
| related_information[0].location.range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 11 } } | ||
| severity: Some(Error) | ||
| source: Some("oxc") | ||
| tags: None |
4 changes: 4 additions & 0 deletions
4
...es/oxc_language_server/src/linter/snapshots/fixtures_linter_hello_world.js@no_errors.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| source: crates/oxc_language_server/src/linter/tester.rs | ||
| --- | ||
| No diagnostic reports |
28 changes: 28 additions & 0 deletions
28
crates/oxc_language_server/src/linter/snapshots/fixtures_linter_issue_9958_issue.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- | ||
| source: crates/oxc_language_server/src/linter/tester.rs | ||
| --- | ||
| code: "eslint(no-extra-boolean-cast)" | ||
| code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-extra-boolean-cast" | ||
| message: "Redundant double negation\nhelp: Remove the double negation as it will already be coerced to a boolean" | ||
| range: Range { start: Position { line: 3, character: 14 }, end: Position { line: 3, character: 17 } } | ||
| related_information[0].message: "" | ||
| related_information[0].location.uri: "file://<variable>/fixtures/linter/issue_9958/issue.ts" | ||
| related_information[0].location.range: Range { start: Position { line: 3, character: 14 }, end: Position { line: 3, character: 17 } } | ||
| severity: Some(Error) | ||
| source: Some("oxc") | ||
| tags: None | ||
|
|
||
|
|
||
| code: "typescript-eslint(no-non-null-asserted-optional-chain)" | ||
| code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/typescript_eslint/no-non-null-asserted-optional-chain" | ||
| message: "non-null assertions after an optional chain expression\nhelp: Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong. You should remove the non-null assertion." | ||
| range: Range { start: Position { line: 11, character: 18 }, end: Position { line: 11, character: 18 } } | ||
| related_information[0].message: "" | ||
| related_information[0].location.uri: "file://<variable>/fixtures/linter/issue_9958/issue.ts" | ||
| related_information[0].location.range: Range { start: Position { line: 11, character: 18 }, end: Position { line: 11, character: 18 } } | ||
| related_information[1].message: "" | ||
| related_information[1].location.uri: "file://<variable>/fixtures/linter/issue_9958/issue.ts" | ||
| related_information[1].location.range: Range { start: Position { line: 11, character: 21 }, end: Position { line: 11, character: 21 } } | ||
| severity: Some(Error) | ||
| source: Some("oxc") | ||
| tags: None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use oxc_linter::Linter; | ||
| use tower_lsp::lsp_types::{CodeDescription, NumberOrString, Url}; | ||
|
|
||
| use super::{error_with_position::DiagnosticReport, server_linter::ServerLinter}; | ||
|
|
||
| /// Given a file path relative to the crate root directory, return the URI of the file. | ||
| pub fn get_file_uri(relative_file_path: &str) -> Url { | ||
| let absolute_file_path = | ||
| std::env::current_dir().expect("could not get current dir").join(relative_file_path); | ||
| Url::from_file_path(absolute_file_path).expect("failed to convert file path to URL") | ||
| } | ||
|
|
||
| fn get_snapshot_from_report(report: &DiagnosticReport) -> String { | ||
| let code = match &report.diagnostic.code { | ||
| Some(NumberOrString::Number(code)) => code.to_string(), | ||
| Some(NumberOrString::String(code)) => code.to_string(), | ||
| None => "None".to_string(), | ||
| }; | ||
| let code_description_href = match &report.diagnostic.code_description { | ||
| Some(CodeDescription { href }) => href.to_string(), | ||
| None => "None".to_string(), | ||
| }; | ||
| let message = report.diagnostic.message.clone(); | ||
| let range = report.diagnostic.range; | ||
| let related_information = match &report.diagnostic.related_information { | ||
| Some(infos) => { | ||
| infos | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(i, info)| { | ||
| let mut result = String::new(); | ||
| result.push_str(&format!( | ||
| "related_information[{}].message: {:?}", | ||
| i, info.message | ||
| )); | ||
| // replace everything between `file://` and `oxc_language_server` with `<variable>`, to avoid | ||
| // the absolute path causing snapshot test failures in different environments | ||
| let mut location = info.location.uri.to_string(); | ||
| let start = | ||
| location.find("file://").expect("file:// protocol not found in URI"); | ||
| let end = location | ||
| .find("oxc_language_server") | ||
| .expect("oxc_language_server not found in URI"); | ||
| location.replace_range( | ||
| start + "file://".len()..end + "oxc_language_server".len(), | ||
| "<variable>", | ||
| ); | ||
|
|
||
| result.push_str(&format!( | ||
| "\nrelated_information[{i}].location.uri: {location:?}", | ||
| )); | ||
| result.push_str(&format!( | ||
| "\nrelated_information[{}].location.range: {:?}", | ||
| i, info.location.range | ||
| )); | ||
| result | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
| None => "related_information: None".to_string(), | ||
| }; | ||
| let severity = report.diagnostic.severity; | ||
| let source = report.diagnostic.source.clone(); | ||
| let tags = report.diagnostic.tags.clone(); | ||
| format!( | ||
| r" | ||
| code: {code:?} | ||
| code_description.href: {code_description_href:?} | ||
| message: {message:?} | ||
| range: {range:?} | ||
| {related_information} | ||
| severity: {severity:?} | ||
| source: {source:?} | ||
| tags: {tags:?} | ||
| " | ||
| ) | ||
| } | ||
|
|
||
| /// Testing struct for the [linter server][crate::linter::server_linter::ServerLinter]. | ||
| pub struct Tester<'t> { | ||
| server_linter: ServerLinter, | ||
| snapshot_suffix: Option<&'t str>, | ||
| } | ||
|
|
||
| impl Tester<'_> { | ||
| pub fn new() -> Self { | ||
| Self { snapshot_suffix: None, server_linter: ServerLinter::new() } | ||
| } | ||
|
|
||
| pub fn new_with_linter(linter: Linter) -> Self { | ||
| Self { snapshot_suffix: None, server_linter: ServerLinter::new_with_linter(linter) } | ||
| } | ||
|
|
||
| pub fn with_snapshot_suffix(mut self, suffix: &'static str) -> Self { | ||
| self.snapshot_suffix = Some(suffix); | ||
| self | ||
| } | ||
|
|
||
| /// Given a relative file path (relative to `oxc_language_server` crate root), run the linter | ||
| /// and return the resulting diagnostics in a custom snapshot format. | ||
| #[expect(clippy::disallowed_methods)] | ||
| pub fn test_and_snapshot_single_file(&self, relative_file_path: &str) { | ||
| let uri = get_file_uri(relative_file_path); | ||
| let content = std::fs::read_to_string(uri.to_file_path().unwrap()) | ||
| .expect("could not read fixture file"); | ||
| let reports = self.server_linter.run_single(&uri, Some(content)).unwrap(); | ||
| let snapshot = if reports.is_empty() { | ||
| "No diagnostic reports".to_string() | ||
| } else { | ||
| reports.iter().map(get_snapshot_from_report).collect::<Vec<_>>().join("\n") | ||
| }; | ||
|
|
||
| let snapshot_name = relative_file_path.replace('/', "_"); | ||
| let mut settings = insta::Settings::clone_current(); | ||
| settings.set_prepend_module_to_snapshot(false); | ||
| settings.set_omit_expression(true); | ||
| if let Some(suffix) = self.snapshot_suffix { | ||
| settings.set_snapshot_suffix(suffix); | ||
| } | ||
| settings.bind(|| { | ||
| insta::assert_snapshot!(snapshot_name, snapshot); | ||
| }); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.