Skip to content

Commit 0b3c271

Browse files
committed
Various fixes
1 parent ce46e9f commit 0b3c271

6 files changed

Lines changed: 96 additions & 15 deletions

File tree

deployment/schema.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,18 @@
645645
"description": "Ex. `function<T>()`"
646646
}]
647647
},
648+
"functionExpression.flatIife": {
649+
"description": "Whether to skip indenting the body of a function expression used in an IIFE.",
650+
"type": "boolean",
651+
"default": false,
652+
"oneOf": [{
653+
"const": true,
654+
"description": "Does not indent the body of the function in `(function() { ... })();`."
655+
}, {
656+
"const": false,
657+
"description": "Indents the body as normal."
658+
}]
659+
},
648660
"getAccessor.spaceBeforeParentheses": {
649661
"description": "Whether to add a space before the parentheses of a get accessor.",
650662
"type": "boolean",
@@ -973,6 +985,9 @@
973985
"functionExpression.spaceAfterFunctionKeyword": {
974986
"$ref": "#/definitions/functionExpression.spaceAfterFunctionKeyword"
975987
},
988+
"functionExpression.flatIife": {
989+
"$ref": "#/definitions/functionExpression.flatIife"
990+
},
976991
"getAccessor.spaceBeforeParentheses": {
977992
"$ref": "#/definitions/getAccessor.spaceBeforeParentheses"
978993
},

src/configuration/builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,12 @@ impl ConfigurationBuilder {
348348
self.insert("functionExpression.spaceAfterFunctionKeyword", value.into())
349349
}
350350

351-
/// Whether to indent the body of a function expression used in an IIFE.
351+
/// Whether to skip indenting the body of a function expression used in an IIFE.
352352
///
353-
/// `true` (default) - Indents the body as normal.
354-
/// `false` - Does not indent the body of the function in `(function() { ... })();`.
355-
pub fn function_expression_indent_inside_iife(&mut self, value: bool) -> &mut Self {
356-
self.insert("functionExpression.indentInsideIife", value.into())
353+
/// `true` - Does not indent the body of the function in `(function() { ... })();`.
354+
/// `false` (default) - Indents the body as normal.
355+
pub fn function_expression_flat_iife(&mut self, value: bool) -> &mut Self {
356+
self.insert("functionExpression.flatIife", value.into())
357357
}
358358

359359
/// Whether to add a space before the parentheses of a get accessor.

src/configuration/resolve_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
289289
function_declaration_space_before_parentheses: get_value(&mut config, "functionDeclaration.spaceBeforeParentheses", false, &mut diagnostics),
290290
function_expression_space_before_parentheses: get_value(&mut config, "functionExpression.spaceBeforeParentheses", false, &mut diagnostics),
291291
function_expression_space_after_function_keyword: get_value(&mut config, "functionExpression.spaceAfterFunctionKeyword", false, &mut diagnostics),
292-
function_expression_indent_inside_iife: get_value(&mut config, "functionExpression.indentInsideIife", true, &mut diagnostics),
292+
function_expression_flat_iife: get_value(&mut config, "functionExpression.flatIife", false, &mut diagnostics),
293293
get_accessor_space_before_parentheses: get_value(&mut config, "getAccessor.spaceBeforeParentheses", false, &mut diagnostics),
294294
if_statement_space_after_if_keyword: get_value(&mut config, "ifStatement.spaceAfterIfKeyword", true, &mut diagnostics),
295295
import_declaration_space_surrounding_named_imports: get_value(&mut config, "importDeclaration.spaceSurroundingNamedImports", true, &mut diagnostics),

src/configuration/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ pub struct Configuration {
604604
pub function_expression_space_before_parentheses: bool,
605605
#[serde(rename = "functionExpression.spaceAfterFunctionKeyword")]
606606
pub function_expression_space_after_function_keyword: bool,
607-
#[serde(rename = "functionExpression.indentInsideIife")]
608-
pub function_expression_indent_inside_iife: bool,
607+
#[serde(rename = "functionExpression.flatIife")]
608+
pub function_expression_flat_iife: bool,
609609
#[serde(rename = "getAccessor.spaceBeforeParentheses")]
610610
pub get_accessor_space_before_parentheses: bool,
611611
#[serde(rename = "ifStatement.spaceAfterIfKeyword")]

src/generation/generate.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,17 +2639,23 @@ fn gen_expr_with_type_args<'a>(node: &TsExprWithTypeArgs<'a>, context: &mut Cont
26392639
}
26402640

26412641
fn is_iife_fn_expr(node: &FnExpr) -> bool {
2642-
let parent = node.parent();
2643-
if parent.kind() == NodeKind::ParenExpr {
2644-
if let Some(grandparent) = parent.parent() {
2645-
return grandparent.kind() == NodeKind::CallExpr;
2646-
}
2642+
let mut current: Node = node.into();
2643+
while let Some(parent) = current.parent() {
2644+
if parent.is::<ParenExpr>() {
2645+
current = parent;
2646+
continue;
2647+
}
2648+
return match parent {
2649+
Node::CallExpr(call) => call.callee.range() == current.range(),
2650+
Node::OptCall(call) => call.callee.range() == current.range(),
2651+
_ => false,
2652+
};
26472653
}
26482654
false
26492655
}
26502656

26512657
fn gen_fn_expr<'a>(node: &FnExpr<'a>, context: &mut Context<'a>) -> PrintItems {
2652-
if !context.config.function_expression_indent_inside_iife && is_iife_fn_expr(node) {
2658+
if context.config.function_expression_flat_iife && is_iife_fn_expr(node) {
26532659
context.skip_iife_body_indent = true;
26542660
}
26552661

tests/specs/expressions/FunctionExpression/FunctionExpression_IndentInsideIife_False.txt renamed to tests/specs/expressions/FunctionExpression/FunctionExpression_FlatIife_True.txt

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
~~ functionExpression.indentInsideIife: false ~~
1+
~~ functionExpression.flatIife: true ~~
22
== should not indent body of IIFE ==
33
(function() {
44
const a = "foobar";
@@ -92,3 +92,63 @@ return inner();
9292

9393
[expect]
9494
(function() {})();
95+
96+
== should not indent body of IIFE wrapped in extra parens ==
97+
((function() {
98+
const a = "foobar";
99+
return { a };
100+
}))();
101+
102+
[expect]
103+
(function() {
104+
const a = "foobar";
105+
return { a };
106+
})();
107+
108+
== should not indent body of optional IIFE ==
109+
(function() {
110+
const a = "foobar";
111+
return { a };
112+
})?.();
113+
114+
[expect]
115+
(function() {
116+
const a = "foobar";
117+
return { a };
118+
})?.();
119+
120+
== should not indent body of bare-callee IIFE ==
121+
void function() {
122+
const a = "foobar";
123+
return { a };
124+
}();
125+
126+
[expect]
127+
void function() {
128+
const a = "foobar";
129+
return { a };
130+
}();
131+
132+
== should still indent body of function expression in member call ==
133+
(function() {
134+
const a = "foobar";
135+
return { a };
136+
}).call(null);
137+
138+
[expect]
139+
(function() {
140+
const a = "foobar";
141+
return { a };
142+
}).call(null);
143+
144+
== should still indent body of function expression as call argument ==
145+
foo((function() {
146+
const a = "foobar";
147+
return { a };
148+
}));
149+
150+
[expect]
151+
foo(function() {
152+
const a = "foobar";
153+
return { a };
154+
});

0 commit comments

Comments
 (0)