changeset 24029:429b4f8d2fac v8.2.2556

patch 8.2.2556: Vim9: :import with "as" not fully supported Commit: https://github.com/vim/vim/commit/0a84284e603e39c1288d71f8f96f66eb7eb1ca61 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Feb 27 22:41:19 2021 +0100 patch 8.2.2556: Vim9: :import with "as" not fully supported Problem: Vim9: :import with "as" not fully supported. Solution: Implement "as" for more cases.
author Bram Moolenaar <Bram@vim.org>
date Sat, 27 Feb 2021 22:45:05 +0100
parents 38f3d0098db2
children 45c1d2b15801
files src/testdir/test_vim9_script.vim src/version.c src/vim9script.c
diffstat 3 files changed, 99 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -1218,6 +1218,36 @@ def Test_vim9_import_export()
   delete('Xvim9_script')
 enddef
 
+def Test_import_as()
+  var export_lines =<< trim END
+    vim9script
+    export var one = 1
+    export var yes = 'yes'
+  END
+  writefile(export_lines, 'XexportAs')
+
+  var import_lines =<< trim END
+    vim9script
+    import one as thatOne from './XexportAs'
+    assert_equal(1, thatOne)
+    import yes as yesYes from './XexportAs'
+    assert_equal('yes', yesYes)
+  END
+  CheckScriptSuccess(import_lines)
+
+  import_lines =<< trim END
+    vim9script
+    import {one as thatOne, yes as yesYes} from './XexportAs'
+    assert_equal(1, thatOne)
+    assert_equal('yes', yesYes)
+    assert_fails('echo one', 'E121:')
+    assert_fails('echo yes', 'E121:')
+  END
+  CheckScriptSuccess(import_lines)
+
+  delete('XexportAs')
+enddef
+
 func g:Trigger()
   source Ximport.vim
   return "echo 'yes'\<CR>"
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2556,
+/**/
     2555,
 /**/
     2554,
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -325,82 +325,43 @@ handle_import(
 {
     char_u	*arg = arg_start;
     char_u	*cmd_end = NULL;
-    char_u	*as_name = NULL;
     int		ret = FAIL;
     typval_T	tv;
     int		sid = -1;
     int		res;
+    int		mult = FALSE;
     garray_T	names;
+    garray_T	as_names;
 
     ga_init2(&names, sizeof(char_u *), 10);
+    ga_init2(&as_names, sizeof(char_u *), 10);
     if (*arg == '{')
     {
 	// "import {item, item} from ..."
+	mult = TRUE;
 	arg = skipwhite_and_linebreak(arg + 1, evalarg);
-	for (;;)
-	{
-	    char_u  *p = arg;
-	    int	    had_comma = FALSE;
+    }
 
+    for (;;)
+    {
+	char_u	    *p = arg;
+	int	    had_comma = FALSE;
+	char_u	    *as_name = NULL;
+
+	// accept "*" or "Name"
+	if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
+	    ++arg;
+	else
 	    while (eval_isnamec(*arg))
 		++arg;
-	    if (p == arg)
-		break;
-	    if (ga_grow(&names, 1) == FAIL)
-		goto erret;
-	    ((char_u **)names.ga_data)[names.ga_len] =
-						      vim_strnsave(p, arg - p);
-	    ++names.ga_len;
-	    if (*arg == ',')
-	    {
-		had_comma = TRUE;
-		++arg;
-	    }
-	    arg = skipwhite_and_linebreak(arg, evalarg);
-	    if (*arg == '}')
-	    {
-		arg = skipwhite_and_linebreak(arg + 1, evalarg);
-		break;
-	    }
-	    if (!had_comma)
-	    {
-		emsg(_(e_missing_comma_in_import));
-		goto erret;
-	    }
-	}
-	if (names.ga_len == 0)
-	{
-	    emsg(_(e_syntax_error_in_import));
+	if (p == arg)
+	    break;
+	if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
 	    goto erret;
-	}
-    }
-    else
-    {
-	// "import Name from ..."
-	// "import * as Name from ..."
-	// "import item [as Name] from ..."
-	arg = skipwhite_and_linebreak(arg, evalarg);
-	if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
-	    arg = skipwhite_and_linebreak(arg + 1, evalarg);
-	else if (eval_isnamec1(*arg))
-	{
-	    char_u  *p = arg;
+	((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
+	++names.ga_len;
 
-	    while (eval_isnamec(*arg))
-		++arg;
-	    if (ga_grow(&names, 1) == FAIL)
-		goto erret;
-	    ((char_u **)names.ga_data)[names.ga_len] =
-						      vim_strnsave(p, arg - p);
-	    ++names.ga_len;
-	    arg = skipwhite_and_linebreak(arg, evalarg);
-	}
-	else
-	{
-	    emsg(_(e_syntax_error_in_import));
-	    goto erret;
-	}
-
+	arg = skipwhite_and_linebreak(arg, evalarg);
 	if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
 	{
 	    char_u *p;
@@ -421,6 +382,35 @@ handle_import(
 	    emsg(_(e_missing_as_after_star));
 	    goto erret;
 	}
+	// without "as Name" the as_names entry is NULL
+	((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
+	++as_names.ga_len;
+
+	if (!mult)
+	    break;
+	if (*arg == ',')
+	{
+	    had_comma = TRUE;
+	    ++arg;
+	}
+	arg = skipwhite_and_linebreak(arg, evalarg);
+	if (*arg == '}')
+	{
+	    ++arg;
+	    break;
+	}
+	if (!had_comma)
+	{
+	    emsg(_(e_missing_comma_in_import));
+	    goto erret;
+	}
+    }
+    arg = skipwhite_and_linebreak(arg, evalarg);
+
+    if (names.ga_len == 0)
+    {
+	emsg(_(e_syntax_error_in_import));
+	goto erret;
     }
 
     if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
@@ -501,8 +491,10 @@ handle_import(
 
     if (*arg_start == '*')
     {
-	imported_T *imported;
+	imported_T  *imported;
+	char_u	    *as_name = ((char_u **)as_names.ga_data)[0];
 
+	// "import * as That"
 	imported = find_imported(as_name, STRLEN(as_name), cctx);
 	if (imported != NULL && imported->imp_sid == sid)
 	{
@@ -521,7 +513,7 @@ handle_import(
 	if (imported == NULL)
 	    goto erret;
 	imported->imp_name = as_name;
-	as_name = NULL;
+	((char_u **)as_names.ga_data)[0] = NULL;
 	imported->imp_sid = sid;
 	imported->imp_flags = IMP_FLAGS_STAR;
     }
@@ -535,6 +527,7 @@ handle_import(
 	for (i = 0; i < names.ga_len; ++i)
 	{
 	    char_u	*name = ((char_u **)names.ga_data)[i];
+	    char_u	*as_name = ((char_u **)as_names.ga_data)[i];
 	    size_t	len = STRLEN(name);
 	    int		idx;
 	    imported_T	*imported;
@@ -572,10 +565,17 @@ handle_import(
 		if (imported == NULL)
 		    goto erret;
 
-		// TODO: check for "as" following
-		// imported->imp_name = vim_strsave(as_name);
-		imported->imp_name = name;
-		((char_u **)names.ga_data)[i] = NULL;
+		if (as_name == NULL)
+		{
+		    imported->imp_name = name;
+		    ((char_u **)names.ga_data)[i] = NULL;
+		} 
+		else
+		{
+		    // "import This as That ..."
+		    imported->imp_name = as_name;
+		    ((char_u **)as_names.ga_data)[i] = NULL;
+		}
 		imported->imp_sid = sid;
 		if (idx >= 0)
 		{
@@ -592,7 +592,7 @@ handle_import(
     }
 erret:
     ga_clear_strings(&names);
-    vim_free(as_name);
+    ga_clear_strings(&as_names);
     return cmd_end;
 }