Skip to content
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
9 changes: 6 additions & 3 deletions crates/oxc_ast/src/ast/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,14 +616,16 @@ macro_rules! inherit_variants {
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 32,
/// Inherited from [`TSType`]
TSUnionType(Box<'a, TSUnionType<'a>>) = 33,
/// Inherited from [`TSType`]
TSParenthesizedType(Box<'a, TSParenthesizedType<'a>>) = 34,

// JSDoc
/// Inherited from [`TSType`]
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 34,
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 35,
/// Inherited from [`TSType`]
JSDocNonNullableType(Box<'a, JSDocNonNullableType<'a>>) = 35,
JSDocNonNullableType(Box<'a, JSDocNonNullableType<'a>>) = 36,
/// Inherited from [`TSType`]
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 36,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 37,

$($rest)*
}
Expand Down Expand Up @@ -672,6 +674,7 @@ macro_rules! inherit_variants {
TSTypeQuery,
TSTypeReference,
TSUnionType,
TSParenthesizedType,
JSDocNullableType,
JSDocNonNullableType,
JSDocUnknownType,
Expand Down
18 changes: 15 additions & 3 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ pub enum TSType<'a> {
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 31,
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 32,
TSUnionType(Box<'a, TSUnionType<'a>>) = 33,
TSParenthesizedType(Box<'a, TSParenthesizedType<'a>>) = 34,
// JSDoc
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 34,
JSDocNonNullableType(Box<'a, JSDocNonNullableType<'a>>) = 35,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 36,
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 35,
JSDocNonNullableType(Box<'a, JSDocNonNullableType<'a>>) = 36,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 37,
}

/// Macro for matching `TSType`'s variants.
Expand Down Expand Up @@ -216,6 +217,7 @@ macro_rules! match_ts_type {
| $ty::TSTypeQuery(_)
| $ty::TSTypeReference(_)
| $ty::TSUnionType(_)
| $ty::TSParenthesizedType(_)
| $ty::JSDocNullableType(_)
| $ty::JSDocNonNullableType(_)
| $ty::JSDocUnknownType(_)
Expand Down Expand Up @@ -265,6 +267,16 @@ pub struct TSIntersectionType<'a> {
pub types: Vec<'a, TSType<'a>>,
}

#[visited_node]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
pub struct TSParenthesizedType<'a> {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub type_annotation: TSType<'a>,
}

/// keyof unique readonly
///
/// <https://www.typescriptlang.org/docs/handbook/2/keyof-types.html>
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,11 @@ impl<'a> AstBuilder<'a> {
TSType::TSUnionType(self.alloc(TSUnionType { span, types }))
}

#[inline]
pub fn ts_parenthesized_type(self, span: Span, ty: TSType<'a>) -> TSType<'a> {
TSType::TSParenthesizedType(self.alloc(TSParenthesizedType { span, type_annotation: ty }))
}

#[inline]
pub fn ts_intersection_type(self, span: Span, types: Vec<'a, TSType<'a>>) -> TSType<'a> {
TSType::TSIntersectionType(self.alloc(TSIntersectionType { span, types }))
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ impl<'a> Hash for TSTypeParameter<'a> {
}
}

impl<'a> TSType<'a> {
/// Remove nested parentheses from this type.
pub fn without_parenthesized(&self) -> &Self {
match self {
Self::TSParenthesizedType(expr) => expr.type_annotation.without_parenthesized(),
_ => self,
}
}
}

