Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ impl ConfigurationBuilder {
self.insert("functionExpression.spaceAfterFunctionKeyword", value.into())
}

/// Whether to indent the body of a function expression used in an IIFE.
///
/// `true` (default) - Indents the body as normal.
/// `false` - Does not indent the body of the function in `(function() { ... })();`.
pub fn function_expression_indent_inside_iife(&mut self, value: bool) -> &mut Self {
self.insert("functionExpression.indentInsideIife", value.into())
}

/// Whether to add a space before the parentheses of a get accessor.
///
/// `true` - Ex. `get myProp ()`
Expand Down
3 changes: 2 additions & 1 deletion src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
function_declaration_space_before_parentheses: get_value(&mut config, "functionDeclaration.spaceBeforeParentheses", false, &mut diagnostics),
function_expression_space_before_parentheses: get_value(&mut config, "functionExpression.spaceBeforeParentheses", false, &mut diagnostics),
function_expression_space_after_function_keyword: get_value(&mut config, "functionExpression.spaceAfterFunctionKeyword", false, &mut diagnostics),
function_expression_indent_inside_iife: get_value(&mut config, "functionExpression.indentInsideIife", true, &mut diagnostics),
get_accessor_space_before_parentheses: get_value(&mut config, "getAccessor.spaceBeforeParentheses", false, &mut diagnostics),
if_statement_space_after_if_keyword: get_value(&mut config, "ifStatement.spaceAfterIfKeyword", true, &mut diagnostics),
import_declaration_space_surrounding_named_imports: get_value(&mut config, "importDeclaration.spaceSurroundingNamedImports", true, &mut diagnostics),
Expand Down Expand Up @@ -353,8 +354,8 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)

#[cfg(test)]
mod tests {
use dprint_core::configuration::resolve_global_config;
use dprint_core::configuration::NewLineKind;
use dprint_core::configuration::resolve_global_config;

use super::super::builder::ConfigurationBuilder;
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ pub struct Configuration {
pub function_expression_space_before_parentheses: bool,
#[serde(rename = "functionExpression.spaceAfterFunctionKeyword")]
pub function_expression_space_after_function_keyword: bool,
#[serde(rename = "functionExpression.indentInsideIife")]
pub function_expression_indent_inside_iife: bool,
#[serde(rename = "getAccessor.spaceBeforeParentheses")]
pub get_accessor_space_before_parentheses: bool,
#[serde(rename = "ifStatement.spaceAfterIfKeyword")]
Expand Down
8 changes: 5 additions & 3 deletions src/generation/context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::parser::token::TokenAndSpan;
use deno_ast::view::*;
use deno_ast::MediaType;
use deno_ast::SourcePos;
use deno_ast::SourceRange;
use deno_ast::SourceRanged;
use deno_ast::SourceRangedForSpanned;
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::parser::token::TokenAndSpan;
use deno_ast::view::*;
Comment thread
bartlomieju marked this conversation as resolved.
Outdated
use dprint_core::formatting::ConditionReference;
use dprint_core::formatting::IndentLevel;
use dprint_core::formatting::IsStartOfLine;
Expand Down Expand Up @@ -68,6 +68,7 @@ pub struct Context<'a> {
stored_lsil: FxHashMap<(SourcePos, SourcePos), LineStartIndentLevel>,
stored_ln: FxHashMap<(SourcePos, SourcePos), LineNumber>,
stored_il: FxHashMap<(SourcePos, SourcePos), IndentLevel>,
pub skip_iife_body_indent: bool,
pub end_statement_or_member_lns: Stack<LineNumber>,
before_comments_start_info_stack: Stack<(SourceRange, LineNumber, IsStartOfLine)>,
if_stmt_last_brace_condition_ref: Option<ConditionReference>,
Expand Down Expand Up @@ -102,6 +103,7 @@ impl<'a> Context<'a> {
stored_lsil: FxHashMap::default(),
stored_ln: FxHashMap::default(),
stored_il: FxHashMap::default(),
skip_iife_body_indent: false,
end_statement_or_member_lns: Default::default(),
before_comments_start_info_stack: Default::default(),
if_stmt_last_brace_condition_ref: None,
Expand Down
55 changes: 28 additions & 27 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::common::comments::CommentKind;
use deno_ast::swc::parser::token::BinOpToken;
use deno_ast::swc::parser::token::Token;
use deno_ast::swc::parser::token::TokenAndSpan;
use deno_ast::view::*;
use deno_ast::CommentsIterator;
use deno_ast::MediaType;
use deno_ast::ParsedSource;
use deno_ast::SourcePos;
use deno_ast::SourceRange;
use deno_ast::SourceRanged;
use deno_ast::SourceRangedForSpanned;
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::common::comments::CommentKind;
use deno_ast::swc::parser::token::BinOpToken;
use deno_ast::swc::parser::token::Token;
use deno_ast::swc::parser::token::TokenAndSpan;
use deno_ast::view::*;
Comment thread
bartlomieju marked this conversation as resolved.
Outdated
use dprint_core::formatting::condition_resolvers;
use dprint_core::formatting::conditions::*;
use dprint_core::formatting::ir_helpers::*;
Expand Down Expand Up @@ -115,11 +115,7 @@ fn gen_node_with_inner_gen<'a>(node: Node<'a>, context: &mut Context<'a>, inner_
// keep the leading text, but leave the trailing text to be formatted if on a separate line
let node_text = node.text_fast(context.program);
let end_trim = node_text.trim_end();
if node_text[end_trim.len()..].contains('\n') {
end_trim
} else {
node_text
}
if node_text[end_trim.len()..].contains('\n') { end_trim } else { node_text }
Comment thread
bartlomieju marked this conversation as resolved.
Outdated
} else {
node.text_fast(context.program)
};
Expand Down Expand Up @@ -1134,11 +1130,7 @@ fn gen_export_named_decl<'a>(node: &NamedExport<'a>, context: &mut Context<'a>)
items.push_sc(sc!(";"));
}

if should_single_line {
with_no_new_lines(items)
} else {
items
}
if should_single_line { with_no_new_lines(items) } else { items }
Comment thread
bartlomieju marked this conversation as resolved.
Outdated
}

