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): should parser error when function declaration has no name #3461

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
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod tests {
fn test_switch_always_explicit() {
// Return Explicit
let always_explicit = r#"
function() {
function d() {
switch (a) {
case "C":
switch (b) {
Expand All @@ -299,7 +299,7 @@ mod tests {
#[test]
fn test_switch_always_implicit() {
let always_implicit = r#"
function() {
function d() {
switch (a) {
case "C":
switch (b) {
Expand All @@ -319,7 +319,7 @@ mod tests {
#[test]
fn test_switch_always_mixed() {
let always_mixed = r#"
function() {
function d() {
switch (a) {
case "C":
switch (b) {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn test() {
("let obj = Intl();", None),
("let newObj = new Reflect();", None),
("let obj = Reflect();", None),
("function() { JSON.parse(Atomics()) }", None),
("function d() { JSON.parse(Atomics()) }", None),
// reference test cases
("let j = JSON; j();", None),
("let a = JSON; let b = a; let c = b; b();", None),
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/rules/jsdoc/require_yields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ impl Rule for RequireYields {
// This rule checks generator function should have JSDoc `@yields` tag.
// By default, this rule only checks:
// ```
// function*() { yield withValue; }
// function*d() { yield withValue; }
// ```
//
// If `config.forceRequireYields` is `true`, also checks:
// ```
// function*() {}
// function*() { yield; }
// function*d() {}
// function*d() { yield; }
// ```
//
// If generator function does not have JSDoc, it will be skipped.
Expand Down Expand Up @@ -673,7 +673,7 @@ fn test() {
* @generator
* @yields
*/
function*() {yield 1;}
function*d() {yield 1;}
",
Some(serde_json::json!([
{
Expand Down Expand Up @@ -853,7 +853,7 @@ fn test() {
* @function
* @generator
*/
function*() {}
function*d() {}
",
Some(serde_json::json!([
{
Expand Down Expand Up @@ -1446,7 +1446,7 @@ fn test() {
* fail(`@generator`+missing `@yields`, with config)
* @generator
*/
function*() {}
function*d() {}
",
Some(serde_json::json!([{ "withGeneratorTag": true, }])),
None,
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/snapshots/no_obj_calls.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ expression: no_obj_calls
help: Reflect is not a function.

⚠ eslint(no-obj-calls): Disallow calling some global objects as functions
╭─[no_obj_calls.tsx:1:25]
1 │ function() { JSON.parse(Atomics()) }
· ─────────
╭─[no_obj_calls.tsx:1:27]
1 │ function d() { JSON.parse(Atomics()) }
· ─────────
╰────
help: Atomics is not a function.

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/snapshots/require_yields.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ expression: require_yields
⚠ eslint-plugin-jsdoc(require-yields): Missing JSDoc `@yields` declaration for generator function.
╭─[require_yields.tsx:6:25]
5 │ */
6 │ function*() {}
· ──────────────
6 │ function*d() {}
· ──────────────
7 │
╰────
help: Add `@yields` tag to the JSDoc comment.
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_module_lexer/tests/esm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ import { g } from './test-circular2.js';

#[test]
fn comments() {
let source = " /*\n VERSION\n */\nimport util from 'util';\n\n//\nfunction x() {\n}\n\n/**/\n// '\n/* / */\n/*\n\n * export { b }\n\\*/\nexport { a }\n\n function () {\n/***/\n }\n ";
let source = " /*\n VERSION\n */\nimport util from 'util';\n\n//\nfunction x() {\n}\n\n/**/\n// '\n/* / */\n/*\n\n * export { b }\n\\*/\nexport { a }\n\n function d() {\n/***/\n }\n ";
let ModuleLexer { imports, exports, .. } = parse(source);
assert_eq!(imports.len(), 1);
assert_eq!(source.slice(imports[0].s, imports[0].e), "util");
Expand Down
25 changes: 15 additions & 10 deletions crates/oxc_parser/src/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{list::FormalParameterList, FunctionKind};

impl FunctionKind {
pub(crate) fn is_id_required(self) -> bool {
matches!(self, Self::Declaration { single_statement: true })
matches!(self, Self::Declaration)
}

pub(crate) fn is_expression(self) -> bool {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl<'a> ParserImpl<'a> {
}

let function_type = match func_kind {
FunctionKind::Declaration { .. } | FunctionKind::DefaultExport => {
FunctionKind::Declaration | FunctionKind::DefaultExport => {
if body.is_none() {
FunctionType::TSDeclareFunction
} else {
Expand Down Expand Up @@ -123,8 +123,7 @@ impl<'a> ParserImpl<'a> {
&mut self,
stmt_ctx: StatementContext,
) -> Result<Statement<'a>> {
let func_kind =
FunctionKind::Declaration { single_statement: stmt_ctx.is_single_statement() };
let func_kind = FunctionKind::Declaration;
let decl = self.parse_function_impl(func_kind)?;
if stmt_ctx.is_single_statement() {
if decl.r#async {
Expand Down Expand Up @@ -153,7 +152,7 @@ impl<'a> ParserImpl<'a> {
let r#async = self.eat(Kind::Async);
self.expect(Kind::Function)?;
let generator = self.eat(Kind::Star);
let id = self.parse_function_id(func_kind, r#async, generator);
let id = self.parse_function_id(func_kind, r#async, generator)?;
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())
}

Expand All @@ -168,7 +167,7 @@ impl<'a> ParserImpl<'a> {
let r#async = modifiers.contains(ModifierKind::Async);
self.expect(Kind::Function)?;
let generator = self.eat(Kind::Star);
let id = self.parse_function_id(func_kind, r#async, generator);
let id = self.parse_function_id(func_kind, r#async, generator)?;
self.parse_function(start_span, id, r#async, generator, func_kind, modifiers)
}

Expand All @@ -182,7 +181,7 @@ impl<'a> ParserImpl<'a> {
self.expect(Kind::Function)?;

let generator = self.eat(Kind::Star);
let id = self.parse_function_id(func_kind, r#async, generator);
let id = self.parse_function_id(func_kind, r#async, generator)?;
let function =
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())?;

Expand Down Expand Up @@ -257,7 +256,7 @@ impl<'a> ParserImpl<'a> {
kind: FunctionKind,
r#async: bool,
generator: bool,
) -> Option<BindingIdentifier<'a>> {
) -> Result<Option<BindingIdentifier<'a>>> {
let ctx = self.ctx;
if kind.is_expression() {
self.ctx = self.ctx.and_await(r#async).and_yield(generator);
Expand All @@ -270,9 +269,15 @@ impl<'a> ParserImpl<'a> {
self.ctx = ctx;

if kind.is_id_required() && id.is_none() {
self.error(diagnostics::expect_function_name(self.cur_token().span()));
match self.cur_kind() {
Kind::LParen => {
self.error(diagnostics::expect_function_name(self.cur_token().span()));
}
kind if kind.is_reserved_keyword() => self.expect_without_advance(Kind::Ident)?,
_ => {}
}
}

id
Ok(id)
}
}
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum Tristate {

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum FunctionKind {
Declaration { single_statement: bool },
Declaration,
Expression,
DefaultExport,
TSDeclaration,
Expand Down
12 changes: 4 additions & 8 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,10 @@ impl<'a> ParserImpl<'a> {
self.parse_ts_declare_function(start_span, modifiers)
.map(Declaration::FunctionDeclaration)
} else if self.ts_enabled() {
self.parse_ts_function_impl(
start_span,
FunctionKind::Declaration { single_statement: true },
modifiers,
)
.map(Declaration::FunctionDeclaration)
self.parse_ts_function_impl(start_span, FunctionKind::Declaration, modifiers)
.map(Declaration::FunctionDeclaration)
} else {
self.parse_function_impl(FunctionKind::Declaration { single_statement: true })
self.parse_function_impl(FunctionKind::Declaration)
.map(Declaration::FunctionDeclaration)
}
}
Expand All @@ -348,7 +344,7 @@ impl<'a> ParserImpl<'a> {
let r#async = modifiers.contains(ModifierKind::Async);
self.expect(Kind::Function)?;
let func_kind = FunctionKind::TSDeclaration;
let id = self.parse_function_id(func_kind, r#async, false);
let id = self.parse_function_id(func_kind, r#async, false)?;
self.parse_function(start_span, id, r#async, false, func_kind, modifiers)
}

Expand Down
Loading