impl TSAccessibility {
pub fn is_private(&self) -> bool {
matches!(self, TSAccessibility::Private)
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl<'a> AstKind<'a> {
Self::TSTypeLiteral(_) => "TSTypeLiteral".into(),
Self::TSTypeReference(_) => "TSTypeReference".into(),
Self::TSUnionType(_) => "TSUnionType".into(),
Self::TSParenthesizedType(_) => "TSParenthesizedType".into(),
Self::TSVoidKeyword(_) => "TSVoidKeyword".into(),
Self::TSBigIntKeyword(_) => "TSBigIntKeyword".into(),
Self::TSBooleanKeyword(_) => "TSBooleanKeyword".into(),
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_ast/src/generated/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum AstType {
TSLiteralType,
TSUnionType,
TSIntersectionType,
TSParenthesizedType,
TSIndexedAccessType,
TSNamedTupleMember,
TSAnyKeyword,
Expand Down Expand Up @@ -272,6 +273,7 @@ pub enum AstKind<'a> {
TSLiteralType(&'a TSLiteralType<'a>),
TSUnionType(&'a TSUnionType<'a>),
TSIntersectionType(&'a TSIntersectionType<'a>),
TSParenthesizedType(&'a TSParenthesizedType<'a>),
TSIndexedAccessType(&'a TSIndexedAccessType<'a>),
TSNamedTupleMember(&'a TSNamedTupleMember<'a>),
TSAnyKeyword(&'a TSAnyKeyword),
Expand Down Expand Up @@ -438,6 +440,7 @@ impl<'a> GetSpan for AstKind<'a> {
Self::TSLiteralType(it) => it.span(),
Self::TSUnionType(it) => it.span(),
Self::TSIntersectionType(it) => it.span(),
Self::TSParenthesizedType(it) => it.span(),
Self::TSIndexedAccessType(it) => it.span(),
Self::TSNamedTupleMember(it) => it.span(),
Self::TSAnyKeyword(it) => it.span(),
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_ast/src/generated/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@ impl<'a> GetSpan for TSType<'a> {
Self::TSTypeQuery(it) => it.span(),
Self::TSTypeReference(it) => it.span(),
Self::TSUnionType(it) => it.span(),
Self::TSParenthesizedType(it) => it.span(),
Self::JSDocNullableType(it) => it.span(),
Self::JSDocNonNullableType(it) => it.span(),
Self::JSDocUnknownType(it) => it.span(),
Expand Down Expand Up @@ -1412,6 +1413,13 @@ impl<'a> GetSpan for TSIntersectionType<'a> {
}
}

impl<'a> GetSpan for TSParenthesizedType<'a> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl<'a> GetSpan for TSTypeOperator<'a> {
#[inline]
fn span(&self) -> Span {
Expand Down Expand Up @@ -1500,6 +1508,7 @@ impl<'a> GetSpan for TSTupleElement<'a> {
Self::TSTypeQuery(it) => it.span(),
Self::TSTypeReference(it) => it.span(),
Self::TSUnionType(it) => it.span(),
Self::TSParenthesizedType(it) => it.span(),
Self::JSDocNullableType(it) => it.span(),
Self::JSDocNonNullableType(it) => it.span(),
Self::JSDocUnknownType(it) => it.span(),
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/visit/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,7 @@ pub mod walk {
TSType::TSTypeQuery(ty) => visitor.visit_ts_type_query(ty),
TSType::TSTypeReference(ty) => visitor.visit_ts_type_reference(ty),
TSType::TSUnionType(ty) => visitor.visit_ts_union_type(ty),
TSType::TSParenthesizedType(ty) => visitor.visit_ts_type(&ty.type_annotation),
// JSDoc
TSType::JSDocNullableType(_)
| TSType::JSDocNonNullableType(_)
Expand Down
25 changes: 12 additions & 13 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSType<'a> {
Self::TSArrayType(ty) => ty.gen(p, ctx),
Self::TSTupleType(ty) => ty.gen(p, ctx),
Self::TSUnionType(ty) => ty.gen(p, ctx),
Self::TSParenthesizedType(ty) => ty.gen(p, ctx),
Self::TSIntersectionType(ty) => ty.gen(p, ctx),
Self::TSConditionalType(ty) => ty.gen(p, ctx),
Self::TSInferType(ty) => ty.gen(p, ctx),
Expand Down Expand Up @@ -2708,9 +2709,8 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSType<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSArrayType<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.print_str(b"(");
self.element_type.gen(p, ctx);
p.print_str(b")[]");
p.print_str(b"[]");
}
}

Expand All @@ -2728,18 +2728,22 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSUnionType<'a> {
self.types[0].gen(p, ctx);
return;
}
p.print_str(b"(");
for (index, item) in self.types.iter().enumerate() {
if index != 0 {
p.print_soft_space();
p.print_str(b"|");
p.print_soft_space();
}
p.print_str(b"(");
item.gen(p, ctx);
p.print_str(b")");
}
p.print_str(b")");
}
}

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSParenthesizedType<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.print(b'(');
self.type_annotation.gen(p, ctx);
p.print(b')');
}
}

Expand All @@ -2749,28 +2753,23 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSIntersectionType<'a> {
self.types[0].gen(p, ctx);
return;
}

p.print_str(b"(");
for (index, item) in self.types.iter().enumerate() {
if index != 0 {
p.print_soft_space();
p.print_str(b"&");
p.print_soft_space();
}
p.print_str(b"(");
item.gen(p, ctx);
p.print_str(b")");
}
p.print_str(b")");
}
}

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSConditionalType<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
self.check_type.gen(p, ctx);
p.print_str(b" extends (");
p.print_str(b" extends ");
self.extends_type.gen(p, ctx);
p.print_str(b") ? ");
p.print_str(b" ? ");
self.true_type.gen(p, ctx);
p.print_str(b" : ");
self.false_type.gen(p, ctx);
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn typescript() {

test_ts(
"let x: string[] = ['abc', 'def', 'ghi'];",
"let x: (string)[] = ['abc', 'def', 'ghi'];\n",
"let x: string[] = ['abc', 'def', 'ghi'];\n",
false,
);
test_ts(
Expand All @@ -160,8 +160,8 @@ fn typescript() {
"let x: [string, number] = ['abc', 123];\n",
false,
);
test_ts("let x: string | number = 'abc';", "let x: ((string) | (number)) = 'abc';\n", false);
test_ts("let x: string & number = 'abc';", "let x: ((string) & (number)) = 'abc';\n", false);
test_ts("let x: string | number = 'abc';", "let x: string | number = 'abc';\n", false);
test_ts("let x: string & number = 'abc';", "let x: string & number = 'abc';\n", false);
test_ts("let x: typeof String = 'string';", "let x: typeof String = 'string';\n", false);
test_ts("let x: keyof string = 'length';", "let x: keyof string = 'length';\n", false);
test_ts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts
==================== .D.TS ====================

declare function foo(): number;
declare function bar(): ((number) | (undefined));
declare function bar(): number | undefined;
declare function baz();
declare function qux(): string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/readonly.ts
==================== .D.TS ====================

export declare const EMPTY_OBJ: {readonly [key: string]: any};
export declare const EMPTY_ARR: readonly (never)[];
export declare const EMPTY_ARR: readonly never[];
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/typescript/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ fn check_and_report_error_generic(
if matches!(config, ArrayOption::Array) {
return;
}
let type_param = type_param.without_parenthesized();
if matches!(config, ArrayOption::ArraySimple) && is_simple_type(type_param) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,11 @@ impl<'a> ParserImpl<'a> {
}

fn parse_parenthesized_type(&mut self) -> Result<TSType<'a>> {
let span = self.start_span();
self.bump_any(); // bump `(`
let result = self.parse_ts_type()?;
let ty = self.parse_ts_type()?;
self.expect(Kind::RParen)?;
Ok(result)
Ok(self.ast.ts_parenthesized_type(self.end_span(span), ty))
}

fn parse_literal_type_node(&mut self, negative: bool) -> Result<TSType<'a>> {
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ impl<'a> Format<'a> for TSType<'a> {
TSType::TSTypeQuery(v) => v.format(p),
TSType::TSTypeReference(v) => v.format(p),
TSType::TSUnionType(v) => v.format(p),
TSType::TSParenthesizedType(v) => v.format(p),
TSType::JSDocNullableType(v) => v.format(p),
TSType::JSDocNonNullableType(v) => v.format(p),
TSType::JSDocUnknownType(v) => v.format(p),
Expand Down Expand Up @@ -920,6 +921,12 @@ impl<'a> Format<'a> for TSTypeReference<'a> {
}
}

impl<'a> Format<'a> for TSParenthesizedType<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
wrap!(p, self, TSParenthesizedType, { self.type_annotation.format(p) })
}
}

impl<'a> Format<'a> for TSUnionType<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
line!()
Expand Down
Loading