`export {foo as default}`有效的ES2015? [英] Is `export { foo as default }` valid ES2015?
问题描述
我在GitHub上收到了关于ESL15的ES2015模块导入/导出验证插件的问题不能以下列语法识别 default
export:
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:
export {
foo as default,
bar
}
我的插件将lint以下(等效?)语法没有问题:
where my plugin will lint the following (equivalent?) syntax no problem:
export default foo;
export const bar = ..;
Babel 和 Esprima 解析类似的语法,没有错误,这适用于使用Babel两端的代码(导入和导出)。
Both Babel and Esprima parse similar syntax without errors, and this works for code using Babel on both ends (import and export).
然而,我不相信 spec 允许以前的导出{x作为默认}
形式:
However, I'm not convinced the spec allows the former export { x as default }
form:
对于
中的每个
标识符名称
n
ReferencedBindingsExportClause
:如果n的StringValue为ReservedWord,或者如果n的值为implements,则为语法错误, interface,let,package,private,protected,public,static或yield。
For each
IdentifierName
n
inReferencedBindings
ofExportClause
: 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".
ReservedWord
确实包括 default
,虽然我认为可以认为 ReferencedBindings
是指具体涉及导出的模块 - 本地标识符名称 (即 foo
),而不是导出的名称本身。
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 (i.e. foo
) and not the exported name itself.
通常似乎还有一个奇怪的事情可以导出保留字Babel也会很高兴地允许像
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'
所以,总结:是 export {foo as default}
一种在ES2015中导出默认值的有效方法?
So, in summary: is export { foo as default }
a valid way to export a default in ES2015?
推荐答案
是的, 引用绑定 仅引用第一个IdentifierName。所以
Yes, ReferencedBindings refers only to the first IdentifierName. So
export { default as something } // or
export { default }
无效,但
export { something as default }
不是。 ESLint需要在这里修复。
is not. ESLint will need a fix here.
这篇关于`export {foo as default}`有效的ES2015?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!