Skip to content

Commit

Permalink
[parser] update ExportDeclaration AST node
Browse files Browse the repository at this point in the history
Summary:
flow's AST was using `ExportDeclaration`, which was from the defunct `esprima` harmony branch. estree, esprima and babel use `ExportNamedDeclaration` and `ExportDefaultDeclaration`.

this gets us mostly into spec. we still use `ExportBatchSpecifier` instead of `ExportAllDeclaration` + `ExportNamespaceSpecifier`/`ExportDefaultSpecifier`, but will follow up on those.

Reviewed By: samwgoldman

Differential Revision: D3938899

fbshipit-source-id: 5db3c99880294a7fa7cfd77a70ee701117dd9a58
  • Loading branch information
mroch authored and Facebook Github Bot committed Sep 28, 2016
1 parent 7c352f3 commit 9ce80b1
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 38 deletions.
5 changes: 1 addition & 4 deletions newtests/tool_test_example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default suite(({addFile, addFiles, addCode}) => [
],
"body": [
{
"type": "ExportDeclaration",
"type": "ExportDefaultDeclaration",
"loc": {
"source": null,
"start": {
Expand All @@ -217,7 +217,6 @@ export default suite(({addFile, addFiles, addCode}) => [
0,
19
],
"default": true,
"declaration": {
"type": "Literal",
"loc": {
Expand All @@ -238,8 +237,6 @@ export default suite(({addFile, addFiles, addCode}) => [
"value": 123,
"raw": "123"
},
"specifiers": [],
"source": null,
"exportKind": "value"
}
],
Expand Down
8 changes: 2 additions & 6 deletions src/parser/estree_translator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ end with type t = Impl.t) = struct
"typeAnnotation", type_annotation annot
|]
| loc, ExportNamedDeclaration export -> ExportNamedDeclaration.(
node "ExportDeclaration" loc [|
"default", bool false;
node "ExportNamedDeclaration" loc [|
"declaration", option statement export.declaration;
"specifiers", export_specifiers export.specifiers;
"source", option literal export.source;
Expand All @@ -279,11 +278,8 @@ end with type t = Impl.t) = struct
| Declaration stmt -> statement stmt
| ExportDefaultDeclaration.Expression expr -> expression expr
in
node "ExportDeclaration" loc [|
"default", bool true;
node "ExportDefaultDeclaration" loc [|
"declaration", declaration;
"specifiers", array [||];
"source", null;
"exportKind", string (export_kind export.exportKind);
|]
)
Expand Down
21 changes: 21 additions & 0 deletions src/parser/test/custom_ast_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ def("DeclareExportDeclaration")
null
))

// See https://github.com/benjamn/ast-types/issues/180
def("ExportDefaultDeclaration")
.bases("Declaration")
.build("declaration", "exportKind")
.field("declaration", or(def("Declaration"), def("Expression")))
.field("exportKind", or("type", "value"));

// See https://github.com/benjamn/ast-types/issues/180
def("ExportNamedDeclaration")
.bases("Declaration")
.build("declaration", "specifiers", "source", "exportKind")
.field("declaration", or(def("Declaration"), null))
// TODO: this is non-standard. should be this:
// .field("specifiers", [def("ExportSpecifier")], defaults.emptyArray)
.field("specifiers", [or(
def("ExportSpecifier"),
def("ExportBatchSpecifier")
)], defaults.emptyArray)
.field("source", or(def("Literal"), null), defaults["null"])
.field("exportKind", or("type", "value"));

var BinaryOperator = or(
"==", "!=", "===", "!==",
"<", "<=", ">", ">=",
Expand Down
273 changes: 256 additions & 17 deletions src/parser/test/esprima_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4209,29 +4209,249 @@ module.exports = {
'Export': {
'options': { sourceType: "module" },
'tests': [
'export * from "foo";',
'export * from "foo"',
{
content: 'export * from "foo";',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export * from "foo"',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
/* This should be supported...
'export {} from "foo";',
*/
'export { bar } from "foo";',
'export { bar } from "foo"',
'export { bar, baz } from "foo";',
'export { bar };',
{
content: 'export { bar } from "foo";',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export { bar } from "foo"',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export { bar, baz } from "foo";',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export { bar };',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
/* Esprima should support trailing comma
'export { bar, }',
*/
'export { bar, baz };',
'export var x, y',
'export var y = 12',
'export let x, y',
'export let y = 12',
{
content: 'export { bar, baz };',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export var x, y',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export var y = 12',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export let x, y',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export let y = 12',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
'export const x, y',
'export const y = 12',
'export function foo() {}',
'export class A {}',
'export default 1 + 1;',
'export default 1 + 1',
{
content: 'export const y = 12',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export function foo() {}',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export class A {}',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportNamedDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
},
},
{
content: 'export default 1 + 1;',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportDefaultDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
'root.body.0.specifiers': {
type: 'Missing property',
},
'root.body.0.source': {
type: 'Missing property',
},
},
},
{
content: 'export default 1 + 1',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportDefaultDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
'root.body.0.specifiers': {
type: 'Missing property',
},
'root.body.0.source': {
type: 'Missing property',
},
},
},
/* Esprima parses default exports wrong
'export default function foo() {}',
'export default function *foo() {}',
Expand All @@ -4247,7 +4467,26 @@ module.exports = {
'export class {}',
*/
'export function {}',
'export default function() {}',
{
content: 'export default function() {}',
explanation: 'esprima-fb is outdated',
expected_differences: {
'root.body.0.type': {
type: 'Wrong string',
expected: 'ExportDeclaration',
actual: 'ExportDefaultDeclaration',
},
'root.body.0.default': {
type: 'Missing property',
},
'root.body.0.specifiers': {
type: 'Missing property',
},
'root.body.0.source': {
type: 'Missing property',
},
},
},
/* Esprima parses default exports wrong
'export default class A {}',
*/
Expand Down
Loading

0 comments on commit 9ce80b1

Please sign in to comment.