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

fix(parser): only show flow error if it's a flow file #5069

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ impl<'a> ParserImpl<'a> {
let (program, panicked) = match self.parse_program() {
Ok(program) => (program, false),
Err(error) => {
self.error(
self.flow_error().unwrap_or_else(|| self.overlong_error().unwrap_or(error)),
);
let error =
self.flow_error().unwrap_or_else(|| self.overlong_error().unwrap_or(error));
self.error(error);
let program = self.ast.program(
Span::default(),
self.source_type,
Expand Down Expand Up @@ -359,12 +359,17 @@ impl<'a> ParserImpl<'a> {

/// Check for Flow declaration if the file cannot be parsed.
/// The declaration must be [on the first line before any code](https://flow.org/en/docs/usage/#toc-prepare-your-code-for-flow)
fn flow_error(&self) -> Option<OxcDiagnostic> {
fn flow_error(&mut self) -> Option<OxcDiagnostic> {
if !self.source_type.is_javascript() {
return None;
};
let span = self.lexer.trivia_builder.comments.first()?.span;
span.source_text(self.source_text).contains("@flow").then(|| diagnostics::flow(span))
if span.source_text(self.source_text).contains("@flow") {
self.errors.clear();
Some(diagnostics::flow(span))
} else {
None
}
}

/// Check if source length exceeds MAX_LEN, if the file cannot be parsed.
Expand Down Expand Up @@ -447,6 +452,7 @@ mod test {
for source in sources {
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.program.is_empty());
assert_eq!(ret.errors.len(), 1);
assert_eq!(ret.errors.first().unwrap().to_string(), "Flow is not supported");
}
}
Expand Down