ソースを参照

Add full module path to module name errors (#8758)

* add full module path to module name errors

* test with package

* update messages
Aleksandr Kuzmenko 5 年 前
コミット
2b6424c1a2

+ 14 - 10
src/context/typecore.ml

@@ -227,28 +227,32 @@ let add_local ctx k n t p =
 	ctx.locals <- PMap.add n v ctx.locals;
 	v
 
-let check_identifier_name ctx name kind p =
+let display_identifier_error ctx ?prepend_msg msg p =
+	let prepend = match prepend_msg with Some s -> s ^ " " | _ -> "" in
+	display_error ctx (prepend ^ msg) p
+
+let check_identifier_name ?prepend_msg ctx name kind p =
 	if starts_with name '$' then
-		display_error ctx ((StringHelper.capitalize kind) ^ " names starting with a dollar are not allowed: \"" ^ name ^ "\"") p
+		display_identifier_error ctx ?prepend_msg ((StringHelper.capitalize kind) ^ " names starting with a dollar are not allowed: \"" ^ name ^ "\"") p
 	else if not (Lexer.is_valid_identifier name) then
-		display_error ctx ("\"" ^ (StringHelper.s_escape name) ^ "\" is not a valid " ^ kind ^ " name") p
+		display_identifier_error ctx ?prepend_msg ("\"" ^ (StringHelper.s_escape name) ^ "\" is not a valid " ^ kind ^ " name.") p
 
 let check_field_name ctx name p =
 	match name with
 	| "new" -> () (* the only keyword allowed in field names *)
 	| _ -> check_identifier_name ctx name "field" p
 
-let check_uppercase_identifier_name ctx name kind p =
+let check_uppercase_identifier_name ?prepend_msg ctx name kind p =
 	if String.length name = 0 then
-		display_error ctx ((StringHelper.capitalize kind) ^ " name must not be empty") p
+		display_identifier_error ?prepend_msg ctx ((StringHelper.capitalize kind) ^ " name must not be empty.") p
 	else if Ast.is_lower_ident name then
-		display_error ctx ((StringHelper.capitalize kind) ^ " name should start with an uppercase letter: \"" ^ name ^ "\"") p
+		display_identifier_error ?prepend_msg ctx ((StringHelper.capitalize kind) ^ " name should start with an uppercase letter: \"" ^ name ^ "\"") p
 	else
-		check_identifier_name ctx name kind p
+		check_identifier_name ?prepend_msg ctx name kind p
 
-let check_module_path ctx path p =
-	check_uppercase_identifier_name ctx (snd path) "module" p;
-	let pack = fst path in
+let check_module_path ctx (pack,name) p =
+	let full_path = StringHelper.s_escape (if pack = [] then name else (String.concat "." pack) ^ "." ^ name) in
+	check_uppercase_identifier_name ~prepend_msg:("Module \"" ^ full_path ^ "\" does not have a valid name.") ctx name "module" p;
 	try
 		List.iter (fun part -> Path.check_package_name part) pack;
 	with Failure msg ->

+ 3 - 3
tests/misc/projects/Issue8019/compile3-fail.hxml.stderr

@@ -1,3 +1,3 @@
-Macro2.hx:7: characters 17-18 : Module name must not be empty
-Macro2.hx:7: characters 17-18 : "0" is not a valid module name
-Macro2.hx:7: characters 17-18 : "Type+" is not a valid module name
+Macro2.hx:7: characters 17-18 : Module "" does not have a valid name. Module name must not be empty.
+Macro2.hx:7: characters 17-18 : Module "0" does not have a valid name. "0" is not a valid module name.
+Macro2.hx:7: characters 17-18 : Module "Type+" does not have a valid name. "Type+" is not a valid module name.

+ 18 - 0
tests/misc/projects/Issue8750/Main2.hx

@@ -0,0 +1,18 @@
+class Main2 {
+	static function main () {
+		#if !macro
+		define();
+		#end
+	}
+
+	macro static public function define() {
+		haxe.macro.Context.defineModule("some.+", [{
+			pos: (macro 0).pos,
+			pack: ["some"],
+			name: "+",
+			kind: TDClass(),
+			fields: []
+		}]);
+		return macro {};
+	}
+}

+ 1 - 0
tests/misc/projects/Issue8750/compile-fail.hxml

@@ -0,0 +1 @@
+-main Main2

+ 1 - 0
tests/misc/projects/Issue8750/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main2.hx:10: characters 16-17 : Module "some.+" does not have a valid name. "+" is not a valid module name.

+ 2 - 2
tests/misc/projects/issue5002/compile-fail.hxml.stderr

@@ -1,4 +1,4 @@
-Main.hx:1: characters 1-8 : "0" is not a valid field name
+Main.hx:1: characters 1-8 : "0" is not a valid field name.
 Main.hx:2: lines 2-4 : Defined in this class
-Main.hx:1: characters 1-8 : "this" is not a valid field name
+Main.hx:1: characters 1-8 : "this" is not a valid field name.
 Main.hx:2: lines 2-4 : Defined in this class

+ 10 - 10
tests/misc/projects/issue5002/compile2-fail.hxml.stderr

@@ -1,18 +1,18 @@
-"0_variable" is not a valid variable name
+"0_variable" is not a valid variable name.
 Main2.hx:6: characters 3-32 : Called from macro here
-"var" is not a valid variable name
+"var" is not a valid variable name.
 Main2.hx:7: characters 3-25 : Called from macro here
-"new" is not a valid variable name
+"new" is not a valid variable name.
 Main2.hx:8: characters 3-25 : Called from macro here
-"foo \"\t\n" is not a valid variable name
+"foo \"\t\n" is not a valid variable name.
 Main2.hx:9: characters 3-32 : Called from macro here
-"0_catchVariable" is not a valid catch variable name
+"0_catchVariable" is not a valid catch variable name.
 Main2.hx:10: characters 3-25 : Called from macro here
-"0_function" is not a valid function name
+"0_function" is not a valid function name.
 Main2.hx:11: characters 3-24 : Called from macro here
-"0_argument" is not a valid function argument name
+"0_argument" is not a valid function argument name.
 Main2.hx:12: characters 3-32 : Called from macro here
 Main2.hx:13: characters 3-27 : Pattern variables must be lower-case
-Main2.hx:14: characters 3-23 : "0_forVariable" is not a valid for variable name
-Main2.hx:15: characters 3-31 : "0_forVariableKey" is not a valid for variable name
-Main2.hx:15: characters 3-31 : "0_forVariableValue" is not a valid for variable name
+Main2.hx:14: characters 3-23 : "0_forVariable" is not a valid for variable name.
+Main2.hx:15: characters 3-31 : "0_forVariableKey" is not a valid for variable name.
+Main2.hx:15: characters 3-31 : "0_forVariableValue" is not a valid for variable name.

+ 6 - 6
tests/misc/projects/issue5002/compile3-fail.hxml.stderr

@@ -1,6 +1,6 @@
-Main3.hx:9: characters 17-18 : Module name should start with an uppercase letter: "lowercase"
-Main3.hx:9: characters 17-18 : "0_class" is not a valid module name
-Main3.hx:9: characters 17-18 : "0_enum" is not a valid module name
-Main3.hx:9: characters 17-18 : "0_struct" is not a valid module name
-Main3.hx:9: characters 17-18 : "0_abstract_Impl_" is not a valid type name
-Main3.hx:9: characters 17-18 : "0_abstract" is not a valid module name
+Main3.hx:9: characters 17-18 : Module "lowercase" does not have a valid name. Module name should start with an uppercase letter: "lowercase"
+Main3.hx:9: characters 17-18 : Module "0_class" does not have a valid name. "0_class" is not a valid module name.
+Main3.hx:9: characters 17-18 : Module "0_enum" does not have a valid name. "0_enum" is not a valid module name.
+Main3.hx:9: characters 17-18 : Module "0_struct" does not have a valid name. "0_struct" is not a valid module name.
+Main3.hx:9: characters 17-18 : "0_abstract_Impl_" is not a valid type name.
+Main3.hx:9: characters 17-18 : Module "0_abstract" does not have a valid name. "0_abstract" is not a valid module name.