fn gen_function_decl<'a>(node: &FnDecl<'a>, context: &mut Context<'a>) -> PrintItems {
Expand Down Expand Up @@ -1349,11 +1341,7 @@ fn gen_import_decl<'a>(node: &ImportDecl<'a>, context: &mut Context<'a>) -> Prin
items.push_sc(sc!(";"));
}

if should_single_line {
with_no_new_lines(items)
} else {
items
}
if should_single_line { with_no_new_lines(items) } else { items }
}

fn gen_import_equals_decl<'a>(node: &TsImportEqualsDecl<'a>, context: &mut Context<'a>) -> PrintItems {
Expand Down Expand Up @@ -2638,7 +2626,21 @@ fn gen_expr_with_type_args<'a>(node: &TsExprWithTypeArgs<'a>, context: &mut Cont
items
}

fn is_iife_fn_expr(node: &FnExpr) -> bool {
let parent = node.parent();
if parent.kind() == NodeKind::ParenExpr {
if let Some(grandparent) = parent.parent() {
return grandparent.kind() == NodeKind::CallExpr;
}
}
false
}

fn gen_fn_expr<'a>(node: &FnExpr<'a>, context: &mut Context<'a>) -> PrintItems {
if !context.config.function_expression_indent_inside_iife && is_iife_fn_expr(node) {
context.skip_iife_body_indent = true;
}

let items = gen_function_decl_or_expr(
FunctionDeclOrExprNode {
node: node.into(),
Expand Down Expand Up @@ -7775,11 +7777,7 @@ fn gen_close_paren_with_type<'a>(opts: GenCloseParenWithTypeOptions<'a>, context
items.extend(generated_type_node);
items.push_info(type_node_end_ln);

if use_new_line_group {
new_line_group(items)
} else {
items
}
if use_new_line_group { new_line_group(items) } else { items }
Comment thread
bartlomieju marked this conversation as resolved.
Outdated
} else {
items
};
Expand Down Expand Up @@ -9475,6 +9473,8 @@ struct GenBlockOptions<'a> {

fn gen_block<'a>(gen_inner: impl FnOnce(Vec<Node<'a>>, &mut Context<'a>) -> PrintItems, opts: GenBlockOptions<'a>, context: &mut Context<'a>) -> PrintItems {
let mut items = PrintItems::new();
let skip_indent = context.skip_iife_body_indent;
context.skip_iife_body_indent = false;
let before_open_token_ln = LineNumber::new("after_open_token_info");
let first_member_range = opts.children.first().map(|x| x.range());
let range = opts.range;
Expand All @@ -9493,7 +9493,8 @@ fn gen_block<'a>(gen_inner: impl FnOnce(Vec<Node<'a>>, &mut Context<'a>) -> Prin
items.push_signal(Signal::NewLine);
}
items.push_line_and_column(start_inner_lc);
items.extend(ir_helpers::with_indent(gen_inner(opts.children, context)));
let inner = gen_inner(opts.children, context);
items.extend(if skip_indent { inner } else { ir_helpers::with_indent(inner) });
items.push_line_and_column(end_inner_lc);

if is_tokens_same_line_and_empty {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
~~ functionExpression.indentInsideIife: false ~~
== should not indent body of IIFE ==
(function() {
const a = "foobar";
return { a };
})();

[expect]
(function() {
const a = "foobar";
return { a };
})();

== should not indent body of named IIFE ==
(function init() {
const a = "foobar";
return { a };
})();

[expect]
(function init() {
const a = "foobar";
return { a };
})();

== should not indent body of IIFE with args ==
(function(x) {
const a = x;
return { a };
})(42);

[expect]
(function(x) {
const a = x;
return { a };
})(42);

== should still indent body of regular function expression ==
const fn = function() {
const a = "foobar";
return { a };
};

[expect]
const fn = function() {
const a = "foobar";
return { a };
};

== should still indent body of function expression in call argument ==
call(function() {
const a = "foobar";
return { a };
});

[expect]
call(function() {
const a = "foobar";
return { a };
});

== should not affect arrow function IIFE ==
(() => {
const a = "foobar";
return { a };
})();

[expect]
(() => {
const a = "foobar";
return { a };
})();

== should handle IIFE with nested function ==
(function() {
function inner() {
return 1;
}
return inner();
})();

[expect]
(function() {
function inner() {
return 1;
}
return inner();
})();

== should handle empty IIFE ==
(function() {})();

[expect]
(function() {})();
Loading