Skip to content

Commit

Permalink
feat(rome_js_parser): export default function in declare module rome#…
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Nov 17, 2022
1 parent 00266da commit c5238db
Show file tree
Hide file tree
Showing 20 changed files with 795 additions and 14 deletions.
58 changes: 58 additions & 0 deletions crates/rome_js_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions crates/rome_js_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions crates/rome_js_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6318,6 +6318,21 @@ impl IntoFormat<crate::JsFormatContext> for rome_js_syntax::JsFunctionExportDefa
FormatOwnedWithRule :: new (self , crate :: js :: declarations :: function_export_default_declaration :: FormatJsFunctionExportDefaultDeclaration :: default ())
}
}
impl FormatRule < rome_js_syntax :: TsDeclareFunctionExportDefaultDeclaration > for crate :: ts :: declarations :: declare_function_export_default_declaration :: FormatTsDeclareFunctionExportDefaultDeclaration { type Context = JsFormatContext ; # [inline (always)] fn fmt (& self , node : & rome_js_syntax :: TsDeclareFunctionExportDefaultDeclaration , f : & mut JsFormatter) -> FormatResult < () > { FormatNodeRule :: < rome_js_syntax :: TsDeclareFunctionExportDefaultDeclaration > :: fmt (self , node , f) } }
impl AsFormat for rome_js_syntax::TsDeclareFunctionExportDefaultDeclaration {
type Format < 'a > = FormatRefWithRule < 'a , rome_js_syntax :: TsDeclareFunctionExportDefaultDeclaration , crate :: ts :: declarations :: declare_function_export_default_declaration :: FormatTsDeclareFunctionExportDefaultDeclaration > ;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule :: new (self , crate :: ts :: declarations :: declare_function_export_default_declaration :: FormatTsDeclareFunctionExportDefaultDeclaration :: default ())
}
}
impl IntoFormat<crate::JsFormatContext>
for rome_js_syntax::TsDeclareFunctionExportDefaultDeclaration
{
type Format = FormatOwnedWithRule < rome_js_syntax :: TsDeclareFunctionExportDefaultDeclaration , crate :: ts :: declarations :: declare_function_export_default_declaration :: FormatTsDeclareFunctionExportDefaultDeclaration > ;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule :: new (self , crate :: ts :: declarations :: declare_function_export_default_declaration :: FormatTsDeclareFunctionExportDefaultDeclaration :: default ())
}
}
impl FormatRule<rome_js_syntax::JsExportNamedShorthandSpecifier>
for crate::js::module::export_named_shorthand_specifier::FormatJsExportNamedShorthandSpecifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ impl FormatRule<JsAnyExportDefaultDeclaration> for FormatJsAnyExportDefaultDecla
node.format().fmt(f)
}
JsAnyExportDefaultDeclaration::TsInterfaceDeclaration(node) => node.format().fmt(f),
JsAnyExportDefaultDeclaration::TsDeclareFunctionExportDefaultDeclaration(node) => {
node.format().fmt(f)
}
}
}
}
12 changes: 2 additions & 10 deletions crates/rome_js_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,17 +787,9 @@ function() {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
// different output than prettier
function Component4() {
return (
<div>
{fn(datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata)}{' '}
<div/>
</div>
);
declare module 'x' {
export default function (option: any): void
}
"#;
let syntax = SourceType::tsx();
let tree = parse(src, FileId::zero(), syntax);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::prelude::*;
use rome_js_syntax::TsDeclareFunctionExportDefaultDeclaration;
use rome_rowan::AstNode;
#[derive(Debug, Clone, Default)]
pub struct FormatTsDeclareFunctionExportDefaultDeclaration;
impl FormatNodeRule<TsDeclareFunctionExportDefaultDeclaration>
for FormatTsDeclareFunctionExportDefaultDeclaration
{
fn fmt_fields(
&self,
node: &TsDeclareFunctionExportDefaultDeclaration,
f: &mut JsFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
}
}
1 change: 1 addition & 0 deletions crates/rome_js_formatter/src/ts/declarations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Generated file, do not edit by hand, see `xtask/codegen`

pub(crate) mod declare_function_declaration;
pub(crate) mod declare_function_export_default_declaration;
pub(crate) mod enum_declaration;
pub(crate) mod external_module_declaration;
pub(crate) mod global_declaration;
Expand Down
41 changes: 37 additions & 4 deletions crates/rome_js_parser/src/syntax/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(super) fn parse_function_declaration(

let m = p.start();
let mut function = if p.state.in_ambient_context() {
parse_ambient_function(p, m)
parse_ambient_function(p, m, AmbientFunctionKind::Declaration)
} else {
parse_function(
p,
Expand Down Expand Up @@ -124,12 +124,24 @@ pub(super) fn parse_function_export_default_declaration(p: &mut Parser) -> Parse
let m = p.start();

Present(if p.state.in_ambient_context() {
parse_ambient_function(p, m)
parse_ambient_function(p, m, AmbientFunctionKind::ExportDefault)
} else {
parse_function(p, m, FunctionKind::ExportDefault)
})
}

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum AmbientFunctionKind {
Declaration,
ExportDefault,
}

impl AmbientFunctionKind {
fn is_export_default(&self) -> bool {
matches!(self, AmbientFunctionKind::ExportDefault)
}
}

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum FunctionKind {
Declaration {
Expand Down Expand Up @@ -344,7 +356,7 @@ fn parse_function_id(p: &mut Parser, kind: FunctionKind, flags: SignatureFlags)
// declare module a {
// function test(): string;
// }
pub(crate) fn parse_ambient_function(p: &mut Parser, m: Marker) -> CompletedMarker {
fn parse_ambient_function(p: &mut Parser, m: Marker, kind: AmbientFunctionKind) -> CompletedMarker {
let stmt_start = p.cur_range().start();

// test_err ts ts_declare_async_function
Expand All @@ -359,7 +371,17 @@ pub(crate) fn parse_ambient_function(p: &mut Parser, m: Marker) -> CompletedMark
}

p.expect(T![function]);
parse_binding(p).or_add_diagnostic(p, expected_binding);
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
// declare module 'x' {
// export function(option: any): void
// }
binding.or_add_diagnostic(p, expected_binding);
}

parse_ts_type_parameters(p).ok();
parse_parameter_list(p, ParameterContext::Declaration, SignatureFlags::empty())
.or_add_diagnostic(p, expected_parameters);
Expand All @@ -379,7 +401,18 @@ pub(crate) fn parse_ambient_function(p: &mut Parser, m: Marker) -> CompletedMark

if is_async {
m.complete(p, JS_UNKNOWN_STATEMENT)
} else if is_binding_absent && kind.is_export_default() {
// test ts ts_declare_function_export_default_declaration_without_id
// declare module 'x' {
// export default function(option: any): void
// }
m.complete(p, TS_DECLARE_FUNCTION_EXPORT_DEFAULT_DECLARATION)
} else {
// test ts ts_declare_function_export_declaration
// declare module 'x' {
// export function test(option: any): void
// export default function test(option: any): void
// }
m.complete(p, TS_DECLARE_FUNCTION_DECLARATION)
}
}
Expand Down
Loading

0 comments on commit c5238db

Please sign in to comment.