|
1 | 1 | use oxc_allocator::Vec; |
2 | 2 | use oxc_ast::ast::*; |
3 | | -use oxc_span::{GetSpan, Span}; |
4 | 3 |
|
5 | | -use crate::{format::print::call_arguments, group, ir::Doc, text, Format, Prettier}; |
| 4 | +use crate::{ |
| 5 | + array, dynamic_text, format::print::call_arguments, group, indent, ir::Doc, line, softline, |
| 6 | + text, Format, Prettier, |
| 7 | +}; |
6 | 8 |
|
7 | 9 | pub enum CallExpressionLike<'a, 'b> { |
8 | 10 | CallExpression(&'b CallExpression<'a>), |
@@ -43,38 +45,86 @@ impl<'a> CallExpressionLike<'a, '_> { |
43 | 45 | } |
44 | 46 | } |
45 | 47 |
|
46 | | -impl GetSpan for CallExpressionLike<'_, '_> { |
47 | | - fn span(&self) -> Span { |
48 | | - match self { |
49 | | - CallExpressionLike::CallExpression(call) => call.span, |
50 | | - CallExpressionLike::NewExpression(new) => new.span, |
51 | | - } |
52 | | - } |
53 | | -} |
54 | | - |
55 | 48 | pub fn print_call_expression<'a>( |
56 | 49 | p: &mut Prettier<'a>, |
57 | | - expression: &CallExpressionLike<'a, '_>, |
| 50 | + expr: &CallExpressionLike<'a, '_>, |
58 | 51 | ) -> Doc<'a> { |
| 52 | + // TODO: |
| 53 | + // if ( |
| 54 | + // isTemplateLiteralSingleArg || |
| 55 | + // // Dangling comments are not handled, all these special cases should have arguments #9668 |
| 56 | + // // We want to keep CommonJS- and AMD-style require calls, and AMD-style |
| 57 | + // // define calls, as a unit. |
| 58 | + // // e.g. `define(["some/lib"], (lib) => {` |
| 59 | + // isCommonsJsOrAmdModuleDefinition(path) || |
| 60 | + // // Keep test declarations on a single line |
| 61 | + // // e.g. `it('long name', () => {` |
| 62 | + // isTestCall(node, path.parent) |
| 63 | + // ) { |
| 64 | + // const printed = []; |
| 65 | + // iterateCallArgumentsPath(path, () => printed.push(print())); |
| 66 | + // if (!printed[0].label?.embed) { |
| 67 | + // return [ |
| 68 | + // isNew ? "new " : "", |
| 69 | + // printCallee(path, print), |
| 70 | + // optional, |
| 71 | + // printFunctionTypeParameters(path, options, print), |
| 72 | + // "(", |
| 73 | + // join(", ", printed), |
| 74 | + // ")", |
| 75 | + // ]; |
| 76 | + // } |
| 77 | + // } |
| 78 | + |
| 79 | + // TODO: |
| 80 | + // if ( |
| 81 | + // !isNew && |
| 82 | + // isMemberish(node.callee) && |
| 83 | + // !path.call( |
| 84 | + // (path) => pathNeedsParens(path, options), |
| 85 | + // "callee", |
| 86 | + // ...(node.callee.type === "ChainExpression" ? ["expression"] : []), |
| 87 | + // ) |
| 88 | + // ) |
| 89 | + // return printMemberChain(path, options, print); |
| 90 | + |
59 | 91 | let mut parts = Vec::new_in(p.allocator); |
60 | 92 |
|
61 | | - if expression.is_new() { |
| 93 | + if expr.is_new() { |
62 | 94 | parts.push(text!("new ")); |
63 | 95 | }; |
64 | | - |
65 | | - parts.push(expression.callee().format(p)); |
66 | | - |
67 | | - if let Some(type_parameters) = expression.type_parameters() { |
| 96 | + parts.push(expr.callee().format(p)); |
| 97 | + if expr.optional() { |
| 98 | + parts.push(text!("?.")); |
| 99 | + } |
| 100 | + if let Some(type_parameters) = expr.type_parameters() { |
68 | 101 | parts.push(type_parameters.format(p)); |
69 | 102 | } |
| 103 | + parts.push(call_arguments::print_call_arguments(p, expr)); |
70 | 104 |
|
71 | | - if expression.optional() { |
72 | | - parts.push(text!("?.")); |
| 105 | + if expr.callee().is_call_expression() { |
| 106 | + return group!(p, parts); |
73 | 107 | } |
74 | 108 |
|
75 | | - parts.push(call_arguments::print_call_arguments(p, expression)); |
| 109 | + array!(p, parts) |
| 110 | +} |
| 111 | + |
| 112 | +// In Prettier, `printCallExpression()` has 4 branches. |
| 113 | +// But for `ImportExpression`, it only passes the 1st and 3rd branches. |
| 114 | +// - if (isTemplateLiteralSingleArg) return [callee, "(", source, ")"]; |
| 115 | +// - return group([callee, callArguments([source, arguments])]); |
| 116 | +pub fn print_import_expression<'a>(p: &mut Prettier<'a>, expr: &ImportExpression<'a>) -> Doc<'a> { |
| 117 | + let callee_doc = { |
| 118 | + if let Some(phase) = &expr.phase { |
| 119 | + array!(p, [text!("import."), dynamic_text!(p, phase.as_str())]); |
| 120 | + } |
| 121 | + text!("import") |
| 122 | + }; |
| 123 | + |
| 124 | + // TODO: isTemplateLiteralSingleArg branch |
| 125 | + // return [callee, "(", source, ")"]; |
76 | 126 |
|
77 | | - group!(p, parts) |
| 127 | + group!(p, [callee_doc, call_arguments::print_import_source_and_arguments(p, expr)]) |
78 | 128 | } |
79 | 129 |
|
80 | 130 | /// <https://github.com/prettier/prettier/blob/7aecca5d6473d73f562ca3af874831315f8f2581/src/language-js/print/call-expression.js#L93-L116> |
|
0 commit comments