From d20d58646010d96ce5102cda35718e91ee0b66ff Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:02:04 +0100 Subject: [PATCH 01/16] Add feature flags --- Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 66d356e8..8344b5b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,8 @@ bench = false default = [] luau = ["full_moon/roblox"] lua52 = ["full_moon/lua52"] +lua53 = ["full_moon/lua53"] +lua54 = ["full_moon/lua54"] [dependencies] anyhow = "1.0.53" From 49af9a65938b36abd245f2dcc2aece8a923500cf Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:03:05 +0100 Subject: [PATCH 02/16] Update README --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d577db7..77ffa244 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ -An opinionated code formatter for Lua 5.1, Lua 5.2 and [Luau](https://roblox.github.io/luau/), built using [full-moon](https://github.com/Kampfkarren/full-moon). +An opinionated code formatter for Lua 5.1, 5.2, 5.3, 5.4 and [Luau](https://roblox.github.io/luau/), built using [full-moon](https://github.com/Kampfkarren/full-moon). StyLua is inspired by the likes of [prettier](https://github.com/prettier/prettier), it parses your Lua codebase, and prints it back out from scratch, enforcing a consistent code style. @@ -26,11 +26,13 @@ If you would like to format a specific Lua version only, see [installing from cr If you have [Rust](https://www.rust-lang.org/) installed, you can install StyLua using cargo. By default, this builds for just Lua 5.1. -You can pass the `--features ` argument to build for Lua 5.2 (`lua52`) or Luau (`luau`) +You can pass the `--features ` argument to build for Lua 5.2 (`lua52`), Lua 5.3 (`lua53`), Lua 5.4 (`lua54`) or Luau (`luau`) ```sh cargo install stylua cargo install stylua --features lua52 +cargo install stylua --features lua53 +cargo install stylua --features lua54 cargo install stylua --features luau ``` From 609eca59f9952487f8ce85795a1970c37e13c0e6 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:03:44 +0100 Subject: [PATCH 03/16] Update flags --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8344b5b6..d047c7de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,8 @@ bench = false default = [] luau = ["full_moon/roblox"] lua52 = ["full_moon/lua52"] -lua53 = ["full_moon/lua53"] -lua54 = ["full_moon/lua54"] +lua53 = ["lua52", "full_moon/lua53"] +lua54 = ["lua53", "full_moon/lua54"] [dependencies] anyhow = "1.0.53" From 671d8506a39106b90dc6cc1ce64d959d94d196f1 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:03:57 +0100 Subject: [PATCH 04/16] Add test cases --- tests/inputs-lua53/binary-operators.lua | 6 ++++++ tests/inputs-lua53/unary-operators.lua | 1 + tests/inputs-lua54/attributes-1.lua | 5 +++++ tests/tests.rs | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 tests/inputs-lua53/binary-operators.lua create mode 100644 tests/inputs-lua53/unary-operators.lua create mode 100644 tests/inputs-lua54/attributes-1.lua diff --git a/tests/inputs-lua53/binary-operators.lua b/tests/inputs-lua53/binary-operators.lua new file mode 100644 index 00000000..3593689e --- /dev/null +++ b/tests/inputs-lua53/binary-operators.lua @@ -0,0 +1,6 @@ +local a = 1 & 2 +local b = 1 | 2 +local c = 1 << 2 +local d = 1 >> 2 +local e = 1 ~ 2 +local f = 1 // 2 diff --git a/tests/inputs-lua53/unary-operators.lua b/tests/inputs-lua53/unary-operators.lua new file mode 100644 index 00000000..ed803461 --- /dev/null +++ b/tests/inputs-lua53/unary-operators.lua @@ -0,0 +1 @@ +local x = ~1 diff --git a/tests/inputs-lua54/attributes-1.lua b/tests/inputs-lua54/attributes-1.lua new file mode 100644 index 00000000..2979db1e --- /dev/null +++ b/tests/inputs-lua54/attributes-1.lua @@ -0,0 +1,5 @@ +local a = 5 +local d +local e , f = 1, 2 +local g , h +local i , j, k diff --git a/tests/tests.rs b/tests/tests.rs index e5066ee4..ea40ac0f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -49,6 +49,24 @@ fn test_lua52() { }) } +#[test] +#[cfg(feature = "lua53")] +fn test_lua53() { + insta::glob!("inputs-lua53/*.lua", |path| { + let contents = std::fs::read_to_string(path).unwrap(); + insta::assert_snapshot!(format(&contents)); + }) +} + +#[test] +#[cfg(feature = "lua54")] +fn test_lua53() { + insta::glob!("inputs-lua54/*.lua", |path| { + let contents = std::fs::read_to_string(path).unwrap(); + insta::assert_snapshot!(format(&contents)); + }) +} + #[test] fn test_ignores() { insta::glob!("inputs-ignore/*.lua", |path| { From 43b0fbb0396d5ac111dcd2555e8dc133617b395d Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:06:49 +0100 Subject: [PATCH 05/16] Fix test name --- tests/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.rs b/tests/tests.rs index ea40ac0f..daa084c6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -60,7 +60,7 @@ fn test_lua53() { #[test] #[cfg(feature = "lua54")] -fn test_lua53() { +fn test_lua54() { insta::glob!("inputs-lua54/*.lua", |path| { let contents = std::fs::read_to_string(path).unwrap(); insta::assert_snapshot!(format(&contents)); From 7d5ba86fd5573d481ba4573d395ddf0ca9aa1e85 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:18:55 +0100 Subject: [PATCH 06/16] Handle Lua 5.3 introductions --- src/formatters/expression.rs | 17 ++++++++++++++++- src/formatters/trivia.rs | 17 ++++++++++++++++- src/formatters/trivia_util.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index b549032a..ffded764 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -35,9 +35,10 @@ use crate::{ #[macro_export] macro_rules! fmt_op { - ($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($operator:ident = $output:expr,)+ }) => { + ($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($(#[$inner:meta])* $operator:ident = $output:expr,)+ }) => { match $value { $( + $(#[$inner])* $enum::$operator(token) => $enum::$operator(fmt_symbol!($ctx, token, $output, $shape)), )+ other => panic!("unknown node {:?}", other), @@ -79,6 +80,18 @@ pub fn format_binop(ctx: &Context, binop: &BinOp, shape: Shape) -> BinOp { TildeEqual = " ~= ", TwoDots = " .. ", TwoEqual = " == ", + #[cfg(feature = "lua53")] + Ampersand = " & ", + #[cfg(feature = "lua53")] + DoubleSlash = " // ", + #[cfg(feature = "lua53")] + DoubleLessThan = " << ", + #[cfg(feature = "lua53")] + Pipe = " | ", + #[cfg(feature = "lua53")] + DoubleGreaterThan = " >> ", + #[cfg(feature = "lua53")] + Tilde = " ~ ", }) } @@ -781,6 +794,8 @@ pub fn format_unop(ctx: &Context, unop: &UnOp, shape: Shape) -> UnOp { Minus = "-", Not = "not ", Hash = "#", + #[cfg(feature = "lua53")] + Tilde = "~", }) } diff --git a/src/formatters/trivia.rs b/src/formatters/trivia.rs index 7981bd22..f5209abd 100644 --- a/src/formatters/trivia.rs +++ b/src/formatters/trivia.rs @@ -158,9 +158,10 @@ macro_rules! define_update_trailing_trivia { } macro_rules! binop_trivia { - ($enum:ident, $value:ident, $leading_trivia:ident, $trailing_trivia:ident, { $($operator:ident,)+ }) => { + ($enum:ident, $value:ident, $leading_trivia:ident, $trailing_trivia:ident, { $($(#[$inner:meta])* $operator:ident,)+ }) => { match $value { $( + $(#[$inner])* $enum::$operator(token) => $enum::$operator(token.update_trivia($leading_trivia, $trailing_trivia)), )+ other => panic!("unknown node {:?}", other), @@ -185,6 +186,18 @@ define_update_trivia!(BinOp, |this, leading, trailing| { TildeEqual, TwoDots, TwoEqual, + #[cfg(feature = "lua53")] + Ampersand, + #[cfg(feature = "lua53")] + DoubleSlash, + #[cfg(feature = "lua53")] + DoubleLessThan, + #[cfg(feature = "lua53")] + Pipe, + #[cfg(feature = "lua53")] + DoubleGreaterThan, + #[cfg(feature = "lua53")] + Tilde, }) }); @@ -643,6 +656,8 @@ define_update_leading_trivia!(UnOp, |this, leading| { UnOp::Hash(token_reference) => UnOp::Hash(token_reference.update_leading_trivia(leading)), UnOp::Minus(token_reference) => UnOp::Minus(token_reference.update_leading_trivia(leading)), UnOp::Not(token_reference) => UnOp::Not(token_reference.update_leading_trivia(leading)), + #[cfg(feature = "lua53")] + UnOp::Tilde(token_reference) => UnOp::Tilde(token_reference.update_leading_trivia(leading)), other => panic!("unknown node {:?}", other), } }); diff --git a/src/formatters/trivia_util.rs b/src/formatters/trivia_util.rs index 6392d4c6..26f986b1 100644 --- a/src/formatters/trivia_util.rs +++ b/src/formatters/trivia_util.rs @@ -372,6 +372,8 @@ pub fn get_expression_leading_trivia(expression: &Expression) -> Vec { UnOp::Minus(token_ref) | UnOp::Not(token_ref) | UnOp::Hash(token_ref) => { token_ref.leading_trivia().map(|x| x.to_owned()).collect() } + #[cfg(feature = "lua53")] + UnOp::Tilde(token_ref) => token_ref.leading_trivia().cloned().collect(), other => panic!("unknown node {:?}", other), }, Expression::BinaryOperator { lhs, .. } => get_expression_leading_trivia(lhs), @@ -441,6 +443,17 @@ pub fn binop_leading_comments(binop: &BinOp) -> Vec { .filter(|token| trivia_is_comment(token)) .map(|x| x.to_owned()) .collect(), + #[cfg(feature = "lua53")] + BinOp::Ampersand(token) + | BinOp::DoubleSlash(token) + | BinOp::DoubleLessThan(token) + | BinOp::Pipe(token) + | BinOp::DoubleGreaterThan(token) + | BinOp::Tilde(token) => token + .leading_trivia() + .filter(|token| trivia_is_comment(token)) + .cloned() + .collect(), other => panic!("unknown node {:?}", other), } } @@ -471,6 +484,20 @@ pub fn binop_trailing_comments(binop: &BinOp) -> Vec { }) .collect() } + #[cfg(feature = "lua53")] + BinOp::Ampersand(token) + | BinOp::DoubleSlash(token) + | BinOp::DoubleLessThan(token) + | BinOp::Pipe(token) + | BinOp::DoubleGreaterThan(token) + | BinOp::Tilde(token) => token + .trailing_trivia() + .filter(|token| trivia_is_comment(token)) + .flat_map(|x| { + // Prepend a single space beforehand + vec![Token::new(TokenType::spaces(1)), x.to_owned()] + }) + .collect(), other => panic!("unknown node {:?}", other), } } From f87918723264ab5557a58ffe22cb9d856d3995fd Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:19:03 +0100 Subject: [PATCH 07/16] Update Lua 5.3 tests --- .../snapshots/tests__lua53@binary-operators.lua.snap | 11 +++++++++++ tests/snapshots/tests__lua53@unary-operators.lua.snap | 6 ++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/snapshots/tests__lua53@binary-operators.lua.snap create mode 100644 tests/snapshots/tests__lua53@unary-operators.lua.snap diff --git a/tests/snapshots/tests__lua53@binary-operators.lua.snap b/tests/snapshots/tests__lua53@binary-operators.lua.snap new file mode 100644 index 00000000..30ff8905 --- /dev/null +++ b/tests/snapshots/tests__lua53@binary-operators.lua.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests.rs +expression: format(&contents) +--- +local a = 1 & 2 +local b = 1 | 2 +local c = 1 << 2 +local d = 1 >> 2 +local e = 1 ~ 2 +local f = 1 // 2 + diff --git a/tests/snapshots/tests__lua53@unary-operators.lua.snap b/tests/snapshots/tests__lua53@unary-operators.lua.snap new file mode 100644 index 00000000..e660cb68 --- /dev/null +++ b/tests/snapshots/tests__lua53@unary-operators.lua.snap @@ -0,0 +1,6 @@ +--- +source: tests/tests.rs +expression: format(&contents) +--- +local x = ~1 + From efbd7805ed0bcb54a345fa2b619cbcbc0d17574a Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:56:25 +0100 Subject: [PATCH 08/16] Handle 5.4 attributes --- src/formatters/assignment.rs | 31 +++++++++-- src/formatters/lua54.rs | 21 ++++++++ src/formatters/mod.rs | 2 + src/formatters/trivia.rs | 22 ++++++++ src/formatters/trivia_util.rs | 96 ++++++++++++++++++++++++++++++----- tests/tests.rs | 1 + 6 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 src/formatters/lua54.rs diff --git a/src/formatters/assignment.rs b/src/formatters/assignment.rs index f28f3164..119d9da0 100644 --- a/src/formatters/assignment.rs +++ b/src/formatters/assignment.rs @@ -9,6 +9,8 @@ use full_moon::{ tokenizer::TokenType, }; +#[cfg(feature = "lua54")] +use crate::formatters::lua54::format_attribute; #[cfg(feature = "luau")] use crate::formatters::luau::format_type_specifier; use crate::{ @@ -386,6 +388,12 @@ fn format_local_no_assignment( Some(1), ); + #[cfg(feature = "lua54")] + let attributes = assignment + .attributes() + .map(|x| x.map(|attribute| format_attribute(ctx, attribute, shape))) + .collect(); + #[cfg(feature = "luau")] let type_specifiers: Vec> = assignment .type_specifiers() @@ -393,6 +401,8 @@ fn format_local_no_assignment( .collect(); let local_assignment = LocalAssignment::new(name_list); + #[cfg(feature = "lua54")] + let local_assignment = local_assignment.with_attributes(attributes); #[cfg(feature = "luau")] let local_assignment = local_assignment.with_type_specifiers(type_specifiers); @@ -439,21 +449,30 @@ pub fn format_local_assignment_no_trivia( format_expression, ); + #[cfg(feature = "lua54")] + let attributes: Vec> = assignment + .attributes() + .map(|x| x.map(|attribute| format_attribute(ctx, attribute, shape))) + .collect(); + #[cfg(feature = "luau")] let type_specifiers: Vec> = assignment .type_specifiers() .map(|x| x.map(|type_specifier| format_type_specifier(ctx, type_specifier, shape))) .collect(); - let type_specifier_len; - #[cfg(feature = "luau")] + + let mut type_specifier_len = 0; + #[cfg(feature = "lua54")] { - type_specifier_len = type_specifiers.iter().fold(0, |acc, x| { + type_specifier_len += attributes.iter().fold(0, |acc, x| { acc + x.as_ref().map_or(0, |y| y.to_string().len()) }); } - #[cfg(not(feature = "luau"))] + #[cfg(feature = "luau")] { - type_specifier_len = 0; + type_specifier_len += type_specifiers.iter().fold(0, |acc, x| { + acc + x.as_ref().map_or(0, |y| y.to_string().len()) + }); } // Test the assignment to see if its over width @@ -483,6 +502,8 @@ pub fn format_local_assignment_no_trivia( } let local_assignment = LocalAssignment::new(name_list); + #[cfg(feature = "lua54")] + let local_assignment = local_assignment.with_attributes(attributes); #[cfg(feature = "luau")] let local_assignment = local_assignment.with_type_specifiers(type_specifiers); local_assignment diff --git a/src/formatters/lua54.rs b/src/formatters/lua54.rs new file mode 100644 index 00000000..36e0192a --- /dev/null +++ b/src/formatters/lua54.rs @@ -0,0 +1,21 @@ +use crate::{ + context::Context, + formatters::{ + general::{format_contained_span, format_token_reference}, + trivia::{FormatTriviaType, UpdateLeadingTrivia}, + }, + shape::Shape, +}; +use full_moon::{ + ast::lua54::Attribute, + tokenizer::{Token, TokenType}, +}; + +pub fn format_attribute(ctx: &Context, attribute: &Attribute, shape: Shape) -> Attribute { + let brackets = format_contained_span(ctx, attribute.brackets(), shape).update_leading_trivia( + FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]), + ); + let name = format_token_reference(ctx, attribute.name(), shape); + + Attribute::new(name).with_brackets(brackets) +} diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 4deb13ab..642fe700 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -9,6 +9,8 @@ pub mod expression; pub mod functions; #[cfg(feature = "lua52")] pub mod lua52; +#[cfg(feature = "lua54")] +pub mod lua54; #[cfg(feature = "luau")] pub mod luau; pub mod stmt; diff --git a/src/formatters/trivia.rs b/src/formatters/trivia.rs index f5209abd..407ff046 100644 --- a/src/formatters/trivia.rs +++ b/src/formatters/trivia.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "lua54")] +use full_moon::ast::lua54::Attribute; #[cfg(feature = "luau")] use full_moon::ast::types::{ ElseIfExpression, GenericDeclaration, GenericDeclarationParameter, GenericParameterInfo, @@ -490,6 +492,12 @@ define_update_trivia!(Assignment, |this, leading, trailing| { .with_expressions(this.expressions().update_trailing_trivia(trailing)) }); +#[cfg(feature = "lua54")] +define_update_trivia!(Attribute, |this, leading, trailing| { + this.to_owned() + .with_brackets(this.brackets().update_trivia(leading, trailing)) +}); + define_update_trivia!(LocalAssignment, |this, leading, trailing| { if this.expressions().is_empty() { // Handle if the last item had a type specifier set @@ -507,6 +515,20 @@ define_update_trivia!(LocalAssignment, |this, leading, trailing| { } ); + cfg_if::cfg_if!( + if #[cfg(feature = "lua54")] { + let mut attributes = this.attributes().map(|x| x.cloned()).collect::>(); + + if let Some(Some(attribute)) = attributes.pop() { + attributes.push(Some(attribute.update_trailing_trivia(trailing))); + + return this.clone() + .with_local_token(this.local_token().update_leading_trivia(leading)) + .with_attributes(attributes); + } + } + ); + this.clone() .with_local_token(this.local_token().update_leading_trivia(leading)) .with_names(this.names().update_trailing_trivia(trailing)) diff --git a/src/formatters/trivia_util.rs b/src/formatters/trivia_util.rs index 26f986b1..abbfa8f9 100644 --- a/src/formatters/trivia_util.rs +++ b/src/formatters/trivia_util.rs @@ -8,12 +8,12 @@ use full_moon::ast::span::ContainedSpan; #[cfg(feature = "luau")] use full_moon::ast::types::{ GenericDeclarationParameter, GenericParameterInfo, IndexedTypeInfo, TypeArgument, - TypeDeclaration, TypeInfo, + TypeDeclaration, TypeInfo, TypeSpecifier, }; use full_moon::{ ast::{ - BinOp, Block, Call, Expression, Field, FunctionArgs, Index, LastStmt, Parameter, Prefix, - Stmt, Suffix, TableConstructor, UnOp, Value, Var, + BinOp, Block, Call, Expression, Field, FunctionArgs, Index, LastStmt, LocalAssignment, + Parameter, Prefix, Stmt, Suffix, TableConstructor, UnOp, Value, Var, }, node::Node, tokenizer::{Token, TokenKind, TokenReference, TokenType}, @@ -884,6 +884,81 @@ fn get_type_declaration_trailing_trivia( ) } +#[cfg(feature = "luau")] +fn type_specifier_trailing_trivia(type_specifier: &TypeSpecifier) -> Vec { + get_type_info_trailing_trivia(type_specifier.type_info().clone()).1 +} + +fn get_empty_local_assignment_trailing_trivia( + local_assignment: LocalAssignment, +) -> (LocalAssignment, Vec) { + let mut trailing_trivia = Vec::new(); + + #[cfg(feature = "luau")] + { + let mut type_specifiers = local_assignment + .type_specifiers() + .map(|x| x.cloned()) + .collect::>(); + + if let Some(Some(type_specifier)) = type_specifiers.pop() { + trailing_trivia = type_specifier_trailing_trivia(&type_specifier); + + type_specifiers.push(Some( + type_specifier.update_trailing_trivia(FormatTriviaType::Replace(vec![])), + )); + + return ( + local_assignment.with_type_specifiers(type_specifiers), + trailing_trivia, + ); + } + } + + #[cfg(feature = "lua54")] + { + let mut attributes = local_assignment + .attributes() + .map(|x| x.cloned()) + .collect::>(); + + if let Some(Some(attribute)) = attributes.pop() { + trailing_trivia = attribute + .brackets() + .tokens() + .1 + .trailing_trivia() + .cloned() + .collect(); + + attributes.push(Some( + attribute.update_trailing_trivia(FormatTriviaType::Replace(vec![])), + )); + + return ( + local_assignment.with_attributes(attributes), + trailing_trivia, + ); + } + } + + // Unassigned local variable + let mut formatted_name_list = local_assignment.names().to_owned(); + // Retrieve last item and take its trailing comments + if let Some(last_pair) = formatted_name_list.pop() { + let pair = last_pair.map(|value| { + trailing_trivia = value.trailing_trivia().map(|x| x.to_owned()).collect(); + value.update_trailing_trivia(FormatTriviaType::Replace(vec![])) + }); + formatted_name_list.push(pair); + } + + ( + local_assignment.with_names(formatted_name_list), + trailing_trivia, + ) +} + pub fn get_stmt_trailing_trivia(stmt: Stmt) -> (Stmt, Vec) { let (updated_stmt, trailing_trivia) = match stmt { Stmt::Assignment(assignment) => { @@ -906,17 +981,10 @@ pub fn get_stmt_trailing_trivia(stmt: Stmt) -> (Stmt, Vec) { Stmt::LocalAssignment(local_assignment) => { let mut trailing_trivia = Vec::new(); let new_assignment = if local_assignment.expressions().is_empty() { - // Unassigned local variable - let mut formatted_name_list = local_assignment.names().to_owned(); - // Retrieve last item and take its trailing comments - if let Some(last_pair) = formatted_name_list.pop() { - let pair = last_pair.map(|value| { - trailing_trivia = value.trailing_trivia().map(|x| x.to_owned()).collect(); - value.update_trailing_trivia(FormatTriviaType::Replace(vec![])) - }); - formatted_name_list.push(pair); - } - local_assignment.with_names(formatted_name_list) + let (assignment, trivia) = + get_empty_local_assignment_trailing_trivia(local_assignment); + trailing_trivia = trivia; + assignment } else { // Add newline at the end of LocalAssignment expression list // Expression list should already be formatted diff --git a/tests/tests.rs b/tests/tests.rs index daa084c6..51fdc674 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -26,6 +26,7 @@ fn test_full_moon_test_suite() { #[cfg(feature = "luau")] fn test_luau() { insta::glob!("inputs-luau/*.lua", |path| { + dbg!(path); let contents = std::fs::read_to_string(path).unwrap(); insta::assert_snapshot!(format(&contents)); }) From 18baffa6e173d2fafa4e1e296b9123ce32d8ec51 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:56:31 +0100 Subject: [PATCH 09/16] Update snapshot --- tests/snapshots/tests__lua54@attributes-1.lua.snap | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/snapshots/tests__lua54@attributes-1.lua.snap diff --git a/tests/snapshots/tests__lua54@attributes-1.lua.snap b/tests/snapshots/tests__lua54@attributes-1.lua.snap new file mode 100644 index 00000000..338bc103 --- /dev/null +++ b/tests/snapshots/tests__lua54@attributes-1.lua.snap @@ -0,0 +1,10 @@ +--- +source: tests/tests.rs +expression: format(&contents) +--- +local a = 5 +local d +local e , f = 1, 2 +local g , h +local i , j, k + From 2224579bbe8c5a0f3006ef6178046877d370386c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:57:44 +0100 Subject: [PATCH 10/16] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5bca8b0..f25c230a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added support for Lua 5.3, gated behind the `lua53` feature flag ([#534](https://github.com/JohnnyMorganz/StyLua/issues/534)) +- Added support for Lua 5.4, gated behind the `lua54` feature flag ([#533](https://github.com/JohnnyMorganz/StyLua/issues/533)) - Added `--allow-hidden` flag to allow entering and formatting hidden files/directories ([#562](https://github.com/JohnnyMorganz/StyLua/issues/562)) - Added `--output-format=summary` which can be used with `--check` to output a summary of the list of files not correctly formatted ([#573](https://github.com/JohnnyMorganz/StyLua/issues/573)) From 30cf5fd579c2e78b62003ef9882fe41f6ae0c0d1 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 16:59:25 +0100 Subject: [PATCH 11/16] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 77ffa244..91c6d4ae 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ There are multiple ways to install StyLua: Pre-built binaries are available on the [GitHub Releases Page](https://github.com/JohnnyMorganz/StyLua/releases). -By default, these are built with **both Luau and Lua 5.2 features enabled**, to cover all possible codebases. +By default, these are built with **all syntax variants enabled (Lua 5.2, 5.3, 5.4 and Luau)**, to cover all possible codebases. If you would like to format a specific Lua version only, see [installing from crates.io](#from-cratesio). ### From Crates.io From 36bf4612cd6ec3551f4a9c48910abc33bcf2530b Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 15 Sep 2022 18:16:10 +0100 Subject: [PATCH 12/16] Handle DoubleGreaterThan change --- src/formatters/expression.rs | 18 +++++++++++++----- src/formatters/luau.rs | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index ffded764..ab595d7e 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -35,13 +35,13 @@ use crate::{ #[macro_export] macro_rules! fmt_op { - ($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($(#[$inner:meta])* $operator:ident = $output:expr,)+ }) => { + ($ctx:expr, $enum:ident, $value:ident, $shape:expr, { $($(#[$inner:meta])* $operator:ident = $output:expr,)+ }, $other:expr) => { match $value { $( $(#[$inner])* $enum::$operator(token) => $enum::$operator(fmt_symbol!($ctx, token, $output, $shape)), )+ - other => panic!("unknown node {:?}", other), + other => $other(other), } }; } @@ -89,9 +89,17 @@ pub fn format_binop(ctx: &Context, binop: &BinOp, shape: Shape) -> BinOp { #[cfg(feature = "lua53")] Pipe = " | ", #[cfg(feature = "lua53")] - DoubleGreaterThan = " >> ", - #[cfg(feature = "lua53")] Tilde = " ~ ", + }, |other: &BinOp| match other { + #[cfg(feature = "lua53")] + BinOp::DoubleGreaterThan(token) => BinOp::DoubleGreaterThan( + format_token_reference(ctx, token, shape) + .update_trivia( + FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]), + FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]) + ) + ), + other => panic!("unknown node {:?}", other) }) } @@ -796,7 +804,7 @@ pub fn format_unop(ctx: &Context, unop: &UnOp, shape: Shape) -> UnOp { Hash = "#", #[cfg(feature = "lua53")] Tilde = "~", - }) + }, |other| panic!("unknown node {:?}", other)) } /// Pushes a [`BinOp`] onto a newline, and indent its depending on indent_level. diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index 8e27c943..f88f1755 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -41,7 +41,7 @@ pub fn format_compound_op(ctx: &Context, compound_op: &CompoundOp, shape: Shape) PercentEqual = " %= ", CaretEqual = " ^= ", TwoDotsEqual = " ..= ", - }) + }, |other| panic!("unknown node {:?}", other)) } pub fn format_compound_assignment( From 4bf451c746d60ea67711b39e86116e411812f8c3 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Mon, 19 Sep 2022 16:04:56 +0100 Subject: [PATCH 13/16] Allow unused mut --- src/formatters/assignment.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/formatters/assignment.rs b/src/formatters/assignment.rs index 119d9da0..0c7532fc 100644 --- a/src/formatters/assignment.rs +++ b/src/formatters/assignment.rs @@ -461,6 +461,7 @@ pub fn format_local_assignment_no_trivia( .map(|x| x.map(|type_specifier| format_type_specifier(ctx, type_specifier, shape))) .collect(); + #[allow(unused_mut)] let mut type_specifier_len = 0; #[cfg(feature = "lua54")] { From a7c23e90f38c21bcb355a1b86e244d4617ff8259 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Mon, 19 Sep 2022 16:05:04 +0100 Subject: [PATCH 14/16] Run tests in CI --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da53f178..d54e4461 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,12 @@ jobs: - name: Test (Lua 5.2) run: cargo test --features lua52 + - name: Test (Lua 5.3) + run: cargo test --features lua53 + + - name: Test (Lua 5.4) + run: cargo test --features lua54 + - name: Test Build (wasm) run: | rustup target add wasm32-unknown-unknown From dec69654dc11bb3491efdee77424901c65584477 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Mon, 19 Sep 2022 16:05:15 +0100 Subject: [PATCH 15/16] Temp full moon version --- Cargo.lock | 64 +++++++++++++++++++++++++++--------------------------- Cargo.toml | 3 ++- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a110b2a..bcf7e8da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -318,15 +324,14 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "full_moon" version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf176fa61b7bfd03d311cf17618bb8cb16115e9077ab591a847c3437c8a6001" +source = "git+https://github.com/Kampfkarren/full-moon#c563e99b6a4f3a9f6aeefdedc32a4849a15db8f4" dependencies = [ "bytecount", "cfg-if", "derive_more", "full_moon_derive", + "logos", "paste", - "peg", "serde", "smol_str", ] @@ -334,8 +339,7 @@ dependencies = [ [[package]] name = "full_moon_derive" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87826a47a649663aa287e57ea9f31450a56fabcd553a2fe17b2f669f1b5e855b" +source = "git+https://github.com/Kampfkarren/full-moon#c563e99b6a4f3a9f6aeefdedc32a4849a15db8f4" dependencies = [ "indexmap", "proc-macro2", @@ -484,6 +488,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "logos" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8b031682c67a8e3d5446840f9573eb7fe26efe7ec8d195c9ac4c0647c502f1" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-derive" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d849148dbaf9661a6151d1ca82b13bb4c4c128146a88d05253b38d4e2f496c" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn", +] + [[package]] name = "memchr" version = "2.4.1" @@ -558,33 +585,6 @@ dependencies = [ "proc-macro-hack", ] -[[package]] -name = "peg" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07c0b841ea54f523f7aa556956fbd293bcbe06f2e67d2eb732b7278aaf1d166a" -dependencies = [ - "peg-macros", - "peg-runtime", -] - -[[package]] -name = "peg-macros" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aa52829b8decbef693af90202711348ab001456803ba2a98eb4ec8fb70844c" -dependencies = [ - "peg-runtime", - "proc-macro2", - "quote", -] - -[[package]] -name = "peg-runtime" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c719dcf55f09a3a7e764c6649ab594c18a177e3599c467983cdf644bfc0a4088" - [[package]] name = "pest" version = "2.1.3" diff --git a/Cargo.toml b/Cargo.toml index d047c7de..c08787e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,8 @@ clap = { version = "3.1.6", features = ["derive"] } console = "0.15.0" crossbeam-channel = "0.5.4" env_logger = { version = "0.9.0", default-features = false } -full_moon = "0.15.1" +# full_moon = "0.15.1" +full_moon = { git = "https://github.com/Kampfkarren/full-moon" } globset = "0.4.8" ignore = "0.4.18" lazy_static = "1.4.0" From f16b2ba41ba793f71818f79896bbc03e5cae852b Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 21 Sep 2022 18:31:02 +0100 Subject: [PATCH 16/16] Use published full-moon --- Cargo.lock | 10 ++++++---- Cargo.toml | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bcf7e8da..9047c593 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -323,8 +323,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "full_moon" -version = "0.15.1" -source = "git+https://github.com/Kampfkarren/full-moon#c563e99b6a4f3a9f6aeefdedc32a4849a15db8f4" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043b0dacc57e32aada47d0ba75baa31cf079e3297fa4fb32575791fda616f968" dependencies = [ "bytecount", "cfg-if", @@ -338,8 +339,9 @@ dependencies = [ [[package]] name = "full_moon_derive" -version = "0.10.0" -source = "git+https://github.com/Kampfkarren/full-moon#c563e99b6a4f3a9f6aeefdedc32a4849a15db8f4" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99b4bd12ce56927d1dc5478d21528ea8c4b93ca85ff8f8043b6a5351a2a3c6f7" dependencies = [ "indexmap", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index c08787e9..73f426dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,8 +36,7 @@ clap = { version = "3.1.6", features = ["derive"] } console = "0.15.0" crossbeam-channel = "0.5.4" env_logger = { version = "0.9.0", default-features = false } -# full_moon = "0.15.1" -full_moon = { git = "https://github.com/Kampfkarren/full-moon" } +full_moon = "0.16.0" globset = "0.4.8" ignore = "0.4.18" lazy_static = "1.4.0"