Skip to content

Commit 615fb96

Browse files
committed
Fix object fields (and clean uo quoted ids).
1 parent b4d5a9c commit 615fb96

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

analysis/src/SemanticTokens.ml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ let emitFromLoc ~loc ~type_ emitter =
124124

125125
let emitLongident ?(backwards = false) ?(jsx = false)
126126
?(lowerCaseToken = if jsx then Token.JsxLowercase else Token.Variable)
127-
?(upperCaseToken = Token.Namespace) ?(lastToken = None) ~pos ~lid ~debug
128-
emitter =
127+
?(upperCaseToken = Token.Namespace) ?(lastToken = None) ?(posEnd = None)
128+
~pos ~lid ~debug emitter =
129129
let rec flatten acc lid =
130130
match lid with
131131
| Longident.Lident txt -> txt :: acc
@@ -142,10 +142,19 @@ let emitLongident ?(backwards = false) ?(jsx = false)
142142
| Some type_ -> type_
143143
| None -> if isUppercaseId id then upperCaseToken else lowerCaseToken
144144
in
145+
let posAfter = (fst pos, snd pos + String.length id) in
146+
let posEnd, lenMismatch =
147+
(* There could be a length mismatch when ids are quoted
148+
e.g. variable /"true" or object field {"x":...} *)
149+
match posEnd with
150+
| Some posEnd -> (posEnd, posEnd <> posAfter)
151+
| None -> (posAfter, false)
152+
in
145153
if debug then
146-
Printf.printf "Lident: %s %s %s\n" id (posToString pos)
154+
Printf.printf "Lident: %s %s%s %s\n" id (posToString pos)
155+
(if lenMismatch then "->" ^ posToString posEnd else "")
147156
(Token.tokenTypeDebug type_);
148-
emitter |> emitFromPos pos (fst pos, snd pos + String.length id) ~type_
157+
emitter |> emitFromPos pos posEnd ~type_
149158
| id :: segments when isUppercaseId id || isLowercaseId id ->
150159
let type_ = if isUppercaseId id then upperCaseToken else lowerCaseToken in
151160
if debug then
@@ -190,6 +199,7 @@ let emitRecordLabel ~(label : Longident.t Location.loc) ~debug emitter =
190199
emitter
191200
|> emitLongident ~lowerCaseToken:Token.Property
192201
~pos:(Utils.tupleOfLexing label.loc.loc_start)
202+
~posEnd:(Some (Utils.tupleOfLexing label.loc.loc_end))
193203
~lid:label.txt ~debug
194204

195205
let emitVariant ~(name : Longident.t Location.loc) ~debug emitter =
@@ -240,23 +250,13 @@ let parser ~debug ~emitter ~path =
240250
in
241251
let expr (mapper : Ast_mapper.mapper) (e : Parsetree.expression) =
242252
match e.pexp_desc with
243-
| Pexp_ident {txt = Lident id}
244-
when id <> "=" && id <> "=="
245-
&& snd (Utils.tupleOfLexing e.pexp_loc.loc_end)
246-
- snd (Utils.tupleOfLexing e.pexp_loc.loc_start)
247-
> String.length id
248-
(* /"stuff" *) ->
249-
let type_ = Token.Variable in
250-
if debug then
251-
Printf.printf "QuotedIdent: %s %s %s\n" id
252-
(posToString (Utils.tupleOfLexing e.pexp_loc.loc_start))
253-
(Token.tokenTypeDebug type_);
254-
emitter |> emitFromLoc ~loc:e.pexp_loc ~type_;
255-
Ast_mapper.default_mapper.expr mapper e
256253
| Pexp_ident {txt = lid; loc} ->
257254
if lid <> Lident "not" then
258255
emitter
259-
|> emitLongident ~pos:(Utils.tupleOfLexing loc.loc_start) ~lid ~debug;
256+
|> emitLongident
257+
~pos:(Utils.tupleOfLexing loc.loc_start)
258+
~posEnd:(Some (Utils.tupleOfLexing loc.loc_end))
259+
~lid ~debug;
260260
Ast_mapper.default_mapper.expr mapper e
261261
| Pexp_apply ({pexp_desc = Pexp_ident lident; pexp_loc}, args)
262262
when Res_parsetree_viewer.isJsxExpression e ->

analysis/tests/src/Parser.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,5 @@ let _ = x =>
130130
let _ = 3 == 3 || 3 === 3
131131

132132
let _ = (~_type_ as _) => ()
133+
134+
let _ = {"abc": 34}

analysis/tests/src/expected/Parser.res.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Parse tests/src/Parser.res
2-
structure items:37 diagnostics:0
2+
structure items:38 diagnostics:0
33
Lident: M (0,7) Namespace
44
Lident: C (1,9) Namespace
55
Lident: Component (1,13) Namespace
@@ -122,7 +122,7 @@ Variable: make (100,6)->(100,10)
122122
JsxTag <: (104,8)
123123
Lident: ToAsProp (104,9) Namespace
124124
Variable: true (107,4)->(107,11)
125-
QuotedIdent: true (108,8) Variable
125+
Lident: true (108,8)->(108,15) Variable
126126
Ldot: T (110,19) Namespace
127127
Lident: A (110,21) EnumMember
128128
Variable: enumInModule (110,4)->(110,16)
@@ -139,4 +139,5 @@ Ldot: QQ (126,8) Namespace
139139
Lident: somePolyEnumType (126,11) Type
140140
Lident: x (124,9) Variable
141141
Variable: x (123,8)->(123,9)
142+
Lident: abc (133,9)->(133,14) Property
142143

0 commit comments

Comments
 (0)