Skip to content

Add support for default imports/exports #98

Closed
@alangpierce

Description

@alangpierce

Simple example:

add.ts

export default function add(a: i32, b: i32): i32 {
  return a + b;
}

main.ts

import add from './add';

export function getValue(): i32 {
  return add(40, 2);
}

Currently both default import and default export syntax are a syntax error. In JS, this is mostly just syntax sugar over a named export called "default", so I think the expected behavior is reasonably straightforward.

I can try taking this on. Some initial thoughts on the steps/details:

  • Add support for import {default as foo} from './foo'; and export {foo as default}; so the other syntaxes can be tested individually.
  • Extend ImportStatement to have a default-imported name and make it behave like import {default as foo}.
  • Add a new CommonFlags.EXPORT_DEFAULT and mark it on export default function and export default class statements. I think these are technically live bindings for the class/function name, if specified, but maybe AssemblyScript should/does disallow overwriting classes and functions anyway?
  • Add a new statement type ExportDefaultStatement for export default [expression];. Hopefully this can be implemented similarly to export const.

Does that sound reasonable?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions