From 76ec1e5a6f643b2416dc61bbd3f7b8b65863393c Mon Sep 17 00:00:00 2001 From: Patrick Stapfer Date: Wed, 26 May 2021 11:53:50 +0200 Subject: [PATCH 1/2] Better error message on capitalized type --- src/res_core.ml | 25 +++++++++++++++++++++--- tests/parsing/errors/typeDef/typeDef.res | 3 +++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/res_core.ml b/src/res_core.ml index a03e1af1..2b053cff 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -728,8 +728,14 @@ let parseValuePath p = | Lident ident -> Longident.Lident ident | Uident ident -> Parser.next p; - Parser.expect Dot p; - aux p (Lident ident) + begin match p.token with + | Dot -> + Parser.next p; + aux p (Lident ident) + | _ -> + Parser.err p (Diagnostics.message "Identifier needs to be lower-cased."); + Longident.Lident ident + end | token -> Parser.err p (Diagnostics.unexpected token p.breadcrumbs); Longident.Lident "_" @@ -5223,7 +5229,20 @@ and parseTypeDefinitionOrExtension ~attrs p = Asttypes.Nonrecursive | _ -> Asttypes.Nonrecursive in - let name = parseValuePath p in + (*let name = parseValuePath p in*) + let name = match p.token with + | Uident ident -> + let identStartPos = p.startPos in + let identEndPos = p.endPos in + Parser.next p; + begin match p.token with + | Dot -> parseValuePathTail p startPos (Longident.Lident ident) + | _ -> + Parser.err p ~startPos:identStartPos ~endPos:identEndPos (Diagnostics.message "Type names need to start with a lower-cased letter."); + Location.mkloc (Longident.Lident ident) (mkLoc identStartPos identEndPos) + end + | _ -> parseValuePath p + in let params = parseTypeParams ~parent:name p in match p.Parser.token with | PlusEqual -> diff --git a/tests/parsing/errors/typeDef/typeDef.res b/tests/parsing/errors/typeDef/typeDef.res index ac77125f..b6091195 100644 --- a/tests/parsing/errors/typeDef/typeDef.res +++ b/tests/parsing/errors/typeDef/typeDef.res @@ -9,3 +9,6 @@ type t = // missing type type state = + +// name cannot be capitalized +type User = string From 204acfb601fa7e029f20f8f61cb2ffc36f4f1a80 Mon Sep 17 00:00:00 2001 From: Iwan Date: Wed, 26 May 2021 13:00:42 +0200 Subject: [PATCH 2/2] Tweak parsing of type definition names to parse the correct path --- src/res_core.ml | 21 +++++++++--------- .../errors/typeDef/expected/typeDef.res.txt | 22 +++++++++++++++---- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/res_core.ml b/src/res_core.ml index 2b053cff..262fa3e5 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -5229,18 +5229,19 @@ and parseTypeDefinitionOrExtension ~attrs p = Asttypes.Nonrecursive | _ -> Asttypes.Nonrecursive in - (*let name = parseValuePath p in*) let name = match p.token with - | Uident ident -> - let identStartPos = p.startPos in - let identEndPos = p.endPos in + | Uident ident -> + let identStartPos = p.startPos in + let identEndPos = p.endPos in + Parser.next p; + begin match p.token with + | Dot -> Parser.next p; - begin match p.token with - | Dot -> parseValuePathTail p startPos (Longident.Lident ident) - | _ -> - Parser.err p ~startPos:identStartPos ~endPos:identEndPos (Diagnostics.message "Type names need to start with a lower-cased letter."); - Location.mkloc (Longident.Lident ident) (mkLoc identStartPos identEndPos) - end + parseValuePathTail p identStartPos (Longident.Lident ident) + | _ -> + Parser.err p ~startPos:identStartPos ~endPos:identEndPos (Diagnostics.message "Type names need to start with a lower-cased letter."); + Location.mkloc (Longident.Lident ident) (mkLoc identStartPos identEndPos) + end | _ -> parseValuePath p in let params = parseTypeParams ~parent:name p in diff --git a/tests/parsing/errors/typeDef/expected/typeDef.res.txt b/tests/parsing/errors/typeDef/expected/typeDef.res.txt index 8b73f7a8..097b5095 100644 --- a/tests/parsing/errors/typeDef/expected/typeDef.res.txt +++ b/tests/parsing/errors/typeDef/expected/typeDef.res.txt @@ -29,21 +29,35 @@ 10 │ // missing type 11 │ type state = 12 │ + 13 │ // name cannot be capitalized Missing a type here Syntax error! - tests/parsing/errors/typeDef/typeDef.res:12:1 + tests/parsing/errors/typeDef/typeDef.res:14:1-4 - 10 │ // missing type - 11 │ type state = 12 │ + 13 │ // name cannot be capitalized + 14 │ type User = string + 15 │ Missing a type here + + Syntax error! + tests/parsing/errors/typeDef/typeDef.res:14:6-9 + + 12 │ + 13 │ // name cannot be capitalized + 14 │ type User = string + 15 │ + + Type names need to start with a lower-cased letter. + type stack = | Empty type nonrec bar = string type nonrec t = [%rescript.typehole ] -type nonrec state = [%rescript.typehole ] \ No newline at end of file +type nonrec state = [%rescript.typehole ] +type nonrec User = string \ No newline at end of file