Skip to content

Commit 350cd91

Browse files
authored
1 parent baed1ca commit 350cd91

File tree

12 files changed

+159
-49
lines changed

12 files changed

+159
-49
lines changed

crates/oxc_linter/src/rules/eslint/array_callback_return/return_checker.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ mod tests {
279279
fn test_switch_always_explicit() {
280280
// Return Explicit
281281
let always_explicit = r#"
282-
function() {
282+
function d() {
283283
switch (a) {
284284
case "C":
285285
switch (b) {
@@ -299,7 +299,7 @@ mod tests {
299299
#[test]
300300
fn test_switch_always_implicit() {
301301
let always_implicit = r#"
302-
function() {
302+
function d() {
303303
switch (a) {
304304
case "C":
305305
switch (b) {
@@ -319,7 +319,7 @@ mod tests {
319319
#[test]
320320
fn test_switch_always_mixed() {
321321
let always_mixed = r#"
322-
function() {
322+
function d() {
323323
switch (a) {
324324
case "C":
325325
switch (b) {

crates/oxc_linter/src/rules/eslint/no_obj_calls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn test() {
199199
("let obj = Intl();", None),
200200
("let newObj = new Reflect();", None),
201201
("let obj = Reflect();", None),
202-
("function() { JSON.parse(Atomics()) }", None),
202+
("function d() { JSON.parse(Atomics()) }", None),
203203
// reference test cases
204204
("let j = JSON; j();", None),
205205
("let a = JSON; let b = a; let c = b; b();", None),

crates/oxc_linter/src/rules/jsdoc/require_yields.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ impl Rule for RequireYields {
102102
// This rule checks generator function should have JSDoc `@yields` tag.
103103
// By default, this rule only checks:
104104
// ```
105-
// function*() { yield withValue; }
105+
// function*d() { yield withValue; }
106106
// ```
107107
//
108108
// If `config.forceRequireYields` is `true`, also checks:
109109
// ```
110-
// function*() {}
111-
// function*() { yield; }
110+
// function*d() {}
111+
// function*d() { yield; }
112112
// ```
113113
//
114114
// If generator function does not have JSDoc, it will be skipped.
@@ -673,7 +673,7 @@ fn test() {
673673
* @generator
674674
* @yields
675675
*/
676-
function*() {yield 1;}
676+
function*d() {yield 1;}
677677
",
678678
Some(serde_json::json!([
679679
{
@@ -853,7 +853,7 @@ fn test() {
853853
* @function
854854
* @generator
855855
*/
856-
function*() {}
856+
function*d() {}
857857
",
858858
Some(serde_json::json!([
859859
{
@@ -1446,7 +1446,7 @@ fn test() {
14461446
* fail(`@generator`+missing `@yields`, with config)
14471447
* @generator
14481448
*/
1449-
function*() {}
1449+
function*d() {}
14501450
",
14511451
Some(serde_json::json!([{ "withGeneratorTag": true, }])),
14521452
None,

crates/oxc_linter/src/snapshots/no_obj_calls.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ expression: no_obj_calls
108108
help: Reflect is not a function.
109109

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

crates/oxc_linter/src/snapshots/require_yields.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ expression: require_yields
9797
eslint-plugin-jsdoc(require-yields): Missing JSDoc `@yields` declaration for generator function.
9898
╭─[require_yields.tsx:6:25]
9999
5 │ */
100-
6 │ function*() {}
101-
· ──────────────
100+
6 │ function*d() {}
101+
· ──────────────
102102
7
103103
╰────
104104
help: Add `@yields` tag to the JSDoc comment.

crates/oxc_module_lexer/tests/esm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ import { g } from './test-circular2.js';
728728

729729
#[test]
730730
fn comments() {
731-
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 ";
731+
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 ";
732732
let ModuleLexer { imports, exports, .. } = parse(source);
733733
assert_eq!(imports.len(), 1);
734734
assert_eq!(source.slice(imports[0].s, imports[0].e), "util");

crates/oxc_parser/src/js/function.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::{list::FormalParameterList, FunctionKind};
1111

1212
impl FunctionKind {
1313
pub(crate) fn is_id_required(self) -> bool {
14-
matches!(self, Self::Declaration { single_statement: true })
14+
matches!(self, Self::Declaration)
1515
}
1616

1717
pub(crate) fn is_expression(self) -> bool {
@@ -80,7 +80,7 @@ impl<'a> ParserImpl<'a> {
8080
}
8181

8282
let function_type = match func_kind {
83-
FunctionKind::Declaration { .. } | FunctionKind::DefaultExport => {
83+
FunctionKind::Declaration | FunctionKind::DefaultExport => {
8484
if body.is_none() {
8585
FunctionType::TSDeclareFunction
8686
} else {
@@ -123,8 +123,7 @@ impl<'a> ParserImpl<'a> {
123123
&mut self,
124124
stmt_ctx: StatementContext,
125125
) -> Result<Statement<'a>> {
126-
let func_kind =
127-
FunctionKind::Declaration { single_statement: stmt_ctx.is_single_statement() };
126+
let func_kind = FunctionKind::Declaration;
128127
let decl = self.parse_function_impl(func_kind)?;
129128
if stmt_ctx.is_single_statement() {
130129
if decl.r#async {
@@ -153,7 +152,7 @@ impl<'a> ParserImpl<'a> {
153152
let r#async = self.eat(Kind::Async);
154153
self.expect(Kind::Function)?;
155154
let generator = self.eat(Kind::Star);
156-
let id = self.parse_function_id(func_kind, r#async, generator);
155+
let id = self.parse_function_id(func_kind, r#async, generator)?;
157156
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())
158157
}
159158

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

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

184183
let generator = self.eat(Kind::Star);
185-
let id = self.parse_function_id(func_kind, r#async, generator);
184+
let id = self.parse_function_id(func_kind, r#async, generator)?;
186185
let function =
187186
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())?;
188187

@@ -257,7 +256,7 @@ impl<'a> ParserImpl<'a> {
257256
kind: FunctionKind,
258257
r#async: bool,
259258
generator: bool,
260-
) -> Option<BindingIdentifier<'a>> {
259+
) -> Result<Option<BindingIdentifier<'a>>> {
261260
let ctx = self.ctx;
262261
if kind.is_expression() {
263262
self.ctx = self.ctx.and_await(r#async).and_yield(generator);
@@ -270,9 +269,15 @@ impl<'a> ParserImpl<'a> {
270269
self.ctx = ctx;
271270

272271
if kind.is_id_required() && id.is_none() {
273-
self.error(diagnostics::expect_function_name(self.cur_token().span()));
272+
match self.cur_kind() {
273+
Kind::LParen => {
274+
self.error(diagnostics::expect_function_name(self.cur_token().span()));
275+
}
276+
kind if kind.is_reserved_keyword() => self.expect_without_advance(Kind::Ident)?,
277+
_ => {}
278+
}
274279
}
275280

276-
id
281+
Ok(id)
277282
}
278283
}

crates/oxc_parser/src/js/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum Tristate {
2525

2626
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2727
pub enum FunctionKind {
28-
Declaration { single_statement: bool },
28+
Declaration,
2929
Expression,
3030
DefaultExport,
3131
TSDeclaration,

crates/oxc_parser/src/ts/statement.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,10 @@ impl<'a> ParserImpl<'a> {
325325
self.parse_ts_declare_function(start_span, modifiers)
326326
.map(Declaration::FunctionDeclaration)
327327
} else if self.ts_enabled() {
328-
self.parse_ts_function_impl(
329-
start_span,
330-
FunctionKind::Declaration { single_statement: true },
331-
modifiers,
332-
)
333-
.map(Declaration::FunctionDeclaration)
328+
self.parse_ts_function_impl(start_span, FunctionKind::Declaration, modifiers)
329+
.map(Declaration::FunctionDeclaration)
334330
} else {
335-
self.parse_function_impl(FunctionKind::Declaration { single_statement: true })
331+
self.parse_function_impl(FunctionKind::Declaration)
336332
.map(Declaration::FunctionDeclaration)
337333
}
338334
}
@@ -348,7 +344,7 @@ impl<'a> ParserImpl<'a> {
348344
let r#async = modifiers.contains(ModifierKind::Async);
349345
self.expect(Kind::Function)?;
350346
let func_kind = FunctionKind::TSDeclaration;
351-
let id = self.parse_function_id(func_kind, r#async, false);
347+
let id = self.parse_function_id(func_kind, r#async, false)?;
352348
self.parse_function(start_span, id, r#async, false, func_kind, modifiers)
353349
}
354350

0 commit comments

Comments
 (0)