Skip to content

Commit

Permalink
Update to full-moon v0.19.0 (#821)
Browse files Browse the repository at this point in the history
* Update to full-moon v0.19.0

* Fix handling of type assertion

* Fix type assertion handling

* Add test case

* Fix compilation
  • Loading branch information
JohnnyMorganz authored Nov 11, 2023
1 parent d45a774 commit 711b0db
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 635 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Test (Luau)
run: cargo test --features luau
run: cargo test --features luau --release # TODO: ideally we don't need to run in release mode!

test_lua52:
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added flag `--respect-ignores`. By default, files explicitly passed to stylua (e.g. `stylua foo.lua`) will always be formatted, regardless of whether the file is ignored. Enabling this flag will consider `.styluaignore` or glob matches before formatting the file. ([#765](https://github.com/JohnnyMorganz/StyLua/issues/765))
- Note: for backwards compatibility reasons, formatting via stdin always respects ignores. This behaviour will change in the next major release

### Changed

- Updated parser crate with following changes:
- Support Luau floor division (`//`)
- Fix Luau string interpolation parsing
- Fix Luau `\z` escape parsing

### Fixed

- Wasm build now correctly supports configuring sort requires ([#818](https://github.com/JohnnyMorganz/StyLua/issues/818))
Expand Down
23 changes: 16 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ console = "0.15.7"
crossbeam-channel = "0.5.8"
ec4rs = { version = "1.0.2", optional = true }
env_logger = { version = "0.10.0", default-features = false }
full_moon = "0.18.1"
full_moon = "0.19.0"
globset = "0.4.13"
ignore = "0.4.20"
lazy_static = "1.4.0"
Expand Down
33 changes: 15 additions & 18 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use full_moon::tokenizer::{Token, TokenReference};
use full_moon::{
ast::{
punctuated::{Pair, Punctuated},
Assignment, Call, Expression, FunctionArgs, FunctionCall, LocalAssignment, Suffix, Value,
Assignment, Call, Expression, FunctionArgs, FunctionCall, LocalAssignment, Suffix,
},
tokenizer::TokenType,
};
Expand Down Expand Up @@ -39,11 +39,10 @@ use crate::{
/// https://github.com/JohnnyMorganz/StyLua/issues/274
pub fn calculate_hang_level(expression: &Expression) -> Option<usize> {
match expression {
Expression::Value { value, .. } => match **value {
Value::ParenthesesExpression(_) => None,
_ => Some(1),
},
Expression::Parentheses { .. } => None,
Expression::UnaryOperator { expression, .. } => calculate_hang_level(expression),
#[cfg(feature = "luau")]
Expression::TypeAssertion { expression, .. } => calculate_hang_level(expression),
_ => Some(1),
}
}
Expand Down Expand Up @@ -128,12 +127,11 @@ fn is_complex_function_call(function_call: &FunctionCall) -> bool {
let mut complexity_count = 0;

for argument in arguments {
if let Expression::Value { value, .. } = argument {
match &**value {
Value::Function(_) => return true,
Value::TableConstructor(_) => complexity_count += 1,
_ => (),
}
match argument {
Expression::Function(_) => return true,
Expression::TableConstructor(_) => complexity_count += 1,
// TODO: should we handle embedded expr in Expression::TypeAssertion { expression } here?
_ => (),
}
}

Expand All @@ -152,13 +150,12 @@ fn is_complex_function_call(function_call: &FunctionCall) -> bool {
/// Determines whether we should prevent hanging at the equals token depending on the RHS expression
fn prevent_equals_hanging(expression: &Expression) -> bool {
match expression {
Expression::Value { value, .. } => match &**value {
Value::Function(_) => true,
Value::FunctionCall(function_call) => is_complex_function_call(function_call),
#[cfg(feature = "luau")]
Value::IfExpression(_) => true,
_ => false,
},
Expression::Function(_) => true,
Expression::FunctionCall(function_call) => is_complex_function_call(function_call),
#[cfg(feature = "luau")]
Expression::IfExpression(_) => true,
#[cfg(feature = "luau")]
Expression::TypeAssertion { expression, .. } => prevent_equals_hanging(expression),
_ => false,
}
}
Expand Down
40 changes: 21 additions & 19 deletions src/formatters/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
shape::Shape,
};
use full_moon::ast::{
punctuated::Punctuated, Block, Expression, LastStmt, Prefix, Return, Stmt, Value, Var,
punctuated::Punctuated, Block, Expression, LastStmt, Prefix, Return, Stmt, Var,
};
use full_moon::tokenizer::TokenType;
use full_moon::tokenizer::{Token, TokenReference};
Expand All @@ -33,12 +33,10 @@ macro_rules! update_first_token {
}

fn is_function_or_table_constructor(expression: &Expression) -> bool {
match expression {
Expression::Value { value, .. } => {
matches!(&**value, Value::TableConstructor(_) | Value::Function(_))
}
_ => false,
}
matches!(
expression,
Expression::TableConstructor(_) | Expression::Function(_)
)
}

pub fn format_return(ctx: &Context, return_node: &Return, shape: Shape) -> Return {
Expand Down Expand Up @@ -280,22 +278,22 @@ fn prefix_remove_leading_newlines(prefix: &Prefix) -> Prefix {
let leading_trivia = trivia_remove_leading_newlines(token.leading_trivia().collect());
Prefix::Name(token.update_leading_trivia(FormatTriviaType::Replace(leading_trivia)))
}
Prefix::Expression(expr) => Prefix::Expression(match expr {
Prefix::Expression(expr) => Prefix::Expression(match &**expr {
Expression::Parentheses {
contained,
expression,
} => {
let (start_parens, end_parens) = contained.tokens();
let leading_trivia =
trivia_remove_leading_newlines(start_parens.leading_trivia().collect());
Expression::Parentheses {
Box::new(Expression::Parentheses {
contained: full_moon::ast::span::ContainedSpan::new(
start_parens
.update_leading_trivia(FormatTriviaType::Replace(leading_trivia)),
end_parens.to_owned(),
),
expression: Box::new(*expression.to_owned()),
}
})
}
other => {
unreachable!("got non-parentheses expression as prefix {:?}", other)
Expand All @@ -314,7 +312,7 @@ fn var_remove_leading_newline(var: Var) -> Var {
}
Var::Expression(var_expr) => {
let prefix = prefix_remove_leading_newlines(var_expr.prefix());
Var::Expression(var_expr.with_prefix(prefix))
Var::Expression(Box::new(var_expr.with_prefix(prefix)))
}
other => panic!("unknown node {:?}", other),
}
Expand Down Expand Up @@ -481,17 +479,21 @@ pub fn format_block(ctx: &Context, block: &Block, shape: Shape) -> Block {
| Stmt::Repeat(_) => {
let next_stmt = stmt_iterator.peek();
match next_stmt {
Some((Stmt::FunctionCall(function_call), _)) => matches!(
function_call.prefix(),
Prefix::Expression(Expression::Parentheses { .. })
),
Some((Stmt::FunctionCall(function_call), _)) => match function_call.prefix() {
Prefix::Expression(expression) => {
matches!(&**expression, Expression::Parentheses { .. })
}
_ => false,
},
Some((Stmt::Assignment(assignment), _)) => {
match assignment.variables().iter().next() {
Some(Var::Expression(var_expression)) => {
matches!(
var_expression.prefix(),
Prefix::Expression(Expression::Parentheses { .. })
)
match var_expression.prefix() {
Prefix::Expression(expression) => {
matches!(&**expression, Expression::Parentheses { .. })
}
_ => false,
}
}
_ => false,
}
Expand Down
Loading

0 comments on commit 711b0db

Please sign in to comment.