Skip to content

[Suggestion] Allow opting out of Native Imports #1296

@DuncanUszkay1

Description

@DuncanUszkay1

Currently with the --use argument in asc we can replace implementations of the STD with our own- however in certain cases it'd be really nice to be able to prevent the module from being imported at all. I think this feature is important for AssemblyScript to be host-agnostic, since auto-importing features that may only make sense in a browser is unfriendly to those of us who aren't running in a browser.

This is useful when you know ahead of time that a module like Date won't be available in the host. In those cases, you'd want compilation to fail if you tried to reference Date in your program or it's dependencies.

From a compiler perspective it can be implemented by just removing the elementsByName entry associated with the import that we don't want when the asc options indicate the import isn't wanted. Afterwards we get a nice error message (or at least nicer than having it blow up later on the host):

ERROR TS2304: Cannot find name 'Date'.

 Date.now();
 ~~~~
 in main.ts(1,1)

In terms of the syntax of this I have a few ideas:

--unload Date <OtherName> ...
--use Date=
--use Date=None

Let me know if you think this feature would make sense, or if you have any other suggestions to make this more friendly to non-browser hosts.

If this sounds good I'll gladly throw up a PR

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