branch: externals/js2-mode commit 4eaf98e3cc3789381a37a05cd70a248db9ac8c1b Author: Dmitry Gutov <dgu...@yandex.ru> Commit: Dmitry Gutov <dgu...@yandex.ru>
Support for dynamic imports and `import.meta` Resolves #547. For future reference, Mozilla's corresponding bug reports are: https://bugzilla.mozilla.org/show_bug.cgi?id=1427610 https://bugzilla.mozilla.org/show_bug.cgi?id=1484948 (Changes in src/frontend/Parser.cpp) But there's little point in creating new node types for these syntax cases for our purposes, so here goes the simpler change. --- NEWS.md | 2 ++ js2-mode.el | 9 ++++++++- tests/parser.el | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index bf6f109..932d99d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Next +* Support for dynamic imports and `import.meta` + ([#547](https://github.com/mooz/js2-mode/issues/547)). * Support for trailing comma in arrow function parameters ([#480](https://github.com/mooz/js2-mode/issues/480)). * Support for `for await of` syntax ([#543](https://github.com/mooz/js2-mode/pull/543)). diff --git a/js2-mode.el b/js2-mode.el index 8327ca3..a3630b6 100644 --- a/js2-mode.el +++ b/js2-mode.el @@ -8612,7 +8612,7 @@ node are given relative start positions and correct lengths." (aset parsers js2-FOR #'js2-parse-for) (aset parsers js2-FUNCTION #'js2-parse-function-stmt) (aset parsers js2-IF #'js2-parse-if) - (aset parsers js2-IMPORT #'js2-parse-import) + (aset parsers js2-IMPORT #'js2-parse-import-declaration-or-expr) (aset parsers js2-LC #'js2-parse-block) (aset parsers js2-LET #'js2-parse-let-stmt) (aset parsers js2-NAME #'js2-parse-name-or-label) @@ -8742,6 +8742,11 @@ Return value is a list (EXPR LP RP), with absolute paren positions." (js2-node-add-children pn (car cond) if-true if-false) pn)) +(defun js2-parse-import-declaration-or-expr () + (if (memq (js2-peek-token) `(,js2-LP ,js2-DOT)) + (js2-parse-expr-stmt) + (js2-parse-import))) + (defun js2-parse-import () "Parse import statement. The current token must be js2-IMPORT." (unless (js2-ast-root-p js2-current-scope) @@ -10651,6 +10656,8 @@ array-literals, array comprehensions and regular expressions." (js2-parse-attribute-access)) ((= tt js2-NAME) (js2-parse-name tt)) + ((= tt js2-IMPORT) + (js2-create-name-node nil nil "import")) ((= tt js2-NUMBER) (setq node (make-js2-number-node)) (when (and js2-in-use-strict-directive diff --git a/tests/parser.el b/tests/parser.el index 78115f3..95d3f5d 100644 --- a/tests/parser.el +++ b/tests/parser.el @@ -802,6 +802,15 @@ the test." (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';") (js2-deftest-parse import-from-this-module "import {url} from this module;") +(js2-deftest-parse import-meta + "console.log(import.meta);") + +(js2-deftest-parse import-dynamic-stmt + "import('/modules/my-module.js').then((module) => {});") + +(js2-deftest-parse import-dynamic-expr + "let v = import('/modules/my-module.js').then((module) => { module.getV();\n});") + ;; Module Exports (js2-deftest export-rexport "export * from 'other/lib'"