Skip to content

Commit

Permalink
chore: remove unused, always empty fields from TsTypeElement
Browse files Browse the repository at this point in the history
- TsPropertySignature can not have params, type params, or an initializer.
- TsGetterSignature can not be optional or readonly.
- TsSetterSignature can not be optional or readonly.
- TsMethodSignature can not be readonly.
  • Loading branch information
lucacasonato authored and kdy1 committed Jun 14, 2024
1 parent e560198 commit d6bc36a
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 61 deletions.
9 changes: 0 additions & 9 deletions bindings/binding_core_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2376,37 +2376,28 @@ export interface TsPropertySignature extends Node, HasSpan {
computed: boolean;
optional: boolean;
init?: Expression;
params: TsFnParameter[];
typeAnnotation?: TsTypeAnnotation;
typeParams?: TsTypeParameterDeclaration;
}
export interface TsGetterSignature extends Node, HasSpan {
type: "TsGetterSignature";
readonly: boolean;
key: Expression;
computed: boolean;
optional: boolean;
typeAnnotation?: TsTypeAnnotation;
}
export interface TsSetterSignature extends Node, HasSpan {
type: "TsSetterSignature";
readonly: boolean;
key: Expression;
computed: boolean;
optional: boolean;
param: TsFnParameter;
}
export interface TsMethodSignature extends Node, HasSpan {
type: "TsMethodSignature";
readonly: boolean;
key: Expression;
computed: boolean;
optional: boolean;
Expand Down
10 changes: 0 additions & 10 deletions crates/swc_ecma_ast/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,17 @@ pub struct TsPropertySignature {
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
#[cfg_attr(feature = "serde-impl", serde(default))]
pub init: Option<Box<Expr>>,
pub params: Vec<TsFnParam>,
#[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
pub type_ann: Option<Box<TsTypeAnn>>,
#[cfg_attr(feature = "serde-impl", serde(default))]
pub type_params: Option<Box<TsTypeParamDecl>>,
}

#[ast_node("TsGetterSignature")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TsGetterSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
#[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
pub type_ann: Option<Box<TsTypeAnn>>,
}
Expand All @@ -212,10 +205,8 @@ pub struct TsGetterSignature {
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TsSetterSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
pub param: TsFnParam,
}

Expand All @@ -224,7 +215,6 @@ pub struct TsSetterSignature {
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TsMethodSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
Expand Down
13 changes: 0 additions & 13 deletions crates/swc_ecma_codegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,6 @@ where
fn emit_ts_method_signature(&mut self, n: &TsMethodSignature) -> Result {
self.emit_leading_comments_of_span(n.span(), false)?;

if n.readonly {
keyword!("readonly");
}

if n.computed {
punct!("[");
emit!(n.key);
Expand Down Expand Up @@ -751,8 +747,6 @@ where
punct!("?");
}

emit!(n.type_params);

// punct!("(");
// self.emit_list(n.span, Some(&n.params), ListFormat::Parameters)?;
// punct!(")");
Expand All @@ -762,13 +756,6 @@ where
formatting_space!();
emit!(type_ann);
}

if let Some(init) = &n.init {
formatting_space!();
punct!("=");
formatting_space!();
emit!(init);
}
}

