-
-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for default imports/exports #98
Comments
I think implicitly naming default imports |
Progress toward AssemblyScript#98 See the imports and exports grammar from the spec: https://www.ecma-international.org/ecma-262/8.0/index.html#sec-imports https://www.ecma-international.org/ecma-262/8.0/index.html#sec-exports In particular, ImportSpecifier and ExportSpecifier use IdentifierName to identify the import/export name rather than the more restrictive ImportedBinding (or just Identifier). That means any keyword is allowed. There are (at least) three contexts to think about in JS identifier parsing: * In most contexts, identifiers like variable names can be any name except a reserved word. * Some keywords, like "as", are normally not keywords but have special keyword-like meaning in specific situations. Acorn and Babel call these "contextual keywords", and `tokenIsAlsoIdentifier` and `preferIdentifier` in AssemblyScript appear to handle that case. * In some contexts, like object keys and named imports/exports, any name is allowed, even keywords. This change adds that third case in order to handle imports/exports. I added a `forceIdentifier` flag that tells the tokenizer that any identifier name should be treated as a plain identifier, then used it in the import and export cases.
Hmm, I worry about dropping the distinction at the parser level. One concern is just error messages and other tooling-related things; I've definitely seen custom error messages referencing default imports/exports, and I know CoffeeScript tooling is notoriously hard to write because so much information is dropped by the lexer/parser. Also worth noting that every AST format in https://astexplorer.net/ (as far as I can tell) has a special case for default imports/exports, so it certainly seems common. A few other cases:
The syntax
|
Does not implement combinations like 'import theDefault, *' yet
Does not implement combinations like 'import theDefault, *' yet
Quick update: There is some support for default exports and imports now, internally translating these to become a |
Must be supported for quite some time |
Partially support. So stay opened |
I'm not sure if this ticket is related to the issue I'm having, but exporting the default exports from other files seems like it's not working within AssemblyScript. In my case, I've created an export { default as Product } from './Product'; When I try to compile, I get the following error:
As far as I can tell, these exports are valid, and the docs don't imply that this shouldn't work. |
@dcodeIO It could be also closed I guess |
Closing this issue as part of 2020 vacuum as it seems to have been fixed meanwhile, implemented as a |
Simple example:
add.ts
main.ts
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:
import {default as foo} from './foo';
andexport {foo as default};
so the other syntaxes can be tested individually.ImportStatement
to have a default-imported name and make it behave likeimport {default as foo}
.CommonFlags.EXPORT_DEFAULT
and mark it onexport default function
andexport 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?ExportDefaultStatement
forexport default [expression];
. Hopefully this can be implemented similarly toexport const
.Does that sound reasonable?
The text was updated successfully, but these errors were encountered: