Skip to content

Commit

Permalink
feat(parser,linter)!: add ParserReturn::is_flow_language; linter ig…
Browse files Browse the repository at this point in the history
…nore flow error (#7373)

closes #7123
  • Loading branch information
Boshen committed Nov 20, 2024
1 parent 666b6c1 commit 878189c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions apps/oxlint/fixtures/flow/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow
import { type Node, type ElementRef } from 'react';
1 change: 1 addition & 0 deletions apps/oxlint/fixtures/flow/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './flow.js'
9 changes: 9 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub(crate) const MAX_LEN: usize = if std::mem::size_of::<usize>() >= 8 {
/// [`program`]: ParserReturn::program
/// [`errors`]: ParserReturn::errors
/// [`panicked`]: ParserReturn::panicked
#[non_exhaustive]
pub struct ParserReturn<'a> {
/// The parsed AST.
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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<Expression<'a>, Vec<OxcDiagnostic>> {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 878189c

Please sign in to comment.