Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed shape calculation of the RHS of a binary expression not correctly reset when hanging, causing it to expand unnecessarily ([#432](https://github.com/JohnnyMorganz/StyLua/issues/432))
- Fixed unstable formatting of tables at column width boundary ([#436](https://github.com/JohnnyMorganz/StyLua/issues/436))
- Fixed assignments no longer hanging at equals token if a comment is present, but the expression is not hangable at a binop. ([#439](https://github.com/JohnnyMorganz/StyLua/issues/439))
- Fixed unstable formatting around comments within type declarations ([#397](https://github.com/JohnnyMorganz/StyLua/issues/397), [#430](https://github.com/JohnnyMorganz/StyLua/issues/430))

## [0.13.0] - 2022-03-31
### Added
Expand Down
305 changes: 216 additions & 89 deletions src/formatters/luau.rs

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/formatters/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,28 @@ define_update_leading_trivia!(GenericDeclarationParameter, |this, leading| {
this.to_owned().with_parameter(parameter_info)
});

#[cfg(feature = "luau")]
define_update_trailing_trivia!(GenericDeclarationParameter, |this, trailing| {
if let Some(default_type) = this.default_type() {
let default_type = default_type.update_trailing_trivia(trailing);
this.to_owned()
.with_default(Some((this.equals().unwrap().to_owned(), default_type)))
} else {
let parameter_info = match this.parameter() {
GenericParameterInfo::Name(token) => {
GenericParameterInfo::Name(token.update_trailing_trivia(trailing))
}
GenericParameterInfo::Variadic { name, ellipse } => GenericParameterInfo::Variadic {
name: name.to_owned(),
ellipse: ellipse.update_trailing_trivia(trailing),
},
other => panic!("unknown node {:?}", other),
};

this.to_owned().with_parameter(parameter_info)
}
});

#[cfg(feature = "luau")]
define_update_leading_trivia!(IfExpression, |this, leading| {
this.to_owned()
Expand Down
37 changes: 36 additions & 1 deletion src/formatters/trivia_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::{
#[cfg(feature = "luau")]
use full_moon::ast::span::ContainedSpan;
#[cfg(feature = "luau")]
use full_moon::ast::types::{IndexedTypeInfo, TypeArgument, TypeDeclaration, TypeInfo};
use full_moon::ast::types::{
GenericDeclarationParameter, GenericParameterInfo, IndexedTypeInfo, TypeArgument,
TypeDeclaration, TypeInfo,
};
use full_moon::ast::{Block, FunctionBody, Parameter};
use full_moon::{
ast::{
Expand Down Expand Up @@ -225,6 +228,38 @@ pub fn type_info_trailing_trivia(type_info: &TypeInfo) -> Vec<Token> {
}
}

#[cfg(feature = "luau")]
fn generic_declaration_parameter_trailing_trivia(
parameter: &GenericDeclarationParameter,
) -> Vec<Token> {
if let Some(default_type) = parameter.default_type() {
type_info_trailing_trivia(default_type)
} else {
match parameter.parameter() {
GenericParameterInfo::Name(token) => token.trailing_trivia().cloned().collect(),
GenericParameterInfo::Variadic { ellipse, .. } => {
ellipse.trailing_trivia().cloned().collect()
}
other => panic!("unknown node {:?}", other),
}
}
}

#[cfg(feature = "luau")]
pub fn take_generic_parameter_trailing_comments(
parameter: &GenericDeclarationParameter,
) -> (GenericDeclarationParameter, Vec<Token>) {
let trailing_comments = generic_declaration_parameter_trailing_trivia(parameter)
.iter()
.filter(|x| trivia_is_comment(x))
.cloned()
.collect();
(
parameter.update_trailing_trivia(FormatTriviaType::Replace(vec![])),
trailing_comments,
)
}

fn var_trailing_trivia(var: &Var) -> Vec<Token> {
match var {
Var::Name(token_reference) => token_reference
Expand Down
39 changes: 39 additions & 0 deletions tests/inputs-luau/type-comments-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/397

--[[opening type comment]]
type Doo<
T --[[ per-generic argument comment]]
> =
--[[ opening RHS comment]]
string --[[ per-RHS comment]]

type Foo<T = --[[leading]]
string
--[[trailing]]> = { baz: T, }

type Bar<T
--[[ Trailing comment ]]> = {}

-- This is a comment before
type Foo = --[[ Comment before Bar ]]
Bar<--[[ Before X ]]
X, --[[ After X ]]
--[[ Before Y ]]
Y, --[[ After Y ]]
--[[ Before Z ]]
Z
--[[ After Z ]]> -- This is a comment after

--[[comment]]
type Doo
--[[comment]]
<
--[[comment]]
T
--[[comment]]
>
--[[comment]]
=
--[[comment]]
string
--[[comment]]
47 changes: 47 additions & 0 deletions tests/snapshots/tests__luau@type-comments-2.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
source: tests/tests.rs
assertion_line: 30
expression: format(&contents)

---
-- https://github.com/JohnnyMorganz/StyLua/issues/397

--[[opening type comment]]
type Doo<
T--[[ per-generic argument comment]]
> =
--[[ opening RHS comment]]
string --[[ per-RHS comment]]

type Foo<
T = --[[leading]]
string
--[[trailing]]
> = { baz: T }

type Bar<
T
--[[ Trailing comment ]]
> = {}

-- This is a comment before
type Foo = --[[ Comment before Bar ]]
Bar<--[[ Before X ]]
X, --[[ After X ]]
--[[ Before Y ]]
Y, --[[ After Y ]]
--[[ Before Z ]]
Z
--[[ After Z ]]
> -- This is a comment after

--[[comment]]
type Doo --[[comment]] <
--[[comment]]
T
--[[comment]]
> --[[comment]] =
--[[comment]]
string
--[[comment]]

41 changes: 21 additions & 20 deletions tests/snapshots/tests__luau@type-multiline-comments.lua.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ expression: format(&contents)
---
-- Shouldn't hang since multiline comments arent an issue

export type GraphQLEnumType = --[[ <T> ]]{
name: string,
description: string?,
extensions: ReadOnlyObjMap<any>?,
astNode: EnumTypeDefinitionNode?,
extensionASTNodes: Array<EnumTypeExtensionNode>?,
export type GraphQLEnumType = --[[ <T> ]]
{
name: string,
description: string?,
extensions: ReadOnlyObjMap<any>?,
astNode: EnumTypeDefinitionNode?,
extensionASTNodes: Array<EnumTypeExtensionNode>?,

_values: Array<GraphQLEnumValue --[[ <T> ]]>,
_valueLookup: Map<any --[[ T ]], GraphQLEnumValue>,
_nameLookup: ObjMap<GraphQLEnumValue>,
-- ROBLOX deviation: add self parameter for all ':' operator methods
getValues: (self: GraphQLEnumType) -> Array<GraphQLEnumValue --[[ <T> ]]>,
getValue: (self: GraphQLEnumType, string) -> GraphQLEnumValue?,
serialize: (
self: GraphQLEnumType,
any --[[ T ]]
) -> string?,
parseValue: (self: GraphQLEnumType, any) -> any?, --[[ T ]]
parseLiteral: (self: GraphQLEnumType, ValueNode, ObjMap<any>?) -> any?, --[[ T ]]
toConfig: (self: GraphQLEnumType) -> GraphQLEnumTypeNormalizedConfig,
}
_values: Array<GraphQLEnumValue --[[ <T> ]]>,
_valueLookup: Map<any --[[ T ]], GraphQLEnumValue>,
_nameLookup: ObjMap<GraphQLEnumValue>,
-- ROBLOX deviation: add self parameter for all ':' operator methods
getValues: (self: GraphQLEnumType) -> Array<GraphQLEnumValue --[[ <T> ]]>,
getValue: (self: GraphQLEnumType, string) -> GraphQLEnumValue?,
serialize: (
self: GraphQLEnumType,
any --[[ T ]]
) -> string?,
parseValue: (self: GraphQLEnumType, any) -> any?, --[[ T ]]
parseLiteral: (self: GraphQLEnumType, ValueNode, ObjMap<any>?) -> any?, --[[ T ]]
toConfig: (self: GraphQLEnumType) -> GraphQLEnumTypeNormalizedConfig,
}