feat(rome_js_parser): Improve parser error for generator function in an ambient context
MODIFY
crates/rome_js_formatter/src/js/declarations/function_declaration.rs
crates/rome_js_formatter/src/js/declarations/function_declaration.rs
@@ -57,9 +57,11 @@ impl FormatFunction {
FormatFunction::JsFunctionExportDefaultDeclaration(declaration) => {
declaration.function_token()
}
- FormatFunction::TsDeclareFunctionDeclaration(member) => member.function_token(),
- FormatFunction::TsDeclareFunctionExportDefaultDeclaration(member) => {
- member.function_token()
+ FormatFunction::TsDeclareFunctionDeclaration(declaration) => {
+ declaration.function_token()
+ }
+ FormatFunction::TsDeclareFunctionExportDefaultDeclaration(declaration) => {
+ declaration.function_token()
}
}
}
@@ -81,8 +83,10 @@ impl FormatFunction {
FormatFunction::JsFunctionDeclaration(declaration) => declaration.id().map(Some),
FormatFunction::JsFunctionExpression(expression) => Ok(expression.id()),
FormatFunction::JsFunctionExportDefaultDeclaration(declaration) => Ok(declaration.id()),
- FormatFunction::TsDeclareFunctionDeclaration(member) => member.id().map(Some),
- FormatFunction::TsDeclareFunctionExportDefaultDeclaration(member) => Ok(member.id()),
+ FormatFunction::TsDeclareFunctionDeclaration(declaration) => declaration.id().map(Some),
+ FormatFunction::TsDeclareFunctionExportDefaultDeclaration(declaration) => {
+ Ok(declaration.id())
+ }
}
}
@@ -93,9 +97,11 @@ impl FormatFunction {
FormatFunction::JsFunctionExportDefaultDeclaration(declaration) => {
declaration.type_parameters()
}
- FormatFunction::TsDeclareFunctionDeclaration(member) => member.type_parameters(),
- FormatFunction::TsDeclareFunctionExportDefaultDeclaration(member) => {
- member.type_parameters()
+ FormatFunction::TsDeclareFunctionDeclaration(declaration) => {
+ declaration.type_parameters()
+ }
+ FormatFunction::TsDeclareFunctionExportDefaultDeclaration(declaration) => {
+ declaration.type_parameters()
}
}
}
@@ -107,9 +113,9 @@ impl FormatFunction {
FormatFunction::JsFunctionExportDefaultDeclaration(declaration) => {
declaration.parameters()
}
- FormatFunction::TsDeclareFunctionDeclaration(member) => member.parameters(),
- FormatFunction::TsDeclareFunctionExportDefaultDeclaration(member) => {
- member.parameters()
+ FormatFunction::TsDeclareFunctionDeclaration(declaration) => declaration.parameters(),
+ FormatFunction::TsDeclareFunctionExportDefaultDeclaration(declaration) => {
+ declaration.parameters()
}
}
}
@@ -123,9 +129,11 @@ impl FormatFunction {
FormatFunction::JsFunctionExportDefaultDeclaration(declaration) => {
declaration.return_type_annotation()
}
- FormatFunction::TsDeclareFunctionDeclaration(member) => member.return_type_annotation(),
- FormatFunction::TsDeclareFunctionExportDefaultDeclaration(member) => {
- member.return_type_annotation()
+ FormatFunction::TsDeclareFunctionDeclaration(declaration) => {
+ declaration.return_type_annotation()
+ }
+ FormatFunction::TsDeclareFunctionExportDefaultDeclaration(declaration) => {
+ declaration.return_type_annotation()
}
}
}

MODIFY
crates/rome_js_parser/src/syntax/function.rs
crates/rome_js_parser/src/syntax/function.rs
@@ -383,17 +383,24 @@ fn parse_ambient_function(p: &mut Parser, m: Marker, kind: AmbientFunctionKind)
}
p.expect(T![function]);
- let binding = parse_binding(p);
- let is_binding_absent = binding.is_absent();
- if !kind.is_export_default() && is_binding_absent {
- // test_err ts ts_declare_function_export_declaration_missing_id
+ let is_generator = p.at(T![*]);
+ if is_generator {
+ // test_err ts ts_declare_generator_function
+ // declare function* test(): void;
// declare module 'x' {
- // export function(option: any): void
+ // export default function* test(): void
// }
- binding.or_add_diagnostic(p, expected_binding);
+ p.error(p.err_builder(
+ "Generators are not allowed in an ambient context.",
+ p.cur_range(),
+ ));
+ p.bump(T![*]);
}
+ let binding = parse_binding(p);
+ let binding_range = p.cur_range();
+
parse_ts_type_parameters(p).ok();
parse_parameter_list(p, ParameterContext::Declaration, SignatureFlags::empty())
.or_add_diagnostic(p, expected_parameters);
@@ -423,6 +430,13 @@ fn parse_ambient_function(p: &mut Parser, m: Marker, kind: AmbientFunctionKind)
// }
m.complete(p, TS_DECLARE_FUNCTION_EXPORT_DEFAULT_DECLARATION)
} else {
+ // test_err ts ts_declare_function_export_declaration_missing_id
+ // declare module 'x' {
+ // export function(option: any): void
+ // }
+ if binding.is_absent() {
+ p.error(expected_binding(p, binding_range));
+ }
// test ts ts_declare_function_export_declaration
// declare module 'x' {
// export function test(option: any): void

ADD
crates/rome_js_parser/test_data/inline/err/ts_declare_generator_function.ts
+declare function* test(): void;
+declare module 'x' {
+ export default function* test(): void
+}

feat(rome_js_parser): Improve parser error for generator function in an ambient context (#3778)