swc icon indicating copy to clipboard operation
swc copied to clipboard

Ideas for AST changes

Open kdy1 opened this issue 1 year ago • 12 comments

We want to collect ideas about AST changes.

  • Change boxing rules: https://github.com/swc-project/swc/pull/9158

kdy1 avatar Jul 06 '24 04:07 kdy1

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 avatar Jul 06 '24 04:07 magic-akari

Merge ForInStmt/ForOfStmt into forXStmt


Use 👍 or 👎 to express your opinion.

magic-akari avatar Jul 06 '24 05:07 magic-akari

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.

devongovett avatar Jul 06 '24 06:07 devongovett

Class

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

SWC AST Viewer

magic-akari avatar Jul 06 '24 09:07 magic-akari

  • BindingIdent should not have ident: Ident.
  • PrivateName should not have id: Ident.

kdy1 avatar Jul 07 '24 06:07 kdy1

Rename: ImportStarAsSpecifier => ImportNamespaceSpecifier.

kdy1 avatar Jul 07 '24 06:07 kdy1

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 avatar Jul 07 '24 06:07 kdy1

Fix TypeScript namespace vs module

kdy1 avatar Jul 07 '24 06:07 kdy1

Remove Key and merge PrivateName into PropName

kdy1 avatar Jul 07 '24 06:07 kdy1

  • JSX => Jsx

or

  • Ts => TS

kdy1 avatar Jul 07 '24 06:07 kdy1

Take trait => Default trait

kdy1 avatar Jul 07 '24 14:07 kdy1

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?

devongovett avatar Aug 10 '24 17:08 devongovett