Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/lexer/trivia_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct TriviaBuilder {
// NOTE(lucab): This is a set of unique comments. Duplicated
// comments could be generated in case of rewind; they are
// filtered out at insertion time.
comments: Vec<Comment>,
pub(crate) comments: Vec<Comment>,
irregular_whitespaces: Vec<Span>,
}

Expand Down
35 changes: 19 additions & 16 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,11 @@ 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> {
if self.source_type.is_javascript()
&& (self.source_text.starts_with("// @flow")
|| self.source_text.starts_with("/* @flow */"))
{
return Some(diagnostics::flow(Span::new(0, 8)));
}
None
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))
}

/// Check if source length exceeds MAX_LEN, if the file cannot be parsed.
Expand Down Expand Up @@ -439,15 +437,20 @@ mod test {
fn flow_error() {
let allocator = Allocator::default();
let source_type = SourceType::default();
let source = "// @flow\nasdf adsf";
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.program.is_empty());
assert_eq!(ret.errors.first().unwrap().to_string(), "Flow is not supported");

let source = "/* @flow */\n asdf asdf";
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.program.is_empty());
assert_eq!(ret.errors.first().unwrap().to_string(), "Flow is not supported");
let sources = [
"// @flow\nasdf adsf",
"/* @flow */\n asdf asdf",
"/**
* @flow
*/
asdf asdf
",
];
for source in sources {
let ret = Parser::new(&allocator, source, source_type).parse();
assert!(ret.program.is_empty());
assert_eq!(ret.errors.first().unwrap().to_string(), "Flow is not supported");
}
}

#[test]
Expand Down