简体   繁体   English

是`export {foo as default}`有效的ES2015?

[英]Is `export { foo as default }` valid ES2015?

I received an issue on GitHub about my ES2015 module import/export validating plugin for ESLint not recognizing the default export in the following syntax: 在GitHub上收到一个关于ESLint的ES2015模块导入/导出验证插件的问题,但没有通过以下语法识别default导出:

export { 
    foo as default,
    bar
}

where my plugin will lint the following (equivalent?) syntax no problem: 我的插件将lint下面(等效?)语法没问题:

export default foo;
export const bar = ..;

Both Babel and Esprima parse similar syntax without errors, and this works for code using Babel on both ends (import and export). BabelEsprima都解析了类似的语法而没有错误,这适用于两端使用Babel的代码(导入和导出)。

However, I'm not convinced the spec allows the former export { x as default } form: 但是,我不相信规范允许前export { x as default }形式:

For each IdentifierName n in ReferencedBindings of ExportClause : It is a Syntax Error if StringValue of n is a ReservedWord or if the StringValue of n is one of: "implements", "interface", "let", "package", "private", "protected", "public", "static", or "yield". 对于ExportClause ReferencedBindings的每个IdentifierName n :如果n的StringValue是ReservedWord,或者n的StringValue是以下之一,则它是语法错误:“implements”,“interface”,“let”,“package”,“private” ,“受保护”,“公共”,“静态”或“收益”。

ReservedWord does include default , though I think one could argue that ReferencedBindings is referring specifically to the module-local identifier names that are being exported (ie foo ) and not the exported name itself. ReservedWord 确实包含default ,但我认为有人可能认为ReferencedBindings 特指的是正在导出 的模块本地标识符名称 (即foo )而不是导出的名称本身。

It also generally seems like a weird thing to be able to export reserved words; 能够导出保留字通常似乎是一件奇怪的事情; Babel will happily also allow something like 巴别塔也很乐意也允许这样的事情

// ./foo.js
export { foo as yield }
// ./mod.js
import { yield as nonReservedIdentifier } from './foo'

So, in summary: is export { foo as default } a valid way to export a default in ES2015? 那么,总结一下: export { foo as default }是在ES2015中导出默认值的有效方法吗?

Yes, ReferencedBindings refers only to the first IdentifierName. 是的, ReferencedBindings仅指第一个IdentifierName。 So 所以

export { default as something } // or
export { default }

is invalid, but 是无效的,但是

export { something as default }

is not. 不是。 ESLint will need a fix here. ESLint需要在这里修复。

Yes, it's valid. 是的,它是有效的。 I'll break it down. 我会把它分解。

  1. :

     export { foo as default } 

    This matches the following productions (from least to most specific): 这符合以下制作(从最少到最具体):

     export ExportClause ExportClause : { ExportsList } ExportsList : ExportSpecifier ExportSpecifier : IdentifierName as IdentifierName 
  2. Then you have the early error semantics : 然后你有早期的错误语义

    15.2.3.1 Static Semantics: Early Errors 15.2.3.1静态语义:早期错误

    ExportDeclaration : export ExportClause ;

    For each IdentifierName n in ReferencedBindings of ExportClause : It is a Syntax Error if StringValue of n is a ReservedWord ... 对于ExportClause ReferencedBindings的每个IdentifierName n :如果n StringValueReservedWord ,则为语法错误...

    These apply to any productions matching export ExportClause , including your example syntax. 这些适用于与export ExportClause匹配的任何产品,包括您的示例语法。 This invokes the ReferencedBindings algorithm. 这将调用ReferencedBindings算法。

  3. The ReferencedBindings algorithm that applies to that most specific production matched by this syntax is: 适用于与此语法匹配的最特定生产的ReferencedBindings算法是:

    ExportSpecifier : IdentifierName as IdentifierName

    Return a List containing the first IdentifierName . 返回包含第一个IdentifierNameList

So you see that the restriction regarding ReservedWord and the other listed values is only applied to the foo part of the syntax in your example. 因此,您会看到有关ReservedWord和其他列出值的限制仅适用于示例中语法的foo部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM