[njs] Added support for export {name as default} statement.

Vadim Zhestikov v.zhestikov at f5.com
Tue Mar 21 04:18:03 UTC 2023


details:   https://hg.nginx.org/njs/rev/fbd36220ea17
branches:  
changeset: 2073:fbd36220ea17
user:      Vadim Zhestikov <v.zhestikov at f5.com>
date:      Mon Mar 20 21:09:58 2023 -0700
description:
Added support for export {name as default} statement.

This fixes #624 issue on Github.

diffstat:

 src/njs_parser.c                       |  78 +++++++++++++++++++++++++++++++++-
 test/js/import_as_default.t.js         |   9 +++
 test/js/import_as_default_compare.t.js |  14 ++++++
 test/js/import_as_default_double.t.js  |   9 +++
 test/js/module/lib4.js                 |   2 +
 test/js/module/lib5.js                 |   6 ++
 test/js/module/lib6-1.js               |   6 ++
 test/js/module/lib6.js                 |   6 ++
 8 files changed, 128 insertions(+), 2 deletions(-)

diffs (175 lines):

diff -r 95f57822bdaa -r fbd36220ea17 src/njs_parser.c
--- a/src/njs_parser.c	Fri Mar 10 14:15:29 2023 -0800
+++ b/src/njs_parser.c	Mon Mar 20 21:09:58 2023 -0700
@@ -7973,6 +7973,9 @@ njs_parser_export(njs_parser_t *parser, 
     njs_queue_link_t *current)
 {
     njs_parser_node_t  *node;
+    njs_lexer_token_t  *peek;
+
+    static const njs_str_t  as_string = njs_str("as");
 
     if (!parser->module) {
         njs_parser_syntax_error(parser, "Illegal export statement");
@@ -7980,8 +7983,79 @@ njs_parser_export(njs_parser_t *parser, 
     }
 
     if (token->type != NJS_TOKEN_DEFAULT) {
-        njs_parser_syntax_error(parser, "Non-default export is not supported");
-        return NJS_DONE;
+
+        if (token->type != NJS_TOKEN_OPEN_BRACE) {
+            njs_parser_syntax_error(parser,
+                                    "Non-default export is not supported");
+            return NJS_DONE;
+        }
+
+        /*
+         * 'export {'
+         *    supported only: export {identifier as default};
+         */
+
+        njs_lexer_consume_token(parser->lexer, 1);
+
+        token = njs_lexer_token(parser->lexer, 0);
+        if (njs_slow_path(token == NULL)) {
+            return NJS_ERROR;
+        }
+
+        if (token->type != NJS_TOKEN_NAME) {
+            njs_parser_syntax_error(parser, "Identifier expected");
+            return NJS_DONE;
+        }
+
+        peek = njs_lexer_peek_token(parser->lexer, token, 0);
+        if (njs_slow_path(peek == NULL)) {
+            return NJS_ERROR;
+        }
+
+        if (peek->type != NJS_TOKEN_NAME ||
+            !njs_strstr_eq(&peek->text, &as_string))
+        {
+            njs_parser_syntax_error(parser, "'as' expected");
+            return NJS_DONE;
+        }
+
+        peek = njs_lexer_peek_token(parser->lexer, peek, 0);
+        if (njs_slow_path(peek == NULL)) {
+            return NJS_ERROR;
+        }
+
+        if (peek->type != NJS_TOKEN_DEFAULT) {
+            njs_parser_syntax_error(parser,
+                                    "Non-default export is not supported");
+            return NJS_DONE;
+        }
+
+        peek = njs_lexer_peek_token(parser->lexer, peek, 1);
+        if (njs_slow_path(token == NULL)) {
+            return NJS_ERROR;
+        }
+
+        if (peek->type != NJS_TOKEN_CLOSE_BRACE) {
+            njs_parser_syntax_error(parser, "Close brace is expected");
+            return NJS_DONE;
+        }
+
+        node = njs_parser_node_new(parser, NJS_TOKEN_EXPORT);
+        if (node == NULL) {
+            return NJS_ERROR;
+        }
+
+        node->token_line = parser->line;
+        node->right = njs_parser_reference(parser, token);
+        if (node->right == NULL) {
+            return NJS_ERROR;
+        }
+
+        parser->node = node;
+
+        njs_lexer_consume_token(parser->lexer, 4);
+
+        return njs_parser_stack_pop(parser);
     }
 
     njs_lexer_consume_token(parser->lexer, 1);
diff -r 95f57822bdaa -r fbd36220ea17 test/js/import_as_default.t.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/import_as_default.t.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,9 @@
+/*---
+includes: []
+flags: []
+paths: [test/js/module/]
+---*/
+
+import imp from 'lib4.js';
+
+assert.sameValue(imp, 10);
diff -r 95f57822bdaa -r fbd36220ea17 test/js/import_as_default_compare.t.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/import_as_default_compare.t.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,14 @@
+/*---
+includes: []
+flags: []
+paths: [test/js/module]
+---*/
+
+import a from 'lib6.js';
+import b from 'lib6-1.js';
+
+assert.sameValue(a.a, 1);
+assert.sameValue(a.b, 2);
+
+assert.sameValue(a.a, b.a);
+assert.sameValue(a.b, b.b);
diff -r 95f57822bdaa -r fbd36220ea17 test/js/import_as_default_double.t.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/import_as_default_double.t.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,9 @@
+/*---
+includes: []
+flags: []
+paths: [test/js/module]
+negative:
+  phase: runtime
+---*/
+
+import m from 'lib5.js';
diff -r 95f57822bdaa -r fbd36220ea17 test/js/module/lib4.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/module/lib4.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,2 @@
+var var10 = 10;
+export { var10 as default };
diff -r 95f57822bdaa -r fbd36220ea17 test/js/module/lib5.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/module/lib5.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,6 @@
+var uu = 10;
+var uu1 = 11;
+
+export { uu as default };
+
+export { uu1 as default };
diff -r 95f57822bdaa -r fbd36220ea17 test/js/module/lib6-1.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/module/lib6-1.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,6 @@
+var obj = {
+  a:1,
+  b:2
+}
+
+export default obj;
diff -r 95f57822bdaa -r fbd36220ea17 test/js/module/lib6.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/module/lib6.js	Mon Mar 20 21:09:58 2023 -0700
@@ -0,0 +1,6 @@
+var obj = {
+  a:1,
+  b:2
+}
+
+export {obj as default};


More information about the nginx-devel mailing list