mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(parser): detect @flow in `/** @flow */ comment (#4861)
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.
This commit is contained in:
parent
a105df3adf
commit
1bdde2c117
2 changed files with 20 additions and 17 deletions
|
|
@ -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>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Reference in a new issue