#[emitter]
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum SyntaxError {
ArrowNotAllowed,
ExportNotAllowed,
GetterSetterCannotBeReadonly,
GetterSetterCannotBeOptional,
GetterParam,
SetterParam,

Expand Down Expand Up @@ -563,6 +564,9 @@ impl SyntaxError {
SyntaxError::GetterSetterCannotBeReadonly => {
"A getter or a setter cannot be readonly".into()
}
SyntaxError::GetterSetterCannotBeOptional => {
"A getter or a setter cannot be optional".into()
}
SyntaxError::GetterParam => "A `get` accessor cannot have parameters".into(),
SyntaxError::SetterParam => "A `set` accessor must have exactly one parameter".into(),
SyntaxError::RestPatInSetter => "Rest pattern is not allowed in setter".into(),
Expand Down
20 changes: 8 additions & 12 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,11 @@ impl<I: Tokens> Parser<I> {

let optional = eat!(self, '?');

if !readonly && is_one_of!(self, '(', '<') {
if is_one_of!(self, '(', '<') {
if readonly {
syntax_error!(self, SyntaxError::ReadOnlyMethod)
}

let type_params = self.try_parse_ts_type_params(false, true)?;
expect!(self, '(');
let params = self.parse_ts_binding_list_for_signature()?;
Expand All @@ -1432,7 +1436,6 @@ impl<I: Tokens> Parser<I> {
Ok(Either::Right(TsMethodSignature {
span: span!(self, start),
computed,
readonly,
key,
optional,
type_params,
Expand All @@ -1449,9 +1452,6 @@ impl<I: Tokens> Parser<I> {
readonly,
key,
optional,
init: None,
type_params: None,
params: vec![],
type_ann,
}))
}
Expand Down Expand Up @@ -1491,7 +1491,9 @@ impl<I: Tokens> Parser<I> {
if let Some(v) = self.try_parse_ts(|p| {
let start = p.input.cur_pos();

let readonly = p.parse_ts_modifier(&["readonly"], false)?.is_some();
if readonly {
syntax_error!(p, SyntaxError::GetterSetterCannotBeReadonly)
}

let is_get = if eat!(p, "get") {
true
Expand All @@ -1502,8 +1504,6 @@ impl<I: Tokens> Parser<I> {

let (computed, key) = p.parse_ts_property_name()?;

let optional = eat!(p, '?');

if is_get {
expect!(p, '(');
expect!(p, ')');
Expand All @@ -1513,10 +1513,8 @@ impl<I: Tokens> Parser<I> {

Ok(Some(TsTypeElement::TsGetterSignature(TsGetterSignature {
span: span!(p, start),
readonly,
key,
computed,
optional,
type_ann,
})))
} else {
Expand All @@ -1531,10 +1529,8 @@ impl<I: Tokens> Parser<I> {

Ok(Some(TsTypeElement::TsSetterSignature(TsSetterSignature {
span: span!(p, start),
readonly,
key,
computed,
optional,
param,
})))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type A = {
get m?(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Expected '{', got 'A'
,-[$DIR/tests/typescript-errors/type-lit/optional-getter/input.ts:1:1]
1 | export type A = {
: ^
2 | get m?(): string;
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type A = {
set m?(val: string);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Expected '{', got 'A'
,-[$DIR/tests/typescript-errors/type-lit/optional-setter/input.ts:1:1]
1 | export type A = {
: ^
2 | set m?(val: string);
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type A = {
readonly get m(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Expected '{', got 'A'
,-[$DIR/tests/typescript-errors/type-lit/readonly-getter/input.ts:1:1]
1 | export type A = {
: ^
2 | readonly get m(): string;
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type A = {
readonly m(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Expected '{', got 'A'
,-[$DIR/tests/typescript-errors/type-lit/readonly-method/input.ts:1:1]
1 | export type A = {
: ^
2 | readonly m(): string;
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type A = {
readonly set m(val: string);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Expected '{', got 'A'
,-[$DIR/tests/typescript-errors/type-lit/readonly-setter/input.ts:1:1]
1 | export type A = {
: ^
2 | readonly set m(val: string);
`----
3 changes: 0 additions & 3 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,9 +1401,6 @@ impl<'a> VisitMut for Resolver<'a> {
self.with_child(ScopeKind::Fn, |child| {
child.in_type = true;

n.type_params.visit_mut_with(child);
n.init.visit_mut_with(child);
n.params.visit_mut_with(child);
n.type_ann.visit_mut_with(child);
});
}
Expand Down
3 changes: 0 additions & 3 deletions crates/swc_ecma_transforms_base/tests/ts_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ impl Visit for AssertNoEmptyCtxt {
n.key.visit_with(self);
}

n.init.visit_with(self);
n.params.visit_with(self);
n.type_ann.visit_with(self);
n.type_params.visit_with(self);
}

fn visit_ts_setter_signature(&mut self, n: &TsSetterSignature) {
Expand Down
8 changes: 0 additions & 8 deletions crates/swc_ecma_visit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1531,32 +1531,24 @@ define!({
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
pub init: Option<Box<Expr>>,
pub params: Vec<TsFnParam>,
pub type_ann: Option<Box<TsTypeAnn>>,
pub type_params: Option<Box<TsTypeParamDecl>>,
}

pub struct TsGetterSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
pub type_ann: Option<Box<TsTypeAnn>>,
}

pub struct TsSetterSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
pub param: TsFnParam,
}
pub struct TsMethodSignature {
pub span: Span,
pub readonly: bool,
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
Expand Down
2 changes: 0 additions & 2 deletions crates/swc_estree_ast/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ pub struct TSPropertySignature {
#[serde(default)]
pub type_annotation: Option<Box<TSTypeAnnotation>>,
#[serde(default)]
pub initializer: Option<Box<Expression>>,
#[serde(default)]
pub computed: Option<bool>,
#[serde(default)]
pub optional: Option<bool>,
Expand Down
1 change: 0 additions & 1 deletion crates/swc_estree_compat/src/babelify/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ impl Babelify for TsPropertySignature {
type_annotation: self
.type_ann
.map(|ann| Box::alloc().init(ann.babelify(ctx))),
initializer: self.init.map(|i| Box::alloc().init(i.babelify(ctx).into())),
computed: Some(self.computed),
optional: Some(self.optional),
readonly: Some(self.readonly),
Expand Down

0 comments on commit d6bc36a

Please sign in to comment.