swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.92k stars 1.21k forks source link

Ideas for AST changes #9161

Open kdy1 opened 2 months ago

kdy1 commented 2 months ago

We want to collect ideas about AST changes.

magic-akari commented 2 months ago

Use Function everywhere

const obj = {
    x() {}, // Function ✅
    set a(v) {}, // should use Function
    get b() {}, // should use Function
}

function foo() {} // Function ✅
const a = () => {} // should use Function
const b = () => expr; // We should use another form of representation, but it can be conveniently converted into a Function.

class clazz {
    x() {} // Function ✅
    set a(v) {} // Function ✅
    get b() {} // Function ✅
}

SWC AST Viewer


Use 👍 or 👎 to express your opinion.

magic-akari commented 2 months ago

Merge ForInStmt/ForOfStmt into forXStmt


Use 👍 or 👎 to express your opinion.

devongovett commented 2 months ago

Try using an arena allocator (eg bumpalo) for heap allocated values like Vec and Box. Would require passing lifetimes around unfortunately but apparently this is where oxc gets some of its performance wins. Could be worth testing to see how much impact it has.

magic-akari commented 2 months ago

Class

The attribute is_abstract should belong to the ClassDecl, but now it is an attribute of the Class.

SWC AST Viewer

kdy1 commented 2 months ago
kdy1 commented 2 months ago

Rename: ImportStarAsSpecifier => ImportNamespaceSpecifier.

kdy1 commented 2 months ago

Rename variants in TypeScript enums.

Current:

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum TsTypeElement {
    #[tag("TsCallSignatureDeclaration")]
    TsCallSignatureDecl(TsCallSignatureDecl),

    #[tag("TsConstructSignatureDeclaration")]
    TsConstructSignatureDecl(TsConstructSignatureDecl),

    #[tag("TsPropertySignature")]
    TsPropertySignature(TsPropertySignature),

    #[tag("TsGetterSignature")]
    TsGetterSignature(TsGetterSignature),

    #[tag("TsSetterSignature")]
    TsSetterSignature(TsSetterSignature),

    #[tag("TsMethodSignature")]
    TsMethodSignature(TsMethodSignature),

    #[tag("TsIndexSignature")]
    TsIndexSignature(TsIndexSignature),
}

New:

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum TsTypeElement {
    #[tag("TsCallSignatureDeclaration")]
    CallSignature(TsCallSignatureDecl),

    #[tag("TsConstructSignatureDeclaration")]
    Construct(TsConstructSignatureDecl),

    #[tag("TsPropertySignature")]
    Property(TsPropertySignature),

    #[tag("TsGetterSignature")]
    Getter(TsGetterSignature),

    #[tag("TsSetterSignature")]
    Setter(TsSetterSignature),

    #[tag("TsMethodSignature")]
    Method(TsMethodSignature),

    #[tag("TsIndexSignature")]
    Index(TsIndexSignature),
}
kdy1 commented 2 months ago

Fix TypeScript namespace vs module

kdy1 commented 2 months ago

Remove Key and merge PrivateName into PropName

kdy1 commented 2 months ago

or

kdy1 commented 2 months ago

Take trait => Default trait

devongovett commented 1 month ago

Perhaps I should open a separate issue for this, but would you consider supporting a way to include custom types of expressions in the AST? For example:

enum Expr {
  // ...
  Custom(Box<dyn CustomExpr>)
}

trait CustomExpr: Eq, Hash, ..., swc_ecma_codegen::Node {}

The use case for this that I have is to store placeholder references in the AST for dependencies such as require, new URL(...) and other expressions that might get replaced by a bundler.

I did something similar to this in lightningcss, except using generics instead of dyn. But I'd recommend not doing that - it was quite painful to propagate the generics everywhere.

Edit: Just remembered about a previous discussion of this: #7874. Was there any further progress on that?