Skip to content

Commit

Permalink
fix(parser): detect @flow in `/** @flow */ comment (#4861)
Browse files Browse the repository at this point in the history
Discovered in https://github.com/oxc-project/monitor-oxc

There are files with

```
/**
 * @flow
 */
```

https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/__mocks__/JSXAttributeMock.js#L1

So I changed the logic to checking the first comment instead.
  • Loading branch information
Boshen committed Aug 13, 2024
1 parent a105df3 commit 1bdde2c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
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

0 comments on commit 1bdde2c

Please sign in to comment.