Skip to content

Allow default export for any declaration #160

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = grammar({
[$.primary_expression, $.statement_block, 'object'],
[$.import_statement, $.import],
[$.export_statement, $.primary_expression],
[$.export_clause, $.object],
],

conflicts: $ => [
Expand All @@ -79,6 +80,9 @@ module.exports = grammar({
[$.assignment_expression, $.object_assignment_pattern],
[$.labeled_statement, $._property_name],
[$.computed_property_name, $.array],
[$.export_clause, $.object, $.object_pattern],
[$._import_export_specifier, $.object, $.object_pattern],
[$.export_statement, $._property_name],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you report what error you get if you remove these conflicts? I'm assuming that, by allowing export <expression>, we're creating ambiguities between "export clauses (e.g. export {foo as bar, baz}) and exported object literals (e.g. export {foo: bar}).

I'm wondering if the latter case is really allowed, or if we should keep default as required when exporting an expression.

Copy link
Contributor Author

@resolritter resolritter Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I comprehend it, this syntax is for exporting identifiers, not objects. It's sugar for export const c = c so you don't have to repeat the c twice. Fairly common to see in the wild.

From what I've tested in the links above, the "object version" of export { foo: bar } is not valid.


The error I get by removing the conflicts is:

Unresolved conflict for symbol sequence:

  'export'  '{'  ','  •  '}'  …

Possible interpretations:

  1:  'export'  '{'  (object_pattern_repeat1  ',')  •  '}'  …
  2:  'export'  '{'  (object_repeat1  ',')  •  '}'  …
  3:  'export'  (export_clause  '{'  ','  •  '}')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed the suggested fix, consisting in allowing expressions only after export default, not directly after export.

I made a new PR for this: #168

],

word: $ => $.identifier,
Expand Down Expand Up @@ -107,10 +111,10 @@ module.exports = grammar({
seq(
repeat(field('decorator', $.decorator)),
'export',
optional('default'),
choice(
field('declaration', $.declaration),
seq(
'default',
field('value', $.expression),
$._semicolon
)
Expand Down
40 changes: 36 additions & 4 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@
"type": "STRING",
"value": "export"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "default"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
Expand All @@ -126,10 +138,6 @@
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "default"
},
{
"type": "FIELD",
"name": "value",
Expand Down Expand Up @@ -6244,6 +6252,20 @@
[
"computed_property_name",
"array"
],
[
"export_clause",
"object",
"object_pattern"
],
[
"_import_export_specifier",
"object",
"object_pattern"
],
[
"export_statement",
"_property_name"
]
],
"precedences": [
Expand Down Expand Up @@ -6398,6 +6420,16 @@
"type": "SYMBOL",
"name": "primary_expression"
}
],
[
{
"type": "SYMBOL",
"name": "export_clause"
},
{
"type": "SYMBOL",
"name": "object"
}
]
],
"externals": [
Expand Down
16 changes: 16 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@
{
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "member_expression",
"named": true
}
]
}
Expand Down Expand Up @@ -1804,6 +1812,14 @@
{
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "member_expression",
"named": true
}
]
},
Expand Down
Loading