From 878189c407d4ec7e3d2a4beeb000095ba1666c85 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:50:24 +0000 Subject: [PATCH] feat(parser,linter)!: add `ParserReturn::is_flow_language`; linter ignore flow error (#7373) closes #7123 --- apps/oxlint/fixtures/flow/flow.js | 2 ++ apps/oxlint/fixtures/flow/index.mjs | 1 + apps/oxlint/src/lint.rs | 9 +++++++++ crates/oxc_linter/src/service/runtime.rs | 6 +++++- crates/oxc_parser/src/lib.rs | 10 +++++++++- 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 apps/oxlint/fixtures/flow/flow.js create mode 100644 apps/oxlint/fixtures/flow/index.mjs diff --git a/apps/oxlint/fixtures/flow/flow.js b/apps/oxlint/fixtures/flow/flow.js new file mode 100644 index 0000000000000..7b9f49cc147ac --- /dev/null +++ b/apps/oxlint/fixtures/flow/flow.js @@ -0,0 +1,2 @@ +// @flow +import { type Node, type ElementRef } from 'react'; diff --git a/apps/oxlint/fixtures/flow/index.mjs b/apps/oxlint/fixtures/flow/index.mjs new file mode 100644 index 0000000000000..307a5e3cdf37e --- /dev/null +++ b/apps/oxlint/fixtures/flow/index.mjs @@ -0,0 +1 @@ +import './flow.js' diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index e9524802a8f08..78e2f1b8bfb6e 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -378,6 +378,15 @@ mod test { assert_eq!(result.number_of_errors, 0); } + #[test] + fn ignore_flow() { + let args = &["--import-plugin", "fixtures/flow/index.mjs"]; + let result = test(args); + assert_eq!(result.number_of_files, 1); + assert_eq!(result.number_of_warnings, 0); + assert_eq!(result.number_of_errors, 0); + } + #[test] fn filter_allow_all() { let args = &["-A", "all", "fixtures/linter"]; diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 5d54da9063828..2e012b455cfe2 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -199,7 +199,11 @@ impl Runtime { .parse(); if !ret.errors.is_empty() { - return ret.errors.into_iter().map(|err| Message::new(err, None)).collect(); + return if ret.is_flow_language { + vec![] + } else { + ret.errors.into_iter().map(|err| Message::new(err, None)).collect() + }; }; // Build the module record to unblock other threads from waiting for too long. diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index 4d8f7013d368b..4b991161d68e3 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -135,6 +135,7 @@ pub(crate) const MAX_LEN: usize = if std::mem::size_of::() >= 8 { /// [`program`]: ParserReturn::program /// [`errors`]: ParserReturn::errors /// [`panicked`]: ParserReturn::panicked +#[non_exhaustive] pub struct ParserReturn<'a> { /// The parsed AST. /// @@ -168,6 +169,9 @@ pub struct ParserReturn<'a> { /// [`program`]: ParserReturn::program /// [`errors`]: ParserReturn::errors pub panicked: bool, + + /// Whether the file is [flow](https://flow.org). + pub is_flow_language: bool, } /// Parse options @@ -415,10 +419,12 @@ impl<'a> ParserImpl<'a> { }; self.check_unfinished_errors(); + let mut is_flow_language = false; let mut errors = vec![]; // only check for `@flow` if the file failed to parse. if !self.lexer.errors.is_empty() || !self.errors.is_empty() { if let Some(error) = self.flow_error() { + is_flow_language = true; errors.push(error); } } @@ -429,7 +435,7 @@ impl<'a> ParserImpl<'a> { } let irregular_whitespaces = self.lexer.trivia_builder.irregular_whitespaces.into_boxed_slice(); - ParserReturn { program, errors, irregular_whitespaces, panicked } + ParserReturn { program, errors, irregular_whitespaces, panicked, is_flow_language } } pub fn parse_expression(mut self) -> std::result::Result, Vec> { @@ -570,6 +576,7 @@ mod test { let ret = Parser::new(&allocator, source, source_type).parse(); assert!(ret.program.is_empty()); assert!(ret.errors.is_empty()); + assert!(!ret.is_flow_language); } #[test] @@ -597,6 +604,7 @@ mod test { ]; for source in sources { let ret = Parser::new(&allocator, source, source_type).parse(); + assert!(ret.is_flow_language); assert_eq!(ret.errors.len(), 1); assert_eq!(ret.errors.first().unwrap().to_string(), "Flow is not supported"); }