Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary parentheses around Luau types #632

Merged
merged 3 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/formatters/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ pub fn format_type_info(ctx: &Context, type_info: &TypeInfo, shape: Shape) -> Ty
take_type_info_trailing_comments,
shape,
)
} else if types.len() == 1
&& !matches!(
types.iter().next().unwrap(),
TypeInfo::Callback { .. }
| TypeInfo::Union { .. }
| TypeInfo::Intersection { .. }
)
{
// If its just a single type inside parentheses, and its not a function or composite type, then remove the parens
return singleline_types.into_iter().next().unwrap();
} else {
(singleline_parentheses, singleline_types)
};
Expand Down
4 changes: 4 additions & 0 deletions tests/inputs-luau/excess-parentheses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ foo(
-- testing
(x :: string) -- testing
)

-- https://github.com/JohnnyMorganz/StyLua/issues/611
local function foo(): (number)
end
3 changes: 3 additions & 0 deletions tests/snapshots/tests__luau@excess-parentheses.lua.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ foo(
x :: string -- testing
)

-- https://github.com/JohnnyMorganz/StyLua/issues/611
local function foo(): number end

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export type ObservableQueryFields<TData, TVariables> = ObservableQueryPick<TData
fetchMore: ((
_self: any,
fetchMoreOptions: FetchMoreQueryOptions<TVariables, TData> & FetchMoreOptions<TData, TVariables>
) -> Promise<ApolloQueryResult<TData>>) & ((<TData2, TVariables2>(
) -> Promise<ApolloQueryResult<TData>>) & (<TData2, TVariables2>(
_self: any,
fetchMoreOptions: { query: (DocumentNode | TypedDocumentNode<TData, TVariables>)? }
& FetchMoreQueryOptions<TVariables2, TData>
& FetchMoreOptions<TData2, TVariables2>
) -> Promise<ApolloQueryResult<TData2>>)),
) -> Promise<ApolloQueryResult<TData2>>),
}

export type ObservableQueryFields<TData, TVariables> = ObservableQueryPick<TData, TVariables> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type MyCallbackType = (cost: number, name: string) -> string
local cb: (amount: number) -> number
local function foo(cb: (name: string) -> ()) end

local function bar(x: (number)?): (baz: string) -> string end
local function bar(x: number?): (baz: string) -> string end

local function bar(x: (number)?): (baz: string) -> ((names: Array<string>) -> ...any) end
local function bar(x: number?): (baz: string) -> ((names: Array<string>) -> ...any) end

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Thunk<T> = (() -> T) | T

export type PromiseLike<T> = {
andThen: (
((T) -> T)? | (PromiseLike<T>)?, -- resolve
((T) -> T)? | PromiseLike<T>?, -- resolve
((any) -> () | PromiseLike<T>)? -- reject
) -> PromiseLike<T>,
}
Expand Down